adduser: more fixes to "add user to specified group"
function old new delta update_passwd 1246 1295 +49 adduser_main 727 725 -2 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
02dd96f688
commit
4653e442ba
@ -81,6 +81,7 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
FILE *new_fp;
|
FILE *new_fp;
|
||||||
char *fnamesfx;
|
char *fnamesfx;
|
||||||
char *sfx_char;
|
char *sfx_char;
|
||||||
|
char *name_colon;
|
||||||
unsigned user_len;
|
unsigned user_len;
|
||||||
int old_fd;
|
int old_fd;
|
||||||
int new_fd;
|
int new_fd;
|
||||||
@ -103,8 +104,8 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
/* New passwd file, "/etc/passwd+" for now */
|
/* New passwd file, "/etc/passwd+" for now */
|
||||||
fnamesfx = xasprintf("%s+", filename);
|
fnamesfx = xasprintf("%s+", filename);
|
||||||
sfx_char = &fnamesfx[strlen(fnamesfx)-1];
|
sfx_char = &fnamesfx[strlen(fnamesfx)-1];
|
||||||
name = xasprintf("%s:", name);
|
name_colon = xasprintf("%s:", name);
|
||||||
user_len = strlen(name);
|
user_len = strlen(name_colon);
|
||||||
|
|
||||||
if (shadow)
|
if (shadow)
|
||||||
old_fp = fopen(filename, "r+");
|
old_fp = fopen(filename, "r+");
|
||||||
@ -166,7 +167,7 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
line = xmalloc_fgetline(old_fp);
|
line = xmalloc_fgetline(old_fp);
|
||||||
if (!line) /* EOF/error */
|
if (!line) /* EOF/error */
|
||||||
break;
|
break;
|
||||||
if (strncmp(name, line, user_len) != 0) {
|
if (strncmp(name_colon, line, user_len) != 0) {
|
||||||
fprintf(new_fp, "%s\n", line);
|
fprintf(new_fp, "%s\n", line);
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
@ -225,11 +226,11 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
/* move past old change date */
|
/* move past old change date */
|
||||||
cp = strchrnul(cp + 1, ':');
|
cp = strchrnul(cp + 1, ':');
|
||||||
/* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
|
/* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
|
||||||
fprintf(new_fp, "%s%s:%u%s\n", name, new_passwd,
|
fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd,
|
||||||
(unsigned)(time(NULL)) / (24*60*60), cp);
|
(unsigned)(time(NULL)) / (24*60*60), cp);
|
||||||
} else {
|
} else {
|
||||||
/* "name:" + "new_passwd" + ":rest of line" */
|
/* "name:" + "new_passwd" + ":rest of line" */
|
||||||
fprintf(new_fp, "%s%s%s\n", name, new_passwd, cp);
|
fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp);
|
||||||
}
|
}
|
||||||
changed_lines++;
|
changed_lines++;
|
||||||
} /* else delete user or group: skip the line */
|
} /* else delete user or group: skip the line */
|
||||||
@ -238,15 +239,19 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (changed_lines == 0) {
|
if (changed_lines == 0) {
|
||||||
#if ENABLE_FEATURE_DEL_USER_FROM_GROUP
|
#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
|
||||||
if (member)
|
if (member) {
|
||||||
|
if (ENABLE_ADDGROUP && applet_name[0] == 'a')
|
||||||
|
bb_error_msg("can't find %s in %s", name, filename);
|
||||||
|
if (ENABLE_DELGROUP && applet_name[0] == 'd')
|
||||||
bb_error_msg("can't find %s in %s", member, filename);
|
bb_error_msg("can't find %s in %s", member, filename);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
|
if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
|
||||||
&& applet_name[0] == 'a' && !member
|
&& applet_name[0] == 'a' && !member
|
||||||
) {
|
) {
|
||||||
/* add user or group */
|
/* add user or group */
|
||||||
fprintf(new_fp, "%s%s\n", name, new_passwd);
|
fprintf(new_fp, "%s%s\n", name_colon, new_passwd);
|
||||||
changed_lines++;
|
changed_lines++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -275,6 +280,6 @@ int FAST_FUNC update_passwd(const char *filename,
|
|||||||
free_mem:
|
free_mem:
|
||||||
free(fnamesfx);
|
free(fnamesfx);
|
||||||
free((char *)filename);
|
free((char *)filename);
|
||||||
free((char *)name);
|
free(name_colon);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,12 @@ static void passwd_study(struct passwd *p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void addgroup_wrapper(struct passwd *p)
|
static void addgroup_wrapper(struct passwd *p, const char *group_name)
|
||||||
{
|
{
|
||||||
char *cmd;
|
char *cmd;
|
||||||
|
|
||||||
cmd = xasprintf("addgroup -g %u '%s'", (unsigned)p->pw_gid, p->pw_name);
|
cmd = xasprintf("addgroup '%s' '%s'",
|
||||||
|
p->pw_name, group_name);
|
||||||
system(cmd);
|
system(cmd);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
}
|
}
|
||||||
@ -191,7 +192,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
/* addgroup should be responsible for dealing w/ gshadow */
|
/* addgroup should be responsible for dealing w/ gshadow */
|
||||||
/* if using a pre-existing group, don't create one */
|
/* if using a pre-existing group, don't create one */
|
||||||
if (usegroup)
|
if (usegroup)
|
||||||
addgroup_wrapper(&pw);
|
addgroup_wrapper(&pw, usegroup);
|
||||||
|
|
||||||
/* clear the umask for this process so it doesn't
|
/* clear the umask for this process so it doesn't
|
||||||
* screw up the permissions on the mkdir and chown. */
|
* screw up the permissions on the mkdir and chown. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user