Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / support / pop / RCS / popd.c,v
1 head    1.16;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.16
9 date    93.08.25.17.23.05;      author jromine; state Exp;
10 branches;
11 next    1.15;
12
13 1.15
14 date    93.02.26.22.11.12;      author jromine; state Exp;
15 branches;
16 next    1.14;
17
18 1.14
19 date    92.12.16.22.31.20;      author jromine; state Exp;
20 branches;
21 next    1.13;
22
23 1.13
24 date    92.12.15.00.20.22;      author jromine; state Exp;
25 branches;
26 next    1.12;
27
28 1.12
29 date    92.12.14.17.11.17;      author jromine; state Exp;
30 branches;
31 next    1.11;
32
33 1.11
34 date    92.11.11.22.26.33;      author jromine; state Exp;
35 branches;
36 next    1.10;
37
38 1.10
39 date    92.02.11.18.55.11;      author jromine; state Exp;
40 branches;
41 next    1.9;
42
43 1.9
44 date    92.01.31.22.02.30;      author jromine; state Exp;
45 branches;
46 next    1.8;
47
48 1.8
49 date    90.04.09.09.45.09;      author sources; state Exp;
50 branches;
51 next    1.7;
52
53 1.7
54 date    90.04.05.15.34.33;      author sources; state Exp;
55 branches;
56 next    1.6;
57
58 1.6
59 date    90.04.05.14.53.52;      author sources; state Exp;
60 branches;
61 next    1.5;
62
63 1.5
64 date    90.02.06.13.12.22;      author sources; state Exp;
65 branches;
66 next    1.4;
67
68 1.4
69 date    90.02.05.15.01.40;      author sources; state Exp;
70 branches;
71 next    1.3;
72
73 1.3
74 date    90.02.05.14.07.50;      author sources; state Exp;
75 branches;
76 next    1.2;
77
78 1.2
79 date    90.02.05.13.58.18;      author sources; state Exp;
80 branches;
81 next    1.1;
82
83 1.1
84 date    90.02.05.13.58.08;      author sources; state Exp;
85 branches;
86 next    ;
87
88
89 desc
90 @@
91
92
93 1.16
94 log
95 @off_t fixes for BSD44
96 @
97 text
98 @/* popd.c - the POP server */
99 #ifndef lint
100 static char ident[] = "@@(#)$Id: popd.c,v 1.15 1993/02/26 22:11:12 jromine Exp jromine $";
101 #endif  /* lint */
102
103 /* Author:      Marshall T. Rose        <MRose@@UDel>   (MTR)
104                 Department of Computer Science and Information Sciences
105                 University of Delaware
106                 Newark, DE  19716
107                 302/451-1951
108
109    Date:        Sun Oct 28 16:23:26 1984
110  */
111
112 #include <errno.h>
113 #include <signal.h>
114 #include <stdio.h>
115 #include "../h/strings.h"
116 #include "syslog.h"
117 #include <sys/types.h>
118 #include <sys/file.h>
119 #ifndef NOIOCTLH
120 #include <sys/ioctl.h>
121 #endif
122 #include <sys/socket.h>
123 #include <sys/time.h>
124 #ifdef  SIGTSTP
125 #include <sys/resource.h>
126 #include <sys/wait.h>
127 #endif
128 #include <netinet/in.h>
129 #include <netdb.h>
130 #include <arpa/inet.h>
131 #ifdef KPOP
132 #include <krb.h>
133
134 static Key_schedule schedule;
135 static KTEXT_ST ticket;
136 static AUTH_DAT kdata;
137 #endif
138 #ifdef  SYS5
139 #include <fcntl.h>
140 #endif
141
142
143 #ifdef  SYS5
144 #define u_short ushort
145 #endif
146
147 #define NOTOK   (-1)
148 #define OK      0
149
150 #define NULLCP  ((char *) 0)
151 #define NULLRP  ((struct rusage *) 0)
152
153 #define FAST                    /* fast start-up of BBoards */
154
155 /* \f */
156
157 extern int  errno;
158 #ifndef BSD44
159 extern int  sys_nerr;
160 extern char *sys_errlist[];
161 extern char *sys_siglist[];
162 #endif
163
164
165 int  debug = 0;
166 static int  nbits = ((sizeof (int)) * 8);
167 static int  options = 0;
168
169 #ifndef POPSERVICE
170 #define POPSERVICE      "pop"
171 #endif
172
173 char *myname = "popd";
174 char myhost[BUFSIZ];
175 static char *myprotocol = "tcp";
176 static char *myservice = POPSERVICE;
177
178 static struct sockaddr_in   in_socket;
179 static struct sockaddr_in  *isock = &in_socket;
180
181 #ifdef KPOP
182 static AUTH_DAT kdata;
183 #endif
184
185 static TYPESIG  chldser ();
186 void    padios (), padvise ();
187
188 static  server(), arginit(), envinit();
189 /* \f */
190
191 /* ARGSUSED */
192
193 main (argc, argv, envp)
194 int     argc;
195 char  **argv,
196       **envp;
197 {
198 #ifdef KPOP
199     int     i;
200 #else
201     int     fd,
202             sd;
203     int     on = 1;
204     struct servent *sp;
205     struct sockaddr_in  out_socket,
206                        *osock = &out_socket;
207 #endif
208
209 #ifdef KPOP
210     i = sizeof(in_socket);
211     if (getpeername(0, &in_socket, &i) < 0)
212       padios("getpeername", "bad status");
213 #else
214     if ((sp = getservbyname (myservice, myprotocol)) == NULL)
215         padios (NULLCP, "%s/%s: unknown service", myprotocol, myservice);
216     isock -> sin_family = AF_INET;
217     isock -> sin_port = sp -> s_port;
218     isock -> sin_addr.s_addr = INADDR_ANY;
219 #endif
220     arginit (argv);
221     envinit ();
222
223 #ifndef KPOP
224 #ifdef  RESTART
225     for (;;) {
226         char    reason[BUFSIZ];
227 #if defined(BSD42) && !defined(WAITINT)
228         union wait status;
229 #else
230         int status;
231 #endif
232
233         switch (fork ()) {
234             case NOTOK: 
235                 sleep (5);
236                 continue;
237
238             case OK: 
239                 break;
240
241             default: 
242                 sleep (60);
243                 (void) wait3 (&status, 0, NULLRP);
244                 if (WIFEXITED (status))
245                     (void) sprintf (reason, "exit=0%o", status.w_retcode);
246                 else
247                     if (WIFSIGNALED (status))
248                         (void) sprintf (reason, "signal=%s%s",
249                                 status.w_termsig < NSIG
250                                 ? sys_siglist[status.w_termsig] : "unknown",
251                                 status.w_coredump ? " (core dumped)" : NULL);
252                     else
253                         (void) strcpy (reason, "stopped(!!)");
254                 padvise (NULLCP, LOG_WARNING, "%s/%s server has terminated -- %s",
255                         sp -> s_proto, sp -> s_name, reason);
256                 continue;
257         }
258         break;
259     }
260
261     closelog ();
262 #ifndef BSD43
263     openlog (myname, LOG_PID);
264 #else   /* BSD43 */
265     openlog (myname, LOG_PID, LOG_DAEMON);
266 #endif  /* BSD43 */
267     padvise (NULLCP, LOG_INFO, "restart");
268 #endif  /* RESTART */
269
270 /* \f */
271
272     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK)
273         padios ("socket", "unable to create");
274 #ifndef BSD43
275     if (options & SO_DEBUG)
276         if (setsockopt (sd, SOL_SOCKET, SO_DEBUG, NULL, 0) == NOTOK)
277             padvise ("SO_DEBUG", LOG_WARNING, "unable to set socket option");
278     if (setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, NULL, 0) == NOTOK)
279         padvise ("SO_KEEPALIVE", LOG_WARNING, "unable to set socket option");
280 #else   /* BSD43 */
281     if (options & SO_DEBUG)
282         if (setsockopt (sd, SOL_SOCKET, SO_DEBUG, &on, sizeof on) == NOTOK)
283             padvise ("SO_DEBUG", LOG_WARNING, "unable to set socket option");
284     if (setsockopt (sd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on) == NOTOK)
285         padvise ("SO_KEEPALIVE", LOG_WARNING, "unable to set socket option");
286 #endif  /* BSD43 */
287     if (bind (sd, (struct sockaddr *) isock, sizeof *isock) == NOTOK)
288         padios ("socket", "unable to bind");
289
290 #ifdef  SIGCHLD
291     (void) signal (SIGCHLD, chldser);
292 #endif  /* SIGCHLD */
293     (void) listen (sd, SOMAXCONN);
294 #endif /* KPOP */
295 #ifdef  FAST
296     popinit ();
297 #endif  /* FAST */
298 #ifndef KPOP
299     for (;;) {
300         int     i = sizeof *osock;
301
302         if ((fd = accept (sd, (struct sockaddr *) osock, &i)) == NOTOK) {
303             if (errno != EINTR)
304                 padvise ("socket", LOG_WARNING,
305                     "unable to accept connection on");
306             continue;
307         }
308 #endif
309 #ifdef  FAST
310         popassert ();
311 #endif  /* FAST */
312 #ifndef KPOP
313         switch (fork ()) {
314             case OK: 
315                 (void) close (sd);
316 #ifdef  SIGCHLD
317                 (void) signal (SIGCHLD, SIG_DFL);
318 #endif  /* SIGCHLD */
319                 server (fd, osock);
320                 _exit (0);
321
322             case NOTOK: 
323                 padvise ("socket", LOG_WARNING,
324                     "no forks, so rejecting connection on");
325             default: 
326                 (void) close (fd);
327         }
328     }
329 #else
330     server (0, isock);
331 #endif
332 }
333
334 /* \f */
335
336 static  server (fd, sin)
337 int     fd;
338 struct sockaddr_in *sin;
339 {
340     u_short port;
341     struct hostent *hp;
342     struct in_addr *addr;
343 #ifdef KPOP
344     int auth;
345     int sin_len;
346     struct sockaddr_in faddr;
347     char instance[INST_SZ];
348     char version[9];
349     char user[ANAME_SZ];
350 #endif
351
352     closelog ();
353 #ifndef BSD43
354     openlog (myname, LOG_PID);
355 #else   /* BSD43 */
356     openlog (myname, LOG_PID, LOG_DAEMON);
357 #endif  /* BSD43 */
358     port = ntohs (sin -> sin_port);
359     addr = &sin -> sin_addr;
360     hp = gethostbyaddr ((char *)addr, sizeof *addr, sin -> sin_family);
361     padvise (NULLCP, LOG_INFO, "servicing %s/%d",
362             hp ? hp -> h_name : inet_ntoa (*addr), port);
363     if (fd != 0)
364       {
365         (void) dup2 (fd, 0);
366         (void) close (fd);
367       }
368     (void) dup2 (0, 1);
369
370 #ifdef KPOP
371     sin_len = sizeof (struct sockaddr_in);
372     if (getpeername(0, &faddr, &sin_len) < 0) {
373       padvise("getpeername", LOG_INFO, "");
374       exit(1);
375     }
376     strcpy(instance, "*");
377     auth = krb_recvauth(0L, 0, &ticket, "pop", instance,
378                         &faddr, (struct sockaddr_in *)NULL,
379                         &kdata, "", schedule, version);
380     if (auth == KSUCCESS)
381       auth = krb_kntoln(&kdata, user);
382
383     if (auth != KSUCCESS) {
384       padvise(NULLCP, LOG_INFO, "bad kerberos data, not ok'ing");
385       kpop (0, 1, NULLCP, NULLCP, auth); /* respond(NOTOK, krb_err_txt[auth]); */
386     } else {
387       kpop (0, 1, user, (hp ? hp -> h_name : NULLCP), 0);
388     }
389 #else
390     pop (0, 1, sin -> sin_family == AF_INET && port < IPPORT_RESERVED && hp,
391             hp ? hp -> h_name : NULLCP);
392 #endif
393 }
394         
395 /* \f */
396
397 static  arginit (vec)
398 char    **vec;
399 {
400     int     port;
401     register char  *ap;
402     struct hostent *hp;
403
404     if (myname = rindex (*vec, '/'))
405         myname++;
406     if (myname == NULL || *myname == 0)
407         myname = *vec;
408
409     (void) gethostname (myhost, sizeof myhost);
410     if (hp = gethostbyname (myhost))
411         (void) strcpy (myhost, hp -> h_name);
412 #ifndef BSD42
413     nbits = _NFILE;
414 #else   /* BSD42 */
415     nbits = getdtablesize ();
416 #endif  /* BSD42 */
417
418     for (vec++; ap = *vec; vec++) {
419         if (*ap == '-')
420             switch (*++ap) {
421                 case 'd': 
422                     options |= SO_DEBUG;
423                     continue;
424
425                 case 'p': 
426                     if ((ap = *++vec) == NULL
427                             || *ap == '-'
428                             || (port = atoi (ap)) <= 0)
429                         padios (NULLCP, "usage: %s -p portno", myname);
430                     isock -> sin_port = htons ((u_short) port);
431                     continue;
432
433                 default: 
434                     padios (NULLCP, "-%s: unknown switch", ap);
435             }
436
437         padios (NULLCP, "usage: %s [switches]", myname);
438     }
439 }
440
441 /* \f */
442
443 static  envinit () {
444 #ifndef KPOP
445     int     i,
446             sd;
447
448     if (!(debug = isatty (2))) {
449         for (i = 0; i < 5; i++) {
450             switch (fork ()) {
451                 case NOTOK: 
452                     sleep (5);
453                     continue;
454
455                 case OK: 
456                     break;
457
458                 default: 
459                     _exit (0);
460             }
461             break;
462         }
463
464         (void) chdir ("/");
465
466         if ((sd = open ("/dev/null", O_RDWR)) == NOTOK)
467             padios ("/dev/null", "unable to read");
468         if (sd != 0)
469             (void) dup2 (sd, 0), (void) close (sd);
470         (void) dup2 (0, 1);
471         (void) dup2 (0, 2);
472
473         if ((sd = open ("/dev/tty", O_RDWR)) != NOTOK) {
474 #ifdef  TIOCNOTTY
475             (void) ioctl (sd, TIOCNOTTY, NULLCP);
476 #endif  /* TIOCNOTTY */
477             (void) close (sd);
478         }
479     }
480
481     for (sd = 3; sd < nbits; sd++)
482         (void) close (sd);
483 #endif /* KPOP */
484
485     (void) signal (SIGPIPE, SIG_IGN);
486
487 #ifndef BSD43
488     openlog (myname, LOG_PID);
489 #else   /* BSD43 */
490     openlog (myname, LOG_PID, LOG_DAEMON);
491 #endif  /* BSD43 */
492     padvise (NULLCP, LOG_INFO, "starting");
493     if (debug)
494         padvise (NULLCP, LOG_DEBUG, "options=0x%x port=%d",
495                 options, ntohs (isock -> sin_port));
496 }
497
498 /* \f */
499
500 #ifndef KPOP
501
502 /* ARGSUSED */
503
504 #ifdef  SIGCHLD
505
506 static TYPESIG chldser (sig, code, sc)
507 int     sig;
508 long    code;
509 struct sigcontext *sc;
510 {
511 #if defined(BSD42) && !defined(WAITINT)
512     union wait status;
513 #else
514     int status;
515 #endif
516
517     while (wait3 (&status, WNOHANG, NULLRP) > 0)
518         continue;
519 }
520
521 #endif /* SIGCHLD */
522 #endif /* KPOP */
523
524 /* \f */
525
526 /* VARARGS2 */
527
528 void    padios (what, fmt, a, b, c, d, e, f, g, h, i, j)
529 char   *what,
530        *fmt,
531        *a,
532        *b,
533        *c,
534        *d,
535        *e,
536        *f,
537        *g,
538        *h,
539        *i,
540        *j;
541 {
542     padvise (what, LOG_ERR, fmt, a, b, c, d, e, f, g, h, i, j);
543     _exit (1);
544 }
545
546 /* \f */
547
548 /* VARARGS3 */
549
550 void    padvise (what, code, fmt, a, b, c, d, e, f, g, h, i, j)
551 char   *what,
552        *fmt,
553        *a,
554        *b,
555        *c,
556        *d,
557        *e,
558        *f,
559        *g,
560        *h,
561        *i,
562        *j;
563 int     code;
564 {
565     int     eindex = errno;
566     char    buffer[BUFSIZ];
567
568     (void) sprintf (buffer, fmt, a, b, c, d, e, f, g, h, i, j);
569     if (what)
570         if (eindex > 0 && eindex < sys_nerr)
571             syslog (code, "%s %s: %s", buffer, what, sys_errlist[eindex]);
572         else
573             syslog (code, "%s %s: Error %d", buffer, what, eindex);
574     else
575         syslog (code, "%s", buffer);
576
577     if (debug) {
578         fprintf (stderr, "[%d] %s", code, buffer);
579         if (what)
580             (void) fputc (' ', stderr), perror (what);
581         else
582             (void) fputc ('\n', stderr);
583         (void) fflush (stderr);
584     }
585
586     errno = eindex;
587 }
588 @
589
590
591 1.15
592 log
593 @BSD44
594 case in gethostbyaddr
595 typo
596 @
597 text
598 @d3 1
599 a3 1
600 static char ident[] = "@@(#)$Id: popd.c,v 1.14 1992/12/16 22:31:20 jromine Exp jromine $";
601 d61 1
602 a63 1
603 #ifndef BSD44           /* const */
604 @
605
606
607 1.14
608 log
609 @TYPESIG fixups
610 @
611 text
612 @d3 1
613 a3 1
614 static char ident[] = "@@(#)$Id: popd.c,v 1.13 1992/12/15 00:20:22 jromine Exp jromine $";
615 d63 1
616 d65 1
617 d263 1
618 a263 1
619     hp = gethostbyaddr (addr, sizeof *addr, sin -> sin_family);
620 d415 2
621 a417 2
622 #else
623     union wait status;
624 @
625
626
627 1.13
628 log
629 @endif sugar
630 @
631 text
632 @d3 1
633 a3 1
634 static char ident[] = "@@(#)$Id: popd.c,v 1.12 1992/12/14 17:11:17 jromine Exp jromine $";
635 d86 1
636 a86 1
637 static int      chldser ();
638 d407 1
639 a407 1
640 static int chldser (sig, code, sc)
641 @
642
643
644 1.12
645 log
646 @WAITINT ifdef
647 @
648 text
649 @d3 2
650 a4 2
651 static char ident[] = "@@(#)$Id: popd.c,v 1.11 1992/11/11 22:26:33 jromine Exp jromine $";
652 #endif  lint
653 d165 1
654 a165 1
655 #else   BSD43
656 d167 1
657 a167 1
658 #endif  BSD43
659 d169 1
660 a169 1
661 #endif  RESTART
662 d181 1
663 a181 1
664 #else   BSD43
665 d187 1
666 a187 1
667 #endif  BSD43
668 d193 1
669 a193 1
670 #endif  SIGCHLD
671 d198 1
672 a198 1
673 #endif  FAST
674 d212 1
675 a212 1
676 #endif  FAST
677 d219 1
678 a219 1
679 #endif  SIGCHLD
680 d256 1
681 a256 1
682 #else   BSD43
683 d258 1
684 a258 1
685 #endif  BSD43
686 d315 1
687 a315 1
688 #else   BSD42
689 d317 1
690 a317 1
691 #endif  BSD42
692 d377 1
693 a377 1
694 #endif  TIOCNOTTY
695 d390 1
696 a390 1
697 #else   BSD43
698 d392 1
699 a392 1
700 #endif  BSD43
701 @
702
703
704 1.11
705 log
706 @use union wait only on BSD42
707 @
708 text
709 @d3 1
710 a3 1
711 static char ident[] = "@@(#)$Id: popd.c,v 1.10 1992/02/11 18:55:11 jromine Exp jromine $";
712 d128 1
713 d130 3
714 d412 3
715 a414 1
716 #ifdef  BSD42
717 a415 2
718 #else
719     int status;
720 @
721
722
723 1.10
724 log
725 @use _NFILE for #fds #ifndef BSD42
726 @
727 text
728 @d3 1
729 a3 1
730 static char ident[] = "@@(#)$Id: popd.c,v 1.9 1992/01/31 22:02:30 jromine Exp jromine $";
731 d408 1
732 d410 3
733 @
734
735
736 1.9
737 log
738 @kerberos
739 @
740 text
741 @d3 1
742 a3 1
743 static char ident[] = "@@(#)$Id: popd.c,v 1.8 90/04/09 09:45:09 sources Exp Locker: sources $";
744 d309 3
745 d313 1
746 @
747
748
749 1.8
750 log
751 @POPSERVICE define
752 @
753 text
754 @d3 1
755 a3 1
756 static char ident[] = "@@(#)$Id: popd.c,v 1.7 90/04/05 15:34:33 sources Exp Locker: sources $";
757 d24 1
758 a24 1
759 #endif  NOIOCTLH
760 d30 1
761 a30 1
762 #endif  SIGTSTP
763 d34 7
764 d43 1
765 a43 1
766 #endif  SYS5
767 d48 1
768 a48 1
769 #endif  SYS5
770 d82 3
771 d99 3
772 d108 1
773 d110 5
774 d120 1
775 d124 1
776 d191 1
777 d195 1
778 d205 1
779 d209 1
780 d226 3
781 d240 8
782 d260 5
783 a264 3
784
785     (void) dup2 (fd, 0);
786     (void) close (fd);
787 d267 20
788 d289 1
789 d303 1
790 a303 1
791     if (myname == NULL || *myname == NULL)
792 d337 1
793 d376 1
794 d393 2
795 d410 2
796 a411 1
797 #endif  SIGCHLD
798 @
799
800
801 1.7
802 log
803 @add ID
804 @
805 text
806 @d3 1
807 a3 1
808 static char ident[] = "@@(#)$Id:$";
809 d63 3
810 d70 1
811 a70 1
812 static char *myservice = "pop";
813 @
814
815
816 1.6
817 log
818 @add ID
819 @
820 text
821 @d3 1
822 a3 1
823 static char ident[] = "@@(#)$Id:";
824 @
825
826
827 1.5
828 log
829 @ANSI Compilance
830 @
831 text
832 @d2 3
833 @
834
835
836 1.4
837 log
838 @*** empty log message ***
839 @
840 text
841 @d70 1
842 a70 1
843 int     chldser ();
844 d73 1
845 @
846
847
848 1.3
849 log
850 @SYS5 fixes
851 @
852 text
853 @d19 1
854 d21 1
855 @
856
857
858 1.2
859 log
860 @SYS5 fix
861 @
862 text
863 @d15 2
864 a16 2
865 #include <strings.h>
866 #include <syslog.h>
867 d22 1
868 d25 1
869 d157 1
870 d159 1
871 d179 1
872 d181 1
873 d298 1
874 d300 1
875 d325 2
876 d337 2
877 @
878
879
880 1.1
881 log
882 @Initial revision
883 @
884 text
885 @d27 3
886 d31 4
887 @