Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/write separate the term check stuff.



details:   https://anonhg.NetBSD.org/src/rev/c65dbe51689a
branches:  trunk
changeset: 546138:c65dbe51689a
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Apr 20 23:53:04 2003 +0000

description:
separate the term check stuff.

diffstat:

 usr.bin/write/Makefile   |    4 +-
 usr.bin/write/term_chk.c |  132 +++++++++++++++++++++++++++++++++++++++++++++++
 usr.bin/write/term_chk.h |   40 ++++++++++++++
 usr.bin/write/write.c    |   89 ++++---------------------------
 4 files changed, 186 insertions(+), 79 deletions(-)

diffs (truncated from 358 to 300 lines):

diff -r e1fcc0addce5 -r c65dbe51689a usr.bin/write/Makefile
--- a/usr.bin/write/Makefile    Sun Apr 20 23:43:04 2003 +0000
+++ b/usr.bin/write/Makefile    Sun Apr 20 23:53:04 2003 +0000
@@ -1,10 +1,10 @@
-#      $NetBSD: Makefile,v 1.6 2002/09/18 14:00:44 lukem Exp $
+#      $NetBSD: Makefile,v 1.7 2003/04/20 23:53:04 christos Exp $
 #      from: @(#)Makefile      8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
 
 PROG=  write
-SRCS=  write.c utmpentry.c
+SRCS=  write.c utmpentry.c term_chk.c
 BINMODE=2555
 BINGRP=        tty
 
