blob: 388adf81fb7cc9cb33d9ebdfdcf7514c2ed24e63 [file] [log] [blame]
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * update_passwd
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.
8 *
9 * Moved from loginutils/passwd.c by Alexander Shishkin <virtuoso@slind.org>
10 */
11
12#include "libbb.h"
13
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000014#if ENABLE_SELINUX
15static void check_selinux_update_passwd(const char *username)
16{
17 security_context_t context;
18 char *seuser;
19
20 if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
21 return; /* No need to check */
22
23 if (getprevcon_raw(&context) < 0)
24 bb_perror_msg_and_die("getprevcon failed");
25 seuser = strtok(context, ":");
26 if (!seuser)
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");
31 }
32 if (ENABLE_FEATURE_CLEAN_UP)
33 freecon(context);
34}
35#else
36#define check_selinux_update_passwd(username) ((void)0)
37#endif
38
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000039int update_passwd(const char *filename, const char *username,
40 const char *new_pw)
41{
42 struct stat sb;
43 struct flock lock;
44 FILE *old_fp;
45 FILE *new_fp;
Denis Vlasenko557fb712007-07-21 13:25:28 +000046 char *fnamesfx;
47 char *sfx_char;
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000048 unsigned user_len;
49 int old_fd;
50 int new_fd;
51 int i;
52 int cnt = 0;
Denis Vlasenko557fb712007-07-21 13:25:28 +000053 int ret = -1; /* failure */
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000054
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000055 check_selinux_update_passwd(username);
56
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000057 /* New passwd file, "/etc/passwd+" for now */
Denis Vlasenko557fb712007-07-21 13:25:28 +000058 fnamesfx = xasprintf("%s+", filename);
59 sfx_char = &fnamesfx[strlen(fnamesfx)-1];
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000060 username = xasprintf("%s:", username);
61 user_len = strlen(username);
62
63 old_fp = fopen(filename, "r+");
64 if (!old_fp)
65 goto free_mem;
66 old_fd = fileno(old_fp);
67
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000068 selinux_preserve_fcontext(old_fd);
69
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000070 /* Try to create "/etc/passwd+". Wait if it exists. */
71 i = 30;
72 do {
73 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
Denis Vlasenko557fb712007-07-21 13:25:28 +000074 new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000075 if (new_fd >= 0) goto created;
76 if (errno != EEXIST) break;
77 usleep(100000); /* 0.1 sec */
78 } while (--i);
Denis Vlasenko557fb712007-07-21 13:25:28 +000079 bb_perror_msg("cannot create '%s'", fnamesfx);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000080 goto close_old_fp;
81
82 created:
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);
86 }
87 new_fp = fdopen(new_fd, "w");
88 if (!new_fp) {
89 close(new_fd);
90 goto unlink_new;
91 }
92
93 /* Backup file is "/etc/passwd-" */
Denis Vlasenko557fb712007-07-21 13:25:28 +000094 *sfx_char = '-';
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);
100 *sfx_char = '+';
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000101
102 /* Lock the password file before updating */
103 lock.l_type = F_WRLCK;
104 lock.l_whence = SEEK_SET;
105 lock.l_start = 0;
106 lock.l_len = 0;
107 if (fcntl(old_fd, F_SETLK, &lock) < 0)
108 bb_perror_msg("warning: cannot lock '%s'", filename);
109 lock.l_type = F_UNLCK;
110
Denis Vlasenko557fb712007-07-21 13:25:28 +0000111 /* Read current password file, write updated /etc/passwd+ */
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000112 while (1) {
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: */
Denis Vlasenko557fb712007-07-21 13:25:28 +0000119 cp = strchrnul(cp, ':');
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000120 /* now cp -> ':' after old passwd or -> "" */
121 fprintf(new_fp, "%s%s%s", username, new_pw, cp);
122 cnt++;
123 } else
124 fputs(line, new_fp);
125 free(line);
126 }
127 fcntl(old_fd, F_SETLK, &lock);
128
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))
Denis Vlasenko557fb712007-07-21 13:25:28 +0000131 || rename(fnamesfx, filename)
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000132 ) {
133 /* At least one of those failed */
134 goto unlink_new;
135 }
136 ret = cnt; /* whee, success! */
137
138 unlink_new:
Denis Vlasenko557fb712007-07-21 13:25:28 +0000139 if (ret < 0) unlink(fnamesfx);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000140
141 close_old_fp:
142 fclose(old_fp);
143
144 free_mem:
Denis Vlasenko557fb712007-07-21 13:25:28 +0000145 free(fnamesfx);
146 free((char*)username);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000147 return ret;
148}