blob: a83da03e62bd91417cea3a917cd41cf50f47afe3 [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath061c9002002-09-16 04:21:46 +00002/*
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00003 * mesg implementation for busybox
Glenn L McGrath061c9002002-09-16 04:21:46 +00004 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00005 * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org>
Glenn L McGrath061c9002002-09-16 04:21:46 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath061c9002002-09-16 04:21:46 +00008 */
9
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020010//config:config MESG
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "mesg (1.2 kb)"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020012//config: default y
13//config: help
14//config: Mesg controls access to your terminal by others. It is typically
15//config: used to allow or disallow other users to write to your terminal
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010016//config:
17//config:config FEATURE_MESG_ENABLE_ONLY_GROUP
18//config: bool "Enable writing to tty only by group, not by everybody"
19//config: default y
20//config: depends on MESG
21//config: help
22//config: Usually, ttys are owned by group "tty", and "write" tool is
23//config: setgid to this group. This way, "mesg y" only needs to enable
24//config: "write by owning group" bit in tty mode.
25//config:
26//config: If you set this option to N, "mesg y" will enable writing
27//config: by anybody at all. This is not recommended.
28
29//applet:IF_MESG(APPLET(mesg, BB_DIR_USR_BIN, BB_SUID_DROP))
30
31//kbuild:lib-$(CONFIG_MESG) += mesg.o
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020032
33//usage:#define mesg_trivial_usage
34//usage: "[y|n]"
35//usage:#define mesg_full_usage "\n\n"
36//usage: "Control write access to your terminal\n"
37//usage: " y Allow write access to your terminal\n"
38//usage: " n Disallow write access to your terminal"
39
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Glenn L McGrath061c9002002-09-16 04:21:46 +000041
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010042#if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020043#define S_IWGRP_OR_S_IWOTH S_IWGRP
Glenn L McGrath061c9002002-09-16 04:21:46 +000044#else
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020045#define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
Glenn L McGrath061c9002002-09-16 04:21:46 +000046#endif
47
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010049int mesg_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath061c9002002-09-16 04:21:46 +000050{
51 struct stat sb;
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010052 mode_t m;
Eric Andersen78500142004-08-27 19:55:28 +000053 char c = 0;
Glenn L McGrath061c9002002-09-16 04:21:46 +000054
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010055 argv++;
56
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010057 if (argv[0]
58 && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n'))
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000059 ) {
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010060 bb_show_usage();
Glenn L McGrath061c9002002-09-16 04:21:46 +000061 }
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010062
Denys Vlasenko4a2aecb2011-03-28 00:59:16 +020063 if (!isatty(STDIN_FILENO))
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010064 bb_error_msg_and_die("not a tty");
65
Denys Vlasenko4a2aecb2011-03-28 00:59:16 +020066 xfstat(STDIN_FILENO, &sb, "stderr");
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010067 if (c == 0) {
68 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
69 return EXIT_SUCCESS;
70 }
71 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
72 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
Denys Vlasenko4a2aecb2011-03-28 00:59:16 +020073 if (fchmod(STDIN_FILENO, m) != 0)
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010074 bb_perror_nomsg_and_die();
75 return EXIT_SUCCESS;
Glenn L McGrath061c9002002-09-16 04:21:46 +000076}