aboutsummaryrefslogtreecommitdiff
path: root/loginutils/addgroup.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/addgroup.c
parentf2b39e088d6ccbf4a540c741059c2f661eebc9ac (diff)
downloadbusybox-829bbd3b5701f656c94f1cc672faa39946675d13.tar.gz
*: 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/addgroup.c')
-rw-r--r--loginutils/addgroup.c75
1 files changed, 13 insertions, 62 deletions
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c
index 5032d7b99..5a0cf3fff 100644
--- a/loginutils/addgroup.c
+++ b/loginutils/addgroup.c
@@ -9,7 +9,6 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*
*/
-
#include "libbb.h"
static void xgroup_study(struct group *g)
@@ -45,8 +44,8 @@ static void xgroup_study(struct group *g)
/* append a new user to the passwd file */
static void new_group(char *group, gid_t gid)
{
- FILE *file;
struct group gr;
+ char *p;
/* make sure gid and group haven't already been allocated */
gr.gr_gid = gid;
@@ -54,67 +53,17 @@ static void new_group(char *group, gid_t gid)
xgroup_study(&gr);
/* add entry to group */
- file = xfopen(bb_path_group_file, "a");
- /* group:passwd:gid:userlist */
- fprintf(file, "%s:x:%u:\n", group, (unsigned)gr.gr_gid);
+ p = xasprintf("x:%u:", gr.gr_gid);
+ if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
+ exit(EXIT_FAILURE);
if (ENABLE_FEATURE_CLEAN_UP)
- fclose(file);
+ free(p);
#if ENABLE_FEATURE_SHADOWPASSWDS
- file = fopen_or_warn(bb_path_gshadow_file, "a");
- if (file) {
- fprintf(file, "%s:!::\n", group);
- if (ENABLE_FEATURE_CLEAN_UP)
- fclose(file);
- }
+ /* Ignore errors: if file is missing we suppose admin doesn't want it */
+ update_passwd(bb_path_gshadow_file, group, "!::", NULL);
#endif
}
-#if ENABLE_FEATURE_ADDUSER_TO_GROUP
-static void add_user_to_group(char **args,
- const char *path,
- FILE* FAST_FUNC (*fopen_func)(const char *fileName, const char *mode))
-{
- char *line;
- int len = strlen(args[1]);
- llist_t *plist = NULL;
- FILE *group_file;
-
- group_file = fopen_func(path, "r");
-
- if (!group_file) return;
-
- while ((line = xmalloc_fgetline(group_file)) != NULL) {
- /* Find the group */
- if (!strncmp(line, args[1], len)
- && line[len] == ':'
- ) {
- /* Add the new user */
- line = xasprintf("%s%s%s", line,
- last_char_is(line, ':') ? "" : ",",
- args[0]);
- }
- llist_add_to_end(&plist, line);
- }
-
- if (ENABLE_FEATURE_CLEAN_UP) {
- fclose(group_file);
- group_file = fopen_func(path, "w");
- while ((line = llist_pop(&plist))) {
- if (group_file)
- fprintf(group_file, "%s\n", line);
- free(line);
- }
- if (group_file)
- fclose(group_file);
- } else {
- group_file = fopen_func(path, "w");
- if (group_file)
- while ((line = llist_pop(&plist)))
- fprintf(group_file, "%s\n", line);
- }
-}
-#endif
-
/*
* addgroup will take a login_name as its first parameter.
*
@@ -166,10 +115,12 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv)
return EXIT_SUCCESS;
}
}
- add_user_to_group(argv, bb_path_group_file, xfopen);
-#if ENABLE_FEATURE_SHADOWPASSWDS
- add_user_to_group(argv, bb_path_gshadow_file, fopen_or_warn);
-#endif
+ if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
+ return EXIT_FAILURE;
+ }
+# if ENABLE_FEATURE_SHADOWPASSWDS
+ update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
+# endif
} else
#endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
{