From 829bbd3b5701f656c94f1cc672faa39946675d13 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Tue, 14 Apr 2009 00:51:05 +0000 Subject: *: 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 --- loginutils/adduser.c | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) (limited to 'loginutils/adduser.c') 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 @@ * * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ - #include "libbb.h" #define OPT_DONT_SET_PASS (1 << 4) #define OPT_SYSTEM_ACCOUNT (1 << 5) #define OPT_DONT_MAKE_HOME (1 << 6) - /* remix */ /* recoded such that the uid may be passed in *p */ static void passwd_study(struct passwd *p) @@ -88,10 +86,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) { struct passwd pw; const char *usegroup = NULL; - FILE *file; -#if ENABLE_FEATURE_SHADOWPASSWDS - int fd; -#endif + char *p; #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS applet_long_options = adduser_longopts; @@ -124,32 +119,19 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) /* make sure everything is kosher and setup uid && maybe gid */ passwd_study(&pw); - /* add to passwd */ - file = xfopen(bb_path_passwd_file, "a"); - //fseek(file, 0, SEEK_END); /* paranoia, "a" should ensure that anyway */ - if (putpwent(&pw, file) != 0) { - bb_perror_nomsg_and_die(); + p = xasprintf("x:%u:%u:%s:%s:%s", pw.pw_uid, pw.pw_gid, pw.pw_gecos, pw.pw_dir, pw.pw_shell); + if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) { + return EXIT_FAILURE; } - /* do fclose even if !ENABLE_FEATURE_CLEAN_UP. - * We will exec passwd, files must be flushed & closed before that! */ - fclose(file); + if (ENABLE_FEATURE_CLEAN_UP) + free(p); #if ENABLE_FEATURE_SHADOWPASSWDS - /* add to shadow if necessary */ - /* fopen(..., "a"); would create shadow file, which is wrong. - * If shadow file doesn't exist, admin probably does not want it */ - fd = open_or_warn(bb_path_shadow_file, O_WRONLY | O_APPEND); - if (fd >= 0) { - char *s = xasprintf("%s:!:%u:0:99999:7:::\n", - pw.pw_name, /* username */ - (unsigned)(time(NULL) / 86400) /* sp->sp_lstchg */ - /*0,*/ /* sp->sp_min */ - /*99999,*/ /* sp->sp_max */ - /*7*/ /* sp->sp_warn */ - ); - xwrite_str(fd, s); - close(fd); - } + p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL) / 86400)); /* sp->sp_lstchg */ + /* Ignore errors: if file is missing we suppose admin doesn't want it */ + update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL); + if (ENABLE_FEATURE_CLEAN_UP) + free(p); #endif /* add to group */ -- cgit v1.2.3