diff -r e1fcc0addce5 -r c65dbe51689a usr.bin/write/term_chk.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/write/term_chk.c  Sun Apr 20 23:53:04 2003 +0000
@@ -0,0 +1,132 @@
+/* $NetBSD: term_chk.c,v 1.1 2003/04/20 23:53:04 christos Exp $ */
+
+/*
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: term_chk.c,v 1.1 2003/04/20 23:53:04 christos Exp $");
+#endif
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <paths.h>
+#include <fcntl.h>
+#include <string.h>
+#include <err.h>
+
+#include "term_chk.h"
+
+/*
+ * term_chk - check that a terminal exists, and get the message bit
+ *     and the access time
+ */
+int
+term_chk(uid_t uid, const char *tty, int *msgsokP, time_t *atimeP, int ismytty,
+    gid_t saved_egid)
+{
+       char path[MAXPATHLEN];
+       struct stat s;
+       int i, fd, serrno;
+
+       if (strcspn(tty, "./") != strlen(tty)) {
+               errno = EINVAL; return(-1);
+       }
+       i = snprintf(path, sizeof path, _PATH_DEV "%s", tty);
+       if (i < 0 || i >= sizeof(path)) {
+               errno = ENOMEM; return(-1);
+       }
+
+       (void)setegid(saved_egid);
+       fd = open(path, O_WRONLY, 0);
+       serrno = errno;
+       (void)setegid(getgid());
+       errno = serrno;
+
+       if (fd == -1)
+               return(-1);
+       if (fstat(fd, &s) == -1)
+               goto error;
+       if (!isatty(fd) || s.st_uid != uid)
+               goto error;
+       *msgsokP = (s.st_mode & S_IWGRP) != 0;  /* group write bit */
+       *atimeP = s.st_atime;
+       if (ismytty)
+               (void) close(fd);
+       return(ismytty? 0: fd);
+error:
+       if (fd != -1) {
+               serrno = errno;
+               close(fd);
+               errno = serrno;
+       }
+       return(-1);
+}
+
+char *
+check_sender(time_t *atime, uid_t myuid, gid_t saved_egid)
+{
+       int myttyfd;
+       int msgsok;
+       char *mytty;
+       char *cp;
+
+       /* check that sender has write enabled */
+       if (isatty(fileno(stdin)))
+               myttyfd = fileno(stdin);
+       else if (isatty(fileno(stdout)))
+               myttyfd = fileno(stdout);
+       else if (isatty(fileno(stderr)))
+               myttyfd = fileno(stderr);
+       else
+               errx(1, "can't find your tty");
+       if (!(mytty = ttyname(myttyfd)))
+               errx(1, "can't find your tty's name");
+       if ((cp = strrchr(mytty, '/')) != NULL)
+               mytty = cp + 1;
+       if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1)
+               err(1, "%s%s", _PATH_DEV, mytty);
+       if (!msgsok) {
+               warnx(
+                   "You have write permission turned off; no reply possible");
+       }
+       return mytty;
+}
diff -r e1fcc0addce5 -r c65dbe51689a usr.bin/write/term_chk.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/write/term_chk.h  Sun Apr 20 23:53:04 2003 +0000
@@ -0,0 +1,40 @@
+/*     $NetBSD: term_chk.h,v 1.1 2003/04/20 23:53:04 christos Exp $    */
+
+/*
+ * Copyright (c) 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+int term_chk(uid_t, const char *, int *, time_t *, int, gid_t);
+char *check_sender(time_t *, uid_t, gid_t);
diff -r e1fcc0addce5 -r c65dbe51689a usr.bin/write/write.c
--- a/usr.bin/write/write.c     Sun Apr 20 23:43:04 2003 +0000
+++ b/usr.bin/write/write.c     Sun Apr 20 23:53:04 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: write.c,v 1.21 2002/08/16 20:21:49 itojun Exp $        */
+/*     $NetBSD: write.c,v 1.22 2003/04/20 23:53:05 christos Exp $      */
 
 /*
  * Copyright (c) 1989, 1993
@@ -46,7 +46,7 @@
 #if 0
 static char sccsid[] = "@(#)write.c    8.2 (Berkeley) 4/27/95";
 #else
-__RCSID("$NetBSD: write.c,v 1.21 2002/08/16 20:21:49 itojun Exp $");
+__RCSID("$NetBSD: write.c,v 1.22 2003/04/20 23:53:05 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -67,57 +67,36 @@
 #include <errno.h>
 
 #include "utmpentry.h"
+#include "term_chk.h"
 
 void done(int);
 void do_write(int, const char *, const uid_t);
 void wr_fputs(char *);
-int search_utmp(char *, char *, uid_t);
-int term_chk(uid_t, const char *, int *, time_t *, int);
+int search_utmp(char *, char *, uid_t, gid_t);
 int utmp_chk(const char *, const char *);
 int main(int, char **);
 
-static gid_t   saved_egid;
 
 int
 main(int argc, char **argv)
 {
-       char *cp;
        time_t atime;
        uid_t myuid, uid;
-       int msgsok, myttyfd, ttyfd;
+       int msgsok, ttyfd;
        char *mytty;
+       gid_t saved_egid = getegid();
 
-       saved_egid = getegid();
        if (setegid(getgid()) == -1)
                err(1, "setegid");
        myuid = getuid();
        ttyfd = -1;
 
-       /* check that sender has write enabled */
-       if (isatty(fileno(stdin)))
-               myttyfd = fileno(stdin);
-       else if (isatty(fileno(stdout)))
-               myttyfd = fileno(stdout);
-       else if (isatty(fileno(stderr)))
-               myttyfd = fileno(stderr);
-       else
-               errx(1, "can't find your tty");
-       if (!(mytty = ttyname(myttyfd)))
-               errx(1, "can't find your tty's name");
-       if ((cp = strrchr(mytty, '/')) != NULL)
-               mytty = cp + 1;
-       if (term_chk(myuid, mytty, &msgsok, &atime, 1) == -1)
-               err(1, "%s%s", _PATH_DEV, mytty);
-       if (!msgsok) {
-               (void)fprintf(stderr,
-                   "warning: you have write permission turned off; "
-                   "no reply possible\n");
-       }
+       mytty = check_sender(&atime, myuid, saved_egid);
 
        /* check args */
        switch (argc) {
        case 2:
-               ttyfd = search_utmp(argv[1], mytty, myuid);
+               ttyfd = search_utmp(argv[1], mytty, myuid, saved_egid);
                break;
        case 3:
                if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
@@ -127,7 +106,7 @@
                if (utmp_chk(argv[1], argv[2]))
                        errx(1, "%s is not logged in on %s",
                            argv[1], argv[2]);
-               ttyfd = term_chk(uid, argv[2], &msgsok, &atime, 0);
+               ttyfd = term_chk(uid, argv[2], &msgsok, &atime, 0, saved_egid);
                if (ttyfd == -1)
                        err(1, "%s%s", _PATH_DEV, argv[2]);
                if (myuid && !msgsok)
@@ -177,7 +156,7 @@
  * writing from, unless that's the only terminal with messages enabled.
  */
 int
-search_utmp(char *user, char *mytty, uid_t myuid)
+search_utmp(char *user, char *mytty, uid_t myuid, gid_t saved_egid)
 {
        char tty[MAXPATHLEN];
        time_t bestatime, atime;
@@ -198,7 +177,8 @@
        for (; ep; ep = ep->next)
                if (strcmp(user, ep->name) == 0) {
                        ++nloggedttys;



Home | Main Index | Thread Index | Old Index