blob: e492b6e5a86ee8875394924ffbcf2e98b2124603
1 | /* |
2 | * add-shell and remove-shell implementation for busybox |
3 | * |
4 | * Copyright (C) 2010 Nokia Corporation. All rights reserved. |
5 | * Written by Alexander Shishkin <virtuoso@slind.org> |
6 | * |
7 | * Licensed under GPLv2 or later, see the LICENSE file in this source tree |
8 | * for details. |
9 | */ |
10 | |
11 | //applet:IF_ADD_SHELL( APPLET_ODDNAME(add-shell , add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, add_shell )) |
12 | //applet:IF_REMOVE_SHELL(APPLET_ODDNAME(remove-shell, add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, remove_shell)) |
13 | |
14 | //kbuild:lib-$(CONFIG_ADD_SHELL) += add-remove-shell.o |
15 | //kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o |
16 | |
17 | //config:config ADD_SHELL |
18 | //config: bool "add-shell" |
19 | //config: default y if DESKTOP |
20 | //config: help |
21 | //config: Add shells to /etc/shells. |
22 | //config: |
23 | //config:config REMOVE_SHELL |
24 | //config: bool "remove-shell" |
25 | //config: default y if DESKTOP |
26 | //config: help |
27 | //config: Remove shells from /etc/shells. |
28 | |
29 | //usage:#define add_shell_trivial_usage |
30 | //usage: "SHELL..." |
31 | //usage:#define add_shell_full_usage "\n\n" |
32 | //usage: "Add SHELLs to /etc/shells" |
33 | |
34 | //usage:#define remove_shell_trivial_usage |
35 | //usage: "SHELL..." |
36 | //usage:#define remove_shell_full_usage "\n\n" |
37 | //usage: "Remove SHELLs from /etc/shells" |
38 | |
39 | #include "libbb.h" |
40 | |
41 | #define SHELLS_FILE "/etc/shells" |
42 | |
43 | #define REMOVE_SHELL (ENABLE_REMOVE_SHELL && (!ENABLE_ADD_SHELL || applet_name[0] == 'r')) |
44 | #define ADD_SHELL (ENABLE_ADD_SHELL && (!ENABLE_REMOVE_SHELL || applet_name[0] == 'a')) |
45 | |
46 | /* NB: we use the _address_, not the value, of this string |
47 | * as a "special value of pointer" in the code. |
48 | */ |
49 | static const char dont_add[] ALIGN1 = "\n"; |
50 | |
51 | int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
52 | int add_remove_shell_main(int argc UNUSED_PARAM, char **argv) |
53 | { |
54 | FILE *orig_fp; |
55 | char *orig_fn; |
56 | char *new_fn; |
57 | |
58 | argv++; |
59 | |
60 | orig_fn = xmalloc_follow_symlinks(SHELLS_FILE); |
61 | if (!orig_fn) |
62 | return EXIT_FAILURE; |
63 | orig_fp = fopen_for_read(orig_fn); |
64 | |
65 | new_fn = xasprintf("%s.tmp", orig_fn); |
66 | /* |
67 | * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better, |
68 | * since it prevents races. But: (1) it requires a retry loop, |
69 | * (2) if /etc/shells.tmp is *stale*, then retry loop |
70 | * with O_EXCL will never succeed - it should have a timeout, |
71 | * after which it should revert to O_TRUNC. |
72 | * For now, I settle for O_TRUNC instead. |
73 | */ |
74 | xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO); |
75 | |
76 | /* TODO: |
77 | struct stat sb; |
78 | xfstat(fileno(orig_fp), &sb); |
79 | xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid); |
80 | xfchmod(STDOUT_FILENO, sb.st_mode); |
81 | */ |
82 | |
83 | if (orig_fp) { |
84 | /* Copy old file, possibly skipping removed shell names */ |
85 | char *line; |
86 | while ((line = xmalloc_fgetline(orig_fp)) != NULL) { |
87 | char **cpp = argv; |
88 | while (*cpp) { |
89 | if (strcmp(*cpp, line) == 0) { |
90 | /* Old file has this shell name */ |
91 | if (REMOVE_SHELL) { |
92 | /* we are remove-shell */ |
93 | /* delete this name by not copying it */ |
94 | goto next_line; |
95 | } |
96 | /* we are add-shell */ |
97 | /* mark this name as "do not add" */ |
98 | *cpp = (char*)dont_add; |
99 | } |
100 | cpp++; |
101 | } |
102 | /* copy shell name from old to new file */ |
103 | printf("%s\n", line); |
104 | next_line: |
105 | free(line); |
106 | } |
107 | if (ENABLE_FEATURE_CLEAN_UP) |
108 | fclose(orig_fp); |
109 | } |
110 | |
111 | if (ADD_SHELL) { |
112 | char **cpp = argv; |
113 | while (*cpp) { |
114 | if (*cpp != dont_add) |
115 | printf("%s\n", *cpp); |
116 | cpp++; |
117 | } |
118 | } |
119 | |
120 | /* Ensure we wrote out everything */ |
121 | if (fclose(stdout) != 0) { |
122 | xunlink(new_fn); |
123 | bb_perror_msg_and_die("%s: write error", new_fn); |
124 | } |
125 | |
126 | /* Small hole: if rename fails, /etc/shells.tmp is not removed */ |
127 | xrename(new_fn, orig_fn); |
128 | |
129 | if (ENABLE_FEATURE_CLEAN_UP) { |
130 | free(orig_fn); |
131 | free(new_fn); |
132 | } |
133 | |
134 | return EXIT_SUCCESS; |
135 | } |
136 |