aboutsummaryrefslogtreecommitdiff
path: root/loginutils/adduser.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-04-14 00:51:05 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-04-14 00:51:05 +0000
commit829bbd3b5701f656c94f1cc672faa39946675d13 (patch)
treedbe6672228a3cb51eb8031ba714bb4abb96decf4 /loginutils/adduser.c
parentf2b39e088d6ccbf4a540c741059c2f661eebc9ac (diff)
downloadbusybox-w32-829bbd3b5701f656c94f1cc672faa39946675d13.tar.gz
busybox-w32-829bbd3b5701f656c94f1cc672faa39946675d13.tar.bz2
busybox-w32-829bbd3b5701f656c94f1cc672faa39946675d13.zip
*: unify concurrent-safe update of /etc/{passwd,group,[g]shadow}
by Tito (farmatito AT tiscali.it) function old new delta update_passwd 743 1171 +428 bb_perror_nomsg - 9 +9 find_main 436 444 +8 passwd_main 1023 1027 +4 nameval 202 206 +4 chpasswd_main 315 319 +4 bb__parsespent 119 117 -2 adduser_main 654 650 -4 addgroup_main 345 341 -4 sv_main 1228 1222 -6 deluser_main 173 160 -13 bb_internal_putpwent 69 - -69 add_user_to_group 231 - -231 del_line_matching 460 31 -429 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 5/6 up/down: 457/-758) Total: -301 bytes
Diffstat (limited to 'loginutils/adduser.c')
-rw-r--r--loginutils/adduser.c40
1 files changed, 11 insertions, 29 deletions
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index b94bb3aea..d0a870c54 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -7,14 +7,12 @@
7 * 7 *
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9 */ 9 */
10
11#include "libbb.h" 10#include "libbb.h"
12 11
13#define OPT_DONT_SET_PASS (1 << 4) 12#define OPT_DONT_SET_PASS (1 << 4)
14#define OPT_SYSTEM_ACCOUNT (1 << 5) 13#define OPT_SYSTEM_ACCOUNT (1 << 5)
15#define OPT_DONT_MAKE_HOME (1 << 6) 14#define OPT_DONT_MAKE_HOME (1 << 6)
16 15
17
18/* remix */ 16/* remix */
19/* recoded such that the uid may be passed in *p */ 17/* recoded such that the uid may be passed in *p */
20static void passwd_study(struct passwd *p) 18static void passwd_study(struct passwd *p)
@@ -88,10 +86,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
88{ 86{
89 struct passwd pw; 87 struct passwd pw;
90 const char *usegroup = NULL; 88 const char *usegroup = NULL;
91 FILE *file; 89 char *p;
92#if ENABLE_FEATURE_SHADOWPASSWDS
93 int fd;
94#endif
95 90
96#if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS 91#if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
97 applet_long_options = adduser_longopts; 92 applet_long_options = adduser_longopts;
@@ -124,32 +119,19 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
124 /* make sure everything is kosher and setup uid && maybe gid */ 119 /* make sure everything is kosher and setup uid && maybe gid */
125 passwd_study(&pw); 120 passwd_study(&pw);
126 121
127 /* add to passwd */ 122 p = xasprintf("x:%u:%u:%s:%s:%s", pw.pw_uid, pw.pw_gid, pw.pw_gecos, pw.pw_dir, pw.pw_shell);
128 file = xfopen(bb_path_passwd_file, "a"); 123 if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
129 //fseek(file, 0, SEEK_END); /* paranoia, "a" should ensure that anyway */ 124 return EXIT_FAILURE;
130 if (putpwent(&pw, file) != 0) {
131 bb_perror_nomsg_and_die();
132 } 125 }
133 /* do fclose even if !ENABLE_FEATURE_CLEAN_UP. 126 if (ENABLE_FEATURE_CLEAN_UP)
134 * We will exec passwd, files must be flushed & closed before that! */ 127 free(p);
135 fclose(file);
136 128
137#if ENABLE_FEATURE_SHADOWPASSWDS 129#if ENABLE_FEATURE_SHADOWPASSWDS
138 /* add to shadow if necessary */ 130 p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL) / 86400)); /* sp->sp_lstchg */
139 /* fopen(..., "a"); would create shadow file, which is wrong. 131 /* Ignore errors: if file is missing we suppose admin doesn't want it */
140 * If shadow file doesn't exist, admin probably does not want it */ 132 update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
141 fd = open_or_warn(bb_path_shadow_file, O_WRONLY | O_APPEND); 133 if (ENABLE_FEATURE_CLEAN_UP)
142 if (fd >= 0) { 134 free(p);
143 char *s = xasprintf("%s:!:%u:0:99999:7:::\n",
144 pw.pw_name, /* username */
145 (unsigned)(time(NULL) / 86400) /* sp->sp_lstchg */
146 /*0,*/ /* sp->sp_min */
147 /*99999,*/ /* sp->sp_max */
148 /*7*/ /* sp->sp_warn */
149 );
150 xwrite_str(fd, s);
151 close(fd);
152 }
153#endif 135#endif
154 136
155 /* add to group */ 137 /* add to group */