922b3333d43d7a06c841ec6aa00c015727068f22
[oweals/busybox.git] / loginutils / add-remove-shell.c
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 //config:config ADD_SHELL
11 //config:       bool "add-shell"
12 //config:       default y if DESKTOP
13 //config:       help
14 //config:         Add shells to /etc/shells.
15 //config:
16 //config:config REMOVE_SHELL
17 //config:       bool "remove-shell"
18 //config:       default y if DESKTOP
19 //config:       help
20 //config:         Remove shells from /etc/shells.
21
22 //                       APPLET_ODDNAME:name          main              location         suid_type     help
23 //applet:IF_ADD_SHELL(   APPLET_ODDNAME(add-shell   , add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, add_shell   ))
24 //applet:IF_REMOVE_SHELL(APPLET_ODDNAME(remove-shell, add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, remove_shell))
25
26 //kbuild:lib-$(CONFIG_ADD_SHELL)    += add-remove-shell.o
27 //kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o
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 #define dont_add ((char*)(uintptr_t)1)
47
48 int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
50 {
51         FILE *orig_fp;
52         char *orig_fn;
53         char *new_fn;
54         struct stat sb;
55
56         sb.st_mode = 0666;
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         if (orig_fp)
65                 xfstat(fileno(orig_fp), &sb, orig_fn);
66
67         new_fn = xasprintf("%s.tmp", orig_fn);
68         /*
69          * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better,
70          * since it prevents races. But: (1) it requires a retry loop,
71          * (2) if /etc/shells.tmp is *stale*, then retry loop
72          * with O_EXCL will never succeed - it should have a timeout,
73          * after which it should revert to O_TRUNC.
74          * For now, I settle for O_TRUNC instead.
75          */
76         xmove_fd(xopen3(new_fn, O_WRONLY | O_CREAT | O_TRUNC, sb.st_mode), STDOUT_FILENO);
77         /* TODO?
78         xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
79         */
80
81         if (orig_fp) {
82                 /* Copy old file, possibly skipping removed shell names */
83                 char *line;
84                 while ((line = xmalloc_fgetline(orig_fp)) != NULL) {
85                         char **cpp = argv;
86                         while (*cpp) {
87                                 if (strcmp(*cpp, line) == 0) {
88                                         /* Old file has this shell name */
89                                         if (REMOVE_SHELL) {
90                                                 /* we are remove-shell */
91                                                 /* delete this name by not copying it */
92                                                 goto next_line;
93                                         }
94                                         /* we are add-shell */
95                                         /* mark this name as "do not add" */
96                                         *cpp = dont_add;
97                                 }
98                                 cpp++;
99                         }
100                         /* copy shell name from old to new file */
101                         puts(line);
102  next_line:
103                         free(line);
104                 }
105                 if (ENABLE_FEATURE_CLEAN_UP)
106                         fclose(orig_fp);
107         }
108
109         if (ADD_SHELL) {
110                 char **cpp = argv;
111                 while (*cpp) {
112                         if (*cpp != dont_add)
113                                 puts(*cpp);
114                         cpp++;
115                 }
116         }
117
118         /* Ensure we wrote out everything */
119         if (fclose(stdout) != 0) {
120                 xunlink(new_fn);
121                 bb_perror_msg_and_die("%s: write error", new_fn);
122         }
123
124         /* Small hole: if rename fails, /etc/shells.tmp is not removed */
125         xrename(new_fn, orig_fn);
126
127         if (ENABLE_FEATURE_CLEAN_UP) {
128                 free(orig_fn);
129                 free(new_fn);
130         }
131
132         return EXIT_SUCCESS;
133 }