passwd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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"
  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: "[OPTIONS] [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 Encryption method"
  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. #include <sys/resource.h> /* setrlimit */
  39. static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo)
  40. {
  41. char salt[MAX_PW_SALT_LEN];
  42. char *orig = (char*)"";
  43. char *newp = NULL;
  44. char *cp = NULL;
  45. char *ret = NULL; /* failure so far */
  46. if (myuid != 0 && pw->pw_passwd[0]) {
  47. char *encrypted;
  48. orig = bb_ask_stdin("Old password: "); /* returns ptr to static */
  49. if (!orig)
  50. goto err_ret;
  51. encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
  52. if (strcmp(encrypted, pw->pw_passwd) != 0) {
  53. syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name);
  54. bb_do_delay(LOGIN_FAIL_DELAY);
  55. puts("Incorrect password");
  56. goto err_ret;
  57. }
  58. if (ENABLE_FEATURE_CLEAN_UP)
  59. free(encrypted);
  60. }
  61. orig = xstrdup(orig); /* or else bb_ask_stdin() will destroy it */
  62. newp = bb_ask_stdin("New password: "); /* returns ptr to static */
  63. if (!newp)
  64. goto err_ret;
  65. newp = xstrdup(newp); /* we are going to bb_ask_stdin() again, so save it */
  66. if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
  67. && obscure(orig, newp, pw)
  68. && myuid != 0
  69. ) {
  70. goto err_ret; /* non-root is not allowed to have weak passwd */
  71. }
  72. cp = bb_ask_stdin("Retype password: ");
  73. if (!cp)
  74. goto err_ret;
  75. if (strcmp(cp, newp) != 0) {
  76. puts("Passwords don't match");
  77. goto err_ret;
  78. }
  79. crypt_make_pw_salt(salt, algo);
  80. /* pw_encrypt returns malloced str */
  81. ret = pw_encrypt(newp, salt, 1);
  82. /* whee, success! */
  83. err_ret:
  84. nuke_str(orig);
  85. if (ENABLE_FEATURE_CLEAN_UP) free(orig);
  86. nuke_str(newp);
  87. if (ENABLE_FEATURE_CLEAN_UP) free(newp);
  88. nuke_str(cp);
  89. return ret;
  90. }
  91. int passwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  92. int passwd_main(int argc UNUSED_PARAM, char **argv)
  93. {
  94. enum {
  95. OPT_algo = (1 << 0), /* -a - password algorithm */
  96. OPT_lock = (1 << 1), /* -l - lock account */
  97. OPT_unlock = (1 << 2), /* -u - unlock account */
  98. OPT_delete = (1 << 3), /* -d - delete password */
  99. OPT_lud = OPT_lock | OPT_unlock | OPT_delete,
  100. };
  101. unsigned opt;
  102. int rc;
  103. const char *opt_a = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
  104. const char *filename;
  105. char *myname;
  106. char *name;
  107. char *newp;
  108. struct passwd *pw;
  109. uid_t myuid;
  110. struct rlimit rlimit_fsize;
  111. char c;
  112. #if ENABLE_FEATURE_SHADOWPASSWDS
  113. /* Using _r function to avoid pulling in static buffers */
  114. struct spwd spw;
  115. char buffer[256];
  116. #endif
  117. logmode = LOGMODE_BOTH;
  118. openlog(applet_name, 0, LOG_AUTH);
  119. opt = getopt32(argv, "a:lud", &opt_a);
  120. //argc -= optind;
  121. argv += optind;
  122. myuid = getuid();
  123. /* -l, -u, -d require root priv and username argument */
  124. if ((opt & OPT_lud) && (myuid != 0 || !argv[0]))
  125. bb_show_usage();
  126. /* Will complain and die if username not found */
  127. myname = xstrdup(xuid2uname(myuid));
  128. name = argv[0] ? argv[0] : myname;
  129. pw = xgetpwnam(name);
  130. if (myuid != 0 && pw->pw_uid != myuid) {
  131. /* LOGMODE_BOTH */
  132. bb_error_msg_and_die("%s can't change password for %s", myname, name);
  133. }
  134. #if ENABLE_FEATURE_SHADOWPASSWDS
  135. {
  136. /* getspnam_r may return 0 yet set result to NULL.
  137. * At least glibc 2.4 does this. Be extra paranoid here. */
  138. struct spwd *result = NULL;
  139. errno = 0;
  140. if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result) != 0
  141. || !result /* no error, but no record found either */
  142. || strcmp(result->sp_namp, pw->pw_name) != 0 /* paranoia */
  143. ) {
  144. if (errno != ENOENT) {
  145. /* LOGMODE_BOTH */
  146. bb_perror_msg("no record of %s in %s, using %s",
  147. name, bb_path_shadow_file,
  148. bb_path_passwd_file);
  149. }
  150. /* else: /etc/shadow does not exist,
  151. * apparently we are on a shadow-less system,
  152. * no surprise there */
  153. } else {
  154. pw->pw_passwd = result->sp_pwdp;
  155. }
  156. }
  157. #endif
  158. /* Decide what the new password will be */
  159. newp = NULL;
  160. c = pw->pw_passwd[0] - '!';
  161. if (!(opt & OPT_lud)) {
  162. if (myuid != 0 && !c) { /* passwd starts with '!' */
  163. /* LOGMODE_BOTH */
  164. bb_error_msg_and_die("can't change "
  165. "locked password for %s", name);
  166. }
  167. printf("Changing password for %s\n", name);
  168. newp = new_password(pw, myuid, opt_a);
  169. if (!newp) {
  170. logmode = LOGMODE_STDIO;
  171. bb_error_msg_and_die("password for %s is unchanged", name);
  172. }
  173. } else if (opt & OPT_lock) {
  174. if (!c)
  175. goto skip; /* passwd starts with '!' */
  176. newp = xasprintf("!%s", pw->pw_passwd);
  177. } else if (opt & OPT_unlock) {
  178. if (c)
  179. goto skip; /* not '!' */
  180. /* pw->pw_passwd points to static storage,
  181. * strdup'ing to avoid nasty surprizes */
  182. newp = xstrdup(&pw->pw_passwd[1]);
  183. } else if (opt & OPT_delete) {
  184. newp = (char*)"";
  185. }
  186. rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
  187. setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
  188. bb_signals(0
  189. + (1 << SIGHUP)
  190. + (1 << SIGINT)
  191. + (1 << SIGQUIT)
  192. , SIG_IGN);
  193. umask(077);
  194. xsetuid(0);
  195. #if ENABLE_FEATURE_SHADOWPASSWDS
  196. filename = bb_path_shadow_file;
  197. rc = update_passwd(bb_path_shadow_file, name, newp, NULL);
  198. if (rc > 0)
  199. /* password in /etc/shadow was updated */
  200. newp = (char*) "x";
  201. if (rc >= 0)
  202. /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
  203. #endif
  204. {
  205. filename = bb_path_passwd_file;
  206. rc = update_passwd(bb_path_passwd_file, name, newp, NULL);
  207. }
  208. /* LOGMODE_BOTH */
  209. if (rc < 0)
  210. bb_error_msg_and_die("can't update password file %s", filename);
  211. bb_info_msg("Password for %s changed by %s", name, myname);
  212. /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */
  213. skip:
  214. if (!newp) {
  215. bb_error_msg_and_die("password for %s is already %slocked",
  216. name, (opt & OPT_unlock) ? "un" : "");
  217. }
  218. if (ENABLE_FEATURE_CLEAN_UP)
  219. free(myname);
  220. return 0;
  221. }