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 | * |
| 11 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "libbb.h" |
| 15 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 16 | #if ENABLE_SELINUX |
| 17 | static void check_selinux_update_passwd(const char *username) |
| 18 | { |
| 19 | security_context_t context; |
| 20 | char *seuser; |
| 21 | |
| 22 | if (getuid() != (uid_t)0 || is_selinux_enabled() == 0) |
| 23 | return; /* No need to check */ |
| 24 | |
| 25 | if (getprevcon_raw(&context) < 0) |
| 26 | bb_perror_msg_and_die("getprevcon failed"); |
| 27 | seuser = strtok(context, ":"); |
| 28 | if (!seuser) |
| 29 | bb_error_msg_and_die("invalid context '%s'", context); |
| 30 | if (strcmp(seuser, username) != 0) { |
| 31 | if (checkPasswdAccess(PASSWD__PASSWD) != 0) |
| 32 | bb_error_msg_and_die("SELinux: access denied"); |
| 33 | } |
| 34 | if (ENABLE_FEATURE_CLEAN_UP) |
| 35 | freecon(context); |
| 36 | } |
| 37 | #else |
| 38 | #define check_selinux_update_passwd(username) ((void)0) |
| 39 | #endif |
| 40 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 41 | int FAST_FUNC update_passwd(const char *filename, const char *username, |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 42 | const char *new_pw) |
| 43 | { |
| 44 | struct stat sb; |
| 45 | struct flock lock; |
| 46 | FILE *old_fp; |
| 47 | FILE *new_fp; |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 48 | char *fnamesfx; |
| 49 | char *sfx_char; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 50 | unsigned user_len; |
| 51 | int old_fd; |
| 52 | int new_fd; |
| 53 | int i; |
| 54 | int cnt = 0; |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 55 | int ret = -1; /* failure */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 56 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 57 | filename = xmalloc_follow_symlinks(filename); |
Paul Fox | 53bd401 | 2007-11-08 01:12:38 +0000 | [diff] [blame] | 58 | if (filename == NULL) |
| 59 | return -1; |
| 60 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 61 | check_selinux_update_passwd(username); |
| 62 | |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 63 | /* New passwd file, "/etc/passwd+" for now */ |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 64 | fnamesfx = xasprintf("%s+", filename); |
| 65 | sfx_char = &fnamesfx[strlen(fnamesfx)-1]; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 66 | username = xasprintf("%s:", username); |
| 67 | user_len = strlen(username); |
| 68 | |
| 69 | old_fp = fopen(filename, "r+"); |
| 70 | if (!old_fp) |
| 71 | goto free_mem; |
| 72 | old_fd = fileno(old_fp); |
| 73 | |
Denis Vlasenko | 2edbc2a | 2007-10-20 02:00:49 +0000 | [diff] [blame] | 74 | selinux_preserve_fcontext(old_fd); |
| 75 | |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 76 | /* Try to create "/etc/passwd+". Wait if it exists. */ |
| 77 | i = 30; |
| 78 | do { |
| 79 | // 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] | 80 | new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 81 | if (new_fd >= 0) goto created; |
| 82 | if (errno != EEXIST) break; |
| 83 | usleep(100000); /* 0.1 sec */ |
| 84 | } while (--i); |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 85 | bb_perror_msg("cannot create '%s'", fnamesfx); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 86 | goto close_old_fp; |
| 87 | |
| 88 | created: |
| 89 | if (!fstat(old_fd, &sb)) { |
| 90 | fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */ |
| 91 | fchown(new_fd, sb.st_uid, sb.st_gid); |
| 92 | } |
| 93 | new_fp = fdopen(new_fd, "w"); |
| 94 | if (!new_fp) { |
| 95 | close(new_fd); |
| 96 | goto unlink_new; |
| 97 | } |
| 98 | |
| 99 | /* Backup file is "/etc/passwd-" */ |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 100 | *sfx_char = '-'; |
| 101 | /* Delete old backup */ |
| 102 | i = (unlink(fnamesfx) && errno != ENOENT); |
| 103 | /* Create backup as a hardlink to current */ |
| 104 | if (i || link(filename, fnamesfx)) |
| 105 | bb_perror_msg("warning: cannot create backup copy '%s'", fnamesfx); |
| 106 | *sfx_char = '+'; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 107 | |
| 108 | /* Lock the password file before updating */ |
| 109 | lock.l_type = F_WRLCK; |
| 110 | lock.l_whence = SEEK_SET; |
| 111 | lock.l_start = 0; |
| 112 | lock.l_len = 0; |
| 113 | if (fcntl(old_fd, F_SETLK, &lock) < 0) |
| 114 | bb_perror_msg("warning: cannot lock '%s'", filename); |
| 115 | lock.l_type = F_UNLCK; |
| 116 | |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 117 | /* Read current password file, write updated /etc/passwd+ */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 118 | while (1) { |
| 119 | char *line = xmalloc_fgets(old_fp); |
| 120 | if (!line) break; /* EOF/error */ |
| 121 | if (strncmp(username, line, user_len) == 0) { |
| 122 | /* we have a match with "username:"... */ |
| 123 | const char *cp = line + user_len; |
| 124 | /* now cp -> old passwd, skip it: */ |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 125 | cp = strchrnul(cp, ':'); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 126 | /* now cp -> ':' after old passwd or -> "" */ |
| 127 | fprintf(new_fp, "%s%s%s", username, new_pw, cp); |
| 128 | cnt++; |
| 129 | } else |
| 130 | fputs(line, new_fp); |
| 131 | free(line); |
| 132 | } |
| 133 | fcntl(old_fd, F_SETLK, &lock); |
| 134 | |
| 135 | /* We do want all of them to execute, thus | instead of || */ |
| 136 | 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] | 137 | || rename(fnamesfx, filename) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 138 | ) { |
| 139 | /* At least one of those failed */ |
| 140 | goto unlink_new; |
| 141 | } |
| 142 | ret = cnt; /* whee, success! */ |
| 143 | |
| 144 | unlink_new: |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 145 | if (ret < 0) unlink(fnamesfx); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 146 | |
| 147 | close_old_fp: |
| 148 | fclose(old_fp); |
| 149 | |
| 150 | free_mem: |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 151 | free(fnamesfx); |
Paul Fox | 53bd401 | 2007-11-08 01:12:38 +0000 | [diff] [blame] | 152 | free((char *)filename); |
| 153 | free((char *)username); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 154 | return ret; |
| 155 | } |