blob: 7b67f30cd938acbe10da3fe355a240bd938c04d3 [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>
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000010 *
Denis Vlasenko829bbd32009-04-14 00:51:05 +000011 * Modified to be able to add or delete users, groups and users to/from groups
12 * by Tito Ragusa <farmatito@tiscali.it>
13 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000015 */
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000016#include "libbb.h"
17
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000018#if ENABLE_SELINUX
19static void check_selinux_update_passwd(const char *username)
20{
Denys Vlasenko24966162020-10-06 02:36:47 +020021 security_context_t seuser;
22 char *p;
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000023
24 if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020025 return; /* No need to check */
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000026
Denys Vlasenko24966162020-10-06 02:36:47 +020027 if (getprevcon_raw(&seuser) < 0)
James Byrne69374872019-07-02 11:35:03 +020028 bb_simple_perror_msg_and_die("getprevcon failed");
Denys Vlasenko24966162020-10-06 02:36:47 +020029
30 p = strchr(seuser, ':');
31 if (!p)
32 bb_error_msg_and_die("invalid context '%s'", seuser);
33 *p = '\0';
34
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000035 if (strcmp(seuser, username) != 0) {
Mike Frysingerc6f35242016-12-09 18:30:30 -050036 security_class_t tclass;
37 access_vector_t av;
38
39 tclass = string_to_security_class("passwd");
40 if (tclass == 0)
41 goto die;
42 av = string_to_av_perm(tclass, "passwd");
43 if (av == 0)
44 goto die;
45
46 if (selinux_check_passwd_access(av) != 0)
47 die:
James Byrne69374872019-07-02 11:35:03 +020048 bb_simple_error_msg_and_die("SELinux: access denied");
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000049 }
50 if (ENABLE_FEATURE_CLEAN_UP)
51 freecon(context);
52}
53#else
Denis Vlasenko829bbd32009-04-14 00:51:05 +000054# define check_selinux_update_passwd(username) ((void)0)
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +000055#endif
56
Denis Vlasenko829bbd32009-04-14 00:51:05 +000057/*
58 1) add a user: update_passwd(FILE, USER, REMAINING_PWLINE, NULL)
59 only if CONFIG_ADDUSER=y and applet_name[0] == 'a' like in adduser
60
61 2) add a group: update_passwd(FILE, GROUP, REMAINING_GRLINE, NULL)
62 only if CONFIG_ADDGROUP=y and applet_name[0] == 'a' like in addgroup
63
64 3) add a user to a group: update_passwd(FILE, GROUP, NULL, MEMBER)
65 only if CONFIG_FEATURE_ADDUSER_TO_GROUP=y, applet_name[0] == 'a'
66 like in addgroup and member != NULL
67
68 4) delete a user: update_passwd(FILE, USER, NULL, NULL)
69
70 5) delete a group: update_passwd(FILE, GROUP, NULL, NULL)
71
72 6) delete a user from a group: update_passwd(FILE, GROUP, NULL, MEMBER)
73 only if CONFIG_FEATURE_DEL_USER_FROM_GROUP=y and member != NULL
74
Denys Vlasenko854738d2010-11-03 02:48:43 +010075 7) change user's password: update_passwd(FILE, USER, NEW_PASSWD, NULL)
Denis Vlasenko829bbd32009-04-14 00:51:05 +000076 only if CONFIG_PASSWD=y and applet_name[0] == 'p' like in passwd
77 or if CONFIG_CHPASSWD=y and applet_name[0] == 'c' like in chpasswd
78
Denys Vlasenko936c8802015-03-12 15:30:46 +010079 8) delete a user from all groups: update_passwd(FILE, NULL, NULL, MEMBER)
80
Denis Vlasenko829bbd32009-04-14 00:51:05 +000081 This function does not validate the arguments fed to it
82 so the calling program should take care of that.
83
84 Returns number of lines changed, or -1 on error.
85*/
86int FAST_FUNC update_passwd(const char *filename,
87 const char *name,
88 const char *new_passwd,
89 const char *member)
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000090{
Denis Vlasenko829bbd32009-04-14 00:51:05 +000091#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
92#define member NULL
93#endif
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +000094 struct stat sb;
95 struct flock lock;
96 FILE *old_fp;
97 FILE *new_fp;
Denis Vlasenko557fb712007-07-21 13:25:28 +000098 char *fnamesfx;
99 char *sfx_char;
Denys Vlasenko4653e442010-01-09 20:57:06 +0100100 char *name_colon;
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000101 int old_fd;
102 int new_fd;
103 int i;
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000104 int changed_lines;
Denis Vlasenko557fb712007-07-21 13:25:28 +0000105 int ret = -1; /* failure */
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200106 /* used as a bool: "are we modifying /etc/shadow?" */
107#if ENABLE_FEATURE_SHADOWPASSWDS
108 const char *shadow = strstr(filename, "shadow");
109#else
110# define shadow NULL
111#endif
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000112
Paul Fox599bbfb2007-11-08 20:00:36 +0000113 filename = xmalloc_follow_symlinks(filename);
Paul Fox53bd4012007-11-08 01:12:38 +0000114 if (filename == NULL)
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000115 return ret;
Paul Fox53bd4012007-11-08 01:12:38 +0000116
Denys Vlasenko936c8802015-03-12 15:30:46 +0100117 if (name)
118 check_selinux_update_passwd(name);
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +0000119
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000120 /* New passwd file, "/etc/passwd+" for now */
Denis Vlasenko557fb712007-07-21 13:25:28 +0000121 fnamesfx = xasprintf("%s+", filename);
122 sfx_char = &fnamesfx[strlen(fnamesfx)-1];
Denys Vlasenko936c8802015-03-12 15:30:46 +0100123 name_colon = xasprintf("%s:", name ? name : "");
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000124
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200125 if (shadow)
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000126 old_fp = fopen(filename, "r+");
127 else
128 old_fp = fopen_or_warn(filename, "r+");
Denys Vlasenkoc6fb2a62009-11-02 19:18:49 +0100129 if (!old_fp) {
130 if (shadow)
131 ret = 0; /* missing shadow is not an error */
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000132 goto free_mem;
Denys Vlasenkoc6fb2a62009-11-02 19:18:49 +0100133 }
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000134 old_fd = fileno(old_fp);
135
Denis Vlasenko2edbc2a2007-10-20 02:00:49 +0000136 selinux_preserve_fcontext(old_fd);
137
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000138 /* Try to create "/etc/passwd+". Wait if it exists. */
139 i = 30;
140 do {
141 // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
Denis Vlasenko557fb712007-07-21 13:25:28 +0000142 new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000143 if (new_fd >= 0) goto created;
144 if (errno != EEXIST) break;
145 usleep(100000); /* 0.1 sec */
146 } while (--i);
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000147 bb_perror_msg("can't create '%s'", fnamesfx);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000148 goto close_old_fp;
149
150 created:
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200151 if (fstat(old_fd, &sb) == 0) {
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000152 fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
153 fchown(new_fd, sb.st_uid, sb.st_gid);
154 }
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000155 errno = 0;
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100156 new_fp = xfdopen_for_write(new_fd);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000157
158 /* Backup file is "/etc/passwd-" */
Denis Vlasenko557fb712007-07-21 13:25:28 +0000159 *sfx_char = '-';
160 /* Delete old backup */
161 i = (unlink(fnamesfx) && errno != ENOENT);
162 /* Create backup as a hardlink to current */
163 if (i || link(filename, fnamesfx))
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000164 bb_perror_msg("warning: can't create backup copy '%s'",
165 fnamesfx);
Denis Vlasenko557fb712007-07-21 13:25:28 +0000166 *sfx_char = '+';
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000167
168 /* Lock the password file before updating */
169 lock.l_type = F_WRLCK;
170 lock.l_whence = SEEK_SET;
171 lock.l_start = 0;
172 lock.l_len = 0;
173 if (fcntl(old_fd, F_SETLK, &lock) < 0)
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000174 bb_perror_msg("warning: can't lock '%s'", filename);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000175 lock.l_type = F_UNLCK;
176
Denis Vlasenko557fb712007-07-21 13:25:28 +0000177 /* Read current password file, write updated /etc/passwd+ */
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000178 changed_lines = 0;
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000179 while (1) {
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000180 char *cp, *line;
181
182 line = xmalloc_fgetline(old_fp);
183 if (!line) /* EOF/error */
184 break;
Denys Vlasenko936c8802015-03-12 15:30:46 +0100185
Denys Vlasenko9cc3d3a2016-12-23 02:42:26 +0100186#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
Denys Vlasenko936c8802015-03-12 15:30:46 +0100187 if (!name && member) {
188 /* Delete member from all groups */
189 /* line is "GROUP:PASSWD:[member1[,member2]...]" */
190 unsigned member_len = strlen(member);
191 char *list = strrchr(line, ':');
192 while (list) {
193 list++;
194 next_list_element:
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100195 if (is_prefixed_with(list, member)) {
Denys Vlasenko936c8802015-03-12 15:30:46 +0100196 char c;
197 changed_lines++;
198 c = list[member_len];
199 if (c == '\0') {
200 if (list[-1] == ',')
201 list--;
202 *list = '\0';
203 break;
204 }
205 if (c == ',') {
206 overlapping_strcpy(list, list + member_len + 1);
207 goto next_list_element;
208 }
209 changed_lines--;
210 }
211 list = strchr(list, ',');
212 }
213 fprintf(new_fp, "%s\n", line);
214 goto next;
215 }
Denys Vlasenko9cc3d3a2016-12-23 02:42:26 +0100216#endif
Denys Vlasenko936c8802015-03-12 15:30:46 +0100217
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100218 cp = is_prefixed_with(line, name_colon);
219 if (!cp) {
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000220 fprintf(new_fp, "%s\n", line);
221 goto next;
222 }
223
224 /* We have a match with "name:"... */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100225 /* cp points past "name:" */
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000226
227#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
228 if (member) {
229 /* It's actually /etc/group+, not /etc/passwd+ */
230 if (ENABLE_FEATURE_ADDUSER_TO_GROUP
231 && applet_name[0] == 'a'
232 ) {
233 /* Add user to group */
234 fprintf(new_fp, "%s%s%s\n", line,
235 last_char_is(line, ':') ? "" : ",",
236 member);
237 changed_lines++;
238 } else if (ENABLE_FEATURE_DEL_USER_FROM_GROUP
239 /* && applet_name[0] == 'd' */
240 ) {
241 /* Delete user from group */
242 char *tmp;
243 const char *fmt = "%s";
244
245 /* find the start of the member list: last ':' */
246 cp = strrchr(line, ':');
247 /* cut it */
248 *cp++ = '\0';
249 /* write the cut line name:passwd:gid:
250 * or name:!:: */
251 fprintf(new_fp, "%s:", line);
252 /* parse the tokens of the member list */
253 tmp = cp;
254 while ((cp = strsep(&tmp, ",")) != NULL) {
255 if (strcmp(member, cp) != 0) {
256 fprintf(new_fp, fmt, cp);
257 fmt = ",%s";
258 } else {
259 /* found member, skip it */
260 changed_lines++;
261 }
262 }
263 fprintf(new_fp, "\n");
264 }
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000265 } else
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000266#endif
267 if ((ENABLE_PASSWD && applet_name[0] == 'p')
268 || (ENABLE_CHPASSWD && applet_name[0] == 'c')
269 ) {
270 /* Change passwd */
271 cp = strchrnul(cp, ':'); /* move past old passwd */
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200272
273 if (shadow && *cp == ':') {
274 /* /etc/shadow's field 3 (passwd change date) needs updating */
275 /* move past old change date */
Denys Vlasenkoa5c5dc62019-06-07 12:29:24 +0200276 unsigned time_days = (unsigned long)(time(NULL)) / (24*60*60);
277
278 if (time_days == 0) {
279 /* 0 as change date has special meaning, avoid it */
280 time_days = 1;
281 }
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200282 cp = strchrnul(cp + 1, ':');
283 /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
Denys Vlasenko4653e442010-01-09 20:57:06 +0100284 fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd,
Denys Vlasenkoa5c5dc62019-06-07 12:29:24 +0200285 time_days, cp);
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200286 } else {
287 /* "name:" + "new_passwd" + ":rest of line" */
Denys Vlasenko4653e442010-01-09 20:57:06 +0100288 fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp);
Denys Vlasenko189f74d2009-07-13 01:15:30 +0200289 }
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000290 changed_lines++;
291 } /* else delete user or group: skip the line */
292 next:
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000293 free(line);
294 }
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000295
296 if (changed_lines == 0) {
Denys Vlasenko4653e442010-01-09 20:57:06 +0100297#if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
298 if (member) {
299 if (ENABLE_ADDGROUP && applet_name[0] == 'a')
300 bb_error_msg("can't find %s in %s", name, filename);
301 if (ENABLE_DELGROUP && applet_name[0] == 'd')
302 bb_error_msg("can't find %s in %s", member, filename);
303 }
Denis Vlasenko7f959372009-04-14 08:06:59 +0000304#endif
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000305 if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
306 && applet_name[0] == 'a' && !member
307 ) {
308 /* add user or group */
Denys Vlasenko4653e442010-01-09 20:57:06 +0100309 fprintf(new_fp, "%s%s\n", name_colon, new_passwd);
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000310 changed_lines++;
311 }
312 }
313
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000314 fcntl(old_fd, F_SETLK, &lock);
315
316 /* We do want all of them to execute, thus | instead of || */
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000317 errno = 0;
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000318 if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
Denis Vlasenko557fb712007-07-21 13:25:28 +0000319 || rename(fnamesfx, filename)
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000320 ) {
321 /* At least one of those failed */
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000322 bb_perror_nomsg();
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000323 goto unlink_new;
324 }
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000325 /* Success: ret >= 0 */
326 ret = changed_lines;
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000327
328 unlink_new:
Denis Vlasenko829bbd32009-04-14 00:51:05 +0000329 if (ret < 0)
330 unlink(fnamesfx);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000331
332 close_old_fp:
333 fclose(old_fp);
334
335 free_mem:
Denis Vlasenko557fb712007-07-21 13:25:28 +0000336 free(fnamesfx);
Paul Fox53bd4012007-11-08 01:12:38 +0000337 free((char *)filename);
Denys Vlasenko4653e442010-01-09 20:57:06 +0100338 free(name_colon);
Denis Vlasenko1ec5eae2007-07-20 21:29:49 +0000339 return ret;
340}