]>
git.wh0rd.org - sysvinit.git/blob - src/mesg.c
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 (C) 1991-2001 Miquel van Smoorenburg.
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.
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.
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
25 #include <sys/types.h>
34 char *Version
= "@(#) mesg 2.81 31-Jul-2001 miquels@cistron.nl";
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.
47 if ((grp
= getgrnam(TTYGRP
)) != NULL
)
54 * See if the tty devices group is indeed 'tty'
56 int tty_in_ttygrp(struct stat
*st
)
60 if ((gr
= getgrgid(st
->st_gid
)) == NULL
)
62 if (strcmp(gr
->gr_name
, TTYGRP
) != 0)
68 int main(int argc
, char **argv
)
71 unsigned int ttymode
, st_mode_old
;
77 /* Or should we look in /var/run/utmp? */
78 fprintf(stderr
, "stdin: is not a tty\n");
82 if (fstat(0, &st
) < 0) {
88 it
= tty_in_ttygrp(&st
);
91 ttymode
= (ht
&& it
) ? 020 : 002;
92 printf("is %s\n", (st
.st_mode
& ttymode
) ? "y" : "n");
95 if (argc
> 2 || (argv
[1][0] != 'y' && argv
[1][0] != 'n')) {
96 fprintf(stderr
, "Usage: mesg [y|n]\n");
101 * Security check: allow mesg n when group is
102 * weird, but don't allow mesg y.
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
);
111 st_mode_old
= st
.st_mode
;
112 if (argv
[1][0] == 'y')
113 st
.st_mode
|= ttymode
;
115 st
.st_mode
&= ~(ttymode
);
116 if (st_mode_old
!= st
.st_mode
&& fchmod(0, st
.st_mode
) != 0) {
118 fprintf(stderr
, "mesg: %s: %s\n",
119 ttyname(0), strerror(e
));