term_chk.c revision 1.3 1 1.3 christos /* $NetBSD: term_chk.c,v 1.3 2003/05/13 23:05:34 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1989, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to Berkeley by
8 1.1 christos * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the University of
21 1.1 christos * California, Berkeley and its contributors.
22 1.1 christos * 4. Neither the name of the University nor the names of its contributors
23 1.1 christos * may be used to endorse or promote products derived from this software
24 1.1 christos * without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 christos * SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/cdefs.h>
40 1.1 christos #ifndef lint
41 1.3 christos __RCSID("$NetBSD: term_chk.c,v 1.3 2003/05/13 23:05:34 christos Exp $");
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos #include <sys/types.h>
45 1.1 christos #include <sys/param.h>
46 1.1 christos #include <sys/stat.h>
47 1.1 christos #include <time.h>
48 1.1 christos #include <stdio.h>
49 1.1 christos #include <errno.h>
50 1.1 christos #include <unistd.h>
51 1.1 christos #include <paths.h>
52 1.1 christos #include <fcntl.h>
53 1.1 christos #include <string.h>
54 1.1 christos #include <err.h>
55 1.1 christos
56 1.1 christos #include "term_chk.h"
57 1.1 christos
58 1.1 christos /*
59 1.1 christos * term_chk - check that a terminal exists, and get the message bit
60 1.1 christos * and the access time
61 1.1 christos */
62 1.1 christos int
63 1.1 christos term_chk(uid_t uid, const char *tty, int *msgsokP, time_t *atimeP, int ismytty,
64 1.1 christos gid_t saved_egid)
65 1.1 christos {
66 1.1 christos char path[MAXPATHLEN];
67 1.1 christos struct stat s;
68 1.1 christos int i, fd, serrno;
69 1.1 christos
70 1.1 christos if (strcspn(tty, "./") != strlen(tty)) {
71 1.2 christos errno = EINVAL;
72 1.2 christos return -1;
73 1.1 christos }
74 1.1 christos i = snprintf(path, sizeof path, _PATH_DEV "%s", tty);
75 1.1 christos if (i < 0 || i >= sizeof(path)) {
76 1.2 christos errno = ENOMEM;
77 1.2 christos return -1;
78 1.1 christos }
79 1.1 christos
80 1.1 christos (void)setegid(saved_egid);
81 1.1 christos fd = open(path, O_WRONLY, 0);
82 1.1 christos serrno = errno;
83 1.1 christos (void)setegid(getgid());
84 1.1 christos errno = serrno;
85 1.1 christos
86 1.1 christos if (fd == -1)
87 1.1 christos return(-1);
88 1.1 christos if (fstat(fd, &s) == -1)
89 1.1 christos goto error;
90 1.2 christos if (!isatty(fd))
91 1.1 christos goto error;
92 1.3 christos if (s.st_uid != uid && uid != 0) {
93 1.2 christos errno = EPERM;
94 1.2 christos goto error;
95 1.2 christos }
96 1.1 christos *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
97 1.1 christos *atimeP = s.st_atime;
98 1.1 christos if (ismytty)
99 1.2 christos (void)close(fd);
100 1.2 christos return ismytty ? 0 : fd;
101 1.1 christos error:
102 1.1 christos if (fd != -1) {
103 1.1 christos serrno = errno;
104 1.2 christos (void)close(fd);
105 1.1 christos errno = serrno;
106 1.1 christos }
107 1.2 christos return -1;
108 1.1 christos }
109 1.1 christos
110 1.1 christos char *
111 1.1 christos check_sender(time_t *atime, uid_t myuid, gid_t saved_egid)
112 1.1 christos {
113 1.1 christos int myttyfd;
114 1.1 christos int msgsok;
115 1.1 christos char *mytty;
116 1.1 christos char *cp;
117 1.1 christos
118 1.1 christos /* check that sender has write enabled */
119 1.1 christos if (isatty(fileno(stdin)))
120 1.1 christos myttyfd = fileno(stdin);
121 1.1 christos else if (isatty(fileno(stdout)))
122 1.1 christos myttyfd = fileno(stdout);
123 1.1 christos else if (isatty(fileno(stderr)))
124 1.1 christos myttyfd = fileno(stderr);
125 1.1 christos else
126 1.1 christos errx(1, "can't find your tty");
127 1.1 christos if (!(mytty = ttyname(myttyfd)))
128 1.1 christos errx(1, "can't find your tty's name");
129 1.1 christos if ((cp = strrchr(mytty, '/')) != NULL)
130 1.1 christos mytty = cp + 1;
131 1.1 christos if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1)
132 1.1 christos err(1, "%s%s", _PATH_DEV, mytty);
133 1.1 christos if (!msgsok) {
134 1.1 christos warnx(
135 1.1 christos "You have write permission turned off; no reply possible");
136 1.1 christos }
137 1.1 christos return mytty;
138 1.1 christos }
139