reindent-20030715
[openafs.git] / src / inetd / inetd.c
1 /* inetd.c */
2
3 /*
4  * Copyright (c) 1983 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by the University of California, Berkeley.  The name of the
13  * University may not be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 /*
21  * Inetd - Internet super-server
22  *
23  * This program invokes all internet services as needed.
24  * connection-oriented services are invoked each time a
25  * connection is made, by creating a process.  This process
26  * is passed the connection as file descriptor 0 and is
27  * expected to do a getpeername to find out the source host
28  * and port.
29  *
30  * Datagram oriented services are invoked when a datagram
31  * arrives; a process is created and passed a pending message
32  * on file descriptor 0.  Datagram servers may either connect
33  * to their peer, freeing up the original socket for inetd
34  * to receive further messages on, or ``take over the socket'',
35  * processing all arriving datagrams and, eventually, timing
36  * out.  The first type of server is said to be ``multi-threaded'';
37  * the second type of server ``single-threaded''. 
38  *
39  * Inetd uses a configuration file which is read at startup
40  * and, possibly, at some later time in response to a hangup signal.
41  * The configuration file is ``free format'' with fields given in the
42  * order shown below.  Continuation lines for an entry must being with
43  * a space or tab.  All fields must be present in each entry.
44  *
45  *      service name                    must be in /etc/services
46  *      socket type                     stream/dgram/raw/rdm/seqpacket
47  *      protocol                        must be in /etc/protocols
48  *      wait/nowait                     single-threaded/multi-threaded
49  *      user                            user to run daemon as
50  *      server program                  full path name
51  *      server program arguments        maximum of MAXARGS (5)
52  *
53  * Comment lines are indicated by a `#' in column 1.
54  */
55 #include <afsconfig.h>
56 #include <afs/param.h>
57
58 RCSID
59     ("$Header$");
60
61 #include <sys/param.h>
62 #include <sys/stat.h>
63 #ifdef AFS_SUN5_ENV
64 #define BSD_COMP
65 #endif
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/file.h>
69 #ifdef AFS_SUN5_ENV
70 #include <fcntl.h>
71 #endif
72 #include <sys/wait.h>
73 #include <sys/time.h>
74 #include <sys/resource.h>
75
76 #include <netinet/in.h>
77 #include <arpa/inet.h>
78
79 #include <afs/auth.h>
80 #include <afs/cellconfig.h>
81 #include <afs/afsutil.h>
82 #include <errno.h>
83 #include <signal.h>
84 #include <netdb.h>
85 #ifdef AIX
86 #include <sys/syslog.h>
87 #else
88 #include <syslog.h>
89 #endif /* AIX */
90 #include <pwd.h>
91 #include <stdio.h>
92 #include <ctype.h>
93 #include <string.h>
94 #include <stdlib.h>
95 #include <time.h>
96
97 #define TOOMANY         40      /* don't start more than TOOMANY */
98 #define CNT_INTVL       60      /* servers in CNT_INTVL sec. */
99 #define RETRYTIME       (60*10) /* retry after bind or server fail */
100
101 void config(), reapchild(), retry();
102
103 int debug = 0;
104 int nsock, maxsock;
105 fd_set allsock;
106 int options;
107 int timingout;
108 struct servent *sp;
109
110 struct servtab {
111     char *se_service;           /* name of service */
112     int se_socktype;            /* type of socket to use */
113     char *se_proto;             /* protocol used */
114     short se_wait;              /* single threaded server */
115     short se_checked;           /* looked at during merge */
116     char *se_user;              /* user name to run as */
117     struct biltin *se_bi;       /* if built-in, description */
118     char *se_server;            /* server program */
119 #define MAXARGV 5
120     char *se_argv[MAXARGV + 1]; /* program arguments */
121     int se_fd;                  /* open descriptor */
122     struct sockaddr_in se_ctrladdr;     /* bound address */
123     int se_count;               /* number started since se_time */
124     struct timeval se_time;     /* start of se_count */
125     struct servtab *se_next;
126 } *servtab;
127
128 int echo_stream(), discard_stream(), machtime_stream();
129 int daytime_stream(), chargen_stream();
130 int echo_dg(), discard_dg(), machtime_dg(), daytime_dg(), chargen_dg();
131 int auth_stream(), auth_dg();
132
133 struct biltin {
134     char *bi_service;           /* internally provided service name */
135     int bi_socktype;            /* type of socket supported */
136     short bi_fork;              /* 1 if should fork before call */
137     short bi_wait;              /* 1 if should wait for child */
138     int (*bi_fn) ();            /* function which performs it */
139 } biltins[] = {
140     /* Echo received data */
141     "echo", SOCK_STREAM, 1, 0, echo_stream, "echo", SOCK_DGRAM, 0, 0, echo_dg,
142         /* Internet /dev/null */
143         "discard", SOCK_STREAM, 1, 0, discard_stream, "discard", SOCK_DGRAM,
144         0, 0, discard_dg,
145         /* Return 32 bit time since 1970 */
146         "time", SOCK_STREAM, 0, 0, machtime_stream, "time", SOCK_DGRAM, 0, 0,
147         machtime_dg,
148         /* Return human-readable time */
149         "daytime", SOCK_STREAM, 0, 0, daytime_stream, "daytime", SOCK_DGRAM,
150         0, 0, daytime_dg,
151         /* Familiar character generator */
152         "chargen", SOCK_STREAM, 1, 0, chargen_stream, "chargen", SOCK_DGRAM,
153         0, 0, chargen_dg,
154         /* Remote authentication services */
155 "ta-rauth", SOCK_STREAM, 1, 0, auth_stream, "ta-rauth", SOCK_DGRAM, 0,
156         0, auth_dg, 0};
157
158 #define NUMINT  (sizeof(intab) / sizeof(struct inent))
159 char *CONFIG = "/etc/inetd.conf";
160 char **Argv;
161 char *LastArg;
162 int backlog = 10;               /* listen() queue length */
163
164 #include "AFS_component_version_number.c"
165
166 long allZeroes[100];
167 sigset_t sigNone;
168 sigset_t sigBlock;
169
170 int afs_didsetpag = 0;
171 main(argc, argv, envp)
172      int argc;
173      char *argv[], *envp[];
174 {
175     extern char *optarg;
176     extern int optind;
177     register struct servtab *sep;
178     register struct passwd *pwd;
179     struct passwd *getpwnam();
180     register int tmpint;
181     struct sigaction sa;
182     int ch, pid, dofork;
183     char buf[50];
184 #if defined(AFS_HPUX_ENV)
185     int consoleFid;
186     pid_t newSessionID;
187 #endif /* defined(AFS_HPUX_ENV) */
188
189     memset((char *)allZeroes, '\0', sizeof(allZeroes));
190     memset((char *)allZeroes, 0, sizeof(allZeroes));
191
192     sigNone = *((sigset_t *) allZeroes);
193     allZeroes[0] =
194         (1 << (SIGCHLD - 1)) + (1 << (SIGHUP - 1)) + (1 << (SIGALRM - 1));
195     sigBlock = *((sigset_t *) allZeroes);
196
197     setpag();                   /* disassociate with PAG of person starting inetd */
198
199     Argv = argv;
200     if (envp == 0 || *envp == 0)
201         envp = argv;
202     while (*envp)
203         envp++;
204     LastArg = envp[-1] + strlen(envp[-1]);
205
206     while ((ch = getopt(argc, argv, "dl:")) != EOF)
207         switch (ch) {
208         case 'd':
209             debug = 1;
210             options |= SO_DEBUG;
211             break;
212         case 'l':
213             /* undocumented option to set listen() queue length */
214             backlog = atoi(optarg);
215             break;
216         case '?':
217         default:
218             fprintf(stderr, "usage: inetd [-d]");
219             exit(1);
220         }
221     argc -= optind;
222     argv += optind;
223
224     if (argc > 0)
225         CONFIG = argv[0];
226     if (debug == 0) {
227 #if     defined(AFS_OSF_ENV) && !defined(AFS_OSF32_ENV)
228         daemon(0, 0);
229 #else
230         if (fork()) {
231             exit(0);
232         }
233 #ifdef AFS_HPUX_ENV
234         for (tmpint = 0; tmpint < getnumfds(); tmpint++)
235 #else
236         for (tmpint = 0; tmpint < 10; tmpint++)
237 #endif
238             (void)close(tmpint);
239         (void)open("/", O_RDONLY);
240         (void)dup2(0, 1);
241         (void)dup2(0, 2);
242 #ifndef AFS_HPUX_ENV
243         tmpint = open("/dev/tty", O_RDWR);
244         if (tmpint > 0) {
245             ioctl(tmpint, TIOCNOTTY, NULL);
246             close(tmpint);
247         }
248 #else
249 #ifdef  notdef
250         /*
251          * the way to get rid of the controlling terminal in hp-ux, if we
252          * are not a process group leader
253          */
254         newSessionID = setsid();
255         if (newSessionID == -1) {
256             /*
257              * we are already a process group leader, & extensive experimentation
258              * indicates that (contrary to the documentation, once again), there
259              * doesn't seem to be any way to get rid of our control tty, other
260              * than the following ugliness:
261              */
262             if (fork())
263                 exit(0);
264         }
265 #endif
266 #endif
267 #ifdef  AFS_HPUX_ENV
268         (void)setpgrp();
269 #else
270         (void)setpgrp(0, 0);
271 #endif
272
273 #if !defined(AIX)
274         (void)signal(SIGTSTP, SIG_IGN);
275         (void)signal(SIGTTIN, SIG_IGN);
276         (void)signal(SIGTTOU, SIG_IGN);
277 #endif /* !defined(AIX) */
278 #endif /* AFS_OSF_ENV */
279     }
280
281     openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
282
283     memset((char *)&sa, '\0', sizeof(sa));
284     sa.sa_mask = sigBlock;
285     sa.sa_handler = retry;
286     sigaction(SIGALRM, &sa, NULL);
287     config();
288     sa.sa_handler = config;
289     sigaction(SIGHUP, &sa, NULL);
290     sa.sa_handler = reapchild;
291     sigaction(SIGCHLD, &sa, NULL);
292     {
293         /* space for daemons to overwrite environment for ps */
294 #define DUMMYSIZE       100
295         char dummy[DUMMYSIZE];
296
297         (void)memset(dummy, 'x', sizeof(DUMMYSIZE) - 1);
298         dummy[DUMMYSIZE - 1] = '\0';
299         (void)setenv("inetd_dummy", dummy, 1);
300     }
301
302     for (;;) {
303         int n, ctrl;
304         fd_set readable;
305
306         if (nsock == 0) {
307             (void)sigprocmask(SIG_BLOCK, &sigBlock, (sigset_t *) 0);
308             while (nsock == 0)
309                 sigpause(0L);
310             (void)sigprocmask(SIG_SETMASK, &sigNone, (sigset_t *) 0);
311         }
312         readable = allsock;
313         if ((n =
314              select(maxsock + 1, &readable, (fd_set *) 0, (fd_set *) 0,
315                     NULL)) <= 0) {
316             if (n < 0 && errno != EINTR)
317                 syslog(LOG_WARNING, "select: %m\n");
318             sleep(1);
319             continue;
320         }
321         for (sep = servtab; n && sep; sep = sep->se_next)
322             if ((sep->se_fd != -1) && FD_ISSET(sep->se_fd, &readable)) {
323                 n--;
324                 if (debug)
325                     fprintf(stderr, "someone wants %s\n", sep->se_service);
326                 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
327                     ctrl = accept(sep->se_fd, NULL, (int *)0);
328                     if (debug)
329                         fprintf(stderr, "accept, ctrl %d\n", ctrl);
330                     if (ctrl < 0) {
331                         if (errno == EINTR)
332                             continue;
333                         syslog(LOG_WARNING, "accept: %m");
334                         continue;
335                     }
336                 } else
337                     ctrl = sep->se_fd;
338                 (void)sigprocmask(SIG_BLOCK, &sigBlock, (sigset_t *) 0);
339                 pid = 0;
340                 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
341                 if (dofork) {
342                     if (debug)
343                         fprintf(stderr, "forking\n");
344                     if (sep->se_socktype == SOCK_DGRAM) {
345                         if (sep->se_count++ == 0)
346                             (void)gettimeofday(&sep->se_time, NULL);
347                         else if (sep->se_count >= TOOMANY) {
348                             struct timeval now;
349
350                             (void)gettimeofday(&now, NULL);
351                             if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
352                                 sep->se_time = now;
353                                 sep->se_count = 1;
354                             } else {
355                                 syslog(LOG_ERR,
356                                        "%s/%s server failing (looping), service terminated %d\n",
357                                        sep->se_service, sep->se_proto,
358                                        sep->se_socktype);
359                                 FD_CLR(sep->se_fd, &allsock);
360                                 (void)close(sep->se_fd);
361                                 sep->se_fd = -1;
362                                 sep->se_count = 0;
363                                 nsock--;
364                                 sigprocmask(SIG_SETMASK, &sigNone,
365                                             (sigset_t *) 0);
366                                 if (!timingout) {
367                                     timingout = 1;
368                                     alarm(RETRYTIME);
369                                 }
370                                 continue;
371                             }
372                         }
373                     }
374                     pid = fork();
375                 }
376                 if (pid < 0) {
377                     if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
378                         close(ctrl);
379                     sigprocmask(SIG_SETMASK, &sigNone, (sigset_t *) 0);
380                     sleep(1);
381                     continue;
382                 }
383                 if (pid && sep->se_wait) {
384                     sep->se_wait = pid;
385                     FD_CLR(sep->se_fd, &allsock);
386                     nsock--;
387                 }
388                 sigprocmask(SIG_SETMASK, &sigNone, (sigset_t *) 0);
389                 if (pid == 0) {
390                     if (debug) {
391 #ifdef  AFS_OSF_ENV
392                         setsid();
393 #else
394                         if (dofork && (tmpint = open("/dev/tty", O_RDWR)) > 0) {
395 #ifndef AFS_HPUX_ENV
396                             ioctl(tmpint, TIOCNOTTY, 0);
397 #endif
398                             close(tmpint);
399                         }
400                         (void)setpgrp(0, 0);
401 #if !defined(AIX)
402                         (void)signal(SIGTSTP, SIG_IGN);
403                         (void)signal(SIGTTIN, SIG_IGN);
404                         (void)signal(SIGTTOU, SIG_IGN);
405 #endif /* !defined(AIX) */
406 #endif
407                     }
408                     if (dofork) {
409 #ifdef  AFS_HPUX_ENV
410                         /* make child session leader */
411                         setsid();
412                         resetsignals();
413                         sigsetmask(0L);
414                         for (tmpint = getnumfds(); --tmpint > 2;)
415 #else
416                         for (tmpint = getdtablesize(); --tmpint > 2;)
417 #endif
418                             if (tmpint != ctrl)
419                                 close(tmpint);
420                     }
421                     afs_didsetpag = 0;
422                     if (sep->se_bi)
423                         (*sep->se_bi->bi_fn) (ctrl, sep);
424                     else {
425 #ifdef  AFS_HPUX_ENV
426                         int pgid = -getpid();
427 #else
428 #ifndef AFS_OSF_ENV
429                         (void)setpgrp(0, 0);
430 #endif
431 #endif
432                         dup2(ctrl, 0);
433                         close(ctrl);
434                         dup2(0, 1);
435                         dup2(0, 2);
436 #ifdef  AFS_HPUX_ENV
437                         /* make child socket process group leader */
438                         ioctl(0, SIOCSPGRP, (char *)&pgid);
439 #endif
440                         if ((pwd = getpwnam(sep->se_user)) == NULL) {
441                             fprintf(stderr, "getpwnam failed\n");
442                             syslog(LOG_ERR, "getpwnam: %s: No such user",
443                                    sep->se_user);
444                             if (sep->se_socktype != SOCK_STREAM)
445                                 recv(0, buf, sizeof(buf), 0);
446                             _exit(1);
447                         }
448                         if (pwd->pw_uid) {
449 #ifdef  AFS_HPUX_ENV
450                             (void)initgroups((uid_t) pwd->pw_name,
451                                              (gid_t) pwd->pw_gid);
452                             (void)setgid((gid_t) pwd->pw_gid);
453 #else
454                             (void)setgid((gid_t) pwd->pw_gid);
455                             initgroups(pwd->pw_name, pwd->pw_gid);
456 #endif
457                             (void)setuid((uid_t) pwd->pw_uid);
458                         }
459                         if (!afs_didsetpag
460                             && (!strcmp(sep->se_service, "login")
461                                 || !strcmp(sep->se_service, "shell"))) {
462                             setpag();   /* to disassociate it from current group... */
463                         }
464 #ifdef  AFS_HPUX_ENV
465 #ifdef  notdef
466                         if (sep->se_argv[0] != NULL) {
467                             if (!strcmp(sep->se_argv[0], "%A")) {
468                                 char addrbuf[32];
469                                 sprintf(addrbuf, "%s.%d",
470                                         inet_ntoa(his_addr.sin_addr.s_addr),
471                                         ntohs(his_addr.sin_port));
472                                 execl(sep->se_server,
473                                       strrchr(sep->se_server, '/') + 1,
474                                       sep->se_socktype ==
475                                       SOCK_DGRAM ? (char *)0 : addrbuf, NULL);
476                             } else
477                                 execv(sep->se_server, sep->se_argv);
478                         } else
479 #endif
480 #endif
481                             execv(sep->se_server, sep->se_argv);
482                         if (debug)
483                             fprintf(stderr, "%d execl %s\n", getpid(),
484                                     sep->se_server);
485                         if (sep->se_socktype != SOCK_STREAM)
486                             recv(0, buf, sizeof(buf), 0);
487                         syslog(LOG_ERR, "execv %s: %m", sep->se_server);
488                         _exit(1);
489                     }
490                 }
491                 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
492                     close(ctrl);
493             }
494     }
495 }
496
497 void
498 reapchild()
499 {
500     int status;
501     int pid;
502     register struct servtab *sep;
503
504     for (;;) {
505         pid = wait3(&status, WNOHANG, NULL);
506         if (pid <= 0)
507             break;
508         if (debug)
509             fprintf(stderr, "%d <reaped (status %d)\n", pid, status);
510         for (sep = servtab; sep; sep = sep->se_next)
511             if (sep->se_wait == pid) {
512                 if (status)
513                     syslog(LOG_WARNING, "%s: exit status 0x%x",
514                            sep->se_server, status);
515                 if (debug)
516                     fprintf(stderr, "restored %s, fd %d\n", sep->se_service,
517                             sep->se_fd);
518                 FD_SET(sep->se_fd, &allsock);
519                 nsock++;
520                 sep->se_wait = 1;
521             }
522     }
523 }
524
525 void
526 config()
527 {
528     register struct servtab *sep, *cp, **sepp;
529     struct servtab *getconfigent(), *enter();
530     sigset_t oset;
531
532     if (!setconfig()) {
533         syslog(LOG_ERR, "%s: %m", CONFIG);
534         return;
535     }
536     for (sep = servtab; sep; sep = sep->se_next)
537         sep->se_checked = 0;
538     while (cp = getconfigent()) {
539 #if 0
540         /* fix a bug on rt */
541         if (cp->se_service == 0 || *cp->se_service == '\0')
542             break;
543 #endif /* 0 */
544         for (sep = servtab; sep; sep = sep->se_next)
545             if (strcmp(sep->se_service, cp->se_service) == 0
546                 && strcmp(sep->se_proto, cp->se_proto) == 0)
547                 break;
548         if (sep != 0) {
549             int i;
550
551             sigprocmask(SIG_BLOCK, &sigBlock, &oset);
552             if (cp->se_bi == 0)
553                 sep->se_wait = cp->se_wait;
554 #define SWAP(a, b) { char *c = a; a = b; b = c; }
555             if (cp->se_user)
556                 SWAP(sep->se_user, cp->se_user);
557             if (cp->se_server)
558                 SWAP(sep->se_server, cp->se_server);
559             for (i = 0; i < MAXARGV; i++)
560                 SWAP(sep->se_argv[i], cp->se_argv[i]);
561             sigprocmask(SIG_SETMASK, &oset, (sigset_t *) 0);
562             freeconfig(cp);
563             if (debug)
564                 print_service("REDO", sep);
565         } else {
566             sep = enter(cp);
567             if (debug)
568                 print_service("ADD ", sep);
569         }
570         sep->se_checked = 1;
571         sp = getservbyname(sep->se_service, sep->se_proto);
572         if (sp == 0) {
573             syslog(LOG_ERR, "%s/%s: unknown service", sep->se_service,
574                    sep->se_proto);
575             continue;
576         }
577         if (sp->s_port != sep->se_ctrladdr.sin_port) {
578             sep->se_ctrladdr.sin_port = sp->s_port;
579             if (sep->se_fd != -1)
580                 (void)close(sep->se_fd);
581             sep->se_fd = -1;
582         }
583         if (sep->se_fd == -1)
584             setup(sep);
585     }
586     endconfig();
587     /*
588      * Purge anything not looked at above.
589      */
590     sigprocmask(SIG_BLOCK, &sigBlock, &oset);
591     sepp = &servtab;
592     while (sep = *sepp) {
593         if (sep->se_checked) {
594             sepp = &sep->se_next;
595             continue;
596         }
597         *sepp = sep->se_next;
598         if (sep->se_fd != -1) {
599             FD_CLR(sep->se_fd, &allsock);
600             nsock--;
601             (void)close(sep->se_fd);
602         }
603         if (debug)
604             print_service("FREE", sep);
605         freeconfig(sep);
606         free((char *)sep);
607     }
608     sigprocmask(SIG_SETMASK, &oset, (sigset_t *) 0);
609 }
610
611 void
612 retry()
613 {
614     register struct servtab *sep;
615
616     timingout = 0;
617     for (sep = servtab; sep; sep = sep->se_next)
618         if (sep->se_fd == -1)
619             setup(sep);
620 }
621
622 setup(sep)
623      register struct servtab *sep;
624 {
625     int on = 1;
626
627     if ((sep->se_fd = socket(AF_INET, sep->se_socktype, 0)) < 0) {
628         syslog(LOG_ERR, "%s/%s: socket: %m", sep->se_service, sep->se_proto);
629         return;
630     }
631 #define turnon(fd, opt) \
632 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
633     if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG)
634         && turnon(sep->se_fd, SO_DEBUG) < 0)
635         syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
636     if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
637         syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
638 #undef turnon
639     if (bind
640         (sep->se_fd, (struct sockaddr *)&sep->se_ctrladdr,
641          sizeof(sep->se_ctrladdr)) < 0) {
642         syslog(LOG_ERR, "%s/%s: bind: %m", sep->se_service, sep->se_proto);
643         (void)close(sep->se_fd);
644         sep->se_fd = -1;
645         if (!timingout) {
646             timingout = 1;
647             alarm(RETRYTIME);
648         }
649         return;
650     }
651     if (sep->se_socktype == SOCK_STREAM)
652         listen(sep->se_fd, backlog);
653     FD_SET(sep->se_fd, &allsock);
654     nsock++;
655     if (sep->se_fd > maxsock)
656         maxsock = sep->se_fd;
657 }
658
659 struct servtab *
660 enter(cp)
661      struct servtab *cp;
662 {
663     register struct servtab *sep;
664     sigset_t oset;
665
666     sep = (struct servtab *)malloc(sizeof(*sep));
667     if (sep == NULL) {
668         syslog(LOG_ERR, "Out of memory.");
669         exit(-1);
670     }
671     *sep = *cp;
672     sep->se_fd = -1;
673     sigprocmask(SIG_BLOCK, &sigBlock, &oset);
674     sep->se_next = servtab;
675     servtab = sep;
676     sigprocmask(SIG_SETMASK, &oset, (sigset_t *) 0);
677     return (sep);
678 }
679
680 FILE *fconfig = NULL;
681 struct servtab serv;
682 char line[256];
683 char *skip(), *nextline();
684
685 setconfig()
686 {
687
688     if (fconfig != NULL) {
689         fseek(fconfig, 0L, L_SET);
690         return (1);
691     }
692     fconfig = fopen(CONFIG, "r");
693     return (fconfig != NULL);
694 }
695
696 endconfig()
697 {
698     if (fconfig) {
699         (void)fclose(fconfig);
700         fconfig = NULL;
701     }
702 }
703
704 struct servtab *
705 getconfigent()
706 {
707     register struct servtab *sep = &serv;
708     int argc;
709     char *cp, *arg, *copyofstr();
710
711   more:
712     /* modified to skip blank lines... */
713     while ((cp = nextline(fconfig)) && (*cp == '#' || (strlen(cp) < 2)));
714     if (cp == NULL)
715         return (NULL);
716     sep->se_service = copyofstr(skip(&cp));
717     arg = skip(&cp);
718     if (strcmp(arg, "stream") == 0)
719         sep->se_socktype = SOCK_STREAM;
720     else if (strcmp(arg, "dgram") == 0)
721         sep->se_socktype = SOCK_DGRAM;
722     else if (strcmp(arg, "rdm") == 0)
723         sep->se_socktype = SOCK_RDM;
724     else if (strcmp(arg, "seqpacket") == 0)
725         sep->se_socktype = SOCK_SEQPACKET;
726     else if (strcmp(arg, "raw") == 0)
727         sep->se_socktype = SOCK_RAW;
728     else
729         sep->se_socktype = -1;
730     sep->se_proto = copyofstr(skip(&cp));
731     arg = skip(&cp);
732     sep->se_wait = strcmp(arg, "wait") == 0;
733     sep->se_user = copyofstr(skip(&cp));
734     sep->se_server = copyofstr(skip(&cp));
735     if (strcmp(sep->se_server, "internal") == 0) {
736         register struct biltin *bi;
737
738         for (bi = biltins; bi->bi_service; bi++)
739             if (bi->bi_socktype == sep->se_socktype
740                 && strcmp(bi->bi_service, sep->se_service) == 0)
741                 break;
742         if (bi->bi_service == 0) {
743             syslog(LOG_ERR, "internal service %s unknown\n", sep->se_service);
744             goto more;
745         }
746         sep->se_bi = bi;
747         sep->se_wait = bi->bi_wait;
748     } else
749         sep->se_bi = NULL;
750     argc = 0;
751     for (arg = skip(&cp); cp; arg = skip(&cp))
752         if (argc < MAXARGV)
753             sep->se_argv[argc++] = copyofstr(arg);
754     while (argc <= MAXARGV)
755         sep->se_argv[argc++] = NULL;
756     return (sep);
757 }
758
759 freeconfig(cp)
760      register struct servtab *cp;
761 {
762     int i;
763
764     if (cp->se_service)
765         free(cp->se_service);
766     if (cp->se_proto)
767         free(cp->se_proto);
768     if (cp->se_user)
769         free(cp->se_user);
770     if (cp->se_server)
771         free(cp->se_server);
772     for (i = 0; i < MAXARGV; i++)
773         if (cp->se_argv[i])
774             free(cp->se_argv[i]);
775 }
776
777 char *
778 skip(cpp)
779      char **cpp;
780 {
781     register char *cp = *cpp;
782     char *start;
783
784   again:
785     while (*cp == ' ' || *cp == '\t')
786         cp++;
787     if (*cp == '\0') {
788         char c;
789
790         c = getc(fconfig);
791         (void)ungetc(c, fconfig);
792         if (c == ' ' || c == '\t')
793             if (cp = nextline(fconfig))
794                 goto again;
795         *cpp = NULL;
796         return (NULL);
797     }
798     start = cp;
799     while (*cp && *cp != ' ' && *cp != '\t')
800         cp++;
801     if (*cp != '\0')
802         *cp++ = '\0';
803     *cpp = cp;
804     return (start);
805 }
806
807 char *
808 nextline(fd)
809      FILE *fd;
810 {
811     char *cp;
812
813     if (fgets(line, sizeof(line), fd) == NULL)
814         return (NULL);
815     cp = strchr(line, '\n');
816     if (cp)
817         *cp = '\0';
818     return (line);
819 }
820
821 char *
822 copyofstr(cp)
823      const char *cp;
824 {
825     char *new;
826
827     if (cp == NULL)
828         cp = "";
829     new = malloc((unsigned)(strlen(cp) + 1));
830     if (new == NULL) {
831         syslog(LOG_ERR, "Out of memory.");
832         exit(-1);
833     }
834     (void)strcpy(new, cp);
835     return (new);
836 }
837
838 setproctitle(a, s)
839      char *a;
840      int s;
841 {
842     int size;
843     register char *cp;
844     struct sockaddr_in sin;
845     char buf[80];
846
847     cp = Argv[0];
848     size = sizeof(sin);
849     if (getpeername(s, (struct sockaddr *)&sin, &size) == 0)
850         (void)sprintf(buf, "-%s [%s]", a, inet_ntoa(sin.sin_addr));
851     else
852         (void)sprintf(buf, "-%s", a);
853     strncpy(cp, buf, LastArg - cp);
854     cp += strlen(cp);
855     while (cp < LastArg)
856         *cp++ = ' ';
857 }
858
859 /*
860  * Internet services provided internally by inetd:
861  */
862
863 /* ARGSUSED */
864 echo_stream(s, sep)             /* Echo service -- echo data back */
865      int s;
866      struct servtab *sep;
867 {
868     char buffer[BUFSIZ];
869     int i;
870
871     setproctitle(sep->se_service, s);
872     while ((i = read(s, buffer, sizeof(buffer))) > 0
873            && write(s, buffer, i) > 0);
874     exit(0);
875 }
876
877 /* ARGSUSED */
878 echo_dg(s, sep)                 /* Echo service -- echo data back */
879      int s;
880      struct servtab *sep;
881 {
882     char buffer[BUFSIZ];
883     int i, size;
884     struct sockaddr sa;
885
886     size = sizeof(sa);
887     if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
888         return;
889     (void)sendto(s, buffer, i, 0, &sa, sizeof(sa));
890 }
891
892 /* ARGSUSED */
893 discard_stream(s, sep)          /* Discard service -- ignore data */
894      int s;
895      struct servtab *sep;
896 {
897     char buffer[BUFSIZ];
898
899     setproctitle(sep->se_service, s);
900     while (1) {
901         while (read(s, buffer, sizeof(buffer)) > 0);
902         if (errno != EINTR)
903             break;
904     }
905     exit(0);
906 }
907
908 /* ARGSUSED */
909 discard_dg(s, sep)              /* Discard service -- ignore data */
910      int s;
911      struct servtab *sep;
912 {
913     char buffer[BUFSIZ];
914
915     (void)read(s, buffer, sizeof(buffer));
916 }
917
918 #define LINESIZ 72
919 char ring[128];
920 char *endring;
921
922 initring()
923 {
924     register int i;
925
926     endring = ring;
927
928     for (i = 0; i <= 128; ++i)
929         if (isprint(i))
930             *endring++ = i;
931 }
932
933 /* ARGSUSED */
934 chargen_stream(s, sep)          /* Character generator */
935      int s;
936      struct servtab *sep;
937 {
938     register char *rs;
939     int len;
940     char text[LINESIZ + 2];
941
942     setproctitle(sep->se_service, s);
943
944     if (!endring) {
945         initring();
946         rs = ring;
947     }
948
949     text[LINESIZ] = '\r';
950     text[LINESIZ + 1] = '\n';
951     for (rs = ring;;) {
952         if ((len = endring - rs) >= LINESIZ)
953             memcpy(text, rs, LINESIZ);
954         else {
955             memcpy(text, rs, len);
956             memcpy(text + len, ring, LINESIZ - len);
957         }
958         if (++rs == endring)
959             rs = ring;
960         if (write(s, text, sizeof(text)) != sizeof(text))
961             break;
962     }
963     exit(0);
964 }
965
966 /* ARGSUSED */
967 chargen_dg(s, sep)              /* Character generator */
968      int s;
969      struct servtab *sep;
970 {
971     struct sockaddr sa;
972     static char *rs;
973     int len, size;
974     char text[LINESIZ + 2];
975
976     if (endring == 0) {
977         initring();
978         rs = ring;
979     }
980
981     size = sizeof(sa);
982     if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
983         return;
984
985     if ((len = endring - rs) >= LINESIZ)
986         memcpy(text, rs, LINESIZ);
987     else {
988         memcpy(text, rs, len);
989         memcpy(text + len, ring, LINESIZ - len);
990     }
991     if (++rs == endring)
992         rs = ring;
993     text[LINESIZ] = '\r';
994     text[LINESIZ + 1] = '\n';
995     (void)sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
996 }
997
998 /*
999  * Return a machine readable date and time, in the form of the
1000  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
1001  * returns the number of seconds since midnight, Jan 1, 1970,
1002  * we must add 2208988800 seconds to this figure to make up for
1003  * some seventy years Bell Labs was asleep.
1004  */
1005
1006 afs_int32
1007 machtime()
1008 {
1009     struct timeval tv;
1010
1011     if (gettimeofday(&tv, NULL) < 0) {
1012         fprintf(stderr, "Unable to get time of day\n");
1013         return (0L);
1014     }
1015     return (htonl((afs_int32) tv.tv_sec + 2208988800U));
1016 }
1017
1018 /* ARGSUSED */
1019 machtime_stream(s, sep)
1020      int s;
1021      struct servtab *sep;
1022 {
1023     afs_int32 result;
1024
1025     result = machtime();
1026     (void)write(s, (char *)&result, sizeof(result));
1027 }
1028
1029 /* ARGSUSED */
1030 machtime_dg(s, sep)
1031      int s;
1032      struct servtab *sep;
1033 {
1034     afs_int32 result;
1035     struct sockaddr sa;
1036     int size;
1037
1038     size = sizeof(sa);
1039     if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
1040         return;
1041     result = machtime();
1042     (void)sendto(s, (char *)&result, sizeof(result), 0, &sa, sizeof(sa));
1043 }
1044
1045 /* ARGSUSED */
1046 daytime_stream(s, sep)          /* Return human-readable time of day */
1047      int s;
1048      struct servtab *sep;
1049 {
1050     char buffer[256];
1051     time_t clock;
1052
1053     clock = time((time_t *) 0);
1054
1055     (void)sprintf(buffer, "%.24s\r\n", ctime(&clock));
1056     (void)write(s, buffer, strlen(buffer));
1057 }
1058
1059 /* ARGSUSED */
1060 daytime_dg(s, sep)              /* Return human-readable time of day */
1061      int s;
1062      struct servtab *sep;
1063 {
1064     char buffer[256];
1065     time_t clock;
1066     struct sockaddr sa;
1067     int size;
1068
1069     clock = time((time_t *) 0);
1070
1071     size = sizeof(sa);
1072     if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1073         return;
1074     (void)sprintf(buffer, "%.24s\r\n", ctime(&clock));
1075     (void)sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa));
1076 }
1077
1078 /*
1079  * print_service:
1080  *      Dump relevant information to stderr
1081  */
1082 print_service(action, sep)
1083      char *action;
1084      struct servtab *sep;
1085 {
1086     fprintf(stderr,
1087             "%s: %s proto=%s, wait=%d, user=%s builtin=%x server=%s\n",
1088             action, sep->se_service, sep->se_proto, sep->se_wait,
1089             sep->se_user, (int)sep->se_bi, sep->se_server);
1090 }
1091
1092 /*
1093  * Copyright 2000, International Business Machines Corporation and others.
1094  * All Rights Reserved.
1095  * 
1096  * This software has been released under the terms of the IBM Public
1097  * License.  For details, see the LICENSE file in the top-level source
1098  * directory or online at http://www.openafs.org/dl/license10.html
1099  */
1100
1101 /*
1102  * This program is a front-end for the various remote access programs
1103  * (e.g. rsh, rcp, rlogin, ftp) to allow for weak remote authentication.
1104  * It will be used by a call to a well-known restricted tcp port;  if
1105  * there is a service on that port, we will attempt authentication.
1106  *
1107  * Note, this only affects the Kerberos portion of the authentication;
1108  * the program still requires its existing authentication (although it
1109  * seems reasonable to change this in the future.)
1110  *
1111  * The advantage to this scheme (rather than modifying each program to
1112  * incorporate this authentication scheme) is it allows us to modify
1113  * the authentication mechanism without requiring additonal code
1114  * changes to the other programs.
1115  *
1116  * Current format of authentication packet:
1117  *
1118  *   (1) User;
1119  *   (2) Service;
1120  *   (3) Token length (null terminated);
1121  *   (4) Token (not null terminated).
1122  *
1123  * The code to add/delete to the AFS ticket cache is taken from the
1124  * authentication library.
1125  */
1126
1127 /*
1128  * routine to do the actual authentication.  At this time, it is
1129  * very simple -- the remote end passes a token length and a token.
1130  *
1131  * This routine returns the name of the requested service.
1132  */
1133
1134 #define gettime(tl) do { struct timezone tzp; struct timeval tv; \
1135                          gettimeofday(&tv,&tzp); tl = tv.tv_sec; } while(0)
1136 #define RAUTH_TOKENLIFE (60 * 60 * 2)   /* 2 hours */
1137
1138 #define NOPAG   0xffffffff
1139 int
1140 get_pag_from_groups(g0, g1)
1141      afs_uint32 g0, g1;
1142 {
1143     afs_uint32 h, l, result;
1144
1145     g0 -= 0x3f00;
1146     g1 -= 0x3f00;
1147     if (g0 < 0xc000 && g1 < 0xc000) {
1148         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1149         h = (g0 >> 14);
1150         h = (g1 >> 14) + h + h + h;
1151         result = ((h << 28) | l);
1152         /* Additional testing */
1153         if (((result >> 24) & 0xff) == 'A')
1154             return result;
1155         else
1156             return NOPAG;
1157     }
1158     return NOPAG;
1159 }
1160
1161 auth_stream(s, sepent)
1162      int s;
1163      struct servtab *sepent;
1164 {
1165     char service[100], remoteName[64];
1166     struct sockaddr_in from;
1167     int fromlen;
1168     int code;
1169     struct afsconf_dir *tdir;
1170     struct ktc_principal tserver, tclient;
1171     struct ktc_token token;
1172     register struct servtab *sep;
1173
1174     /*
1175      * First, obtain information on remote end of connection.
1176      */
1177     if (debug)
1178         fprintf(stderr, "auth_stream: entered\n");
1179 #ifndef AFS_SUN5_ENV
1180     if (getpeername(s, &from, &fromlen) < 0) {
1181         syslog(LOG_ERR, "getpeername failed");
1182         if (debug)
1183             fprintf(stderr, "auth_stream: getpeername failed\n");
1184         exit(1);
1185     }
1186 #endif
1187     if (intoken(s, &token, service, remoteName) != 0) {
1188         syslog(LOG_ERR, "invalid remote authentication");
1189         if (debug)
1190             fprintf(stderr, "auth_stream: invalid remote authentication\n");
1191         exit(1);
1192     }
1193     /* lookup the name of the local cell */
1194
1195     if (debug)
1196         fprintf(stderr, "auth_stream: look up local cell name\n");
1197
1198     tdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
1199     if (!tdir) {
1200         syslog(LOG_NOTICE, "Can't open dir %s\n", AFSDIR_CLIENT_ETC_DIRPATH);
1201         if (debug)
1202             fprintf(stderr, "Can't open dir %s\n", AFSDIR_CLIENT_ETC_DIRPATH);
1203         exit(1);
1204     }
1205     /* done with configuration stuff now */
1206     afsconf_Close(tdir);
1207     /* set ticket in local cell */
1208     strcpy(tserver.cell, remoteName);
1209     strcpy(tserver.name, "afs");
1210     tclient = tserver;
1211     /* now, set the token */
1212
1213     if (debug) {
1214         fprintf(stderr,
1215                 "token information: service is %s\ntoken length is %d\n",
1216                 service, token.ticketLen);
1217         fprintf(stderr, "token is %s\n", token.ticket);
1218         fprintf(stderr, "cell name is %s\n", remoteName);
1219     }
1220     setpag();
1221     afs_didsetpag = 1;
1222     code = ktc_SetToken(&tserver, &token, &tclient, 0);
1223     if (code) {
1224         write(s, "0", 1);       /* say "no" to other side */
1225         printf("Login incorrect.(%d)", code);
1226         syslog(LOG_ERR, "Invalid token from %s", inet_ntoa(from.sin_addr));
1227         exit(1);
1228     }
1229     write(s, "1", 1);           /* say "yes" to other side */
1230
1231     if (debug)
1232         fprintf(stderr, "Finished authentication code\n");
1233     for (sep = servtab; sep; sep = sep->se_next)
1234         if (strcmp(sep->se_service, service) == 0) {
1235             int dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
1236             int tmpint;
1237
1238             if (dofork) {
1239                 for (tmpint = getdtablesize(); --tmpint > 2;)
1240                     if (tmpint != s)
1241                         close(tmpint);
1242             }
1243             if (sep->se_bi) {
1244                 (*sep->se_bi->bi_fn) (s, sep);
1245             } else {
1246                 register struct passwd *pwd;
1247                 struct passwd *getpwnam();
1248                 char buf[BUFSIZ];
1249
1250                 if ((pwd = getpwnam(sep->se_user)) == NULL) {
1251                     syslog(LOG_ERR, "getpwnam: %s: No such user",
1252                            sep->se_user);
1253                     if (sep->se_socktype != SOCK_STREAM)
1254                         recv(0, buf, sizeof(buf), 0);
1255                     abort();
1256                     _exit(1);
1257                 }
1258                 if (pwd->pw_uid) {
1259                     (void)setgid((gid_t) pwd->pw_gid);
1260                     initgroups(pwd->pw_name, pwd->pw_gid);
1261                     (void)setuid((uid_t) pwd->pw_uid);
1262                 }
1263                 dup2(s, 0);
1264                 close(s);
1265                 dup2(0, 1);
1266                 if (debug)
1267                     fprintf(stderr, "going to exec program %s(errno = %d)\n",
1268                             sep->se_server, errno);
1269                 errno = 0;
1270                 debug = 0;
1271                 dup2(0, 2);
1272
1273                 if (!afs_didsetpag
1274                     && (!strcmp(sep->se_service, "login")
1275                         || !strcmp(sep->se_service, "shell"))) {
1276                     setpag();   /* to disassociate it from current group... */
1277                 }
1278                 execv(sep->se_server, sep->se_argv);
1279                 abort();
1280                 if (sep->se_socktype != SOCK_STREAM)
1281                     recv(0, buf, sizeof(buf), 0);
1282                 syslog(LOG_ERR, "execv %s: %m", sep->se_server);
1283                 _exit(1);
1284             }
1285         }
1286     fprintf(stderr, "service not available\n");
1287     syslog(LOG_ERR, "auth_stream: invalid service requested %s\n", service);
1288     exit(0);
1289 }
1290
1291
1292 auth_dg()
1293 {
1294     syslog(LOG_NOTICE,
1295            "datagram remote authentication requested, not supported");
1296 }
1297
1298 /*
1299  * intoken:
1300  *
1301  * This routine accepts a token on the specified file handle;
1302  * The input format for a token is:
1303  *
1304  *   Field #    Contents         description
1305  *    (0)       Service Name     char[]
1306  *    (1)       Version #        unsigned integer (< 2^32)
1307  *    (2)       cellName         char[]
1308  *    (3)       startTime        unsigned afs_int32 (< 2^32)
1309  *    (4)       endTime          unsigned afs_int32 (< 2^32)
1310  *    (5)       sessionKey       char[8]
1311  *    (6)       kvno             short (< 2^16)
1312  *    (7)       ticketLen        unsigned integer (< 2^32)
1313  *    (8)       ticket           char[MAXKTCTICKETLEN]
1314  *
1315  * Each field is comma separated;  the last is variable length.  The
1316  * converted token is placed into the token structure pointed to by
1317  * the variable "token".
1318  */
1319
1320 intoken(s, token, svc, cell)
1321      int s;
1322      struct ktc_token *token;
1323      char *svc, *cell;
1324 {
1325     char buf[1024], *bp;
1326     int count;
1327     unsigned index, version;
1328
1329     if (debug)
1330         fprintf(stderr, "intoken: entered\n");
1331
1332     if ((count = recv(s, buf, sizeof buf, 0)) == -1) {
1333         if (debug) {
1334             fprintf(stderr, "error on fd %d\n", s);
1335             perror("intoken recv");
1336         }
1337         return (-1);
1338     }
1339
1340     /* (0) Service Name */
1341     for (index = 0; index < count && buf[index] != ','; index++);
1342
1343     if (index == count) {
1344         if (debug)
1345             fprintf(stderr, "overran buffer while searching for svc name\n");
1346         return (-1);
1347     }
1348
1349     if (buf[index] != ',') {
1350         if (debug)
1351             fprintf(stderr,
1352                     "Didn't stop on a comma, searching for svc name\n");
1353         return (-1);
1354     }
1355
1356     buf[index] = '\0';
1357
1358     strcpy(svc, buf);
1359
1360     /* (1) Version # */
1361
1362     bp = buf + index + 1;
1363
1364     for (; index < count && buf[index] != ','; index++);
1365
1366     if (index == count) {
1367         if (debug)
1368             fprintf(stderr, "overran buffer while searching for version #\n");
1369         return (-1);
1370     }
1371
1372     if (buf[index] != ',') {
1373         if (debug)
1374             fprintf(stderr,
1375                     "Didn't stop on a comma, searching for version #\n");
1376         return (-1);
1377     }
1378
1379     buf[index] = '\0';
1380
1381     sscanf(bp, "%u", &version);
1382
1383     if (version > 2) {
1384         if (debug)
1385             fprintf(stderr, "Incompatible (newer) version encountered: %d\n",
1386                     version);
1387         return (-1);
1388     }
1389
1390     if (version > 1) {
1391         /* we didn't include cell name in prior versions */
1392         bp = buf + index + 1;
1393
1394         for (index = 0; index < count && buf[index] != ','; index++);
1395
1396         if (index == count) {
1397             if (debug)
1398                 fprintf(stderr, "overran buffer while searching for cell\n");
1399             return (-1);
1400         }
1401
1402         if (buf[index] != ',') {
1403             if (debug)
1404                 fprintf(stderr,
1405                         "Didn't stop on a comma, searching for cell\n");
1406             return (-1);
1407         }
1408         buf[index] = '\0';
1409         strcpy(cell, bp);
1410     }
1411
1412     /* (2) startTime */
1413
1414     bp = buf + index + 1;
1415
1416     for (; index < count && buf[index] != ','; index++);
1417
1418     if (index == count) {
1419         if (debug)
1420             fprintf(stderr,
1421                     "overran buffer while searching for startTime #\n");
1422         exit(1);
1423     }
1424
1425     if (buf[index] != ',') {
1426         if (debug)
1427             fprintf(stderr,
1428                     "Didn't stop on a comma, searching for startTime #\n");
1429         return (-1);
1430     }
1431
1432     buf[index] = '\0';
1433
1434     sscanf(bp, "%u", &token->startTime);
1435
1436     /* (3) endTime */
1437
1438     bp = buf + index + 1;
1439
1440     for (; index < count && buf[index] != ','; index++);
1441
1442     if (index == count) {
1443         if (debug)
1444             fprintf(stderr, "overran buffer while searching for endTime #\n");
1445         return (-1);
1446     }
1447
1448     if (buf[index] != ',') {
1449         if (debug)
1450             fprintf(stderr,
1451                     "Didn't stop on a comma, searching for endTime #\n");
1452         return (-1);
1453     }
1454
1455     buf[index] = '\0';
1456
1457     sscanf(bp, "%u", &token->endTime);
1458
1459     /* (4) sessionKey */
1460
1461     bp = buf + index + 1;
1462     memcpy(&token->sessionKey, bp, 8);
1463
1464     /* (5) kvno */
1465
1466     bp += 8;
1467     for (index += 9; index < count && buf[index] != ','; index++);
1468
1469     if (index == count) {
1470         if (debug)
1471             fprintf(stderr, "overran buffer while searching for kvno\n");
1472         return (-1);
1473     }
1474
1475     if (buf[index] != ',') {
1476         if (debug)
1477             fprintf(stderr, "Didn't stop on a comma, searching for kvno\n");
1478         return (-1);
1479     }
1480
1481     buf[index] = '\0';
1482
1483     /* kvno is actually a short, so insist that it scan a short */
1484
1485     sscanf(bp, "%hu", &token->kvno);
1486
1487     /* (6) ticketLen */
1488
1489     bp = buf + index + 1;
1490
1491     for (; index < count && buf[index] != ','; index++);
1492
1493     if (index == count) {
1494         if (debug)
1495             fprintf(stderr, "overran buffer while searching for ticketLen\n");
1496         return (-1);
1497     }
1498
1499     if (buf[index] != ',') {
1500         if (debug)
1501             fprintf(stderr,
1502                     "Didn't stop on a comma, searching for ticketLen\n");
1503         return (-1);
1504     }
1505
1506     buf[index] = '\0';
1507
1508     sscanf(bp, "%u", &token->ticketLen);
1509
1510     /* (7) ticketLen */
1511
1512     bp = buf + index + 1;
1513
1514     if (index + token->ticketLen > count) {
1515         if (debug)
1516             fprintf(stderr, "overran buffer while copying ticket\n");
1517         return (-1);
1518     }
1519
1520     memcpy(token->ticket, bp, token->ticketLen);
1521
1522     return 0;
1523 }
1524
1525 #ifdef  AFS_HPUX_ENV
1526 resetsignals()
1527 {
1528     struct sigaction act, oact;
1529     int sign;
1530
1531     memset(&act, '\0', sizeof(act));
1532     act.sa_handler = SIG_DFL;
1533     for (sign = 1; sign < NSIG; sign++)
1534         sigaction(sign, &act, &oact);
1535 }
1536 #endif