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-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/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 @@
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * 10 *
11 */ 11 */
12
13#include "libbb.h" 12#include "libbb.h"
14 13
15static void xgroup_study(struct group *g) 14static void xgroup_study(struct group *g)
@@ -45,8 +44,8 @@ static void xgroup_study(struct group *g)
45/* append a new user to the passwd file */ 44/* append a new user to the passwd file */
46static void new_group(char *group, gid_t gid) 45static void new_group(char *group, gid_t gid)
47{ 46{
48 FILE *file;
49 struct group gr; 47 struct group gr;
48 char *p;
50 49
51 /* make sure gid and group haven't already been allocated */ 50 /* make sure gid and group haven't already been allocated */
52 gr.gr_gid = gid; 51 gr.gr_gid = gid;
@@ -54,67 +53,17 @@ static void new_group(char *group, gid_t gid)
54 xgroup_study(&gr); 53 xgroup_study(&gr);
55 54
56 /* add entry to group */ 55 /* add entry to group */
57 file = xfopen(bb_path_group_file, "a"); 56 p = xasprintf("x:%u:", gr.gr_gid);
58 /* group:passwd:gid:userlist */ 57 if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
59 fprintf(file, "%s:x:%u:\n", group, (unsigned)gr.gr_gid); 58 exit(EXIT_FAILURE);
60 if (ENABLE_FEATURE_CLEAN_UP) 59 if (ENABLE_FEATURE_CLEAN_UP)
61 fclose(file); 60 free(p);
62#if ENABLE_FEATURE_SHADOWPASSWDS 61#if ENABLE_FEATURE_SHADOWPASSWDS
63 file = fopen_or_warn(bb_path_gshadow_file, "a"); 62 /* Ignore errors: if file is missing we suppose admin doesn't want it */
64 if (file) { 63 update_passwd(bb_path_gshadow_file, group, "!::", NULL);
65 fprintf(file, "%s:!::\n", group);
66 if (ENABLE_FEATURE_CLEAN_UP)
67 fclose(file);
68 }
69#endif 64#endif
70} 65}
71 66
72#if ENABLE_FEATURE_ADDUSER_TO_GROUP
73static void add_user_to_group(char **args,
74 const char *path,
75 FILE* FAST_FUNC (*fopen_func)(const char *fileName, const char *mode))
76{
77 char *line;
78 int len = strlen(args[1]);
79 llist_t *plist = NULL;
80 FILE *group_file;
81
82 group_file = fopen_func(path, "r");
83
84 if (!group_file) return;
85
86 while ((line = xmalloc_fgetline(group_file)) != NULL) {
87 /* Find the group */
88 if (!strncmp(line, args[1], len)
89 && line[len] == ':'
90 ) {
91 /* Add the new user */
92 line = xasprintf("%s%s%s", line,
93 last_char_is(line, ':') ? "" : ",",
94 args[0]);
95 }
96 llist_add_to_end(&plist, line);
97 }
98
99 if (ENABLE_FEATURE_CLEAN_UP) {
100 fclose(group_file);
101 group_file = fopen_func(path, "w");
102 while ((line = llist_pop(&plist))) {
103 if (group_file)
104 fprintf(group_file, "%s\n", line);
105 free(line);
106 }
107 if (group_file)
108 fclose(group_file);
109 } else {
110 group_file = fopen_func(path, "w");
111 if (group_file)
112 while ((line = llist_pop(&plist)))
113 fprintf(group_file, "%s\n", line);
114 }
115}
116#endif
117
118/* 67/*
119 * addgroup will take a login_name as its first parameter. 68 * addgroup will take a login_name as its first parameter.
120 * 69 *
@@ -166,10 +115,12 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv)
166 return EXIT_SUCCESS; 115 return EXIT_SUCCESS;
167 } 116 }
168 } 117 }
169 add_user_to_group(argv, bb_path_group_file, xfopen); 118 if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
170#if ENABLE_FEATURE_SHADOWPASSWDS 119 return EXIT_FAILURE;
171 add_user_to_group(argv, bb_path_gshadow_file, fopen_or_warn); 120 }
172#endif 121# if ENABLE_FEATURE_SHADOWPASSWDS
122 update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
123# endif
173 } else 124 } else
174#endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */ 125#endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
175 { 126 {