be more social about killing attached processes (vhangup takes care of it)
[unix-history] / usr / src / libexec / telnetd / telnetd.c
index e8928a7..a2d93ce 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)telnetd.c  5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)telnetd.c  5.4 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -35,7 +35,7 @@ static char sccsid[] = "@(#)telnetd.c 5.1 (Berkeley) %G%";
 #include <syslog.h>
 
 #define        BELL    '\07'
 #include <syslog.h>
 
 #define        BELL    '\07'
-#define BANNER "\r\n\r\n4.2 BSD UNIX (%s)\r\n\r\r\n\r%s"
+#define BANNER "\r\n\r\n4.3 BSD UNIX (%s)\r\n\r\r\n\r%s"
 
 char   hisopts[256];
 char   myopts[256];
 
 char   hisopts[256];
 char   myopts[256];
@@ -66,6 +66,7 @@ main(argc, argv)
        struct sockaddr_in from;
        int on = 1, fromlen;
 
        struct sockaddr_in from;
        int on = 1, fromlen;
 
+       openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
        fromlen = sizeof (from);
        if (getpeername(0, &from, &fromlen) < 0) {
                fprintf(stderr, "%s: ", argv[0]);
        fromlen = sizeof (from);
        if (getpeername(0, &from, &fromlen) < 0) {
                fprintf(stderr, "%s: ", argv[0]);
@@ -73,7 +74,6 @@ main(argc, argv)
                _exit(1);
        }
        if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
                _exit(1);
        }
        if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
-               openlog(argv[0], LOG_PID, 0);
                syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
        }
        doit(0, &from);
                syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
        }
        doit(0, &from);
@@ -577,7 +577,6 @@ cleanup()
        rmut();
        vhangup();      /* XXX */
        shutdown(net, 2);
        rmut();
        vhangup();      /* XXX */
        shutdown(net, 2);
-       kill(0, SIGKILL);
        exit(1);
 }
 
        exit(1);
 }
 
@@ -585,26 +584,39 @@ cleanup()
 
 struct utmp wtmp;
 char   wtmpf[] = "/usr/adm/wtmp";
 
 struct utmp wtmp;
 char   wtmpf[] = "/usr/adm/wtmp";
-char   utmp[] = "/etc/utmp";
-#define SCPYN(a, b)    strncpy(a, b, sizeof (a))
-#define SCMPN(a, b)    strncmp(a, b, sizeof (a))
+char   utmpf[] = "/etc/utmp";
+#define SCPYN(a, b)    strncpy(a, b, sizeof(a))
+#define SCMPN(a, b)    strncmp(a, b, sizeof(a))
 
 rmut()
 {
        register f;
        int found = 0;
 
 rmut()
 {
        register f;
        int found = 0;
+       struct utmp *u, *utmp;
+       int nutmp;
+       struct stat statbf;
 
 
-       f = open(utmp, O_RDWR);
+       f = open(utmpf, O_RDWR);
        if (f >= 0) {
        if (f >= 0) {
-               while(read(f, (char *)&wtmp, sizeof (wtmp)) == sizeof (wtmp)) {
-                       if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0)
-                               continue;
-                       lseek(f, -(long)sizeof (wtmp), L_INCR);
-                       SCPYN(wtmp.ut_name, "");
-                       SCPYN(wtmp.ut_host, "");
-                       time(&wtmp.ut_time);
-                       write(f, (char *)&wtmp, sizeof (wtmp));
-                       found++;
+               fstat(f, &statbf);
+               utmp = (struct utmp *)malloc(statbf.st_size);
+               if (!utmp)
+                       syslog(LOG_ERR, "utmp malloc failed");
+               if (statbf.st_size && utmp) {
+                       nutmp = read(f, utmp, statbf.st_size);
+                       nutmp /= sizeof(struct utmp);
+               
+                       for (u = utmp ; u < &utmp[nutmp] ; u++) {
+                               if (SCMPN(u->ut_line, line+5) ||
+                                   u->ut_name[0]==0)
+                                       continue;
+                               lseek(f, ((long)u)-((long)utmp), L_SET);
+                               SCPYN(u->ut_name, "");
+                               SCPYN(u->ut_host, "");
+                               time(&u->ut_time);
+                               write(f, (char *)u, sizeof(wtmp));
+                               found++;
+                       }
                }
                close(f);
        }
                }
                close(f);
        }
@@ -615,7 +627,7 @@ rmut()
                        SCPYN(wtmp.ut_name, "");
                        SCPYN(wtmp.ut_host, "");
                        time(&wtmp.ut_time);
                        SCPYN(wtmp.ut_name, "");
                        SCPYN(wtmp.ut_host, "");
                        time(&wtmp.ut_time);
-                       write(f, (char *)&wtmp, sizeof (wtmp));
+                       write(f, (char *)&wtmp, sizeof(wtmp));
                        close(f);
                }
        }
                        close(f);
                }
        }