blob: 35c4ec13d64fde88f22e2c3eb9bb9fda1b0594a7 [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 */
Denys Vlasenkob1db09b2010-10-12 13:47:15 +02009//config:config MESG
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "mesg (1.2 kb)"
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Mesg controls access to your terminal by others. It is typically
14//config: used to allow or disallow other users to write to your terminal
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010015//config:
16//config:config FEATURE_MESG_ENABLE_ONLY_GROUP
17//config: bool "Enable writing to tty only by group, not by everybody"
18//config: default y
19//config: depends on MESG
20//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: Usually, ttys are owned by group "tty", and "write" tool is
22//config: setgid to this group. This way, "mesg y" only needs to enable
23//config: "write by owning group" bit in tty mode.
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010024//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: If you set this option to N, "mesg y" will enable writing
26//config: by anybody at all. This is not recommended.
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010027
Denys Vlasenko65147852017-08-04 19:16:01 +020028//applet:IF_MESG(APPLET_NOFORK(mesg, mesg, BB_DIR_USR_BIN, BB_SUID_DROP, mesg))
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010029
30//kbuild:lib-$(CONFIG_MESG) += mesg.o
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020031
32//usage:#define mesg_trivial_usage
33//usage: "[y|n]"
34//usage:#define mesg_full_usage "\n\n"
35//usage: "Control write access to your terminal\n"
36//usage: " y Allow write access to your terminal\n"
37//usage: " n Disallow write access to your terminal"
38
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000039#include "libbb.h"
Glenn L McGrath061c9002002-09-16 04:21:46 +000040
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010041#if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020042#define S_IWGRP_OR_S_IWOTH S_IWGRP
Glenn L McGrath061c9002002-09-16 04:21:46 +000043#else
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020044#define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
Glenn L McGrath061c9002002-09-16 04:21:46 +000045#endif
46
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010048int mesg_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath061c9002002-09-16 04:21:46 +000049{
50 struct stat sb;
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010051 mode_t m;
Eric Andersen78500142004-08-27 19:55:28 +000052 char c = 0;
Glenn L McGrath061c9002002-09-16 04:21:46 +000053
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010054 argv++;
55
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010056 if (argv[0]
57 && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n'))
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000058 ) {
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010059 bb_show_usage();
Glenn L McGrath061c9002002-09-16 04:21:46 +000060 }
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010061
Denys Vlasenko65147852017-08-04 19:16:01 +020062 /* We are a NOFORK applet.
63 * (Not that it's very useful, but code is trivially NOFORK-safe).
64 * Play nice. Do not leak anything.
65 */
66
Denys Vlasenko4a2aecb2011-03-28 00:59:16 +020067 if (!isatty(STDIN_FILENO))
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010068 bb_error_msg_and_die("not a tty");
69
Denys Vlasenko65147852017-08-04 19:16:01 +020070 xfstat(STDIN_FILENO, &sb, "stdin");
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010071 if (c == 0) {
72 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
73 return EXIT_SUCCESS;
74 }
75 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
76 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
Denys Vlasenko4a2aecb2011-03-28 00:59:16 +020077 if (fchmod(STDIN_FILENO, m) != 0)
Denys Vlasenkobeea5a72011-03-22 06:54:36 +010078 bb_perror_nomsg_and_die();
79 return EXIT_SUCCESS;
Glenn L McGrath061c9002002-09-16 04:21:46 +000080}