work work werk
[mikesnafu-overlay.git] / sysvinit-2.86 / src / mesg.c
blobfd9dd27ea4f086f3820ec0f2bd2711a45ae4d19b
1 /*
2 * mesg.c The "mesg" utility. Gives / restrict access to
3 * your terminal by others.
5 * Usage: mesg [y|n].
6 * Without arguments prints out the current settings.
8 * This file is part of the sysvinit suite,
9 * Copyright 1991-2001 Miquel van Smoorenburg.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <grp.h>
25 char *Version = "@(#) mesg 2.81 31-Jul-2001 miquels@cistron.nl";
27 #define TTYGRP "tty"
30 * See if the system has a special 'tty' group.
31 * If it does, and the tty device is in that group,
32 * we set the modes to -rw--w--- instead if -rw--w--w.
34 int hasttygrp(void)
36 struct group *grp;
38 if ((grp = getgrnam(TTYGRP)) != NULL)
39 return 1;
40 return 0;
45 * See if the tty devices group is indeed 'tty'
47 int tty_in_ttygrp(struct stat *st)
49 struct group *gr;
51 if ((gr = getgrgid(st->st_gid)) == NULL)
52 return 0;
53 if (strcmp(gr->gr_name, TTYGRP) != 0)
54 return 0;
56 return 1;
59 int main(int argc, char **argv)
61 struct stat st;
62 unsigned int ttymode, st_mode_old;
63 int ht;
64 int it;
65 int e;
67 if (!isatty(0)) {
68 /* Or should we look in /var/run/utmp? */
69 fprintf(stderr, "stdin: is not a tty\n");
70 return(1);
73 if (fstat(0, &st) < 0) {
74 perror("fstat");
75 return(1);
78 ht = hasttygrp();
79 it = tty_in_ttygrp(&st);
81 if (argc < 2) {
82 ttymode = (ht && it) ? 020 : 002;
83 printf("is %s\n", (st.st_mode & ttymode) ? "y" : "n");
84 return 0;
86 if (argc > 2 || (argv[1][0] != 'y' && argv[1][0] != 'n')) {
87 fprintf(stderr, "Usage: mesg [y|n]\n");
88 return 1;
92 * Security check: allow mesg n when group is
93 * weird, but don't allow mesg y.
95 ttymode = ht ? 020 : 022;
96 if (ht && !it && argv[1][0] == 'y') {
97 fprintf(stderr, "mesg: error: tty device is not owned "
98 "by group `%s'\n", TTYGRP);
99 exit(1);
102 st_mode_old = st.st_mode;
103 if (argv[1][0] == 'y')
104 st.st_mode |= ttymode;
105 else
106 st.st_mode &= ~(ttymode);
107 if (st_mode_old != st.st_mode && fchmod(0, st.st_mode) != 0) {
108 e = errno;
109 fprintf(stderr, "mesg: %s: %s\n",
110 ttyname(0), strerror(e));
111 exit(1);
114 return 0;