]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mesg.c The "mesg" utility. Gives / restrict access to | |
3 | * your terminal by others. | |
4 | * | |
5 | * Usage: mesg [y|n]. | |
6 | * Without arguments prints out the current settings. | |
7 | * | |
8 | * This file is part of the sysvinit suite, | |
9 | * Copyright (C) 1991-2001 Miquel van Smoorenburg. | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <errno.h> | |
31 | #include <unistd.h> | |
32 | #include <grp.h> | |
33 | ||
34 | char *Version = "@(#) mesg 2.81 31-Jul-2001 miquels@cistron.nl"; | |
35 | ||
36 | #define TTYGRP "tty" | |
37 | ||
38 | /* | |
39 | * See if the system has a special 'tty' group. | |
40 | * If it does, and the tty device is in that group, | |
41 | * we set the modes to -rw--w--- instead if -rw--w--w. | |
42 | */ | |
43 | int hasttygrp(void) | |
44 | { | |
45 | struct group *grp; | |
46 | ||
47 | if ((grp = getgrnam(TTYGRP)) != NULL) | |
48 | return 1; | |
49 | return 0; | |
50 | } | |
51 | ||
52 | ||
53 | /* | |
54 | * See if the tty devices group is indeed 'tty' | |
55 | */ | |
56 | int tty_in_ttygrp(struct stat *st) | |
57 | { | |
58 | struct group *gr; | |
59 | ||
60 | if ((gr = getgrgid(st->st_gid)) == NULL) | |
61 | return 0; | |
62 | if (strcmp(gr->gr_name, TTYGRP) != 0) | |
63 | return 0; | |
64 | ||
65 | return 1; | |
66 | } | |
67 | ||
68 | int main(int argc, char **argv) | |
69 | { | |
70 | struct stat st; | |
71 | unsigned int ttymode, st_mode_old; | |
72 | int ht; | |
73 | int it; | |
74 | int e; | |
75 | ||
76 | if (!isatty(0)) { | |
77 | /* Or should we look in /var/run/utmp? */ | |
78 | fprintf(stderr, "stdin: is not a tty\n"); | |
79 | return(1); | |
80 | } | |
81 | ||
82 | if (fstat(0, &st) < 0) { | |
83 | perror("fstat"); | |
84 | return(1); | |
85 | } | |
86 | ||
87 | ht = hasttygrp(); | |
88 | it = tty_in_ttygrp(&st); | |
89 | ||
90 | if (argc < 2) { | |
91 | ttymode = (ht && it) ? 020 : 002; | |
92 | printf("is %s\n", (st.st_mode & ttymode) ? "y" : "n"); | |
93 | return 0; | |
94 | } | |
95 | if (argc > 2 || (argv[1][0] != 'y' && argv[1][0] != 'n')) { | |
96 | fprintf(stderr, "Usage: mesg [y|n]\n"); | |
97 | return 1; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Security check: allow mesg n when group is | |
102 | * weird, but don't allow mesg y. | |
103 | */ | |
104 | ttymode = ht ? 020 : 022; | |
105 | if (ht && !it && argv[1][0] == 'y') { | |
106 | fprintf(stderr, "mesg: error: tty device is not owned " | |
107 | "by group `%s'\n", TTYGRP); | |
108 | exit(1); | |
109 | } | |
110 | ||
111 | st_mode_old = st.st_mode; | |
112 | if (argv[1][0] == 'y') | |
113 | st.st_mode |= ttymode; | |
114 | else | |
115 | st.st_mode &= ~(ttymode); | |
116 | if (st_mode_old != st.st_mode && fchmod(0, st.st_mode) != 0) { | |
117 | e = errno; | |
118 | fprintf(stderr, "mesg: %s: %s\n", | |
119 | ttyname(0), strerror(e)); | |
120 | exit(1); | |
121 | } | |
122 | ||
123 | return 0; | |
124 | } |