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