]>
Commit | Line | Data |
---|---|---|
7368a6c8 | 1 | /* |
2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
3 | * All rights reserved | |
bcbf86ec | 4 | * |
5 | * As far as I am concerned, the code I have written for this software | |
6 | * can be used freely for any purpose. Any derived versions of this | |
7 | * software must be clearly marked as such, and if the derived work is | |
8 | * incompatible with the protocol description in the RFC file, it must be | |
9 | * called by a name other than "ssh" or "Secure Shell". | |
10 | * | |
e78a59f5 | 11 | * SSH2 support by Markus Friedl. |
a96070d4 | 12 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
bcbf86ec | 13 | * |
14 | * Redistribution and use in source and binary forms, with or without | |
15 | * modification, are permitted provided that the following conditions | |
16 | * are met: | |
17 | * 1. Redistributions of source code must retain the above copyright | |
18 | * notice, this list of conditions and the following disclaimer. | |
19 | * 2. Redistributions in binary form must reproduce the above copyright | |
20 | * notice, this list of conditions and the following disclaimer in the | |
21 | * documentation and/or other materials provided with the distribution. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
e78a59f5 | 33 | */ |
7368a6c8 | 34 | |
35 | #include "includes.h" | |
d5f24f94 | 36 | RCSID("$OpenBSD: session.c,v 1.106 2001/10/09 21:59:41 markus Exp $"); |
7368a6c8 | 37 | |
7368a6c8 | 38 | #include "ssh.h" |
42f11eb2 | 39 | #include "ssh1.h" |
40 | #include "ssh2.h" | |
41 | #include "xmalloc.h" | |
1729c161 | 42 | #include "sshpty.h" |
7368a6c8 | 43 | #include "packet.h" |
44 | #include "buffer.h" | |
7368a6c8 | 45 | #include "mpaux.h" |
7368a6c8 | 46 | #include "uidswap.h" |
47 | #include "compat.h" | |
a5efa1bb | 48 | #include "channels.h" |
e78a59f5 | 49 | #include "bufaux.h" |
e78a59f5 | 50 | #include "auth.h" |
38c295d6 | 51 | #include "auth-options.h" |
42f11eb2 | 52 | #include "pathnames.h" |
53 | #include "log.h" | |
54 | #include "servconf.h" | |
1729c161 | 55 | #include "sshlogin.h" |
42f11eb2 | 56 | #include "serverloop.h" |
57 | #include "canohost.h" | |
5ca51e19 | 58 | #include "session.h" |
e78a59f5 | 59 | |
3206bb3b | 60 | #ifdef WITH_IRIX_PROJECT |
61 | #include <proj.h> | |
62 | #endif /* WITH_IRIX_PROJECT */ | |
d287c664 | 63 | #ifdef WITH_IRIX_JOBS |
64 | #include <sys/resource.h> | |
2b87da3b | 65 | #endif |
260d427b | 66 | #ifdef WITH_IRIX_AUDIT |
67 | #include <sat.h> | |
68 | #endif /* WITH_IRIX_AUDIT */ | |
3206bb3b | 69 | |
089fbbd2 | 70 | #if defined(HAVE_USERSEC_H) |
71 | #include <usersec.h> | |
72 | #endif | |
73 | ||
3c62e7eb | 74 | #ifdef HAVE_CYGWIN |
75 | #include <windows.h> | |
76 | #include <sys/cygwin.h> | |
77 | #define is_winnt (GetVersion() < 0x80000000) | |
78 | #endif | |
79 | ||
3fcce26c | 80 | /* AIX limits */ |
81 | #if defined(HAVE_GETUSERATTR) && !defined(S_UFSIZE_HARD) && defined(S_UFSIZE) | |
2a2cb9e7 | 82 | # define S_UFSIZE_HARD S_UFSIZE "_hard" |
83 | # define S_UCPU_HARD S_UCPU "_hard" | |
84 | # define S_UDATA_HARD S_UDATA "_hard" | |
85 | # define S_USTACK_HARD S_USTACK "_hard" | |
86 | # define S_URSS_HARD S_URSS "_hard" | |
87 | # define S_UCORE_HARD S_UCORE "_hard" | |
88 | # define S_UNOFILE_HARD S_UNOFILE "_hard" | |
3fcce26c | 89 | #endif |
90 | ||
8a968c25 | 91 | #ifdef _AIX |
92 | # include <uinfo.h> | |
93 | #endif | |
94 | ||
7368a6c8 | 95 | /* types */ |
96 | ||
97 | #define TTYSZ 64 | |
98 | typedef struct Session Session; | |
99 | struct Session { | |
100 | int used; | |
101 | int self; | |
ced49be2 | 102 | struct passwd *pw; |
103 | Authctxt *authctxt; | |
7368a6c8 | 104 | pid_t pid; |
105 | /* tty */ | |
106 | char *term; | |
107 | int ptyfd, ttyfd, ptymaster; | |
108 | int row, col, xpixel, ypixel; | |
109 | char tty[TTYSZ]; | |
110 | /* X11 */ | |
111 | char *display; | |
112 | int screen; | |
113 | char *auth_proto; | |
114 | char *auth_data; | |
0b242b12 | 115 | int single_connection; |
7368a6c8 | 116 | /* proto 2 */ |
117 | int chanid; | |
e5d7a405 | 118 | int is_subsystem; |
7368a6c8 | 119 | }; |
120 | ||
121 | /* func */ | |
122 | ||
123 | Session *session_new(void); | |
9c328529 | 124 | void session_set_fds(Session *, int, int, int); |
125 | static void session_pty_cleanup(void *); | |
126 | void session_proctitle(Session *); | |
127 | int session_setup_x11fwd(Session *); | |
128 | void do_exec_pty(Session *, const char *); | |
129 | void do_exec_no_pty(Session *, const char *); | |
130 | void do_exec(Session *, const char *); | |
131 | void do_login(Session *, const char *); | |
8a9ac95d | 132 | #ifdef LOGIN_NEEDS_UTMPX |
133 | static void do_pre_login(Session *s); | |
134 | #endif | |
9c328529 | 135 | void do_child(Session *, const char *); |
f2c2fd71 | 136 | void do_motd(void); |
9c328529 | 137 | int check_quietlogin(Session *, const char *); |
7368a6c8 | 138 | |
396c147e | 139 | static void do_authenticated1(Authctxt *); |
140 | static void do_authenticated2(Authctxt *); | |
9c328529 | 141 | |
396c147e | 142 | static void session_close(Session *); |
143 | static int session_pty_req(Session *); | |
bb5639fe | 144 | |
7368a6c8 | 145 | /* import */ |
146 | extern ServerOptions options; | |
147 | extern char *__progname; | |
148 | extern int log_stderr; | |
149 | extern int debug_flag; | |
1e3b8b07 | 150 | extern u_int utmp_len; |
089fbbd2 | 151 | extern int startup_pipe; |
e78e738a | 152 | extern void destroy_sensitive_data(void); |
089fbbd2 | 153 | |
b5e300c2 | 154 | /* original command from peer. */ |
c95add71 | 155 | const char *original_command = NULL; |
b5e300c2 | 156 | |
7368a6c8 | 157 | /* data */ |
158 | #define MAX_SESSIONS 10 | |
159 | Session sessions[MAX_SESSIONS]; | |
c5d85828 | 160 | |
c1ef8333 | 161 | #ifdef WITH_AIXAUTHENTICATE |
162 | /* AIX's lastlogin message, set in auth1.c */ | |
163 | char *aixloginmsg; | |
164 | #endif /* WITH_AIXAUTHENTICATE */ | |
7368a6c8 | 165 | |
2e73a022 | 166 | #ifdef HAVE_LOGIN_CAP |
167 | static login_cap_t *lc; | |
168 | #endif | |
169 | ||
bb5639fe | 170 | void |
171 | do_authenticated(Authctxt *authctxt) | |
172 | { | |
173 | /* | |
174 | * Cancel the alarm we set to limit the time taken for | |
175 | * authentication. | |
176 | */ | |
177 | alarm(0); | |
178 | if (startup_pipe != -1) { | |
179 | close(startup_pipe); | |
180 | startup_pipe = -1; | |
181 | } | |
182 | #if defined(HAVE_LOGIN_CAP) && defined(HAVE_PW_CLASS_IN_PASSWD) | |
183 | if ((lc = login_getclass(authctxt->pw->pw_class)) == NULL) { | |
184 | error("unable to get login class"); | |
185 | return; | |
186 | } | |
e78e738a | 187 | #ifdef BSD_AUTH |
188 | if (auth_approval(NULL, lc, authctxt->pw->pw_name, "ssh") <= 0) { | |
189 | packet_disconnect("Approval failure for %s", | |
190 | authctxt->pw->pw_name); | |
191 | } | |
192 | #endif | |
bb5639fe | 193 | #endif |
194 | /* setup the channel layer */ | |
195 | if (!no_port_forwarding_flag && options.allow_tcp_forwarding) | |
196 | channel_permit_all_opens(); | |
197 | ||
198 | if (compat20) | |
199 | do_authenticated2(authctxt); | |
200 | else | |
201 | do_authenticated1(authctxt); | |
e4f7282d | 202 | |
b44de2b1 | 203 | /* remove agent socket */ |
e4f7282d | 204 | if (auth_get_socket_name()) |
f0f32b8e | 205 | auth_sock_cleanup_proc(authctxt->pw); |
ced49be2 | 206 | #ifdef KRB4 |
207 | if (options.kerberos_ticket_cleanup) | |
208 | krb4_cleanup_proc(authctxt); | |
209 | #endif | |
210 | #ifdef KRB5 | |
211 | if (options.kerberos_ticket_cleanup) | |
212 | krb5_cleanup_proc(authctxt); | |
213 | #endif | |
bb5639fe | 214 | } |
215 | ||
7368a6c8 | 216 | /* |
217 | * Prepares for an interactive session. This is called after the user has | |
218 | * been successfully authenticated. During this message exchange, pseudo | |
219 | * terminals are allocated, X11, TCP/IP, and authentication agent forwardings | |
220 | * are requested, etc. | |
221 | */ | |
396c147e | 222 | static void |
bb5639fe | 223 | do_authenticated1(Authctxt *authctxt) |
7368a6c8 | 224 | { |
225 | Session *s; | |
7368a6c8 | 226 | char *command; |
653d5f86 | 227 | int success, type, plen, screen_flag; |
bb5639fe | 228 | int compression_level = 0, enable_compression_after_reply = 0; |
1e3b8b07 | 229 | u_int proto_len, data_len, dlen; |
7368a6c8 | 230 | |
231 | s = session_new(); | |
ced49be2 | 232 | s->authctxt = authctxt; |
bb5639fe | 233 | s->pw = authctxt->pw; |
2e73a022 | 234 | |
7368a6c8 | 235 | /* |
236 | * We stay in this loop until the client requests to execute a shell | |
237 | * or a command. | |
238 | */ | |
239 | for (;;) { | |
bb5639fe | 240 | success = 0; |
7368a6c8 | 241 | |
242 | /* Get a packet from the client. */ | |
243 | type = packet_read(&plen); | |
244 | ||
245 | /* Process the packet. */ | |
246 | switch (type) { | |
247 | case SSH_CMSG_REQUEST_COMPRESSION: | |
248 | packet_integrity_check(plen, 4, type); | |
249 | compression_level = packet_get_int(); | |
250 | if (compression_level < 1 || compression_level > 9) { | |
251 | packet_send_debug("Received illegal compression level %d.", | |
252 | compression_level); | |
253 | break; | |
254 | } | |
255 | /* Enable compression after we have responded with SUCCESS. */ | |
256 | enable_compression_after_reply = 1; | |
257 | success = 1; | |
258 | break; | |
259 | ||
260 | case SSH_CMSG_REQUEST_PTY: | |
653d5f86 | 261 | success = session_pty_req(s); |
7368a6c8 | 262 | break; |
263 | ||
264 | case SSH_CMSG_X11_REQUEST_FORWARDING: | |
7368a6c8 | 265 | s->auth_proto = packet_get_string(&proto_len); |
266 | s->auth_data = packet_get_string(&data_len); | |
7368a6c8 | 267 | |
2af09193 | 268 | screen_flag = packet_get_protocol_flags() & |
269 | SSH_PROTOFLAG_SCREEN_NUMBER; | |
270 | debug2("SSH_PROTOFLAG_SCREEN_NUMBER: %d", screen_flag); | |
271 | ||
272 | if (packet_remaining() == 4) { | |
273 | if (!screen_flag) | |
274 | debug2("Buggy client: " | |
275 | "X11 screen flag missing"); | |
7368a6c8 | 276 | s->screen = packet_get_int(); |
c8b058b4 | 277 | } else { |
7368a6c8 | 278 | s->screen = 0; |
c8b058b4 | 279 | } |
8dcd9d5c | 280 | packet_done(); |
281 | success = session_setup_x11fwd(s); | |
282 | if (!success) { | |
283 | xfree(s->auth_proto); | |
284 | xfree(s->auth_data); | |
1500bcdd | 285 | s->auth_proto = NULL; |
286 | s->auth_data = NULL; | |
7368a6c8 | 287 | } |
7368a6c8 | 288 | break; |
7368a6c8 | 289 | |
290 | case SSH_CMSG_AGENT_REQUEST_FORWARDING: | |
291 | if (no_agent_forwarding_flag || compat13) { | |
292 | debug("Authentication agent forwarding not permitted for this authentication."); | |
293 | break; | |
294 | } | |
295 | debug("Received authentication agent forwarding request."); | |
bb5639fe | 296 | success = auth_input_request_forwarding(s->pw); |
7368a6c8 | 297 | break; |
298 | ||
299 | case SSH_CMSG_PORT_FORWARD_REQUEST: | |
300 | if (no_port_forwarding_flag) { | |
301 | debug("Port forwarding not permitted for this authentication."); | |
302 | break; | |
303 | } | |
33de75a3 | 304 | if (!options.allow_tcp_forwarding) { |
305 | debug("Port forwarding not permitted."); | |
306 | break; | |
307 | } | |
7368a6c8 | 308 | debug("Received TCP/IP port forwarding request."); |
bb5639fe | 309 | channel_input_port_forward_request(s->pw->pw_uid == 0, options.gateway_ports); |
7368a6c8 | 310 | success = 1; |
311 | break; | |
312 | ||
313 | case SSH_CMSG_MAX_PACKET_SIZE: | |
314 | if (packet_set_maxsize(packet_get_int()) > 0) | |
315 | success = 1; | |
316 | break; | |
ced49be2 | 317 | |
318 | #if defined(AFS) || defined(KRB5) | |
319 | case SSH_CMSG_HAVE_KERBEROS_TGT: | |
320 | if (!options.kerberos_tgt_passing) { | |
321 | verbose("Kerberos TGT passing disabled."); | |
322 | } else { | |
323 | char *kdata = packet_get_string(&dlen); | |
324 | packet_integrity_check(plen, 4 + dlen, type); | |
325 | ||
326 | /* XXX - 0x41, see creds_to_radix version */ | |
327 | if (kdata[0] != 0x41) { | |
328 | #ifdef KRB5 | |
329 | krb5_data tgt; | |
330 | tgt.data = kdata; | |
331 | tgt.length = dlen; | |
332 | ||
333 | if (auth_krb5_tgt(s->authctxt, &tgt)) | |
334 | success = 1; | |
335 | else | |
336 | verbose("Kerberos v5 TGT refused for %.100s", s->authctxt->user); | |
337 | #endif /* KRB5 */ | |
338 | } else { | |
339 | #ifdef AFS | |
340 | if (auth_krb4_tgt(s->authctxt, kdata)) | |
341 | success = 1; | |
342 | else | |
343 | verbose("Kerberos v4 TGT refused for %.100s", s->authctxt->user); | |
344 | #endif /* AFS */ | |
345 | } | |
346 | xfree(kdata); | |
347 | } | |
348 | break; | |
349 | #endif /* AFS || KRB5 */ | |
350 | ||
351 | #ifdef AFS | |
352 | case SSH_CMSG_HAVE_AFS_TOKEN: | |
353 | if (!options.afs_token_passing || !k_hasafs()) { | |
354 | verbose("AFS token passing disabled."); | |
355 | } else { | |
356 | /* Accept AFS token. */ | |
357 | char *token = packet_get_string(&dlen); | |
358 | packet_integrity_check(plen, 4 + dlen, type); | |
359 | ||
360 | if (auth_afs_token(s->authctxt, token)) | |
361 | success = 1; | |
362 | else | |
363 | verbose("AFS token refused for %.100s", | |
364 | s->authctxt->user); | |
365 | xfree(token); | |
366 | } | |
367 | break; | |
368 | #endif /* AFS */ | |
7368a6c8 | 369 | |
370 | case SSH_CMSG_EXEC_SHELL: | |
371 | case SSH_CMSG_EXEC_CMD: | |
7368a6c8 | 372 | if (type == SSH_CMSG_EXEC_CMD) { |
373 | command = packet_get_string(&dlen); | |
374 | debug("Exec command '%.500s'", command); | |
c95add71 | 375 | do_exec(s, command); |
376 | xfree(command); | |
7368a6c8 | 377 | } else { |
c95add71 | 378 | do_exec(s, NULL); |
7368a6c8 | 379 | } |
c95add71 | 380 | packet_done(); |
fc2a1d28 | 381 | session_close(s); |
7368a6c8 | 382 | return; |
383 | ||
384 | default: | |
385 | /* | |
386 | * Any unknown messages in this phase are ignored, | |
387 | * and a failure message is returned. | |
388 | */ | |
389 | log("Unknown packet type received after authentication: %d", type); | |
390 | } | |
391 | packet_start(success ? SSH_SMSG_SUCCESS : SSH_SMSG_FAILURE); | |
392 | packet_send(); | |
393 | packet_write_wait(); | |
394 | ||
395 | /* Enable compression now that we have replied if appropriate. */ | |
396 | if (enable_compression_after_reply) { | |
397 | enable_compression_after_reply = 0; | |
398 | packet_start_compression(compression_level); | |
399 | } | |
400 | } | |
401 | } | |
402 | ||
403 | /* | |
404 | * This is called to fork and execute a command when we have no tty. This | |
405 | * will call do_child from the child, and server_loop from the parent after | |
406 | * setting up file descriptors and such. | |
407 | */ | |
6ae2364d | 408 | void |
65068d16 | 409 | do_exec_no_pty(Session *s, const char *command) |
7368a6c8 | 410 | { |
411 | int pid; | |
412 | ||
413 | #ifdef USE_PIPES | |
414 | int pin[2], pout[2], perr[2]; | |
415 | /* Allocate pipes for communicating with the program. */ | |
416 | if (pipe(pin) < 0 || pipe(pout) < 0 || pipe(perr) < 0) | |
417 | packet_disconnect("Could not create pipes: %.100s", | |
418 | strerror(errno)); | |
419 | #else /* USE_PIPES */ | |
420 | int inout[2], err[2]; | |
421 | /* Uses socket pairs to communicate with the program. */ | |
422 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) < 0 || | |
423 | socketpair(AF_UNIX, SOCK_STREAM, 0, err) < 0) | |
424 | packet_disconnect("Could not create socket pairs: %.100s", | |
425 | strerror(errno)); | |
426 | #endif /* USE_PIPES */ | |
427 | if (s == NULL) | |
428 | fatal("do_exec_no_pty: no session"); | |
429 | ||
1d1ffb87 | 430 | session_proctitle(s); |
7368a6c8 | 431 | |
b19d61d7 | 432 | #if defined(USE_PAM) |
78220944 | 433 | do_pam_session(s->pw->pw_name, NULL); |
9afbfcfa | 434 | do_pam_setcred(1); |
266140a8 | 435 | #endif /* USE_PAM */ |
436 | ||
7368a6c8 | 437 | /* Fork the child. */ |
438 | if ((pid = fork()) == 0) { | |
439 | /* Child. Reinitialize the log since the pid has changed. */ | |
440 | log_init(__progname, options.log_level, options.log_facility, log_stderr); | |
441 | ||
442 | /* | |
443 | * Create a new session and process group since the 4.4BSD | |
444 | * setlogin() affects the entire process group. | |
445 | */ | |
446 | if (setsid() < 0) | |
447 | error("setsid failed: %.100s", strerror(errno)); | |
448 | ||
449 | #ifdef USE_PIPES | |
450 | /* | |
451 | * Redirect stdin. We close the parent side of the socket | |
452 | * pair, and make the child side the standard input. | |
453 | */ | |
454 | close(pin[1]); | |
455 | if (dup2(pin[0], 0) < 0) | |
456 | perror("dup2 stdin"); | |
457 | close(pin[0]); | |
458 | ||
459 | /* Redirect stdout. */ | |
460 | close(pout[0]); | |
461 | if (dup2(pout[1], 1) < 0) | |
462 | perror("dup2 stdout"); | |
463 | close(pout[1]); | |
464 | ||
465 | /* Redirect stderr. */ | |
466 | close(perr[0]); | |
467 | if (dup2(perr[1], 2) < 0) | |
468 | perror("dup2 stderr"); | |
469 | close(perr[1]); | |
470 | #else /* USE_PIPES */ | |
471 | /* | |
472 | * Redirect stdin, stdout, and stderr. Stdin and stdout will | |
473 | * use the same socket, as some programs (particularly rdist) | |
474 | * seem to depend on it. | |
475 | */ | |
476 | close(inout[1]); | |
477 | close(err[1]); | |
478 | if (dup2(inout[0], 0) < 0) /* stdin */ | |
479 | perror("dup2 stdin"); | |
480 | if (dup2(inout[0], 1) < 0) /* stdout. Note: same socket as stdin. */ | |
481 | perror("dup2 stdout"); | |
482 | if (dup2(err[0], 2) < 0) /* stderr */ | |
483 | perror("dup2 stderr"); | |
484 | #endif /* USE_PIPES */ | |
485 | ||
486 | /* Do processing for the child (exec command etc). */ | |
56b3e9ce | 487 | do_child(s, command); |
7368a6c8 | 488 | /* NOTREACHED */ |
489 | } | |
3c62e7eb | 490 | #ifdef HAVE_CYGWIN |
491 | if (is_winnt) | |
492 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
493 | #endif | |
7368a6c8 | 494 | if (pid < 0) |
495 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
496 | s->pid = pid; | |
b5c334cc | 497 | /* Set interactive/non-interactive mode. */ |
498 | packet_set_interactive(s->display != NULL); | |
7368a6c8 | 499 | #ifdef USE_PIPES |
500 | /* We are the parent. Close the child sides of the pipes. */ | |
501 | close(pin[0]); | |
502 | close(pout[1]); | |
503 | close(perr[1]); | |
504 | ||
e78a59f5 | 505 | if (compat20) { |
e5d7a405 | 506 | session_set_fds(s, pin[1], pout[0], s->is_subsystem ? -1 : perr[0]); |
e78a59f5 | 507 | } else { |
508 | /* Enter the interactive session. */ | |
509 | server_loop(pid, pin[1], pout[0], perr[0]); | |
e5d7a405 | 510 | /* server_loop has closed pin[1], pout[0], and perr[0]. */ |
e78a59f5 | 511 | } |
7368a6c8 | 512 | #else /* USE_PIPES */ |
513 | /* We are the parent. Close the child sides of the socket pairs. */ | |
514 | close(inout[0]); | |
515 | close(err[0]); | |
516 | ||
517 | /* | |
518 | * Enter the interactive session. Note: server_loop must be able to | |
519 | * handle the case that fdin and fdout are the same. | |
520 | */ | |
e78a59f5 | 521 | if (compat20) { |
e5d7a405 | 522 | session_set_fds(s, inout[1], inout[1], s->is_subsystem ? -1 : err[1]); |
e78a59f5 | 523 | } else { |
524 | server_loop(pid, inout[1], inout[1], err[1]); | |
525 | /* server_loop has closed inout[1] and err[1]. */ | |
526 | } | |
7368a6c8 | 527 | #endif /* USE_PIPES */ |
528 | } | |
529 | ||
530 | /* | |
531 | * This is called to fork and execute a command when we have a tty. This | |
532 | * will call do_child from the child, and server_loop from the parent after | |
533 | * setting up file descriptors, controlling tty, updating wtmp, utmp, | |
534 | * lastlog, and other such operations. | |
535 | */ | |
6ae2364d | 536 | void |
65068d16 | 537 | do_exec_pty(Session *s, const char *command) |
7368a6c8 | 538 | { |
7368a6c8 | 539 | int fdout, ptyfd, ttyfd, ptymaster; |
7368a6c8 | 540 | pid_t pid; |
7368a6c8 | 541 | |
542 | if (s == NULL) | |
543 | fatal("do_exec_pty: no session"); | |
544 | ptyfd = s->ptyfd; | |
545 | ttyfd = s->ttyfd; | |
546 | ||
b19d61d7 | 547 | #if defined(USE_PAM) |
bb5639fe | 548 | do_pam_session(s->pw->pw_name, s->tty); |
9afbfcfa | 549 | do_pam_setcred(1); |
27cf96de | 550 | #endif |
266140a8 | 551 | |
7368a6c8 | 552 | /* Fork the child. */ |
553 | if ((pid = fork()) == 0) { | |
57156994 | 554 | |
c345cf9d | 555 | /* Child. Reinitialize the log because the pid has changed. */ |
7368a6c8 | 556 | log_init(__progname, options.log_level, options.log_facility, log_stderr); |
7368a6c8 | 557 | /* Close the master side of the pseudo tty. */ |
558 | close(ptyfd); | |
559 | ||
560 | /* Make the pseudo tty our controlling tty. */ | |
561 | pty_make_controlling_tty(&ttyfd, s->tty); | |
562 | ||
05fd093c | 563 | /* Redirect stdin/stdout/stderr from the pseudo tty. */ |
564 | if (dup2(ttyfd, 0) < 0) | |
565 | error("dup2 stdin: %s", strerror(errno)); | |
566 | if (dup2(ttyfd, 1) < 0) | |
567 | error("dup2 stdout: %s", strerror(errno)); | |
568 | if (dup2(ttyfd, 2) < 0) | |
569 | error("dup2 stderr: %s", strerror(errno)); | |
7368a6c8 | 570 | |
571 | /* Close the extra descriptor for the pseudo tty. */ | |
572 | close(ttyfd); | |
573 | ||
c345cf9d | 574 | /* record login, etc. similar to login(1) */ |
c96a4aaf | 575 | #ifndef HAVE_OSF_SIA |
a22aff1f | 576 | if (!(options.use_login && command == NULL)) |
577 | do_login(s, command); | |
7e2d5fa4 | 578 | # ifdef LOGIN_NEEDS_UTMPX |
579 | else | |
580 | do_pre_login(s); | |
581 | # endif | |
c96a4aaf | 582 | #endif |
7368a6c8 | 583 | |
7368a6c8 | 584 | /* Do common processing for the child, such as execing the command. */ |
56b3e9ce | 585 | do_child(s, command); |
7368a6c8 | 586 | /* NOTREACHED */ |
587 | } | |
3c62e7eb | 588 | #ifdef HAVE_CYGWIN |
589 | if (is_winnt) | |
590 | cygwin_set_impersonation_token(INVALID_HANDLE_VALUE); | |
591 | #endif | |
7368a6c8 | 592 | if (pid < 0) |
593 | packet_disconnect("fork failed: %.100s", strerror(errno)); | |
594 | s->pid = pid; | |
595 | ||
596 | /* Parent. Close the slave side of the pseudo tty. */ | |
597 | close(ttyfd); | |
598 | ||
599 | /* | |
600 | * Create another descriptor of the pty master side for use as the | |
601 | * standard input. We could use the original descriptor, but this | |
602 | * simplifies code in server_loop. The descriptor is bidirectional. | |
603 | */ | |
604 | fdout = dup(ptyfd); | |
605 | if (fdout < 0) | |
606 | packet_disconnect("dup #1 failed: %.100s", strerror(errno)); | |
607 | ||
608 | /* we keep a reference to the pty master */ | |
609 | ptymaster = dup(ptyfd); | |
610 | if (ptymaster < 0) | |
611 | packet_disconnect("dup #2 failed: %.100s", strerror(errno)); | |
612 | s->ptymaster = ptymaster; | |
613 | ||
614 | /* Enter interactive session. */ | |
b5c334cc | 615 | packet_set_interactive(1); |
e78a59f5 | 616 | if (compat20) { |
617 | session_set_fds(s, ptyfd, fdout, -1); | |
618 | } else { | |
619 | server_loop(pid, ptyfd, fdout, -1); | |
620 | /* server_loop _has_ closed ptyfd and fdout. */ | |
e78a59f5 | 621 | } |
7368a6c8 | 622 | } |
623 | ||
7e2d5fa4 | 624 | #ifdef LOGIN_NEEDS_UTMPX |
0e0144b7 | 625 | static void |
7e2d5fa4 | 626 | do_pre_login(Session *s) |
627 | { | |
628 | socklen_t fromlen; | |
629 | struct sockaddr_storage from; | |
630 | pid_t pid = getpid(); | |
631 | ||
632 | /* | |
633 | * Get IP address of client. If the connection is not a socket, let | |
634 | * the address be 0.0.0.0. | |
635 | */ | |
636 | memset(&from, 0, sizeof(from)); | |
637 | if (packet_connection_is_on_socket()) { | |
638 | fromlen = sizeof(from); | |
639 | if (getpeername(packet_get_connection_in(), | |
640 | (struct sockaddr *) & from, &fromlen) < 0) { | |
641 | debug("getpeername: %.100s", strerror(errno)); | |
642 | fatal_cleanup(); | |
643 | } | |
644 | } | |
645 | ||
646 | record_utmp_only(pid, s->tty, s->pw->pw_name, | |
647 | get_remote_name_or_ip(utmp_len, options.reverse_mapping_check), | |
648 | (struct sockaddr *)&from); | |
649 | } | |
650 | #endif | |
651 | ||
dac6753b | 652 | /* |
653 | * This is called to fork and execute a command. If another command is | |
654 | * to be forced, execute that instead. | |
655 | */ | |
656 | void | |
657 | do_exec(Session *s, const char *command) | |
658 | { | |
659 | if (forced_command) { | |
660 | original_command = command; | |
661 | command = forced_command; | |
dac6753b | 662 | debug("Forced command '%.900s'", command); |
663 | } | |
664 | ||
665 | if (s->ttyfd != -1) | |
666 | do_exec_pty(s, command); | |
667 | else | |
668 | do_exec_no_pty(s, command); | |
669 | ||
c95add71 | 670 | original_command = NULL; |
dac6753b | 671 | } |
672 | ||
c345cf9d | 673 | /* administrative, login(1)-like work */ |
674 | void | |
a22aff1f | 675 | do_login(Session *s, const char *command) |
c345cf9d | 676 | { |
c345cf9d | 677 | char *time_string; |
b5e300c2 | 678 | char hostname[MAXHOSTNAMELEN]; |
c345cf9d | 679 | socklen_t fromlen; |
680 | struct sockaddr_storage from; | |
c345cf9d | 681 | time_t last_login_time; |
682 | struct passwd * pw = s->pw; | |
683 | pid_t pid = getpid(); | |
684 | ||
685 | /* | |
686 | * Get IP address of client. If the connection is not a socket, let | |
687 | * the address be 0.0.0.0. | |
688 | */ | |
689 | memset(&from, 0, sizeof(from)); | |
690 | if (packet_connection_is_on_socket()) { | |
691 | fromlen = sizeof(from); | |
692 | if (getpeername(packet_get_connection_in(), | |
693 | (struct sockaddr *) & from, &fromlen) < 0) { | |
694 | debug("getpeername: %.100s", strerror(errno)); | |
695 | fatal_cleanup(); | |
696 | } | |
697 | } | |
698 | ||
bcbf86ec | 699 | /* Get the time and hostname when the user last logged in. */ |
4f4648f9 | 700 | if (options.print_lastlog) { |
701 | hostname[0] = '\0'; | |
702 | last_login_time = get_last_login_time(pw->pw_uid, pw->pw_name, | |
703 | hostname, sizeof(hostname)); | |
704 | } | |
bcbf86ec | 705 | |
c345cf9d | 706 | /* Record that there was a login on that tty from the remote host. */ |
707 | record_login(pid, s->tty, pw->pw_name, pw->pw_uid, | |
46e3af7f | 708 | get_remote_name_or_ip(utmp_len, options.reverse_mapping_check), |
709 | (struct sockaddr *)&from); | |
c345cf9d | 710 | |
2919e060 | 711 | #ifdef USE_PAM |
712 | /* | |
713 | * If password change is needed, do it now. | |
714 | * This needs to occur before the ~/.hushlogin check. | |
715 | */ | |
e11aab29 | 716 | if (is_pam_password_change_required()) { |
2919e060 | 717 | print_pam_messages(); |
718 | do_pam_chauthtok(); | |
719 | } | |
720 | #endif | |
721 | ||
f2c2fd71 | 722 | if (check_quietlogin(s, command)) |
c345cf9d | 723 | return; |
724 | ||
725 | #ifdef USE_PAM | |
e11aab29 | 726 | if (!is_pam_password_change_required()) |
2919e060 | 727 | print_pam_messages(); |
c345cf9d | 728 | #endif /* USE_PAM */ |
729 | #ifdef WITH_AIXAUTHENTICATE | |
730 | if (aixloginmsg && *aixloginmsg) | |
731 | printf("%s\n", aixloginmsg); | |
732 | #endif /* WITH_AIXAUTHENTICATE */ | |
733 | ||
4f4648f9 | 734 | if (options.print_lastlog && last_login_time != 0) { |
c345cf9d | 735 | time_string = ctime(&last_login_time); |
736 | if (strchr(time_string, '\n')) | |
737 | *strchr(time_string, '\n') = 0; | |
f478712e | 738 | if (strcmp(hostname, "") == 0) |
c345cf9d | 739 | printf("Last login: %s\r\n", time_string); |
740 | else | |
bcbf86ec | 741 | printf("Last login: %s from %s\r\n", time_string, hostname); |
c345cf9d | 742 | } |
f2c2fd71 | 743 | |
744 | do_motd(); | |
745 | } | |
746 | ||
747 | /* | |
748 | * Display the message of the day. | |
749 | */ | |
750 | void | |
751 | do_motd(void) | |
752 | { | |
753 | FILE *f; | |
754 | char buf[256]; | |
755 | ||
c345cf9d | 756 | if (options.print_motd) { |
2e73a022 | 757 | #ifdef HAVE_LOGIN_CAP |
758 | f = fopen(login_getcapstr(lc, "welcome", "/etc/motd", | |
759 | "/etc/motd"), "r"); | |
760 | #else | |
c345cf9d | 761 | f = fopen("/etc/motd", "r"); |
2e73a022 | 762 | #endif |
c345cf9d | 763 | if (f) { |
764 | while (fgets(buf, sizeof(buf), f)) | |
765 | fputs(buf, stdout); | |
766 | fclose(f); | |
767 | } | |
768 | } | |
769 | } | |
770 | ||
9c328529 | 771 | |
772 | /* | |
773 | * Check for quiet login, either .hushlogin or command given. | |
774 | */ | |
775 | int | |
776 | check_quietlogin(Session *s, const char *command) | |
777 | { | |
778 | char buf[256]; | |
779 | struct passwd *pw = s->pw; | |
780 | struct stat st; | |
781 | ||
782 | /* Return 1 if .hushlogin exists or a command given. */ | |
783 | if (command != NULL) | |
784 | return 1; | |
785 | snprintf(buf, sizeof(buf), "%.200s/.hushlogin", pw->pw_dir); | |
786 | #ifdef HAVE_LOGIN_CAP | |
787 | if (login_getcapbool(lc, "hushlogin", 0) || stat(buf, &st) >= 0) | |
788 | return 1; | |
789 | #else | |
790 | if (stat(buf, &st) >= 0) | |
791 | return 1; | |
792 | #endif | |
793 | return 0; | |
794 | } | |
795 | ||
7368a6c8 | 796 | /* |
797 | * Sets the value of the given variable in the environment. If the variable | |
798 | * already exists, its value is overriden. | |
799 | */ | |
396c147e | 800 | static void |
1e3b8b07 | 801 | child_set_env(char ***envp, u_int *envsizep, const char *name, |
7368a6c8 | 802 | const char *value) |
803 | { | |
1e3b8b07 | 804 | u_int i, namelen; |
7368a6c8 | 805 | char **env; |
806 | ||
807 | /* | |
808 | * Find the slot where the value should be stored. If the variable | |
809 | * already exists, we reuse the slot; otherwise we append a new slot | |
810 | * at the end of the array, expanding if necessary. | |
811 | */ | |
812 | env = *envp; | |
813 | namelen = strlen(name); | |
814 | for (i = 0; env[i]; i++) | |
815 | if (strncmp(env[i], name, namelen) == 0 && env[i][namelen] == '=') | |
816 | break; | |
817 | if (env[i]) { | |
818 | /* Reuse the slot. */ | |
819 | xfree(env[i]); | |
820 | } else { | |
821 | /* New variable. Expand if necessary. */ | |
822 | if (i >= (*envsizep) - 1) { | |
823 | (*envsizep) += 50; | |
824 | env = (*envp) = xrealloc(env, (*envsizep) * sizeof(char *)); | |
825 | } | |
826 | /* Need to set the NULL pointer at end of array beyond the new slot. */ | |
827 | env[i + 1] = NULL; | |
828 | } | |
829 | ||
830 | /* Allocate space and format the variable in the appropriate slot. */ | |
831 | env[i] = xmalloc(strlen(name) + 1 + strlen(value) + 1); | |
832 | snprintf(env[i], strlen(name) + 1 + strlen(value) + 1, "%s=%s", name, value); | |
833 | } | |
834 | ||
835 | /* | |
836 | * Reads environment variables from the given file and adds/overrides them | |
837 | * into the environment. If the file does not exist, this does nothing. | |
838 | * Otherwise, it must consist of empty lines, comments (line starts with '#') | |
839 | * and assignments of the form name=value. No other forms are allowed. | |
840 | */ | |
396c147e | 841 | static void |
1e3b8b07 | 842 | read_environment_file(char ***env, u_int *envsize, |
7368a6c8 | 843 | const char *filename) |
844 | { | |
845 | FILE *f; | |
846 | char buf[4096]; | |
847 | char *cp, *value; | |
848 | ||
849 | f = fopen(filename, "r"); | |
850 | if (!f) | |
851 | return; | |
852 | ||
853 | while (fgets(buf, sizeof(buf), f)) { | |
854 | for (cp = buf; *cp == ' ' || *cp == '\t'; cp++) | |
855 | ; | |
856 | if (!*cp || *cp == '#' || *cp == '\n') | |
857 | continue; | |
858 | if (strchr(cp, '\n')) | |
859 | *strchr(cp, '\n') = '\0'; | |
860 | value = strchr(cp, '='); | |
861 | if (value == NULL) { | |
862 | fprintf(stderr, "Bad line in %.100s: %.200s\n", filename, buf); | |
863 | continue; | |
864 | } | |
71276795 | 865 | /* |
866 | * Replace the equals sign by nul, and advance value to | |
867 | * the value string. | |
868 | */ | |
7368a6c8 | 869 | *value = '\0'; |
870 | value++; | |
871 | child_set_env(env, envsize, cp, value); | |
872 | } | |
873 | fclose(f); | |
874 | } | |
875 | ||
876 | #ifdef USE_PAM | |
877 | /* | |
878 | * Sets any environment variables which have been specified by PAM | |
879 | */ | |
16995a2c | 880 | void do_pam_environment(char ***env, u_int *envsize) |
7368a6c8 | 881 | { |
882 | char *equals, var_name[512], var_val[512]; | |
883 | char **pam_env; | |
884 | int i; | |
885 | ||
886 | if ((pam_env = fetch_pam_environment()) == NULL) | |
887 | return; | |
2b87da3b | 888 | |
7368a6c8 | 889 | for(i = 0; pam_env[i] != NULL; i++) { |
890 | if ((equals = strstr(pam_env[i], "=")) == NULL) | |
891 | continue; | |
2b87da3b | 892 | |
7368a6c8 | 893 | if (strlen(pam_env[i]) < (sizeof(var_name) - 1)) { |
894 | memset(var_name, '\0', sizeof(var_name)); | |
895 | memset(var_val, '\0', sizeof(var_val)); | |
896 | ||
897 | strncpy(var_name, pam_env[i], equals - pam_env[i]); | |
898 | strcpy(var_val, equals + 1); | |
899 | ||
22d89d24 | 900 | debug3("PAM environment: %s=%s", var_name, var_val); |
7368a6c8 | 901 | |
902 | child_set_env(env, envsize, var_name, var_val); | |
903 | } | |
904 | } | |
905 | } | |
906 | #endif /* USE_PAM */ | |
907 | ||
22d89d24 | 908 | #ifdef HAVE_CYGWIN |
16995a2c | 909 | void copy_environment(char ***env, u_int *envsize) |
22d89d24 | 910 | { |
911 | char *equals, var_name[512], var_val[512]; | |
912 | int i; | |
913 | ||
914 | for(i = 0; environ[i] != NULL; i++) { | |
915 | if ((equals = strstr(environ[i], "=")) == NULL) | |
916 | continue; | |
2b87da3b | 917 | |
22d89d24 | 918 | if (strlen(environ[i]) < (sizeof(var_name) - 1)) { |
919 | memset(var_name, '\0', sizeof(var_name)); | |
920 | memset(var_val, '\0', sizeof(var_val)); | |
921 | ||
922 | strncpy(var_name, environ[i], equals - environ[i]); | |
923 | strcpy(var_val, equals + 1); | |
924 | ||
925 | debug3("Copy environment: %s=%s", var_name, var_val); | |
926 | ||
927 | child_set_env(env, envsize, var_name, var_val); | |
928 | } | |
929 | } | |
930 | } | |
931 | #endif | |
932 | ||
5fa45897 | 933 | #if defined(HAVE_GETUSERATTR) |
934 | /* | |
935 | * AIX-specific login initialisation | |
936 | */ | |
937 | void set_limit(char *user, char *soft, char *hard, int resource, int mult) | |
938 | { | |
939 | struct rlimit rlim; | |
56118702 | 940 | int slim, hlim; |
5fa45897 | 941 | |
942 | getrlimit(resource, &rlim); | |
943 | ||
56118702 | 944 | slim = 0; |
945 | if (getuserattr(user, soft, &slim, SEC_INT) != -1) { | |
946 | if (slim < 0) { | |
947 | rlim.rlim_cur = RLIM_INFINITY; | |
948 | } else if (slim != 0) { | |
949 | /* See the wackiness below */ | |
950 | if (rlim.rlim_cur == slim * mult) | |
951 | slim = 0; | |
952 | else | |
953 | rlim.rlim_cur = slim * mult; | |
954 | } | |
955 | } | |
5fa45897 | 956 | |
56118702 | 957 | hlim = 0; |
958 | if (getuserattr(user, hard, &hlim, SEC_INT) != -1) { | |
959 | if (hlim < 0) { | |
960 | rlim.rlim_max = RLIM_INFINITY; | |
961 | } else if (hlim != 0) { | |
962 | rlim.rlim_max = hlim * mult; | |
963 | } | |
964 | } | |
5fa45897 | 965 | |
56118702 | 966 | /* |
967 | * XXX For cpu and fsize the soft limit is set to the hard limit | |
968 | * if the hard limit is left at its default value and the soft limit | |
969 | * is changed from its default value, either by requesting it | |
970 | * (slim == 0) or by setting it to the current default. At least | |
971 | * that's how rlogind does it. If you're confused you're not alone. | |
972 | * Bug or feature? AIX 4.3.1.2 | |
973 | */ | |
974 | if ((!strcmp(soft, "fsize") || !strcmp(soft, "cpu")) | |
975 | && hlim == 0 && slim != 0) | |
976 | rlim.rlim_max = rlim.rlim_cur; | |
977 | /* A specified hard limit limits the soft limit */ | |
978 | else if (hlim > 0 && rlim.rlim_cur > rlim.rlim_max) | |
979 | rlim.rlim_cur = rlim.rlim_max; | |
980 | /* A soft limit can increase a hard limit */ | |
981 | else if (rlim.rlim_cur > rlim.rlim_max) | |
5fa45897 | 982 | rlim.rlim_max = rlim.rlim_cur; |
983 | ||
984 | if (setrlimit(resource, &rlim) != 0) | |
56118702 | 985 | error("setrlimit(%.10s) failed: %.100s", soft, strerror(errno)); |
5fa45897 | 986 | } |
987 | ||
988 | void set_limits_from_userattr(char *user) | |
989 | { | |
990 | int mask; | |
991 | char buf[16]; | |
992 | ||
993 | set_limit(user, S_UFSIZE, S_UFSIZE_HARD, RLIMIT_FSIZE, 512); | |
994 | set_limit(user, S_UCPU, S_UCPU_HARD, RLIMIT_CPU, 1); | |
995 | set_limit(user, S_UDATA, S_UDATA_HARD, RLIMIT_DATA, 512); | |
996 | set_limit(user, S_USTACK, S_USTACK_HARD, RLIMIT_STACK, 512); | |
997 | set_limit(user, S_URSS, S_URSS_HARD, RLIMIT_RSS, 512); | |
998 | set_limit(user, S_UCORE, S_UCORE_HARD, RLIMIT_CORE, 512); | |
999 | #if defined(S_UNOFILE) | |
1000 | set_limit(user, S_UNOFILE, S_UNOFILE_HARD, RLIMIT_NOFILE, 1); | |
1001 | #endif | |
1002 | ||
1003 | if (getuserattr(user, S_UMASK, &mask, SEC_INT) != -1) { | |
1004 | /* Convert decimal to octal */ | |
1005 | (void) snprintf(buf, sizeof(buf), "%d", mask); | |
1006 | if (sscanf(buf, "%o", &mask) == 1) | |
1007 | umask(mask); | |
1008 | } | |
1009 | } | |
1010 | #endif /* defined(HAVE_GETUSERATTR) */ | |
1011 | ||
7368a6c8 | 1012 | /* |
1013 | * Performs common processing for the child, such as setting up the | |
1014 | * environment, closing extra file descriptors, setting the user and group | |
1015 | * ids, and executing the command or shell. | |
1016 | */ | |
6ae2364d | 1017 | void |
56b3e9ce | 1018 | do_child(Session *s, const char *command) |
7368a6c8 | 1019 | { |
b5e300c2 | 1020 | const char *shell, *hostname = NULL, *cp = NULL; |
ced49be2 | 1021 | struct passwd *pw = s->pw; |
7368a6c8 | 1022 | char buf[256]; |
6fc1e9f3 | 1023 | char cmd[1024]; |
2e73a022 | 1024 | FILE *f = NULL; |
1e3b8b07 | 1025 | u_int envsize, i; |
7368a6c8 | 1026 | char **env; |
1027 | extern char **environ; | |
1028 | struct stat st; | |
1029 | char *argv[10]; | |
1500bcdd | 1030 | int do_xauth; |
3206bb3b | 1031 | #ifdef WITH_IRIX_PROJECT |
1032 | prid_t projid; | |
1033 | #endif /* WITH_IRIX_PROJECT */ | |
d287c664 | 1034 | #ifdef WITH_IRIX_JOBS |
1035 | jid_t jid = 0; | |
1036 | #else | |
1037 | #ifdef WITH_IRIX_ARRAY | |
1038 | int jid = 0; | |
1039 | #endif /* WITH_IRIX_ARRAY */ | |
1040 | #endif /* WITH_IRIX_JOBS */ | |
1041 | ||
1500bcdd | 1042 | do_xauth = |
1043 | s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL; | |
1044 | ||
e78e738a | 1045 | /* remove hostkey from the child's memory */ |
1046 | destroy_sensitive_data(); | |
1047 | ||
fa649821 | 1048 | /* login(1) is only called if we execute the login shell */ |
1049 | if (options.use_login && command != NULL) | |
1050 | options.use_login = 0; | |
1051 | ||
453b4bd0 | 1052 | #if !defined(HAVE_OSF_SIA) |
2e73a022 | 1053 | if (!options.use_login) { |
1054 | # ifdef HAVE_LOGIN_CAP | |
1055 | if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid) | |
1056 | f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN, | |
1057 | _PATH_NOLOGIN), "r"); | |
1058 | # else /* HAVE_LOGIN_CAP */ | |
1059 | if (pw->pw_uid) | |
1060 | f = fopen(_PATH_NOLOGIN, "r"); | |
1061 | # endif /* HAVE_LOGIN_CAP */ | |
1062 | if (f) { | |
1063 | /* /etc/nologin exists. Print its contents and exit. */ | |
1064 | while (fgets(buf, sizeof(buf), f)) | |
1065 | fputs(buf, stderr); | |
1066 | fclose(f); | |
7368a6c8 | 1067 | exit(254); |
2e73a022 | 1068 | } |
7368a6c8 | 1069 | } |
453b4bd0 | 1070 | #endif /* HAVE_OSF_SIA */ |
7368a6c8 | 1071 | |
2e73a022 | 1072 | /* Set login name, uid, gid, and groups. */ |
7368a6c8 | 1073 | /* Login(1) does this as well, and it needs uid 0 for the "-h" |
1074 | switch, so we let login(1) to this for us. */ | |
1075 | if (!options.use_login) { | |
4d33e531 | 1076 | #ifdef HAVE_OSF_SIA |
7a7101ec | 1077 | session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty); |
c96a4aaf | 1078 | if (!check_quietlogin(s, command)) |
1079 | do_motd(); | |
815800e1 | 1080 | #else /* HAVE_OSF_SIA */ |
3c62e7eb | 1081 | #ifdef HAVE_CYGWIN |
1082 | if (is_winnt) { | |
1083 | #else | |
7368a6c8 | 1084 | if (getuid() == 0 || geteuid() == 0) { |
3c62e7eb | 1085 | #endif |
2e73a022 | 1086 | # ifdef HAVE_GETUSERATTR |
5fa45897 | 1087 | set_limits_from_userattr(pw->pw_name); |
2e73a022 | 1088 | # endif /* HAVE_GETUSERATTR */ |
1089 | # ifdef HAVE_LOGIN_CAP | |
1090 | if (setusercontext(lc, pw, pw->pw_uid, | |
1091 | (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) { | |
1092 | perror("unable to set user context"); | |
1093 | exit(1); | |
1094 | } | |
1095 | # else /* HAVE_LOGIN_CAP */ | |
73d6d7fa | 1096 | #if defined(HAVE_GETLUID) && defined(HAVE_SETLUID) |
1097 | /* Sets login uid for accounting */ | |
1098 | if (getluid() == -1 && setluid(pw->pw_uid) == -1) | |
1099 | error("setluid: %s", strerror(errno)); | |
1100 | #endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */ | |
1101 | ||
2e73a022 | 1102 | if (setlogin(pw->pw_name) < 0) |
1103 | error("setlogin failed: %s", strerror(errno)); | |
7368a6c8 | 1104 | if (setgid(pw->pw_gid) < 0) { |
1105 | perror("setgid"); | |
1106 | exit(1); | |
1107 | } | |
1108 | /* Initialize the group list. */ | |
1109 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) { | |
1110 | perror("initgroups"); | |
1111 | exit(1); | |
1112 | } | |
1113 | endgrent(); | |
9afbfcfa | 1114 | # ifdef USE_PAM |
1115 | /* | |
1116 | * PAM credentials may take the form of | |
1117 | * supplementary groups. These will have been | |
1118 | * wiped by the above initgroups() call. | |
1119 | * Reestablish them here. | |
1120 | */ | |
1121 | do_pam_setcred(0); | |
1122 | # endif /* USE_PAM */ | |
d287c664 | 1123 | # ifdef WITH_IRIX_JOBS |
1124 | jid = jlimit_startjob(pw->pw_name, pw->pw_uid, "interactive"); | |
1125 | if (jid == -1) { | |
1126 | fatal("Failed to create job container: %.100s", | |
1127 | strerror(errno)); | |
2b87da3b | 1128 | } |
d287c664 | 1129 | # endif /* WITH_IRIX_JOBS */ |
2e73a022 | 1130 | # ifdef WITH_IRIX_ARRAY |
3206bb3b | 1131 | /* initialize array session */ |
d287c664 | 1132 | if (jid == 0) { |
1133 | if (newarraysess() != 0) | |
1134 | fatal("Failed to set up new array session: %.100s", | |
1135 | strerror(errno)); | |
1136 | } | |
2e73a022 | 1137 | # endif /* WITH_IRIX_ARRAY */ |
1138 | # ifdef WITH_IRIX_PROJECT | |
3206bb3b | 1139 | /* initialize irix project info */ |
1140 | if ((projid = getdfltprojuser(pw->pw_name)) == -1) { | |
1141 | debug("Failed to get project id, using projid 0"); | |
1142 | projid = 0; | |
1143 | } | |
3206bb3b | 1144 | if (setprid(projid)) |
1145 | fatal("Failed to initialize project %d for %s: %.100s", | |
1146 | (int)projid, pw->pw_name, strerror(errno)); | |
2e73a022 | 1147 | # endif /* WITH_IRIX_PROJECT */ |
260d427b | 1148 | #ifdef WITH_IRIX_AUDIT |
1149 | if (sysconf(_SC_AUDIT)) { | |
1150 | debug("Setting sat id to %d", (int) pw->pw_uid); | |
1151 | if (satsetid(pw->pw_uid)) | |
1152 | debug("error setting satid: %.100s", strerror(errno)); | |
1153 | } | |
1154 | #endif /* WITH_IRIX_AUDIT */ | |
1155 | ||
8a968c25 | 1156 | #ifdef _AIX |
1157 | /* | |
1158 | * AIX has a "usrinfo" area where logname and | |
1159 | * other stuff is stored - a few applications | |
1160 | * actually use this and die if it's not set | |
1161 | */ | |
ee110bfb | 1162 | if (s->ttyfd == -1) |
1163 | s->tty[0] = '\0'; | |
f5f6020e | 1164 | cp = xmalloc(22 + strlen(s->tty) + |
8a968c25 | 1165 | 2 * strlen(pw->pw_name)); |
1166 | i = sprintf(cp, "LOGNAME=%s%cNAME=%s%cTTY=%s%c%c", | |
ee110bfb | 1167 | pw->pw_name, 0, pw->pw_name, 0, s->tty, 0, 0); |
8a968c25 | 1168 | if (usrinfo(SETUINFO, cp, i) == -1) |
1169 | fatal("Couldn't set usrinfo: %s", | |
1170 | strerror(errno)); | |
1171 | debug3("AIX/UsrInfo: set len %d", i); | |
1172 | xfree(cp); | |
1173 | #endif | |
1174 | ||
7368a6c8 | 1175 | /* Permanently switch to the desired uid. */ |
63bd8c36 | 1176 | permanently_set_uid(pw); |
2e73a022 | 1177 | # endif /* HAVE_LOGIN_CAP */ |
7368a6c8 | 1178 | } |
2e73a022 | 1179 | #endif /* HAVE_OSF_SIA */ |
1180 | ||
3c62e7eb | 1181 | #ifdef HAVE_CYGWIN |
1182 | if (is_winnt) | |
1183 | #endif | |
7368a6c8 | 1184 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) |
14a9a859 | 1185 | fatal("Failed to set uids to %u.", (u_int) pw->pw_uid); |
7368a6c8 | 1186 | } |
1187 | /* | |
1188 | * Get the shell from the password data. An empty shell field is | |
1189 | * legal, and means /bin/sh. | |
1190 | */ | |
1191 | shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell; | |
2e73a022 | 1192 | #ifdef HAVE_LOGIN_CAP |
1193 | shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell); | |
1194 | #endif | |
7368a6c8 | 1195 | |
7368a6c8 | 1196 | /* Initialize the environment. */ |
1197 | envsize = 100; | |
1198 | env = xmalloc(envsize * sizeof(char *)); | |
1199 | env[0] = NULL; | |
1200 | ||
3c62e7eb | 1201 | #ifdef HAVE_CYGWIN |
1202 | /* | |
1203 | * The Windows environment contains some setting which are | |
1204 | * important for a running system. They must not be dropped. | |
1205 | */ | |
22d89d24 | 1206 | copy_environment(&env, &envsize); |
3c62e7eb | 1207 | #endif |
1208 | ||
7368a6c8 | 1209 | if (!options.use_login) { |
1210 | /* Set basic environment. */ | |
1211 | child_set_env(&env, &envsize, "USER", pw->pw_name); | |
1212 | child_set_env(&env, &envsize, "LOGNAME", pw->pw_name); | |
1213 | child_set_env(&env, &envsize, "HOME", pw->pw_dir); | |
2e73a022 | 1214 | #ifdef HAVE_LOGIN_CAP |
1215 | (void) setusercontext(lc, pw, pw->pw_uid, LOGIN_SETPATH); | |
1216 | child_set_env(&env, &envsize, "PATH", getenv("PATH")); | |
22d89d24 | 1217 | #else /* HAVE_LOGIN_CAP */ |
1218 | # ifndef HAVE_CYGWIN | |
3c62e7eb | 1219 | /* |
1220 | * There's no standard path on Windows. The path contains | |
1221 | * important components pointing to the system directories, | |
1222 | * needed for loading shared libraries. So the path better | |
1223 | * remains intact here. | |
1224 | */ | |
7368a6c8 | 1225 | child_set_env(&env, &envsize, "PATH", _PATH_STDPATH); |
22d89d24 | 1226 | # endif /* HAVE_CYGWIN */ |
1227 | #endif /* HAVE_LOGIN_CAP */ | |
7368a6c8 | 1228 | |
1229 | snprintf(buf, sizeof buf, "%.200s/%.50s", | |
1230 | _PATH_MAILDIR, pw->pw_name); | |
1231 | child_set_env(&env, &envsize, "MAIL", buf); | |
1232 | ||
1233 | /* Normal systems set SHELL by default. */ | |
1234 | child_set_env(&env, &envsize, "SHELL", shell); | |
1235 | } | |
1236 | if (getenv("TZ")) | |
1237 | child_set_env(&env, &envsize, "TZ", getenv("TZ")); | |
1238 | ||
1239 | /* Set custom environment options from RSA authentication. */ | |
1240 | while (custom_environment) { | |
1241 | struct envstring *ce = custom_environment; | |
1242 | char *s = ce->s; | |
1243 | int i; | |
1244 | for (i = 0; s[i] != '=' && s[i]; i++); | |
1245 | if (s[i] == '=') { | |
1246 | s[i] = 0; | |
1247 | child_set_env(&env, &envsize, s, s + i + 1); | |
1248 | } | |
1249 | custom_environment = ce->next; | |
1250 | xfree(ce->s); | |
1251 | xfree(ce); | |
1252 | } | |
1253 | ||
1254 | snprintf(buf, sizeof buf, "%.50s %d %d", | |
1255 | get_remote_ipaddr(), get_remote_port(), get_local_port()); | |
1256 | child_set_env(&env, &envsize, "SSH_CLIENT", buf); | |
1257 | ||
56b3e9ce | 1258 | if (s->ttyfd != -1) |
1259 | child_set_env(&env, &envsize, "SSH_TTY", s->tty); | |
1260 | if (s->term) | |
1261 | child_set_env(&env, &envsize, "TERM", s->term); | |
1262 | if (s->display) | |
1263 | child_set_env(&env, &envsize, "DISPLAY", s->display); | |
b5e300c2 | 1264 | if (original_command) |
1265 | child_set_env(&env, &envsize, "SSH_ORIGINAL_COMMAND", | |
1266 | original_command); | |
7368a6c8 | 1267 | |
1268 | #ifdef _AIX | |
22d89d24 | 1269 | if ((cp = getenv("AUTHSTATE")) != NULL) |
1270 | child_set_env(&env, &envsize, "AUTHSTATE", cp); | |
1271 | if ((cp = getenv("KRB5CCNAME")) != NULL) | |
1272 | child_set_env(&env, &envsize, "KRB5CCNAME", cp); | |
1273 | read_environment_file(&env, &envsize, "/etc/environment"); | |
7368a6c8 | 1274 | #endif |
7368a6c8 | 1275 | #ifdef KRB4 |
ced49be2 | 1276 | if (s->authctxt->krb4_ticket_file) |
1277 | child_set_env(&env, &envsize, "KRBTKFILE", | |
1278 | s->authctxt->krb4_ticket_file); | |
1279 | #endif | |
1280 | #ifdef KRB5 | |
1281 | if (s->authctxt->krb5_ticket_file) | |
1282 | child_set_env(&env, &envsize, "KRB5CCNAME", | |
1283 | s->authctxt->krb5_ticket_file); | |
1284 | #endif | |
7368a6c8 | 1285 | #ifdef USE_PAM |
1286 | /* Pull in any environment variables that may have been set by PAM. */ | |
1287 | do_pam_environment(&env, &envsize); | |
1288 | #endif /* USE_PAM */ | |
1289 | ||
7368a6c8 | 1290 | if (auth_get_socket_name() != NULL) |
1291 | child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME, | |
1292 | auth_get_socket_name()); | |
1293 | ||
1294 | /* read $HOME/.ssh/environment. */ | |
1295 | if (!options.use_login) { | |
71276795 | 1296 | snprintf(buf, sizeof buf, "%.200s/.ssh/environment", |
1297 | pw->pw_dir); | |
7368a6c8 | 1298 | read_environment_file(&env, &envsize, buf); |
1299 | } | |
1300 | if (debug_flag) { | |
1301 | /* dump the environment */ | |
1302 | fprintf(stderr, "Environment:\n"); | |
1303 | for (i = 0; env[i]; i++) | |
1304 | fprintf(stderr, " %.200s\n", env[i]); | |
1305 | } | |
2e73a022 | 1306 | /* we have to stash the hostname before we close our socket. */ |
1307 | if (options.use_login) | |
46e3af7f | 1308 | hostname = get_remote_name_or_ip(utmp_len, |
1309 | options.reverse_mapping_check); | |
7368a6c8 | 1310 | /* |
1311 | * Close the connection descriptors; note that this is the child, and | |
1312 | * the server will still have the socket open, and it is important | |
1313 | * that we do not shutdown it. Note that the descriptors cannot be | |
1314 | * closed before building the environment, as we call | |
1315 | * get_remote_ipaddr there. | |
1316 | */ | |
1317 | if (packet_get_connection_in() == packet_get_connection_out()) | |
1318 | close(packet_get_connection_in()); | |
1319 | else { | |
1320 | close(packet_get_connection_in()); | |
1321 | close(packet_get_connection_out()); | |
1322 | } | |
1323 | /* | |
1324 | * Close all descriptors related to channels. They will still remain | |
1325 | * open in the parent. | |
1326 | */ | |
1327 | /* XXX better use close-on-exec? -markus */ | |
1328 | channel_close_all(); | |
1329 | ||
1330 | /* | |
1331 | * Close any extra file descriptors. Note that there may still be | |
1332 | * descriptors left by system functions. They will be closed later. | |
1333 | */ | |
1334 | endpwent(); | |
1335 | ||
1336 | /* | |
1337 | * Close any extra open file descriptors so that we don\'t have them | |
1338 | * hanging around in clients. Note that we want to do this after | |
1339 | * initgroups, because at least on Solaris 2.3 it leaves file | |
1340 | * descriptors open. | |
1341 | */ | |
1342 | for (i = 3; i < 64; i++) | |
1343 | close(i); | |
1344 | ||
7368a6c8 | 1345 | /* |
1346 | * Must take new environment into use so that .ssh/rc, /etc/sshrc and | |
1347 | * xauth are run in the proper environment. | |
1348 | */ | |
1349 | environ = env; | |
1350 | ||
6fcf67f7 | 1351 | #ifdef AFS |
1352 | /* Try to get AFS tokens for the local cell. */ | |
1353 | if (k_hasafs()) { | |
1354 | char cell[64]; | |
1355 | ||
1356 | if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0) | |
1357 | krb_afslog(cell, 0); | |
1358 | ||
1359 | krb_afslog(0, 0); | |
1360 | } | |
1361 | #endif /* AFS */ | |
1362 | ||
dab89049 | 1363 | /* Change current directory to the user\'s home directory. */ |
1364 | if (chdir(pw->pw_dir) < 0) { | |
1365 | fprintf(stderr, "Could not chdir to home directory %s: %s\n", | |
1366 | pw->pw_dir, strerror(errno)); | |
1367 | #ifdef HAVE_LOGIN_CAP | |
1368 | if (login_getcapbool(lc, "requirehome", 0)) | |
1369 | exit(1); | |
1370 | #endif | |
1371 | } | |
1372 | ||
7368a6c8 | 1373 | /* |
1374 | * Run $HOME/.ssh/rc, /etc/sshrc, or xauth (whichever is found first | |
1375 | * in this order). | |
1376 | */ | |
1377 | if (!options.use_login) { | |
e78e738a | 1378 | /* ignore _PATH_SSH_USER_RC for subsystems */ |
1379 | if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) { | |
503e7e5b | 1380 | snprintf(cmd, sizeof cmd, "%s -c '%s %s'", |
1381 | shell, _PATH_BSHELL, _PATH_SSH_USER_RC); | |
7368a6c8 | 1382 | if (debug_flag) |
503e7e5b | 1383 | fprintf(stderr, "Running %s\n", cmd); |
1384 | f = popen(cmd, "w"); | |
7368a6c8 | 1385 | if (f) { |
56b3e9ce | 1386 | if (do_xauth) |
1387 | fprintf(f, "%s %s\n", s->auth_proto, | |
1388 | s->auth_data); | |
7368a6c8 | 1389 | pclose(f); |
1390 | } else | |
96c63318 | 1391 | fprintf(stderr, "Could not run %s\n", |
56b3e9ce | 1392 | _PATH_SSH_USER_RC); |
42f11eb2 | 1393 | } else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) { |
7368a6c8 | 1394 | if (debug_flag) |
56b3e9ce | 1395 | fprintf(stderr, "Running %s %s\n", _PATH_BSHELL, |
1396 | _PATH_SSH_SYSTEM_RC); | |
42f11eb2 | 1397 | f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w"); |
7368a6c8 | 1398 | if (f) { |
56b3e9ce | 1399 | if (do_xauth) |
1400 | fprintf(f, "%s %s\n", s->auth_proto, | |
1401 | s->auth_data); | |
7368a6c8 | 1402 | pclose(f); |
1403 | } else | |
56b3e9ce | 1404 | fprintf(stderr, "Could not run %s\n", |
1405 | _PATH_SSH_SYSTEM_RC); | |
1406 | } else if (do_xauth && options.xauth_location != NULL) { | |
7368a6c8 | 1407 | /* Add authority data to .Xauthority if appropriate. */ |
56b3e9ce | 1408 | char *screen = strchr(s->display, ':'); |
1409 | ||
1410 | if (debug_flag) { | |
1411 | fprintf(stderr, | |
1412 | "Running %.100s add " | |
1413 | "%.100s %.100s %.100s\n", | |
1414 | options.xauth_location, s->display, | |
1415 | s->auth_proto, s->auth_data); | |
1416 | if (screen != NULL) | |
71276795 | 1417 | fprintf(stderr, |
56b3e9ce | 1418 | "Adding %.*s/unix%s %s %s\n", |
1419 | (int)(screen - s->display), | |
1420 | s->display, screen, | |
1421 | s->auth_proto, s->auth_data); | |
1422 | } | |
1423 | snprintf(cmd, sizeof cmd, "%s -q -", | |
1424 | options.xauth_location); | |
1425 | f = popen(cmd, "w"); | |
1426 | if (f) { | |
1427 | fprintf(f, "add %s %s %s\n", s->display, | |
1428 | s->auth_proto, s->auth_data); | |
1429 | if (screen != NULL) | |
1430 | fprintf(f, "add %.*s/unix%s %s %s\n", | |
1431 | (int)(screen - s->display), | |
1432 | s->display, screen, | |
1433 | s->auth_proto, | |
1434 | s->auth_data); | |
1435 | pclose(f); | |
1436 | } else { | |
1437 | fprintf(stderr, "Could not run %s\n", | |
1438 | cmd); | |
7368a6c8 | 1439 | } |
1440 | } | |
7368a6c8 | 1441 | /* Get the last component of the shell name. */ |
1442 | cp = strrchr(shell, '/'); | |
1443 | if (cp) | |
1444 | cp++; | |
1445 | else | |
1446 | cp = shell; | |
1447 | } | |
1d3c30db | 1448 | |
1449 | /* restore SIGPIPE for child */ | |
1450 | signal(SIGPIPE, SIG_DFL); | |
1451 | ||
7368a6c8 | 1452 | /* |
1453 | * If we have no command, execute the shell. In this case, the shell | |
1454 | * name to be passed in argv[0] is preceded by '-' to indicate that | |
1455 | * this is a login shell. | |
1456 | */ | |
1457 | if (!command) { | |
1458 | if (!options.use_login) { | |
1459 | char buf[256]; | |
1460 | ||
7368a6c8 | 1461 | /* Start the shell. Set initial character to '-'. */ |
1462 | buf[0] = '-'; | |
1463 | strncpy(buf + 1, cp, sizeof(buf) - 1); | |
1464 | buf[sizeof(buf) - 1] = 0; | |
1465 | ||
1466 | /* Execute the shell. */ | |
1467 | argv[0] = buf; | |
1468 | argv[1] = NULL; | |
1469 | execve(shell, argv, env); | |
1470 | ||
1471 | /* Executing the shell failed. */ | |
1472 | perror(shell); | |
1473 | exit(1); | |
1474 | ||
1475 | } else { | |
1476 | /* Launch login(1). */ | |
1477 | ||
2e73a022 | 1478 | execl(LOGIN_PROGRAM, "login", "-h", hostname, |
7e2d5fa4 | 1479 | #ifdef LOGIN_NEEDS_TERM |
1480 | s->term? s->term : "unknown", | |
1481 | #endif | |
ed916b28 | 1482 | "-p", "-f", "--", pw->pw_name, (char *)NULL); |
7368a6c8 | 1483 | |
1484 | /* Login couldn't be executed, die. */ | |
1485 | ||
1486 | perror("login"); | |
1487 | exit(1); | |
1488 | } | |
1489 | } | |
1490 | /* | |
1491 | * Execute the command using the user's shell. This uses the -c | |
1492 | * option to execute the command. | |
1493 | */ | |
1494 | argv[0] = (char *) cp; | |
1495 | argv[1] = "-c"; | |
1496 | argv[2] = (char *) command; | |
1497 | argv[3] = NULL; | |
1498 | execve(shell, argv, env); | |
1499 | perror(shell); | |
1500 | exit(1); | |
1501 | } | |
1502 | ||
1503 | Session * | |
1504 | session_new(void) | |
1505 | { | |
1506 | int i; | |
1507 | static int did_init = 0; | |
1508 | if (!did_init) { | |
1509 | debug("session_new: init"); | |
1510 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1511 | sessions[i].used = 0; | |
7368a6c8 | 1512 | } |
1513 | did_init = 1; | |
1514 | } | |
1515 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1516 | Session *s = &sessions[i]; | |
1517 | if (! s->used) { | |
690d0d7f | 1518 | memset(s, 0, sizeof(*s)); |
7368a6c8 | 1519 | s->chanid = -1; |
1520 | s->ptyfd = -1; | |
1521 | s->ttyfd = -1; | |
7368a6c8 | 1522 | s->used = 1; |
86066315 | 1523 | s->self = i; |
02323c45 | 1524 | debug("session_new: session %d", i); |
7368a6c8 | 1525 | return s; |
1526 | } | |
1527 | } | |
1528 | return NULL; | |
1529 | } | |
1530 | ||
396c147e | 1531 | static void |
7368a6c8 | 1532 | session_dump(void) |
1533 | { | |
1534 | int i; | |
1535 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1536 | Session *s = &sessions[i]; | |
1537 | debug("dump: used %d session %d %p channel %d pid %d", | |
1538 | s->used, | |
1539 | s->self, | |
1540 | s, | |
1541 | s->chanid, | |
1542 | s->pid); | |
1543 | } | |
1544 | } | |
1545 | ||
e78a59f5 | 1546 | int |
57156994 | 1547 | session_open(Authctxt *authctxt, int chanid) |
e78a59f5 | 1548 | { |
1549 | Session *s = session_new(); | |
1550 | debug("session_open: channel %d", chanid); | |
1551 | if (s == NULL) { | |
1552 | error("no more sessions"); | |
1553 | return 0; | |
1554 | } | |
57156994 | 1555 | s->authctxt = authctxt; |
1556 | s->pw = authctxt->pw; | |
e78a59f5 | 1557 | if (s->pw == NULL) |
0725ea77 | 1558 | fatal("no user for session %d", s->self); |
0b242b12 | 1559 | debug("session_open: session %d: link with channel %d", s->self, chanid); |
1560 | s->chanid = chanid; | |
e78a59f5 | 1561 | return 1; |
1562 | } | |
1563 | ||
396c147e | 1564 | static Session * |
e78a59f5 | 1565 | session_by_channel(int id) |
1566 | { | |
1567 | int i; | |
1568 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1569 | Session *s = &sessions[i]; | |
1570 | if (s->used && s->chanid == id) { | |
1571 | debug("session_by_channel: session %d channel %d", i, id); | |
1572 | return s; | |
1573 | } | |
1574 | } | |
1575 | debug("session_by_channel: unknown channel %d", id); | |
1576 | session_dump(); | |
1577 | return NULL; | |
1578 | } | |
1579 | ||
396c147e | 1580 | static Session * |
e78a59f5 | 1581 | session_by_pid(pid_t pid) |
1582 | { | |
1583 | int i; | |
1584 | debug("session_by_pid: pid %d", pid); | |
1585 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1586 | Session *s = &sessions[i]; | |
1587 | if (s->used && s->pid == pid) | |
1588 | return s; | |
1589 | } | |
1590 | error("session_by_pid: unknown pid %d", pid); | |
1591 | session_dump(); | |
1592 | return NULL; | |
1593 | } | |
1594 | ||
396c147e | 1595 | static int |
e78a59f5 | 1596 | session_window_change_req(Session *s) |
1597 | { | |
1598 | s->col = packet_get_int(); | |
1599 | s->row = packet_get_int(); | |
1600 | s->xpixel = packet_get_int(); | |
1601 | s->ypixel = packet_get_int(); | |
6ae2364d | 1602 | packet_done(); |
e78a59f5 | 1603 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
1604 | return 1; | |
1605 | } | |
1606 | ||
396c147e | 1607 | static int |
e78a59f5 | 1608 | session_pty_req(Session *s) |
1609 | { | |
1e3b8b07 | 1610 | u_int len; |
30dcc918 | 1611 | int n_bytes; |
e78a59f5 | 1612 | |
653d5f86 | 1613 | if (no_pty_flag) { |
1614 | debug("Allocating a pty not permitted for this authentication."); | |
38c295d6 | 1615 | return 0; |
653d5f86 | 1616 | } |
1617 | if (s->ttyfd != -1) { | |
1618 | packet_disconnect("Protocol error: you already have a pty."); | |
6ae2364d | 1619 | return 0; |
653d5f86 | 1620 | } |
1621 | ||
e78a59f5 | 1622 | s->term = packet_get_string(&len); |
653d5f86 | 1623 | |
1624 | if (compat20) { | |
1625 | s->col = packet_get_int(); | |
1626 | s->row = packet_get_int(); | |
1627 | } else { | |
1628 | s->row = packet_get_int(); | |
1629 | s->col = packet_get_int(); | |
1630 | } | |
e78a59f5 | 1631 | s->xpixel = packet_get_int(); |
1632 | s->ypixel = packet_get_int(); | |
1633 | ||
1634 | if (strcmp(s->term, "") == 0) { | |
1635 | xfree(s->term); | |
1636 | s->term = NULL; | |
1637 | } | |
653d5f86 | 1638 | |
e78a59f5 | 1639 | /* Allocate a pty and open it. */ |
653d5f86 | 1640 | debug("Allocating pty."); |
e78a59f5 | 1641 | if (!pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty))) { |
653d5f86 | 1642 | if (s->term) |
1643 | xfree(s->term); | |
e78a59f5 | 1644 | s->term = NULL; |
1645 | s->ptyfd = -1; | |
1646 | s->ttyfd = -1; | |
1647 | error("session_pty_req: session %d alloc failed", s->self); | |
6ae2364d | 1648 | return 0; |
e78a59f5 | 1649 | } |
1650 | debug("session_pty_req: session %d alloc %s", s->self, s->tty); | |
653d5f86 | 1651 | |
1652 | /* for SSH1 the tty modes length is not given */ | |
1653 | if (!compat20) | |
1654 | n_bytes = packet_remaining(); | |
1655 | tty_parse_modes(s->ttyfd, &n_bytes); | |
1656 | ||
e78a59f5 | 1657 | /* |
1658 | * Add a cleanup function to clear the utmp entry and record logout | |
1659 | * time in case we call fatal() (e.g., the connection gets closed). | |
1660 | */ | |
86066315 | 1661 | fatal_add_cleanup(session_pty_cleanup, (void *)s); |
e78a59f5 | 1662 | pty_setowner(s->pw, s->tty); |
653d5f86 | 1663 | |
1664 | /* Set window size from the packet. */ | |
e78a59f5 | 1665 | pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); |
1666 | ||
30dcc918 | 1667 | packet_done(); |
1d1ffb87 | 1668 | session_proctitle(s); |
e78a59f5 | 1669 | return 1; |
1670 | } | |
1671 | ||
396c147e | 1672 | static int |
0b242b12 | 1673 | session_subsystem_req(Session *s) |
1674 | { | |
01855277 | 1675 | struct stat st; |
1e3b8b07 | 1676 | u_int len; |
0b242b12 | 1677 | int success = 0; |
01855277 | 1678 | char *cmd, *subsys = packet_get_string(&len); |
38c295d6 | 1679 | int i; |
0b242b12 | 1680 | |
1681 | packet_done(); | |
1682 | log("subsystem request for %s", subsys); | |
1683 | ||
38c295d6 | 1684 | for (i = 0; i < options.num_subsystems; i++) { |
01855277 | 1685 | if (strcmp(subsys, options.subsystem_name[i]) == 0) { |
1686 | cmd = options.subsystem_command[i]; | |
1687 | if (stat(cmd, &st) < 0) { | |
1688 | error("subsystem: cannot stat %s: %s", cmd, | |
1689 | strerror(errno)); | |
1690 | break; | |
1691 | } | |
1692 | debug("subsystem: exec() %s", cmd); | |
e5d7a405 | 1693 | s->is_subsystem = 1; |
01855277 | 1694 | do_exec(s, cmd); |
38c295d6 | 1695 | success = 1; |
1696 | } | |
1697 | } | |
1698 | ||
1699 | if (!success) | |
01855277 | 1700 | log("subsystem request for %s failed, subsystem not found", |
1701 | subsys); | |
38c295d6 | 1702 | |
0b242b12 | 1703 | xfree(subsys); |
1704 | return success; | |
1705 | } | |
1706 | ||
396c147e | 1707 | static int |
0b242b12 | 1708 | session_x11_req(Session *s) |
1709 | { | |
8dcd9d5c | 1710 | int success; |
0b242b12 | 1711 | |
1712 | s->single_connection = packet_get_char(); | |
1713 | s->auth_proto = packet_get_string(NULL); | |
1714 | s->auth_data = packet_get_string(NULL); | |
1715 | s->screen = packet_get_int(); | |
1716 | packet_done(); | |
1717 | ||
8dcd9d5c | 1718 | success = session_setup_x11fwd(s); |
1719 | if (!success) { | |
0b242b12 | 1720 | xfree(s->auth_proto); |
1721 | xfree(s->auth_data); | |
1500bcdd | 1722 | s->auth_proto = NULL; |
1723 | s->auth_data = NULL; | |
0b242b12 | 1724 | } |
8dcd9d5c | 1725 | return success; |
0b242b12 | 1726 | } |
1727 | ||
396c147e | 1728 | static int |
38c295d6 | 1729 | session_shell_req(Session *s) |
1730 | { | |
38c295d6 | 1731 | packet_done(); |
dac6753b | 1732 | do_exec(s, NULL); |
38c295d6 | 1733 | return 1; |
1734 | } | |
1735 | ||
396c147e | 1736 | static int |
38c295d6 | 1737 | session_exec_req(Session *s) |
1738 | { | |
1e3b8b07 | 1739 | u_int len; |
38c295d6 | 1740 | char *command = packet_get_string(&len); |
1741 | packet_done(); | |
dac6753b | 1742 | do_exec(s, command); |
c95add71 | 1743 | xfree(command); |
38c295d6 | 1744 | return 1; |
1745 | } | |
1746 | ||
396c147e | 1747 | static int |
fa08c86b | 1748 | session_auth_agent_req(Session *s) |
1749 | { | |
1750 | static int called = 0; | |
1751 | packet_done(); | |
d343d900 | 1752 | if (no_agent_forwarding_flag) { |
1753 | debug("session_auth_agent_req: no_agent_forwarding_flag"); | |
1754 | return 0; | |
1755 | } | |
fa08c86b | 1756 | if (called) { |
1757 | return 0; | |
1758 | } else { | |
1759 | called = 1; | |
1760 | return auth_input_request_forwarding(s->pw); | |
1761 | } | |
1762 | } | |
1763 | ||
e78a59f5 | 1764 | void |
1765 | session_input_channel_req(int id, void *arg) | |
1766 | { | |
1e3b8b07 | 1767 | u_int len; |
e78a59f5 | 1768 | int reply; |
1769 | int success = 0; | |
1770 | char *rtype; | |
1771 | Session *s; | |
1772 | Channel *c; | |
1773 | ||
1774 | rtype = packet_get_string(&len); | |
1775 | reply = packet_get_char(); | |
1776 | ||
1777 | s = session_by_channel(id); | |
1778 | if (s == NULL) | |
1779 | fatal("session_input_channel_req: channel %d: no session", id); | |
1780 | c = channel_lookup(id); | |
1781 | if (c == NULL) | |
1782 | fatal("session_input_channel_req: channel %d: bad channel", id); | |
1783 | ||
1784 | debug("session_input_channel_req: session %d channel %d request %s reply %d", | |
1785 | s->self, id, rtype, reply); | |
1786 | ||
1787 | /* | |
e5d7a405 | 1788 | * a session is in LARVAL state until a shell, a command |
1789 | * or a subsystem is executed | |
e78a59f5 | 1790 | */ |
1791 | if (c->type == SSH_CHANNEL_LARVAL) { | |
1792 | if (strcmp(rtype, "shell") == 0) { | |
38c295d6 | 1793 | success = session_shell_req(s); |
e78a59f5 | 1794 | } else if (strcmp(rtype, "exec") == 0) { |
38c295d6 | 1795 | success = session_exec_req(s); |
e78a59f5 | 1796 | } else if (strcmp(rtype, "pty-req") == 0) { |
35484284 | 1797 | success = session_pty_req(s); |
0b242b12 | 1798 | } else if (strcmp(rtype, "x11-req") == 0) { |
1799 | success = session_x11_req(s); | |
fa08c86b | 1800 | } else if (strcmp(rtype, "auth-agent-req@openssh.com") == 0) { |
1801 | success = session_auth_agent_req(s); | |
0b242b12 | 1802 | } else if (strcmp(rtype, "subsystem") == 0) { |
1803 | success = session_subsystem_req(s); | |
e78a59f5 | 1804 | } |
1805 | } | |
1806 | if (strcmp(rtype, "window-change") == 0) { | |
1807 | success = session_window_change_req(s); | |
1808 | } | |
1809 | ||
1810 | if (reply) { | |
1811 | packet_start(success ? | |
1812 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); | |
1813 | packet_put_int(c->remote_id); | |
1814 | packet_send(); | |
1815 | } | |
1816 | xfree(rtype); | |
1817 | } | |
1818 | ||
1819 | void | |
1820 | session_set_fds(Session *s, int fdin, int fdout, int fderr) | |
1821 | { | |
1822 | if (!compat20) | |
1823 | fatal("session_set_fds: called for proto != 2.0"); | |
1824 | /* | |
1825 | * now that have a child and a pipe to the child, | |
1826 | * we can activate our channel and register the fd's | |
1827 | */ | |
1828 | if (s->chanid == -1) | |
1829 | fatal("no channel for session %d", s->self); | |
1830 | channel_set_fds(s->chanid, | |
1831 | fdout, fdin, fderr, | |
a22aff1f | 1832 | fderr == -1 ? CHAN_EXTENDED_IGNORE : CHAN_EXTENDED_READ, |
1833 | 1); | |
e78a59f5 | 1834 | } |
1835 | ||
86066315 | 1836 | /* |
1837 | * Function to perform pty cleanup. Also called if we get aborted abnormally | |
1838 | * (e.g., due to a dropped connection). | |
1839 | */ | |
396c147e | 1840 | static void |
86066315 | 1841 | session_pty_cleanup(void *session) |
7368a6c8 | 1842 | { |
86066315 | 1843 | Session *s = session; |
1844 | ||
1845 | if (s == NULL) { | |
1846 | error("session_pty_cleanup: no session"); | |
1847 | return; | |
1848 | } | |
1849 | if (s->ttyfd == -1) | |
7368a6c8 | 1850 | return; |
1851 | ||
0725ea77 | 1852 | debug("session_pty_cleanup: session %d release %s", s->self, s->tty); |
7368a6c8 | 1853 | |
7368a6c8 | 1854 | /* Record that the user has logged out. */ |
86066315 | 1855 | if (s->pid != 0) |
1856 | record_logout(s->pid, s->tty); | |
7368a6c8 | 1857 | |
1858 | /* Release the pseudo-tty. */ | |
1859 | pty_release(s->tty); | |
1860 | ||
1861 | /* | |
1862 | * Close the server side of the socket pairs. We must do this after | |
1863 | * the pty cleanup, so that another process doesn't get this pty | |
1864 | * while we're still cleaning up. | |
1865 | */ | |
1866 | if (close(s->ptymaster) < 0) | |
1867 | error("close(s->ptymaster): %s", strerror(errno)); | |
1868 | } | |
e78a59f5 | 1869 | |
396c147e | 1870 | static void |
e78a59f5 | 1871 | session_exit_message(Session *s, int status) |
1872 | { | |
1873 | Channel *c; | |
1874 | if (s == NULL) | |
1875 | fatal("session_close: no session"); | |
1876 | c = channel_lookup(s->chanid); | |
1877 | if (c == NULL) | |
86066315 | 1878 | fatal("session_exit_message: session %d: no channel %d", |
e78a59f5 | 1879 | s->self, s->chanid); |
1880 | debug("session_exit_message: session %d channel %d pid %d", | |
1881 | s->self, s->chanid, s->pid); | |
1882 | ||
1883 | if (WIFEXITED(status)) { | |
1884 | channel_request_start(s->chanid, | |
1885 | "exit-status", 0); | |
1886 | packet_put_int(WEXITSTATUS(status)); | |
1887 | packet_send(); | |
1888 | } else if (WIFSIGNALED(status)) { | |
1889 | channel_request_start(s->chanid, | |
1890 | "exit-signal", 0); | |
1891 | packet_put_int(WTERMSIG(status)); | |
a64009ad | 1892 | #ifdef WCOREDUMP |
e78a59f5 | 1893 | packet_put_char(WCOREDUMP(status)); |
a64009ad | 1894 | #else /* WCOREDUMP */ |
1895 | packet_put_char(0); | |
1896 | #endif /* WCOREDUMP */ | |
e78a59f5 | 1897 | packet_put_cstring(""); |
1898 | packet_put_cstring(""); | |
1899 | packet_send(); | |
1900 | } else { | |
1901 | /* Some weird exit cause. Just exit. */ | |
1902 | packet_disconnect("wait returned status %04x.", status); | |
1903 | } | |
1904 | ||
1905 | /* disconnect channel */ | |
1906 | debug("session_exit_message: release channel %d", s->chanid); | |
1907 | channel_cancel_cleanup(s->chanid); | |
9da5c3c9 | 1908 | /* |
1909 | * emulate a write failure with 'chan_write_failed', nobody will be | |
1910 | * interested in data we write. | |
1911 | * Note that we must not call 'chan_read_failed', since there could | |
1912 | * be some more data waiting in the pipe. | |
1913 | */ | |
0b242b12 | 1914 | if (c->ostate != CHAN_OUTPUT_CLOSED) |
1915 | chan_write_failed(c); | |
e78a59f5 | 1916 | s->chanid = -1; |
1917 | } | |
1918 | ||
396c147e | 1919 | static void |
86066315 | 1920 | session_close(Session *s) |
e78a59f5 | 1921 | { |
86066315 | 1922 | debug("session_close: session %d pid %d", s->self, s->pid); |
1923 | if (s->ttyfd != -1) { | |
1924 | fatal_remove_cleanup(session_pty_cleanup, (void *)s); | |
1925 | session_pty_cleanup(s); | |
1926 | } | |
e78a59f5 | 1927 | if (s->term) |
1928 | xfree(s->term); | |
1929 | if (s->display) | |
1930 | xfree(s->display); | |
1931 | if (s->auth_data) | |
1932 | xfree(s->auth_data); | |
1933 | if (s->auth_proto) | |
1934 | xfree(s->auth_proto); | |
1935 | s->used = 0; | |
1d1ffb87 | 1936 | session_proctitle(s); |
e78a59f5 | 1937 | } |
1938 | ||
1939 | void | |
1940 | session_close_by_pid(pid_t pid, int status) | |
1941 | { | |
1942 | Session *s = session_by_pid(pid); | |
1943 | if (s == NULL) { | |
eb26141e | 1944 | debug("session_close_by_pid: no session for pid %d", pid); |
e78a59f5 | 1945 | return; |
1946 | } | |
1947 | if (s->chanid != -1) | |
1948 | session_exit_message(s, status); | |
1949 | session_close(s); | |
1950 | } | |
1951 | ||
1952 | /* | |
1953 | * this is called when a channel dies before | |
1954 | * the session 'child' itself dies | |
1955 | */ | |
1956 | void | |
1957 | session_close_by_channel(int id, void *arg) | |
1958 | { | |
1959 | Session *s = session_by_channel(id); | |
1960 | if (s == NULL) { | |
1961 | debug("session_close_by_channel: no session for channel %d", id); | |
1962 | return; | |
1963 | } | |
1964 | /* disconnect channel */ | |
1965 | channel_cancel_cleanup(s->chanid); | |
1966 | s->chanid = -1; | |
1967 | ||
1968 | debug("session_close_by_channel: channel %d kill %d", id, s->pid); | |
d5f24f94 | 1969 | if (s->pid != 0) { |
1970 | /* notify child */ | |
1971 | if (kill(s->pid, SIGHUP) < 0) | |
e78a59f5 | 1972 | error("session_close_by_channel: kill %d: %s", |
1973 | s->pid, strerror(errno)); | |
1974 | } | |
d5f24f94 | 1975 | session_close(s); |
1976 | } | |
1977 | ||
1978 | void | |
1979 | session_close_all(void) | |
1980 | { | |
1981 | int i; | |
1982 | for(i = 0; i < MAX_SESSIONS; i++) { | |
1983 | Session *s = &sessions[i]; | |
1984 | if (s->used) { | |
1985 | if (s->chanid != -1) { | |
1986 | channel_cancel_cleanup(s->chanid); | |
1987 | s->chanid = -1; | |
1988 | } | |
1989 | session_close(s); | |
1990 | } | |
1991 | } | |
e78a59f5 | 1992 | } |
1993 | ||
396c147e | 1994 | static char * |
1d1ffb87 | 1995 | session_tty_list(void) |
1996 | { | |
1997 | static char buf[1024]; | |
1998 | int i; | |
1999 | buf[0] = '\0'; | |
2000 | for(i = 0; i < MAX_SESSIONS; i++) { | |
2001 | Session *s = &sessions[i]; | |
2002 | if (s->used && s->ttyfd != -1) { | |
2003 | if (buf[0] != '\0') | |
2004 | strlcat(buf, ",", sizeof buf); | |
2005 | strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf); | |
2006 | } | |
2007 | } | |
2008 | if (buf[0] == '\0') | |
2009 | strlcpy(buf, "notty", sizeof buf); | |
2010 | return buf; | |
2011 | } | |
2012 | ||
2013 | void | |
2014 | session_proctitle(Session *s) | |
2015 | { | |
2016 | if (s->pw == NULL) | |
2017 | error("no user for session %d", s->self); | |
2018 | else | |
2019 | setproctitle("%s@%s", s->pw->pw_name, session_tty_list()); | |
2020 | } | |
2021 | ||
8dcd9d5c | 2022 | int |
2023 | session_setup_x11fwd(Session *s) | |
2024 | { | |
8dcd9d5c | 2025 | struct stat st; |
2026 | ||
2027 | if (no_x11_forwarding_flag) { | |
2028 | packet_send_debug("X11 forwarding disabled in user configuration file."); | |
2029 | return 0; | |
2030 | } | |
2031 | if (!options.x11_forwarding) { | |
2032 | debug("X11 forwarding disabled in server configuration file."); | |
2033 | return 0; | |
2034 | } | |
2035 | if (!options.xauth_location || | |
2036 | (stat(options.xauth_location, &st) == -1)) { | |
2037 | packet_send_debug("No xauth program; cannot forward with spoofing."); | |
2038 | return 0; | |
2039 | } | |
ff027d84 | 2040 | if (options.use_login) { |
2041 | packet_send_debug("X11 forwarding disabled; " | |
2042 | "not compatible with UseLogin=yes."); | |
2043 | return 0; | |
2044 | } | |
b44de2b1 | 2045 | if (s->display != NULL) { |
8dcd9d5c | 2046 | debug("X11 display already set."); |
2047 | return 0; | |
2048 | } | |
8dcd9d5c | 2049 | s->display = x11_create_display_inet(s->screen, options.x11_display_offset); |
2050 | if (s->display == NULL) { | |
b44de2b1 | 2051 | debug("x11_create_display_inet failed."); |
8dcd9d5c | 2052 | return 0; |
2053 | } | |
8dcd9d5c | 2054 | return 1; |
2055 | } | |
2056 | ||
396c147e | 2057 | static void |
59c97189 | 2058 | do_authenticated2(Authctxt *authctxt) |
e78a59f5 | 2059 | { |
57156994 | 2060 | server_loop2(authctxt); |
e78a59f5 | 2061 | } |