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>
15 static void check_selinux_update_passwd(const char *username)
17 security_context_t context;
20 if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
21 return; /* No need to check */
23 if (getprevcon_raw(&context) < 0)
24 bb_perror_msg_and_die("getprevcon failed");
25 seuser = strtok(context, ":");
27 bb_error_msg_and_die("invalid context '%s'", context);
28 if (strcmp(seuser, username) != 0) {
29 if (checkPasswdAccess(PASSWD__PASSWD) != 0)
30 bb_error_msg_and_die("SELinux: access denied");
32 if (ENABLE_FEATURE_CLEAN_UP)
36 #define check_selinux_update_passwd(username) ((void)0)
39 int update_passwd(const char *filename, const char *username,
53 int ret = -1; /* failure */
55 check_selinux_update_passwd(username);
57 /* New passwd file, "/etc/passwd+" for now */
58 fnamesfx = xasprintf("%s+", filename);
59 sfx_char = &fnamesfx[strlen(fnamesfx)-1];
60 username = xasprintf("%s:", username);
61 user_len = strlen(username);
63 old_fp = fopen(filename, "r+");
66 old_fd = fileno(old_fp);
68 selinux_preserve_fcontext(old_fd);
70 /* Try to create "/etc/passwd+". Wait if it exists. */
73 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
74 new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
75 if (new_fd >= 0) goto created;
76 if (errno != EEXIST) break;
77 usleep(100000); /* 0.1 sec */
79 bb_perror_msg("cannot create '%s'", fnamesfx);
83 if (!fstat(old_fd, &sb)) {
84 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
85 fchown(new_fd, sb.st_uid, sb.st_gid);
87 new_fp = fdopen(new_fd, "w");
93 /* Backup file is "/etc/passwd-" */
95 /* Delete old backup */
96 i = (unlink(fnamesfx) && errno != ENOENT);
97 /* Create backup as a hardlink to current */
98 if (i || link(filename, fnamesfx))
99 bb_perror_msg("warning: cannot create backup copy '%s'", fnamesfx);
102 /* Lock the password file before updating */
103 lock.l_type = F_WRLCK;
104 lock.l_whence = SEEK_SET;
107 if (fcntl(old_fd, F_SETLK, &lock) < 0)
108 bb_perror_msg("warning: cannot lock '%s'", filename);
109 lock.l_type = F_UNLCK;
111 /* Read current password file, write updated /etc/passwd+ */
113 char *line = xmalloc_fgets(old_fp);
114 if (!line) break; /* EOF/error */
115 if (strncmp(username, line, user_len) == 0) {
116 /* we have a match with "username:"... */
117 const char *cp = line + user_len;
118 /* now cp -> old passwd, skip it: */
119 cp = strchrnul(cp, ':');
120 /* now cp -> ':' after old passwd or -> "" */
121 fprintf(new_fp, "%s%s%s", username, new_pw, cp);
127 fcntl(old_fd, F_SETLK, &lock);
129 /* We do want all of them to execute, thus | instead of || */
130 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
131 || rename(fnamesfx, filename)
133 /* At least one of those failed */
136 ret = cnt; /* whee, success! */
139 if (ret < 0) unlink(fnamesfx);
146 free((char*)username);