passwd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. //config:config PASSWD
  6. //config: bool "passwd (21 kb)"
  7. //config: default y
  8. //config: select FEATURE_SYSLOG
  9. //config: help
  10. //config: passwd changes passwords for user and group accounts. A normal user
  11. //config: may only change the password for his/her own account, the super user
  12. //config: may change the password for any account. The administrator of a group
  13. //config: may change the password for the group.
  14. //config:
  15. //config: Note that busybox binary must be setuid root for this applet to
  16. //config: work properly.
  17. //config:
  18. //config:config FEATURE_PASSWD_WEAK_CHECK
  19. //config: bool "Check new passwords for weakness"
  20. //config: default y
  21. //config: depends on PASSWD
  22. //config: help
  23. //config: With this option passwd will refuse new passwords which are "weak".
  24. //applet:/* Needs to be run by root or be suid root - needs to change /etc/{passwd,shadow}: */
  25. //applet:IF_PASSWD(APPLET(passwd, BB_DIR_USR_BIN, BB_SUID_REQUIRE))
  26. //kbuild:lib-$(CONFIG_PASSWD) += passwd.o
  27. //usage:#define passwd_trivial_usage
  28. //usage: "[-a ALG] [-dlu] [USER]"
  29. //usage:#define passwd_full_usage "\n\n"
  30. //usage: "Change USER's password (default: current user)"
  31. //usage: "\n"
  32. //usage: "\n -a ALG "CRYPT_METHODS_HELP_STR
  33. //usage: "\n -d Set password to ''"
  34. //usage: "\n -l Lock (disable) account"
  35. //usage: "\n -u Unlock (enable) account"
  36. #include "libbb.h"
  37. #include <syslog.h>
  38. static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo)
  39. {
  40. char salt[MAX_PW_SALT_LEN];
  41. char *orig = NULL;
  42. char *newp = NULL;
  43. char *cp = NULL;
  44. char *ret = NULL; /* failure so far */
  45. if (myuid != 0 && pw->pw_passwd[0]) {
  46. char *encrypted;
  47. orig = bb_ask_noecho_stdin("Old password: "); /* returns malloced str */
  48. if (!orig)
  49. goto err_ret;
  50. encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
  51. if (strcmp(encrypted, pw->pw_passwd) != 0) {
  52. syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name);
  53. pause_after_failed_login();
  54. puts("Incorrect password");
  55. goto err_ret;
  56. }
  57. if (ENABLE_FEATURE_CLEAN_UP)
  58. free(encrypted);
  59. }
  60. newp = bb_ask_noecho_stdin("New password: "); /* returns malloced str */
  61. if (!newp)
  62. goto err_ret;
  63. if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
  64. && obscure(orig, newp, pw) /* NB: passing NULL orig is ok */
  65. && myuid != 0
  66. ) {
  67. goto err_ret; /* non-root is not allowed to have weak passwd */
  68. }
  69. cp = bb_ask_noecho_stdin("Retype password: ");
  70. if (!cp)
  71. goto err_ret;
  72. if (strcmp(cp, newp) != 0) {
  73. puts("Passwords don't match");
  74. goto err_ret;
  75. }
  76. crypt_make_pw_salt(salt, algo);
  77. /* pw_encrypt returns malloced str */
  78. ret = pw_encrypt(newp, salt, 1);
  79. /* whee, success! */
  80. err_ret:
  81. nuke_str(orig);
  82. if (ENABLE_FEATURE_CLEAN_UP) free(orig);
  83. nuke_str(newp);
  84. if (ENABLE_FEATURE_CLEAN_UP) free(newp);
  85. nuke_str(cp);
  86. if (ENABLE_FEATURE_CLEAN_UP) free(cp);
  87. return ret;
  88. }
  89. int passwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  90. int passwd_main(int argc UNUSED_PARAM, char **argv)
  91. {
  92. enum {
  93. OPT_algo = (1 << 0), /* -a - password algorithm */
  94. OPT_lock = (1 << 1), /* -l - lock account */
  95. OPT_unlock = (1 << 2), /* -u - unlock account */
  96. OPT_delete = (1 << 3), /* -d - delete password */
  97. OPT_lud = OPT_lock | OPT_unlock | OPT_delete,
  98. };
  99. unsigned opt;
  100. int rc;
  101. const char *opt_a = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
  102. const char *filename;
  103. char *myname;
  104. char *name;
  105. char *newp;
  106. struct passwd *pw;
  107. uid_t myuid;
  108. struct rlimit rlimit_fsize;
  109. char c;
  110. #if ENABLE_FEATURE_SHADOWPASSWDS
  111. /* Using _r function to avoid pulling in static buffers */
  112. struct spwd spw;
  113. char buffer[256];
  114. #endif
  115. logmode = LOGMODE_BOTH;
  116. openlog(applet_name, 0, LOG_AUTH);
  117. opt = getopt32(argv, "a:lud", &opt_a);
  118. //argc -= optind;
  119. argv += optind;
  120. myuid = getuid();
  121. /* -l, -u, -d require root priv and username argument */
  122. if ((opt & OPT_lud) && (myuid != 0 || !argv[0]))
  123. bb_show_usage();
  124. /* Will complain and die if username not found */
  125. myname = xstrdup(xuid2uname(myuid));
  126. name = argv[0] ? argv[0] : myname;
  127. pw = xgetpwnam(name);
  128. if (myuid != 0 && pw->pw_uid != myuid) {
  129. /* LOGMODE_BOTH */
  130. bb_error_msg_and_die("%s can't change password for %s", myname, name);
  131. }
  132. #if ENABLE_FEATURE_SHADOWPASSWDS
  133. {
  134. /* getspnam_r may return 0 yet set result to NULL.
  135. * At least glibc 2.4 does this. Be extra paranoid here. */
  136. struct spwd *result = NULL;
  137. errno = 0;
  138. if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result) != 0
  139. || !result /* no error, but no record found either */
  140. || strcmp(result->sp_namp, pw->pw_name) != 0 /* paranoia */
  141. ) {
  142. if (errno != ENOENT) {
  143. /* LOGMODE_BOTH */
  144. bb_perror_msg("no record of %s in %s, using %s",
  145. name, bb_path_shadow_file,
  146. bb_path_passwd_file);
  147. }
  148. /* else: /etc/shadow does not exist,
  149. * apparently we are on a shadow-less system,
  150. * no surprise there */
  151. } else {
  152. pw->pw_passwd = result->sp_pwdp;
  153. }
  154. }
  155. #endif
  156. /* Decide what the new password will be */
  157. newp = NULL;
  158. c = pw->pw_passwd[0] - '!';
  159. if (!(opt & OPT_lud)) {
  160. if (myuid != 0 && !c) { /* passwd starts with '!' */
  161. /* LOGMODE_BOTH */
  162. bb_error_msg_and_die("can't change "
  163. "locked password for %s", name);
  164. }
  165. printf("Changing password for %s\n", name);
  166. newp = new_password(pw, myuid, opt_a);
  167. if (!newp) {
  168. logmode = LOGMODE_STDIO;
  169. bb_error_msg_and_die("password for %s is unchanged", name);
  170. }
  171. } else if (opt & OPT_lock) {
  172. if (!c)
  173. goto skip; /* passwd starts with '!' */
  174. newp = xasprintf("!%s", pw->pw_passwd);
  175. } else if (opt & OPT_unlock) {
  176. if (c)
  177. goto skip; /* not '!' */
  178. /* pw->pw_passwd points to static storage,
  179. * strdup'ing to avoid nasty surprizes */
  180. newp = xstrdup(&pw->pw_passwd[1]);
  181. } else if (opt & OPT_delete) {
  182. newp = (char*)"";
  183. }
  184. rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
  185. setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
  186. bb_signals(0
  187. + (1 << SIGHUP)
  188. + (1 << SIGINT)
  189. + (1 << SIGQUIT)
  190. , SIG_IGN);
  191. umask(077);
  192. xsetuid(0);
  193. #if ENABLE_FEATURE_SHADOWPASSWDS
  194. filename = bb_path_shadow_file;
  195. rc = update_passwd(bb_path_shadow_file, name, newp, NULL);
  196. if (rc > 0)
  197. /* password in /etc/shadow was updated */
  198. newp = (char*) "x";
  199. if (rc >= 0)
  200. /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
  201. #endif
  202. {
  203. filename = bb_path_passwd_file;
  204. rc = update_passwd(bb_path_passwd_file, name, newp, NULL);
  205. }
  206. /* LOGMODE_BOTH */
  207. if (rc < 0)
  208. bb_error_msg_and_die("can't update password file %s", filename);
  209. bb_info_msg("password for %s changed by %s", name, myname);
  210. /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */
  211. skip:
  212. if (!newp) {
  213. bb_error_msg_and_die("password for %s is already %slocked",
  214. name, (opt & OPT_unlock) ? "un" : "");
  215. }
  216. if (ENABLE_FEATURE_CLEAN_UP)
  217. free(myname);
  218. return 0;
  219. }