update_passwd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 source tree.
  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_simple_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. security_class_t tclass;
  31. access_vector_t av;
  32. tclass = string_to_security_class("passwd");
  33. if (tclass == 0)
  34. goto die;
  35. av = string_to_av_perm(tclass, "passwd");
  36. if (av == 0)
  37. goto die;
  38. if (selinux_check_passwd_access(av) != 0)
  39. die:
  40. bb_simple_error_msg_and_die("SELinux: access denied");
  41. }
  42. if (ENABLE_FEATURE_CLEAN_UP)
  43. freecon(context);
  44. }
  45. #else
  46. # define check_selinux_update_passwd(username) ((void)0)
  47. #endif
  48. /*
  49. 1) add a user: update_passwd(FILE, USER, REMAINING_PWLINE, NULL)
  50. only if CONFIG_ADDUSER=y and applet_name[0] == 'a' like in adduser
  51. 2) add a group: update_passwd(FILE, GROUP, REMAINING_GRLINE, NULL)
  52. only if CONFIG_ADDGROUP=y and applet_name[0] == 'a' like in addgroup
  53. 3) add a user to a group: update_passwd(FILE, GROUP, NULL, MEMBER)
  54. only if CONFIG_FEATURE_ADDUSER_TO_GROUP=y, applet_name[0] == 'a'
  55. like in addgroup and member != NULL
  56. 4) delete a user: update_passwd(FILE, USER, NULL, NULL)
  57. 5) delete a group: update_passwd(FILE, GROUP, NULL, NULL)
  58. 6) delete a user from a group: update_passwd(FILE, GROUP, NULL, MEMBER)
  59. only if CONFIG_FEATURE_DEL_USER_FROM_GROUP=y and member != NULL
  60. 7) change user's password: update_passwd(FILE, USER, NEW_PASSWD, NULL)
  61. only if CONFIG_PASSWD=y and applet_name[0] == 'p' like in passwd
  62. or if CONFIG_CHPASSWD=y and applet_name[0] == 'c' like in chpasswd
  63. 8) delete a user from all groups: update_passwd(FILE, NULL, NULL, MEMBER)
  64. This function does not validate the arguments fed to it
  65. so the calling program should take care of that.
  66. Returns number of lines changed, or -1 on error.
  67. */
  68. int FAST_FUNC update_passwd(const char *filename,
  69. const char *name,
  70. const char *new_passwd,
  71. const char *member)
  72. {
  73. #if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
  74. #define member NULL
  75. #endif
  76. struct stat sb;
  77. struct flock lock;
  78. FILE *old_fp;
  79. FILE *new_fp;
  80. char *fnamesfx;
  81. char *sfx_char;
  82. char *name_colon;
  83. int old_fd;
  84. int new_fd;
  85. int i;
  86. int changed_lines;
  87. int ret = -1; /* failure */
  88. /* used as a bool: "are we modifying /etc/shadow?" */
  89. #if ENABLE_FEATURE_SHADOWPASSWDS
  90. const char *shadow = strstr(filename, "shadow");
  91. #else
  92. # define shadow NULL
  93. #endif
  94. filename = xmalloc_follow_symlinks(filename);
  95. if (filename == NULL)
  96. return ret;
  97. if (name)
  98. check_selinux_update_passwd(name);
  99. /* New passwd file, "/etc/passwd+" for now */
  100. fnamesfx = xasprintf("%s+", filename);
  101. sfx_char = &fnamesfx[strlen(fnamesfx)-1];
  102. name_colon = xasprintf("%s:", name ? name : "");
  103. if (shadow)
  104. old_fp = fopen(filename, "r+");
  105. else
  106. old_fp = fopen_or_warn(filename, "r+");
  107. if (!old_fp) {
  108. if (shadow)
  109. ret = 0; /* missing shadow is not an error */
  110. goto free_mem;
  111. }
  112. old_fd = fileno(old_fp);
  113. selinux_preserve_fcontext(old_fd);
  114. /* Try to create "/etc/passwd+". Wait if it exists. */
  115. i = 30;
  116. do {
  117. // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC?
  118. new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600);
  119. if (new_fd >= 0) goto created;
  120. if (errno != EEXIST) break;
  121. usleep(100000); /* 0.1 sec */
  122. } while (--i);
  123. bb_perror_msg("can't create '%s'", fnamesfx);
  124. goto close_old_fp;
  125. created:
  126. if (fstat(old_fd, &sb) == 0) {
  127. fchmod(new_fd, sb.st_mode & 0777); /* ignore errors */
  128. fchown(new_fd, sb.st_uid, sb.st_gid);
  129. }
  130. errno = 0;
  131. new_fp = xfdopen_for_write(new_fd);
  132. /* Backup file is "/etc/passwd-" */
  133. *sfx_char = '-';
  134. /* Delete old backup */
  135. i = (unlink(fnamesfx) && errno != ENOENT);
  136. /* Create backup as a hardlink to current */
  137. if (i || link(filename, fnamesfx))
  138. bb_perror_msg("warning: can't create backup copy '%s'",
  139. fnamesfx);
  140. *sfx_char = '+';
  141. /* Lock the password file before updating */
  142. lock.l_type = F_WRLCK;
  143. lock.l_whence = SEEK_SET;
  144. lock.l_start = 0;
  145. lock.l_len = 0;
  146. if (fcntl(old_fd, F_SETLK, &lock) < 0)
  147. bb_perror_msg("warning: can't lock '%s'", filename);
  148. lock.l_type = F_UNLCK;
  149. /* Read current password file, write updated /etc/passwd+ */
  150. changed_lines = 0;
  151. while (1) {
  152. char *cp, *line;
  153. line = xmalloc_fgetline(old_fp);
  154. if (!line) /* EOF/error */
  155. break;
  156. #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
  157. if (!name && member) {
  158. /* Delete member from all groups */
  159. /* line is "GROUP:PASSWD:[member1[,member2]...]" */
  160. unsigned member_len = strlen(member);
  161. char *list = strrchr(line, ':');
  162. while (list) {
  163. list++;
  164. next_list_element:
  165. if (is_prefixed_with(list, member)) {
  166. char c;
  167. changed_lines++;
  168. c = list[member_len];
  169. if (c == '\0') {
  170. if (list[-1] == ',')
  171. list--;
  172. *list = '\0';
  173. break;
  174. }
  175. if (c == ',') {
  176. overlapping_strcpy(list, list + member_len + 1);
  177. goto next_list_element;
  178. }
  179. changed_lines--;
  180. }
  181. list = strchr(list, ',');
  182. }
  183. fprintf(new_fp, "%s\n", line);
  184. goto next;
  185. }
  186. #endif
  187. cp = is_prefixed_with(line, name_colon);
  188. if (!cp) {
  189. fprintf(new_fp, "%s\n", line);
  190. goto next;
  191. }
  192. /* We have a match with "name:"... */
  193. /* cp points past "name:" */
  194. #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
  195. if (member) {
  196. /* It's actually /etc/group+, not /etc/passwd+ */
  197. if (ENABLE_FEATURE_ADDUSER_TO_GROUP
  198. && applet_name[0] == 'a'
  199. ) {
  200. /* Add user to group */
  201. fprintf(new_fp, "%s%s%s\n", line,
  202. last_char_is(line, ':') ? "" : ",",
  203. member);
  204. changed_lines++;
  205. } else if (ENABLE_FEATURE_DEL_USER_FROM_GROUP
  206. /* && applet_name[0] == 'd' */
  207. ) {
  208. /* Delete user from group */
  209. char *tmp;
  210. const char *fmt = "%s";
  211. /* find the start of the member list: last ':' */
  212. cp = strrchr(line, ':');
  213. /* cut it */
  214. *cp++ = '\0';
  215. /* write the cut line name:passwd:gid:
  216. * or name:!:: */
  217. fprintf(new_fp, "%s:", line);
  218. /* parse the tokens of the member list */
  219. tmp = cp;
  220. while ((cp = strsep(&tmp, ",")) != NULL) {
  221. if (strcmp(member, cp) != 0) {
  222. fprintf(new_fp, fmt, cp);
  223. fmt = ",%s";
  224. } else {
  225. /* found member, skip it */
  226. changed_lines++;
  227. }
  228. }
  229. fprintf(new_fp, "\n");
  230. }
  231. } else
  232. #endif
  233. if ((ENABLE_PASSWD && applet_name[0] == 'p')
  234. || (ENABLE_CHPASSWD && applet_name[0] == 'c')
  235. ) {
  236. /* Change passwd */
  237. cp = strchrnul(cp, ':'); /* move past old passwd */
  238. if (shadow && *cp == ':') {
  239. /* /etc/shadow's field 3 (passwd change date) needs updating */
  240. /* move past old change date */
  241. unsigned time_days = (unsigned long)(time(NULL)) / (24*60*60);
  242. if (time_days == 0) {
  243. /* 0 as change date has special meaning, avoid it */
  244. time_days = 1;
  245. }
  246. cp = strchrnul(cp + 1, ':');
  247. /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */
  248. fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd,
  249. time_days, cp);
  250. } else {
  251. /* "name:" + "new_passwd" + ":rest of line" */
  252. fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp);
  253. }
  254. changed_lines++;
  255. } /* else delete user or group: skip the line */
  256. next:
  257. free(line);
  258. }
  259. if (changed_lines == 0) {
  260. #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP
  261. if (member) {
  262. if (ENABLE_ADDGROUP && applet_name[0] == 'a')
  263. bb_error_msg("can't find %s in %s", name, filename);
  264. if (ENABLE_DELGROUP && applet_name[0] == 'd')
  265. bb_error_msg("can't find %s in %s", member, filename);
  266. }
  267. #endif
  268. if ((ENABLE_ADDUSER || ENABLE_ADDGROUP)
  269. && applet_name[0] == 'a' && !member
  270. ) {
  271. /* add user or group */
  272. fprintf(new_fp, "%s%s\n", name_colon, new_passwd);
  273. changed_lines++;
  274. }
  275. }
  276. fcntl(old_fd, F_SETLK, &lock);
  277. /* We do want all of them to execute, thus | instead of || */
  278. errno = 0;
  279. if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp))
  280. || rename(fnamesfx, filename)
  281. ) {
  282. /* At least one of those failed */
  283. bb_perror_nomsg();
  284. goto unlink_new;
  285. }
  286. /* Success: ret >= 0 */
  287. ret = changed_lines;
  288. unlink_new:
  289. if (ret < 0)
  290. unlink(fnamesfx);
  291. close_old_fp:
  292. fclose(old_fp);
  293. free_mem:
  294. free(fnamesfx);
  295. free((char *)filename);
  296. free(name_colon);
  297. return ret;
  298. }