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 | */ |
| 9 | |
Denys Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 10 | //config:config MESG |
| 11 | //config: bool "mesg" |
| 12 | //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 Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 16 | //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 Vlasenko | b1db09b | 2010-10-12 13:47:15 +0200 | [diff] [blame] | 32 | |
| 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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 42 | #if ENABLE_FEATURE_MESG_ENABLE_ONLY_GROUP |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 43 | #define S_IWGRP_OR_S_IWOTH S_IWGRP |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 44 | #else |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 45 | #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH) |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 48 | int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 49 | int mesg_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 50 | { |
| 51 | struct stat sb; |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 52 | mode_t m; |
Eric Andersen | 7850014 | 2004-08-27 19:55:28 +0000 | [diff] [blame] | 53 | char c = 0; |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 54 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 55 | argv++; |
| 56 | |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 57 | if (argv[0] |
| 58 | && (argv[1] || ((c = argv[0][0]) != 'y' && c != 'n')) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 59 | ) { |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 60 | bb_show_usage(); |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 61 | } |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 62 | |
Denys Vlasenko | 4a2aecb | 2011-03-28 00:59:16 +0200 | [diff] [blame] | 63 | if (!isatty(STDIN_FILENO)) |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 64 | bb_error_msg_and_die("not a tty"); |
| 65 | |
Denys Vlasenko | 4a2aecb | 2011-03-28 00:59:16 +0200 | [diff] [blame] | 66 | xfstat(STDIN_FILENO, &sb, "stderr"); |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 67 | 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 Vlasenko | 4a2aecb | 2011-03-28 00:59:16 +0200 | [diff] [blame] | 73 | if (fchmod(STDIN_FILENO, m) != 0) |
Denys Vlasenko | beea5a7 | 2011-03-22 06:54:36 +0100 | [diff] [blame] | 74 | bb_perror_nomsg_and_die(); |
| 75 | return EXIT_SUCCESS; |
Glenn L McGrath | 061c900 | 2002-09-16 04:21:46 +0000 | [diff] [blame] | 76 | } |