2 * mesg.c The "mesg" utility. Gives / restrict access to
3 * your terminal by others.
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>
25 char *Version
= "@(#) mesg 2.81 31-Jul-2001 miquels@cistron.nl";
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.
38 if ((grp
= getgrnam(TTYGRP
)) != NULL
)
45 * See if the tty devices group is indeed 'tty'
47 int tty_in_ttygrp(struct stat
*st
)
51 if ((gr
= getgrgid(st
->st_gid
)) == NULL
)
53 if (strcmp(gr
->gr_name
, TTYGRP
) != 0)
59 int main(int argc
, char **argv
)
62 unsigned int ttymode
, st_mode_old
;
68 /* Or should we look in /var/run/utmp? */
69 fprintf(stderr
, "stdin: is not a tty\n");
73 if (fstat(0, &st
) < 0) {
79 it
= tty_in_ttygrp(&st
);
82 ttymode
= (ht
&& it
) ? 020 : 002;
83 printf("is %s\n", (st
.st_mode
& ttymode
) ? "y" : "n");
86 if (argc
> 2 || (argv
[1][0] != 'y' && argv
[1][0] != 'n')) {
87 fprintf(stderr
, "Usage: mesg [y|n]\n");
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
);
102 st_mode_old
= st
.st_mode
;
103 if (argv
[1][0] == 'y')
104 st
.st_mode
|= ttymode
;
106 st
.st_mode
&= ~(ttymode
);
107 if (st_mode_old
!= st
.st_mode
&& fchmod(0, st
.st_mode
) != 0) {
109 fprintf(stderr
, "mesg: %s: %s\n",
110 ttyname(0), strerror(e
));