Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/utmp_update The following changes make ftpd able to ...



details:   https://anonhg.NetBSD.org/src/rev/699735a7bb64
branches:  trunk
changeset: 543490:699735a7bb64
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Feb 26 18:16:50 2003 +0000

description:
The following changes make ftpd able to remove its own entries. Ftpd
calls pututxline() with ruid = 0, euid = current-ftp-user. This ends
up calling update_utmp:
- if the real uid is root, don't do password or tty ownership checks
- if we cannot open the tty line, assume that it is a daemon that does
  not use ttys and allow it to change a live entry to a dead one if
  indeed it is the same process that created the entry.

diffstat:

 libexec/utmp_update/utmp_update.c |  60 +++++++++++++++++++++++++-------------
 1 files changed, 39 insertions(+), 21 deletions(-)

diffs (95 lines):

diff -r cdf03e582207 -r 699735a7bb64 libexec/utmp_update/utmp_update.c
--- a/libexec/utmp_update/utmp_update.c Wed Feb 26 18:16:37 2003 +0000
+++ b/libexec/utmp_update/utmp_update.c Wed Feb 26 18:16:50 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: utmp_update.c,v 1.5 2002/12/18 15:20:47 christos Exp $  */
+/*     $NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos Exp $  */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 #include <sys/cdefs.h>
 
-__RCSID("$NetBSD: utmp_update.c,v 1.5 2002/12/18 15:20:47 christos Exp $");
+__RCSID("$NetBSD: utmp_update.c,v 1.6 2003/02/26 18:16:50 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -64,11 +64,12 @@
        struct passwd *pwd;
        struct stat st;
        int fd;
-       uid_t euid;
+       uid_t euid, ruid;
        char tty[MAXPATHLEN];
 
        euid = geteuid();
-       if (seteuid(getuid()) == -1)
+       ruid = getuid();
+       if (seteuid(ruid) == -1)
                err(1, "seteuid");
 
        if (argc != 2) {
@@ -96,27 +97,44 @@
                errx(1, "Invalid utmpx type %d", (int)utx->ut_type);
        }
 
-       if ((pwd = getpwuid(getuid())) == NULL)
-               errx(1, "User %lu does not exist in password database",
-                   (long)getuid());
+       if (ruid != 0) {
+               if ((pwd = getpwuid(ruid)) == NULL)
+                       errx(1, "User %lu does not exist in password database",
+                           (long)ruid);
 
-       if (strcmp(pwd->pw_name, utx->ut_name) != 0)
-               errx(1, "Current user `%s' does not match `%s' in utmpx entry",
-                   pwd->pw_name, utx->ut_name);
+               if (strcmp(pwd->pw_name, utx->ut_name) != 0)
+                       errx(1, "Current user `%s' does not match "
+                           "`%s' in utmpx entry", pwd->pw_name, utx->ut_name);
+       }
 
        (void)snprintf(tty, sizeof(tty), "%s%s", _PATH_DEV, utx->ut_line);
        fd = open(tty, O_RDONLY, 0);
-       if (fd == -1)
-               err(1, "Cannot open `%s'", tty);
-       if (fstat(fd, &st) == -1)
-               err(1, "Cannot stat `%s'", tty);
-       if (st.st_uid != getuid())
-               errx(1, "%s: Is not owned by you", tty);
-       if (!isatty(fd))
-               errx(1, "%s: Not a tty device", tty);
-       (void)close(fd);
-       if (access(tty, W_OK|R_OK) == -1)
-               err(1, "%s", tty);
+       if (fd != -1) {
+               if (fstat(fd, &st) == -1)
+                       err(1, "Cannot stat `%s'", tty);
+               if (ruid != 0 && st.st_uid != ruid)
+                       errx(1, "%s: Is not owned by you", tty);
+               if (!isatty(fd))
+                       errx(1, "%s: Not a tty device", tty);
+               (void)close(fd);
+               if (access(tty, W_OK|R_OK) == -1)
+                       err(1, "%s", tty);
+       } else {
+               struct utmpx utold, *utoldp;
+               /*
+                * A daemon like ftpd that does not use a tty line? 
+                * We only allow it to kill its own existing entries 
+                */
+               if (utx->ut_type != DEAD_PROCESS)
+                       err(1, "Cannot open `%s'", tty);
+
+               (void)memcpy(utold.ut_line, utx->ut_line, sizeof(utx->ut_line));
+               if ((utoldp = getutxline(&utold)) == NULL)
+                       err(1, "Cannot find existing entry for `%s'",
+                           utx->ut_line);
+               if (utoldp->ut_pid != getppid())
+                       err(1, "Cannot modify entry for `%s'", tty);
+       }
 
        (void)seteuid(euid);
        if (pututxline(utx) == NULL)



Home | Main Index | Thread Index | Old Index