3
0

update_passwd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "libbb.h"
  12. int update_passwd(const char *filename, const char *username,
  13. const char *new_pw)
  14. {
  15. struct stat sb;
  16. struct flock lock;
  17. FILE *old_fp;
  18. FILE *new_fp;
  19. char *fnamesfx;
  20. char *sfx_char;
  21. unsigned user_len;
  22. int old_fd;
  23. int new_fd;
  24. int i;
  25. int cnt = 0;
  26. int ret = -1; /* failure */
  27. /* New passwd file, "/etc/passwd+" for now */
  28. fnamesfx = xasprintf("%s+", filename);
  29. sfx_char = &fnamesfx[strlen(fnamesfx)-1];
  30. username = xasprintf("%s:", username);
  31. user_len = strlen(username);
  32. old_fp = fopen(filename, "r+");
  33. if (!old_fp)
  34. goto free_mem;
  35. old_fd = fileno(old_fp);
  36. /* Try to create "/etc/passwd+". Wait if it exists. */
  37. i = 30;
  38. do {
  39. // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
  40. new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
  41. if (new_fd >= 0) goto created;
  42. if (errno != EEXIST) break;
  43. usleep(100000); /* 0.1 sec */
  44. } while (--i);
  45. bb_perror_msg("cannot create '%s'", fnamesfx);
  46. goto close_old_fp;
  47. created:
  48. if (!fstat(old_fd, &sb)) {
  49. fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
  50. fchown(new_fd, sb.st_uid, sb.st_gid);
  51. }
  52. new_fp = fdopen(new_fd, "w");
  53. if (!new_fp) {
  54. close(new_fd);
  55. goto unlink_new;
  56. }
  57. /* Backup file is "/etc/passwd-" */
  58. *sfx_char = '-';
  59. /* Delete old backup */
  60. i = (unlink(fnamesfx) && errno != ENOENT);
  61. /* Create backup as a hardlink to current */
  62. if (i || link(filename, fnamesfx))
  63. bb_perror_msg("warning: cannot create backup copy '%s'", fnamesfx);
  64. *sfx_char = '+';
  65. /* Lock the password file before updating */
  66. lock.l_type = F_WRLCK;
  67. lock.l_whence = SEEK_SET;
  68. lock.l_start = 0;
  69. lock.l_len = 0;
  70. if (fcntl(old_fd, F_SETLK, &lock) < 0)
  71. bb_perror_msg("warning: cannot lock '%s'", filename);
  72. lock.l_type = F_UNLCK;
  73. /* Read current password file, write updated /etc/passwd+ */
  74. while (1) {
  75. char *line = xmalloc_fgets(old_fp);
  76. if (!line) break; /* EOF/error */
  77. if (strncmp(username, line, user_len) == 0) {
  78. /* we have a match with "username:"... */
  79. const char *cp = line + user_len;
  80. /* now cp -> old passwd, skip it: */
  81. cp = strchrnul(cp, ':');
  82. /* now cp -> ':' after old passwd or -> "" */
  83. fprintf(new_fp, "%s%s%s", username, new_pw, cp);
  84. cnt++;
  85. } else
  86. fputs(line, new_fp);
  87. free(line);
  88. }
  89. fcntl(old_fd, F_SETLK, &lock);
  90. /* We do want all of them to execute, thus | instead of || */
  91. if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
  92. || rename(fnamesfx, filename)
  93. ) {
  94. /* At least one of those failed */
  95. goto unlink_new;
  96. }
  97. ret = cnt; /* whee, success! */
  98. unlink_new:
  99. if (ret < 0) unlink(fnamesfx);
  100. close_old_fp:
  101. fclose(old_fp);
  102. free_mem:
  103. free(fnamesfx);
  104. free((char*)username);
  105. return ret;
  106. }