1 /* vi: set sw=4 ts=4: */
5 * update_passwd is a common function for passwd and chpasswd applets;
6 * it is responsible for updating password file (i.e. /etc/passwd or
7 * /etc/shadow) for a given user and password.
9 * Moved from loginutils/passwd.c by Alexander Shishkin <virtuoso@slind.org>
11 * Modified to be able to add or delete users, groups and users to/from groups
12 * by Tito Ragusa <farmatito@tiscali.it>
14 * Licensed under GPLv2, see file LICENSE in this source tree.
19 static void check_selinux_update_passwd(const char *username)
21 security_context_t context;
24 if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
25 return; /* No need to check */
27 if (getprevcon_raw(&context) < 0)
28 bb_perror_msg_and_die("getprevcon failed");
29 seuser = strtok(context, ":");
31 bb_error_msg_and_die("invalid context '%s'", context);
32 if (strcmp(seuser, username) != 0) {
33 security_class_t tclass;
36 tclass = string_to_security_class("passwd");
39 av = string_to_av_perm(tclass, "passwd");
43 if (selinux_check_passwd_access(av) != 0)
45 bb_error_msg_and_die("SELinux: access denied");
47 if (ENABLE_FEATURE_CLEAN_UP)
51 # define check_selinux_update_passwd(username) ((void)0)
55 1) add a user: update_passwd(FILE, USER, REMAINING_PWLINE, NULL)
56 only if CONFIG_ADDUSER=y and applet_name[0] == 'a' like in adduser
58 2) add a group: update_passwd(FILE, GROUP, REMAINING_GRLINE, NULL)
59 only if CONFIG_ADDGROUP=y and applet_name[0] == 'a' like in addgroup
61 3) add a user to a group: update_passwd(FILE, GROUP, NULL, MEMBER)
62 only if CONFIG_FEATURE_ADDUSER_TO_GROUP=y, applet_name[0] == 'a'
63 like in addgroup and member != NULL
65 4) delete a user: update_passwd(FILE, USER, NULL, NULL)
67 5) delete a group: update_passwd(FILE, GROUP, NULL, NULL)
69 6) delete a user from a group: update_passwd(FILE, GROUP, NULL, MEMBER)
70 only if CONFIG_FEATURE_DEL_USER_FROM_GROUP=y and member != NULL
72 7) change user's password: update_passwd(FILE, USER, NEW_PASSWD, NULL)
73 only if CONFIG_PASSWD=y and applet_name[0] == 'p' like in passwd
74 or if CONFIG_CHPASSWD=y and applet_name[0] == 'c' like in chpasswd
76 8) delete a user from all groups: update_passwd(FILE, NULL, NULL, MEMBER)
78 This function does not validate the arguments fed to it
79 so the calling program should take care of that.
81 Returns number of lines changed, or -1 on error.
83 int FAST_FUNC update_passwd(const char *filename,
85 const char *new_passwd,
88 #if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
102 int ret = -1; /* failure */
103 /* used as a bool: "are we modifying /etc/shadow?" */
104 #if ENABLE_FEATURE_SHADOWPASSWDS
105 const char *shadow = strstr(filename, "shadow");
110 filename = xmalloc_follow_symlinks(filename);
111 if (filename == NULL)
115 check_selinux_update_passwd(name);
117 /* New passwd file, "/etc/passwd+" for now */
118 fnamesfx = xasprintf("%s+", filename);
119 sfx_char = &fnamesfx[strlen(fnamesfx)-1];
120 name_colon = xasprintf("%s:", name ? name : "");
123 old_fp = fopen(filename, "r+");
125 old_fp = fopen_or_warn(filename, "r+");
128 ret = 0; /* missing shadow is not an error */
131 old_fd = fileno(old_fp);
133 selinux_preserve_fcontext(old_fd);
135 /* Try to create "/etc/passwd+". Wait if it exists. */
138 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
139 new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
140 if (new_fd >= 0) goto created;
141 if (errno != EEXIST) break;
142 usleep(100000); /* 0.1 sec */
144 bb_perror_msg("can't create '%s'", fnamesfx);
148 if (fstat(old_fd, &sb) == 0) {
149 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
150 fchown(new_fd, sb.st_uid, sb.st_gid);
153 new_fp = xfdopen_for_write(new_fd);
155 /* Backup file is "/etc/passwd-" */
157 /* Delete old backup */
158 i = (unlink(fnamesfx) && errno != ENOENT);
159 /* Create backup as a hardlink to current */
160 if (i || link(filename, fnamesfx))
161 bb_perror_msg("warning: can't create backup copy '%s'",
165 /* Lock the password file before updating */
166 lock.l_type = F_WRLCK;
167 lock.l_whence = SEEK_SET;
170 if (fcntl(old_fd, F_SETLK, &lock) < 0)
171 bb_perror_msg("warning: can't lock '%s'", filename);
172 lock.l_type = F_UNLCK;
174 /* Read current password file, write updated /etc/passwd+ */
179 line = xmalloc_fgetline(old_fp);
180 if (!line) /* EOF/error */
183 #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
184 if (!name && member) {
185 /* Delete member from all groups */
186 /* line is "GROUP:PASSWD:[member1[,member2]...]" */
187 unsigned member_len = strlen(member);
188 char *list = strrchr(line, ':');
192 if (is_prefixed_with(list, member)) {
195 c = list[member_len];
203 overlapping_strcpy(list, list + member_len + 1);
204 goto next_list_element;
208 list = strchr(list, ',');
210 fprintf(new_fp, "%s\n", line);
215 cp = is_prefixed_with(line, name_colon);
217 fprintf(new_fp, "%s\n", line);
221 /* We have a match with "name:"... */
222 /* cp points past "name:" */
224 #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
226 /* It's actually /etc/group+, not /etc/passwd+ */
227 if (ENABLE_FEATURE_ADDUSER_TO_GROUP
228 && applet_name[0] == 'a'
230 /* Add user to group */
231 fprintf(new_fp, "%s%s%s\n", line,
232 last_char_is(line, ':') ? "" : ",",
235 } else if (ENABLE_FEATURE_DEL_USER_FROM_GROUP
236 /* && applet_name[0] == 'd' */
238 /* Delete user from group */
240 const char *fmt = "%s";
242 /* find the start of the member list: last ':' */
243 cp = strrchr(line, ':');
246 /* write the cut line name:passwd:gid:
248 fprintf(new_fp, "%s:", line);
249 /* parse the tokens of the member list */
251 while ((cp = strsep(&tmp, ",")) != NULL) {
252 if (strcmp(member, cp) != 0) {
253 fprintf(new_fp, fmt, cp);
256 /* found member, skip it */
260 fprintf(new_fp, "\n");
264 if ((ENABLE_PASSWD && applet_name[0] == 'p')
265 || (ENABLE_CHPASSWD && applet_name[0] == 'c')
268 cp = strchrnul(cp, ':'); /* move past old passwd */
270 if (shadow && *cp == ':') {
271 /* /etc/shadow's field 3 (passwd change date) needs updating */
272 /* move past old change date */
273 cp = strchrnul(cp + 1, ':');
274 /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
275 fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd,
276 (unsigned)(time(NULL)) / (24*60*60), cp);
278 /* "name:" + "new_passwd" + ":rest of line" */
279 fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp);
282 } /* else delete user or group: skip the line */
287 if (changed_lines == 0) {
288 #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
290 if (ENABLE_ADDGROUP && applet_name[0] == 'a')
291 bb_error_msg("can't find %s in %s", name, filename);
292 if (ENABLE_DELGROUP && applet_name[0] == 'd')
293 bb_error_msg("can't find %s in %s", member, filename);
296 if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
297 && applet_name[0] == 'a' && !member
299 /* add user or group */
300 fprintf(new_fp, "%s%s\n", name_colon, new_passwd);
305 fcntl(old_fd, F_SETLK, &lock);
307 /* We do want all of them to execute, thus | instead of || */
309 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
310 || rename(fnamesfx, filename)
312 /* At least one of those failed */
316 /* Success: ret >= 0 */
328 free((char *)filename);