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