Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 10 | * |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 11 | * Modified to be able to add or delete users, groups and users to/from groups |
| 12 | * by Tito Ragusa <farmatito@tiscali.it> |
| 13 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 14 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 15 | */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
| 17 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 18 | #if ENABLE_SELINUX |
| 19 | static void check_selinux_update_passwd(const char *username) |
| 20 | { |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 21 | security_context_t seuser; |
| 22 | char *p; |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 23 | |
| 24 | if (getuid() != (uid_t)0 || is_selinux_enabled() == 0) |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 25 | return; /* No need to check */ |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 26 | |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 27 | if (getprevcon_raw(&seuser) < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 28 | bb_simple_perror_msg_and_die("getprevcon failed"); |
Denys Vlasenko | 2496616 | 2020-10-06 02:36:47 +0200 | [diff] [blame] | 29 | |
| 30 | p = strchr(seuser, ':'); |
| 31 | if (!p) |
| 32 | bb_error_msg_and_die("invalid context '%s'", seuser); |
| 33 | *p = '\0'; |
| 34 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 35 | if (strcmp(seuser, username) != 0) { |
Mike Frysinger | c6f3524 | 2016-12-09 18:30:30 -0500 | [diff] [blame] | 36 | 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 Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 48 | bb_simple_error_msg_and_die("SELinux: access denied"); |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 49 | } |
| 50 | if (ENABLE_FEATURE_CLEAN_UP) |
| 51 | freecon(context); |
| 52 | } |
| 53 | #else |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 54 | # define check_selinux_update_passwd(username) ((void)0) |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 57 | /* |
| 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 Vlasenko | 854738d | 2010-11-03 02:48:43 +0100 | [diff] [blame] | 75 | 7) change user's password: update_passwd(FILE, USER, NEW_PASSWD, NULL) |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 76 | 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 Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 79 | 8) delete a user from all groups: update_passwd(FILE, NULL, NULL, MEMBER) |
| 80 | |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 81 | 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 | */ |
| 86 | int FAST_FUNC update_passwd(const char *filename, |
| 87 | const char *name, |
| 88 | const char *new_passwd, |
| 89 | const char *member) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 90 | { |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 91 | #if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP) |
| 92 | #define member NULL |
| 93 | #endif |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 94 | struct stat sb; |
| 95 | struct flock lock; |
| 96 | FILE *old_fp; |
| 97 | FILE *new_fp; |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 98 | char *fnamesfx; |
| 99 | char *sfx_char; |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 100 | char *name_colon; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 101 | int old_fd; |
| 102 | int new_fd; |
| 103 | int i; |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 104 | int changed_lines; |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 105 | int ret = -1; /* failure */ |
Denys Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 106 | /* 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 Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 112 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 113 | filename = xmalloc_follow_symlinks(filename); |
Paul Fox | 53bd401 | 2007-11-08 01:12:38 +0000 | [diff] [blame] | 114 | if (filename == NULL) |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 115 | return ret; |
Paul Fox | 53bd401 | 2007-11-08 01:12:38 +0000 | [diff] [blame] | 116 | |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 117 | if (name) |
| 118 | check_selinux_update_passwd(name); |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 120 | /* New passwd file, "/etc/passwd+" for now */ |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 121 | fnamesfx = xasprintf("%s+", filename); |
| 122 | sfx_char = &fnamesfx[strlen(fnamesfx)-1]; |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 123 | name_colon = xasprintf("%s:", name ? name : ""); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 124 | |
Denys Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 125 | if (shadow) |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 126 | old_fp = fopen(filename, "r+"); |
| 127 | else |
| 128 | old_fp = fopen_or_warn(filename, "r+"); |
Denys Vlasenko | c6fb2a6 | 2009-11-02 19:18:49 +0100 | [diff] [blame] | 129 | if (!old_fp) { |
| 130 | if (shadow) |
| 131 | ret = 0; /* missing shadow is not an error */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 132 | goto free_mem; |
Denys Vlasenko | c6fb2a6 | 2009-11-02 19:18:49 +0100 | [diff] [blame] | 133 | } |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 134 | old_fd = fileno(old_fp); |
| 135 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 136 | selinux_preserve_fcontext(old_fd); |
| 137 | |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 138 | /* 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 Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 142 | new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 143 | if (new_fd >= 0) goto created; |
| 144 | if (errno != EEXIST) break; |
| 145 | usleep(100000); /* 0.1 sec */ |
| 146 | } while (--i); |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 147 | bb_perror_msg("can't create '%s'", fnamesfx); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 148 | goto close_old_fp; |
| 149 | |
| 150 | created: |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 151 | if (fstat(old_fd, &sb) == 0) { |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 152 | fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */ |
| 153 | fchown(new_fd, sb.st_uid, sb.st_gid); |
| 154 | } |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 155 | errno = 0; |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 156 | new_fp = xfdopen_for_write(new_fd); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 157 | |
| 158 | /* Backup file is "/etc/passwd-" */ |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 159 | *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 Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 164 | bb_perror_msg("warning: can't create backup copy '%s'", |
| 165 | fnamesfx); |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 166 | *sfx_char = '+'; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 167 | |
| 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 Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 174 | bb_perror_msg("warning: can't lock '%s'", filename); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 175 | lock.l_type = F_UNLCK; |
| 176 | |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 177 | /* Read current password file, write updated /etc/passwd+ */ |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 178 | changed_lines = 0; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 179 | while (1) { |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 180 | char *cp, *line; |
| 181 | |
| 182 | line = xmalloc_fgetline(old_fp); |
| 183 | if (!line) /* EOF/error */ |
| 184 | break; |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 185 | |
Denys Vlasenko | 9cc3d3a | 2016-12-23 02:42:26 +0100 | [diff] [blame] | 186 | #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 187 | 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 Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 195 | if (is_prefixed_with(list, member)) { |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 196 | 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 Vlasenko | 9cc3d3a | 2016-12-23 02:42:26 +0100 | [diff] [blame] | 216 | #endif |
Denys Vlasenko | 936c880 | 2015-03-12 15:30:46 +0100 | [diff] [blame] | 217 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 218 | cp = is_prefixed_with(line, name_colon); |
| 219 | if (!cp) { |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 220 | fprintf(new_fp, "%s\n", line); |
| 221 | goto next; |
| 222 | } |
| 223 | |
| 224 | /* We have a match with "name:"... */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 225 | /* cp points past "name:" */ |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 226 | |
| 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 Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 265 | } else |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 266 | #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 Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 272 | |
| 273 | if (shadow && *cp == ':') { |
| 274 | /* /etc/shadow's field 3 (passwd change date) needs updating */ |
| 275 | /* move past old change date */ |
Denys Vlasenko | a5c5dc6 | 2019-06-07 12:29:24 +0200 | [diff] [blame] | 276 | 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 Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 282 | cp = strchrnul(cp + 1, ':'); |
| 283 | /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */ |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 284 | fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd, |
Denys Vlasenko | a5c5dc6 | 2019-06-07 12:29:24 +0200 | [diff] [blame] | 285 | time_days, cp); |
Denys Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 286 | } else { |
| 287 | /* "name:" + "new_passwd" + ":rest of line" */ |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 288 | fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp); |
Denys Vlasenko | 189f74d | 2009-07-13 01:15:30 +0200 | [diff] [blame] | 289 | } |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 290 | changed_lines++; |
| 291 | } /* else delete user or group: skip the line */ |
| 292 | next: |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 293 | free(line); |
| 294 | } |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 295 | |
| 296 | if (changed_lines == 0) { |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 297 | #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 Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 304 | #endif |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 305 | if ((ENABLE_ADDUSER || ENABLE_ADDGROUP) |
| 306 | && applet_name[0] == 'a' && !member |
| 307 | ) { |
| 308 | /* add user or group */ |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 309 | fprintf(new_fp, "%s%s\n", name_colon, new_passwd); |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 310 | changed_lines++; |
| 311 | } |
| 312 | } |
| 313 | |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 314 | fcntl(old_fd, F_SETLK, &lock); |
| 315 | |
| 316 | /* We do want all of them to execute, thus | instead of || */ |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 317 | errno = 0; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 318 | if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp)) |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 319 | || rename(fnamesfx, filename) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 320 | ) { |
| 321 | /* At least one of those failed */ |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 322 | bb_perror_nomsg(); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 323 | goto unlink_new; |
| 324 | } |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 325 | /* Success: ret >= 0 */ |
| 326 | ret = changed_lines; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 327 | |
| 328 | unlink_new: |
Denis Vlasenko | 829bbd3 | 2009-04-14 00:51:05 +0000 | [diff] [blame] | 329 | if (ret < 0) |
| 330 | unlink(fnamesfx); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 331 | |
| 332 | close_old_fp: |
| 333 | fclose(old_fp); |
| 334 | |
| 335 | free_mem: |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 336 | free(fnamesfx); |
Paul Fox | 53bd401 | 2007-11-08 01:12:38 +0000 | [diff] [blame] | 337 | free((char *)filename); |
Denys Vlasenko | 4653e44 | 2010-01-09 20:57:06 +0100 | [diff] [blame] | 338 | free(name_colon); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 339 | return ret; |
| 340 | } |