Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 2 | /* |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 3 | * mesg implementation for busybox |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 4 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org> |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 9 | //config:config MESG |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "mesg (1.2 kb)" |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //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 Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 15 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 21 | //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 Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 24 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 25 | //config: If you set this option to N, "mesg y" will enable writing |
| 26 | //config: by anybody at all. This is not recommended. |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 27 | |
Denys Vlasenko | 6514785 | 2017-08-04 19:16:01 +0200 | [diff] [blame] | 28 | //applet:IF_MESG(APPLET_NOFORK(mesg, mesg, BB_DIR_USR_BIN, BB_SUID_DROP, mesg)) |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 29 | |
| 30 | //kbuild:lib-$(CONFIG_MESG) += mesg.o |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 31 | |
| 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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 39 | #include "libbb.h" |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 40 | |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 41 | #if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 42 | #define S_IWGRP_OR_S_IWOTH S_IWGRP |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 43 | #else |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 44 | #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH) |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 47 | int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 48 | int mesg_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 49 | { |
| 50 | struct stat sb; |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 51 | mode_t m; |
Eric Andersen | 7850014 | 2004-08-27 19:55:28 +0000 | [diff] [blame] | 52 | char c = 0; |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 53 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 54 | argv++; |
| 55 | |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 56 | if (argv[0] |
| 57 | && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n')) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 58 | ) { |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 59 | bb_show_usage(); |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 60 | } |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 61 | |
Denys Vlasenko | 6514785 | 2017-08-04 19:16:01 +0200 | [diff] [blame] | 62 | /* 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 Vlasenko | 4a2aecb | 2011-03-28 00:59:16 +0200 | [diff] [blame] | 67 | if (!isatty(STDIN_FILENO)) |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 68 | bb_error_msg_and_die("not a tty"); |
| 69 | |
Denys Vlasenko | 6514785 | 2017-08-04 19:16:01 +0200 | [diff] [blame] | 70 | xfstat(STDIN_FILENO, &sb, "stdin"); |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 71 | 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 Vlasenko | 4a2aecb | 2011-03-28 00:59:16 +0200 | [diff] [blame] | 77 | if (fchmod(STDIN_FILENO, m) != 0) |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 78 | bb_perror_nomsg_and_die(); |
| 79 | return EXIT_SUCCESS; |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 80 | } |