update_passwd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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>
  10. *
  11. * Modified to be able to add or delete users, groups and users to/from groups
  12. * by Tito Ragusa <farmatito@tiscali.it>
  13. *
  14. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  15. */
  16. #include "libbb.h"
  17. #if ENABLE_SELINUX
  18. static void check_selinux_update_passwd(const char *username)
  19. {
  20. security_context_t context;
  21. char *seuser;
  22. if (getuid() != (uid_t)0 || is_selinux_enabled() == 0)
  23. return; /* No need to check */
  24. if (getprevcon_raw(&context) < 0)
  25. bb_perror_msg_and_die("getprevcon failed");
  26. seuser = strtok(context, ":");
  27. if (!seuser)
  28. bb_error_msg_and_die("invalid context '%s'", context);
  29. if (strcmp(seuser, username) != 0) {
  30. if (checkPasswdAccess(PASSWD__PASSWD) != 0)
  31. bb_error_msg_and_die("SELinux: access denied");
  32. }
  33. if (ENABLE_FEATURE_CLEAN_UP)
  34. freecon(context);
  35. }
  36. #else
  37. # define check_selinux_update_passwd(username) ((void)0)
  38. #endif
  39. /*
  40. 1) add a user: update_passwd(FILE, USER, REMAINING_PWLINE, NULL)
  41. only if CONFIG_ADDUSER=y and applet_name[0] == 'a' like in adduser
  42. 2) add a group: update_passwd(FILE, GROUP, REMAINING_GRLINE, NULL)
  43. only if CONFIG_ADDGROUP=y and applet_name[0] == 'a' like in addgroup
  44. 3) add a user to a group: update_passwd(FILE, GROUP, NULL, MEMBER)
  45. only if CONFIG_FEATURE_ADDUSER_TO_GROUP=y, applet_name[0] == 'a'
  46. like in addgroup and member != NULL
  47. 4) delete a user: update_passwd(FILE, USER, NULL, NULL)
  48. 5) delete a group: update_passwd(FILE, GROUP, NULL, NULL)
  49. 6) delete a user from a group: update_passwd(FILE, GROUP, NULL, MEMBER)
  50. only if CONFIG_FEATURE_DEL_USER_FROM_GROUP=y and member != NULL
  51. 7) change user's passord: update_passwd(FILE, USER, NEW_PASSWD, NULL)
  52. only if CONFIG_PASSWD=y and applet_name[0] == 'p' like in passwd
  53. or if CONFIG_CHPASSWD=y and applet_name[0] == 'c' like in chpasswd
  54. This function does not validate the arguments fed to it
  55. so the calling program should take care of that.
  56. Returns number of lines changed, or -1 on error.
  57. */
  58. int FAST_FUNC update_passwd(const char *filename,
  59. const char *name,
  60. const char *new_passwd,
  61. const char *member)
  62. {
  63. #if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
  64. #define member NULL
  65. #endif
  66. struct stat sb;
  67. struct flock lock;
  68. FILE *old_fp;
  69. FILE *new_fp;
  70. char *fnamesfx;
  71. char *sfx_char;
  72. char *name_colon;
  73. unsigned user_len;
  74. int old_fd;
  75. int new_fd;
  76. int i;
  77. int changed_lines;
  78. int ret = -1; /* failure */
  79. /* used as a bool: "are we modifying /etc/shadow?" */
  80. #if ENABLE_FEATURE_SHADOWPASSWDS
  81. const char *shadow = strstr(filename, "shadow");
  82. #else
  83. # define shadow NULL
  84. #endif
  85. filename = xmalloc_follow_symlinks(filename);
  86. if (filename == NULL)
  87. return ret;
  88. check_selinux_update_passwd(name);
  89. /* New passwd file, "/etc/passwd+" for now */
  90. fnamesfx = xasprintf("%s+", filename);
  91. sfx_char = &fnamesfx[strlen(fnamesfx)-1];
  92. name_colon = xasprintf("%s:", name);
  93. user_len = strlen(name_colon);
  94. if (shadow)
  95. old_fp = fopen(filename, "r+");
  96. else
  97. old_fp = fopen_or_warn(filename, "r+");
  98. if (!old_fp) {
  99. if (shadow)
  100. ret = 0; /* missing shadow is not an error */
  101. goto free_mem;
  102. }
  103. old_fd = fileno(old_fp);
  104. selinux_preserve_fcontext(old_fd);
  105. /* Try to create "/etc/passwd+". Wait if it exists. */
  106. i = 30;
  107. do {
  108. // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
  109. new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
  110. if (new_fd >= 0) goto created;
  111. if (errno != EEXIST) break;
  112. usleep(100000); /* 0.1 sec */
  113. } while (--i);
  114. bb_perror_msg("can't create '%s'", fnamesfx);
  115. goto close_old_fp;
  116. created:
  117. if (!fstat(old_fd, &sb)) {
  118. fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
  119. fchown(new_fd, sb.st_uid, sb.st_gid);
  120. }
  121. errno = 0;
  122. new_fp = xfdopen_for_write(new_fd);
  123. /* Backup file is "/etc/passwd-" */
  124. *sfx_char = '-';
  125. /* Delete old backup */
  126. i = (unlink(fnamesfx) && errno != ENOENT);
  127. /* Create backup as a hardlink to current */
  128. if (i || link(filename, fnamesfx))
  129. bb_perror_msg("warning: can't create backup copy '%s'",
  130. fnamesfx);
  131. *sfx_char = '+';
  132. /* Lock the password file before updating */
  133. lock.l_type = F_WRLCK;
  134. lock.l_whence = SEEK_SET;
  135. lock.l_start = 0;
  136. lock.l_len = 0;
  137. if (fcntl(old_fd, F_SETLK, &lock) < 0)
  138. bb_perror_msg("warning: can't lock '%s'", filename);
  139. lock.l_type = F_UNLCK;
  140. /* Read current password file, write updated /etc/passwd+ */
  141. changed_lines = 0;
  142. while (1) {
  143. char *cp, *line;
  144. line = xmalloc_fgetline(old_fp);
  145. if (!line) /* EOF/error */
  146. break;
  147. if (strncmp(name_colon, line, user_len) != 0) {
  148. fprintf(new_fp, "%s\n", line);
  149. goto next;
  150. }
  151. /* We have a match with "name:"... */
  152. cp = line + user_len; /* move past name: */
  153. #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
  154. if (member) {
  155. /* It's actually /etc/group+, not /etc/passwd+ */
  156. if (ENABLE_FEATURE_ADDUSER_TO_GROUP
  157. && applet_name[0] == 'a'
  158. ) {
  159. /* Add user to group */
  160. fprintf(new_fp, "%s%s%s\n", line,
  161. last_char_is(line, ':') ? "" : ",",
  162. member);
  163. changed_lines++;
  164. } else if (ENABLE_FEATURE_DEL_USER_FROM_GROUP
  165. /* && applet_name[0] == 'd' */
  166. ) {
  167. /* Delete user from group */
  168. char *tmp;
  169. const char *fmt = "%s";
  170. /* find the start of the member list: last ':' */
  171. cp = strrchr(line, ':');
  172. /* cut it */
  173. *cp++ = '\0';
  174. /* write the cut line name:passwd:gid:
  175. * or name:!:: */
  176. fprintf(new_fp, "%s:", line);
  177. /* parse the tokens of the member list */
  178. tmp = cp;
  179. while ((cp = strsep(&tmp, ",")) != NULL) {
  180. if (strcmp(member, cp) != 0) {
  181. fprintf(new_fp, fmt, cp);
  182. fmt = ",%s";
  183. } else {
  184. /* found member, skip it */
  185. changed_lines++;
  186. }
  187. }
  188. fprintf(new_fp, "\n");
  189. }
  190. } else
  191. #endif
  192. if ((ENABLE_PASSWD && applet_name[0] == 'p')
  193. || (ENABLE_CHPASSWD && applet_name[0] == 'c')
  194. ) {
  195. /* Change passwd */
  196. cp = strchrnul(cp, ':'); /* move past old passwd */
  197. if (shadow && *cp == ':') {
  198. /* /etc/shadow's field 3 (passwd change date) needs updating */
  199. /* move past old change date */
  200. cp = strchrnul(cp + 1, ':');
  201. /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
  202. fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd,
  203. (unsigned)(time(NULL)) / (24*60*60), cp);
  204. } else {
  205. /* "name:" + "new_passwd" + ":rest of line" */
  206. fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp);
  207. }
  208. changed_lines++;
  209. } /* else delete user or group: skip the line */
  210. next:
  211. free(line);
  212. }
  213. if (changed_lines == 0) {
  214. #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
  215. if (member) {
  216. if (ENABLE_ADDGROUP && applet_name[0] == 'a')
  217. bb_error_msg("can't find %s in %s", name, filename);
  218. if (ENABLE_DELGROUP && applet_name[0] == 'd')
  219. bb_error_msg("can't find %s in %s", member, filename);
  220. }
  221. #endif
  222. if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
  223. && applet_name[0] == 'a' && !member
  224. ) {
  225. /* add user or group */
  226. fprintf(new_fp, "%s%s\n", name_colon, new_passwd);
  227. changed_lines++;
  228. }
  229. }
  230. fcntl(old_fd, F_SETLK, &lock);
  231. /* We do want all of them to execute, thus | instead of || */
  232. errno = 0;
  233. if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
  234. || rename(fnamesfx, filename)
  235. ) {
  236. /* At least one of those failed */
  237. bb_perror_nomsg();
  238. goto unlink_new;
  239. }
  240. /* Success: ret >= 0 */
  241. ret = changed_lines;
  242. unlink_new:
  243. if (ret < 0)
  244. unlink(fnamesfx);
  245. close_old_fp:
  246. fclose(old_fp);
  247. free_mem:
  248. free(fnamesfx);
  249. free((char *)filename);
  250. free(name_colon);
  251. return ret;
  252. }