pam_unix_passwd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * pam_unix password management
  3. *
  4. * Main coding by Elliot Lee <sopwith@redhat.com>, Red Hat Software.
  5. * Copyright (C) 1996.
  6. * Copyright (c) Jan Rękorajski, 1999.
  7. * Copyright (c) Red Hat, Inc., 2007, 2008.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, and the entire permission notice in its entirety,
  14. * including the disclaimer of warranties.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote
  19. * products derived from this software without specific prior
  20. * written permission.
  21. *
  22. * ALTERNATIVELY, this product may be distributed under the terms of
  23. * the GNU Public License, in which case the provisions of the GPL are
  24. * required INSTEAD OF the above restrictions. (This clause is
  25. * necessary due to a potential bad interaction between the GPL and
  26. * the restrictions contained in a BSD-style copyright.)
  27. *
  28. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  32. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  38. * OF THE POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #include "config.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <stdarg.h>
  44. #include <string.h>
  45. #include <malloc.h>
  46. #include <unistd.h>
  47. #include <errno.h>
  48. #include <sys/types.h>
  49. #include <pwd.h>
  50. #include <syslog.h>
  51. #include <shadow.h>
  52. #include <time.h> /* for time() */
  53. #include <fcntl.h>
  54. #include <ctype.h>
  55. #include <sys/time.h>
  56. #include <sys/stat.h>
  57. #include <signal.h>
  58. #include <sys/wait.h>
  59. #include <sys/resource.h>
  60. #include <security/_pam_macros.h>
  61. #include <security/pam_modules.h>
  62. #include <security/pam_ext.h>
  63. #include <security/pam_modutil.h>
  64. #include "pam_cc_compat.h"
  65. #include "md5.h"
  66. #include "support.h"
  67. #include "passverify.h"
  68. #include "bigcrypt.h"
  69. #if (HAVE_YP_GET_DEFAULT_DOMAIN || HAVE_GETDOMAINNAME) && HAVE_YP_MASTER
  70. # define HAVE_NIS
  71. #endif
  72. #ifdef HAVE_NIS
  73. # include <rpc/rpc.h>
  74. # if HAVE_RPCSVC_YP_PROT_H
  75. # include <rpcsvc/yp_prot.h>
  76. # endif
  77. # if HAVE_RPCSVC_YPCLNT_H
  78. # include <rpcsvc/ypclnt.h>
  79. # endif
  80. # include "yppasswd.h"
  81. # if !HAVE_DECL_GETRPCPORT &&!HAVE_RPCB_GETADDR
  82. extern int getrpcport(const char *host, unsigned long prognum,
  83. unsigned long versnum, unsigned int proto);
  84. # endif /* GNU libc 2.1 */
  85. #endif
  86. /*
  87. How it works:
  88. Gets in username (has to be done) from the calling program
  89. Does authentication of user (only if we are not running as root)
  90. Gets new password/checks for sanity
  91. Sets it.
  92. */
  93. #define MAX_PASSWD_TRIES 3
  94. #ifdef HAVE_NIS
  95. #ifdef HAVE_RPCB_GETADDR
  96. static unsigned short
  97. __taddr2port (const struct netconfig *nconf, const struct netbuf *nbuf)
  98. {
  99. unsigned short port = 0;
  100. struct __rpc_sockinfo si;
  101. struct sockaddr_in *sin;
  102. struct sockaddr_in6 *sin6;
  103. if (!__rpc_nconf2sockinfo(nconf, &si))
  104. return 0;
  105. switch (si.si_af)
  106. {
  107. case AF_INET:
  108. sin = nbuf->buf;
  109. port = sin->sin_port;
  110. break;
  111. case AF_INET6:
  112. sin6 = nbuf->buf;
  113. port = sin6->sin6_port;
  114. break;
  115. default:
  116. break;
  117. }
  118. return htons (port);
  119. }
  120. #endif
  121. static char *getNISserver(pam_handle_t *pamh, unsigned long long ctrl)
  122. {
  123. char *master;
  124. char *domainname;
  125. int port, err;
  126. #if defined(HAVE_RPCB_GETADDR)
  127. struct netconfig *nconf;
  128. struct netbuf svcaddr;
  129. char addrbuf[INET6_ADDRSTRLEN];
  130. void *handle;
  131. int found;
  132. #endif
  133. #ifdef HAVE_YP_GET_DEFAULT_DOMAIN
  134. if ((err = yp_get_default_domain(&domainname)) != 0) {
  135. pam_syslog(pamh, LOG_WARNING, "can't get local yp domain: %s",
  136. yperr_string(err));
  137. return NULL;
  138. }
  139. #elif defined(HAVE_GETDOMAINNAME)
  140. char domainname_res[256];
  141. if (getdomainname (domainname_res, sizeof (domainname_res)) == 0)
  142. {
  143. if (strcmp (domainname_res, "(none)") == 0)
  144. {
  145. /* If domainname is not set, some systems will return "(none)" */
  146. domainname_res[0] = '\0';
  147. }
  148. domainname = domainname_res;
  149. }
  150. else domainname = NULL;
  151. #endif
  152. if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) {
  153. pam_syslog(pamh, LOG_WARNING, "can't find the master ypserver: %s",
  154. yperr_string(err));
  155. return NULL;
  156. }
  157. #ifdef HAVE_RPCB_GETADDR
  158. svcaddr.len = 0;
  159. svcaddr.maxlen = sizeof (addrbuf);
  160. svcaddr.buf = addrbuf;
  161. port = 0;
  162. found = 0;
  163. handle = setnetconfig();
  164. while ((nconf = getnetconfig(handle)) != NULL) {
  165. if (!strcmp(nconf->nc_proto, "udp")) {
  166. if (rpcb_getaddr(YPPASSWDPROG, YPPASSWDPROC_UPDATE,
  167. nconf, &svcaddr, master)) {
  168. port = __taddr2port (nconf, &svcaddr);
  169. endnetconfig (handle);
  170. found=1;
  171. break;
  172. }
  173. if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
  174. clnt_pcreateerror (master);
  175. pam_syslog (pamh, LOG_ERR,
  176. "rpcb_getaddr (%s) failed!", master);
  177. return NULL;
  178. }
  179. }
  180. }
  181. if (!found) {
  182. pam_syslog (pamh, LOG_ERR,
  183. "Cannot find suitable transport for protocol 'udp'");
  184. return NULL;
  185. }
  186. #else
  187. port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
  188. #endif
  189. if (port == 0) {
  190. pam_syslog(pamh, LOG_WARNING,
  191. "yppasswdd not running on NIS master host");
  192. return NULL;
  193. }
  194. if (port >= IPPORT_RESERVED) {
  195. pam_syslog(pamh, LOG_WARNING,
  196. "yppasswd daemon running on illegal port");
  197. return NULL;
  198. }
  199. if (on(UNIX_DEBUG, ctrl)) {
  200. pam_syslog(pamh, LOG_DEBUG, "Use NIS server on %s with port %d",
  201. master, port);
  202. }
  203. return master;
  204. }
  205. #endif
  206. #ifdef WITH_SELINUX
  207. static int _unix_run_update_binary(pam_handle_t *pamh, unsigned long long ctrl, const char *user,
  208. const char *fromwhat, const char *towhat, int remember)
  209. {
  210. int retval, child, fds[2];
  211. struct sigaction newsa, oldsa;
  212. D(("called."));
  213. /* create a pipe for the password */
  214. if (pipe(fds) != 0) {
  215. D(("could not make pipe"));
  216. return PAM_AUTH_ERR;
  217. }
  218. if (off(UNIX_NOREAP, ctrl)) {
  219. /*
  220. * This code arranges that the demise of the child does not cause
  221. * the application to receive a signal it is not expecting - which
  222. * may kill the application or worse.
  223. *
  224. * The "noreap" module argument is provided so that the admin can
  225. * override this behavior.
  226. */
  227. memset(&newsa, '\0', sizeof(newsa));
  228. newsa.sa_handler = SIG_DFL;
  229. sigaction(SIGCHLD, &newsa, &oldsa);
  230. }
  231. /* fork */
  232. child = fork();
  233. if (child == 0) {
  234. static char *envp[] = { NULL };
  235. const char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
  236. char buffer[16];
  237. /* XXX - should really tidy up PAM here too */
  238. /* reopen stdin as pipe */
  239. if (dup2(fds[0], STDIN_FILENO) != STDIN_FILENO) {
  240. pam_syslog(pamh, LOG_ERR, "dup2 of %s failed: %m", "stdin");
  241. _exit(PAM_AUTHINFO_UNAVAIL);
  242. }
  243. if (pam_modutil_sanitize_helper_fds(pamh, PAM_MODUTIL_IGNORE_FD,
  244. PAM_MODUTIL_PIPE_FD,
  245. PAM_MODUTIL_PIPE_FD) < 0) {
  246. _exit(PAM_AUTHINFO_UNAVAIL);
  247. }
  248. /* exec binary helper */
  249. args[0] = UPDATE_HELPER;
  250. args[1] = user;
  251. args[2] = "update";
  252. if (on(UNIX_SHADOW, ctrl))
  253. args[3] = "1";
  254. else
  255. args[3] = "0";
  256. snprintf(buffer, sizeof(buffer), "%d", remember);
  257. args[4] = buffer;
  258. DIAG_PUSH_IGNORE_CAST_QUAL;
  259. execve(UPDATE_HELPER, (char *const *) args, envp);
  260. DIAG_POP_IGNORE_CAST_QUAL;
  261. /* should not get here: exit with error */
  262. D(("helper binary is not available"));
  263. _exit(PAM_AUTHINFO_UNAVAIL);
  264. } else if (child > 0) {
  265. /* wait for child */
  266. /* if the stored password is NULL */
  267. int rc=0;
  268. if (fromwhat) {
  269. int len = strlen(fromwhat);
  270. if (len > PAM_MAX_RESP_SIZE)
  271. len = PAM_MAX_RESP_SIZE;
  272. pam_modutil_write(fds[1], fromwhat, len);
  273. }
  274. pam_modutil_write(fds[1], "", 1);
  275. if (towhat) {
  276. int len = strlen(towhat);
  277. if (len > PAM_MAX_RESP_SIZE)
  278. len = PAM_MAX_RESP_SIZE;
  279. pam_modutil_write(fds[1], towhat, len);
  280. }
  281. pam_modutil_write(fds[1], "", 1);
  282. close(fds[0]); /* close here to avoid possible SIGPIPE above */
  283. close(fds[1]);
  284. /* wait for helper to complete: */
  285. while ((rc=waitpid(child, &retval, 0)) < 0 && errno == EINTR);
  286. if (rc<0) {
  287. pam_syslog(pamh, LOG_ERR, "unix_update waitpid failed: %m");
  288. retval = PAM_AUTHTOK_ERR;
  289. } else if (!WIFEXITED(retval)) {
  290. pam_syslog(pamh, LOG_ERR, "unix_update abnormal exit: %d", retval);
  291. retval = PAM_AUTHTOK_ERR;
  292. } else {
  293. retval = WEXITSTATUS(retval);
  294. }
  295. } else {
  296. D(("fork failed"));
  297. close(fds[0]);
  298. close(fds[1]);
  299. retval = PAM_AUTH_ERR;
  300. }
  301. if (off(UNIX_NOREAP, ctrl)) {
  302. sigaction(SIGCHLD, &oldsa, NULL); /* restore old signal handler */
  303. }
  304. return retval;
  305. }
  306. #endif
  307. static int check_old_password(const char *forwho, const char *newpass)
  308. {
  309. static char buf[16384];
  310. char *s_pas;
  311. int retval = PAM_SUCCESS;
  312. FILE *opwfile;
  313. size_t len = strlen(forwho);
  314. opwfile = fopen(OLD_PASSWORDS_FILE, "r");
  315. if (opwfile == NULL)
  316. return PAM_ABORT;
  317. while (fgets(buf, 16380, opwfile)) {
  318. if (!strncmp(buf, forwho, len) && (buf[len] == ':' ||
  319. buf[len] == ',')) {
  320. char *sptr;
  321. buf[strlen(buf) - 1] = '\0';
  322. /* s_luser = */ strtok_r(buf, ":,", &sptr);
  323. /* s_uid = */ strtok_r(NULL, ":,", &sptr);
  324. /* s_npas = */ strtok_r(NULL, ":,", &sptr);
  325. s_pas = strtok_r(NULL, ":,", &sptr);
  326. while (s_pas != NULL) {
  327. char *md5pass = Goodcrypt_md5(newpass, s_pas);
  328. if (md5pass == NULL || !strcmp(md5pass, s_pas)) {
  329. _pam_delete(md5pass);
  330. retval = PAM_AUTHTOK_ERR;
  331. break;
  332. }
  333. s_pas = strtok_r(NULL, ":,", &sptr);
  334. _pam_delete(md5pass);
  335. }
  336. break;
  337. }
  338. }
  339. fclose(opwfile);
  340. return retval;
  341. }
  342. static int _do_setpass(pam_handle_t* pamh, const char *forwho,
  343. const char *fromwhat,
  344. char *towhat, unsigned long long ctrl, int remember)
  345. {
  346. struct passwd *pwd = NULL;
  347. int retval = 0;
  348. int unlocked = 0;
  349. D(("called"));
  350. pwd = getpwnam(forwho);
  351. if (pwd == NULL) {
  352. retval = PAM_AUTHTOK_ERR;
  353. goto done;
  354. }
  355. if (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, forwho, 0, 1)) {
  356. #ifdef HAVE_NIS
  357. char *master;
  358. if ((master=getNISserver(pamh, ctrl)) != NULL) {
  359. struct timeval timeout;
  360. struct yppasswd yppwd;
  361. CLIENT *clnt;
  362. int status;
  363. enum clnt_stat err;
  364. /* Unlock passwd file to avoid deadlock */
  365. unlock_pwdf();
  366. unlocked = 1;
  367. /* Initialize password information */
  368. yppwd.newpw.pw_passwd = pwd->pw_passwd;
  369. yppwd.newpw.pw_name = pwd->pw_name;
  370. yppwd.newpw.pw_uid = pwd->pw_uid;
  371. yppwd.newpw.pw_gid = pwd->pw_gid;
  372. yppwd.newpw.pw_gecos = pwd->pw_gecos;
  373. yppwd.newpw.pw_dir = pwd->pw_dir;
  374. yppwd.newpw.pw_shell = pwd->pw_shell;
  375. yppwd.oldpass = fromwhat ? strdup (fromwhat) : strdup ("");
  376. yppwd.newpw.pw_passwd = towhat;
  377. D(("Set password %s for %s", yppwd.newpw.pw_passwd, forwho));
  378. /* The yppasswd.x file said `unix authentication required',
  379. * so I added it. This is the only reason it is in here.
  380. * My yppasswdd doesn't use it, but maybe some others out there
  381. * do. --okir
  382. */
  383. clnt = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
  384. clnt->cl_auth = authunix_create_default();
  385. memset((char *) &status, '\0', sizeof(status));
  386. timeout.tv_sec = 25;
  387. timeout.tv_usec = 0;
  388. err = clnt_call(clnt, YPPASSWDPROC_UPDATE,
  389. (xdrproc_t) xdr_yppasswd, (char *) &yppwd,
  390. (xdrproc_t) xdr_int, (char *) &status,
  391. timeout);
  392. free (yppwd.oldpass);
  393. if (err) {
  394. _make_remark(pamh, ctrl, PAM_TEXT_INFO,
  395. clnt_sperrno(err));
  396. } else if (status) {
  397. D(("Error while changing NIS password.\n"));
  398. }
  399. D(("The password has%s been changed on %s.",
  400. (err || status) ? " not" : "", master));
  401. pam_syslog(pamh, LOG_NOTICE, "password%s changed for %s on %s",
  402. (err || status) ? " not" : "", pwd->pw_name, master);
  403. auth_destroy(clnt->cl_auth);
  404. clnt_destroy(clnt);
  405. if (err || status) {
  406. _make_remark(pamh, ctrl, PAM_TEXT_INFO,
  407. _("NIS password could not be changed."));
  408. retval = PAM_TRY_AGAIN;
  409. }
  410. #ifdef PAM_DEBUG
  411. sleep(5);
  412. #endif
  413. } else {
  414. retval = PAM_TRY_AGAIN;
  415. }
  416. #else
  417. if (on(UNIX_DEBUG, ctrl)) {
  418. pam_syslog(pamh, LOG_DEBUG, "No NIS support available");
  419. }
  420. retval = PAM_TRY_AGAIN;
  421. #endif
  422. }
  423. if (_unix_comesfromsource(pamh, forwho, 1, 0)) {
  424. if(unlocked) {
  425. if (lock_pwdf() != PAM_SUCCESS) {
  426. return PAM_AUTHTOK_LOCK_BUSY;
  427. }
  428. }
  429. #ifdef WITH_SELINUX
  430. if (unix_selinux_confined())
  431. return _unix_run_update_binary(pamh, ctrl, forwho, fromwhat, towhat, remember);
  432. #endif
  433. /* first, save old password */
  434. if (save_old_password(pamh, forwho, fromwhat, remember)) {
  435. retval = PAM_AUTHTOK_ERR;
  436. goto done;
  437. }
  438. if (on(UNIX_SHADOW, ctrl) || is_pwd_shadowed(pwd)) {
  439. retval = unix_update_shadow(pamh, forwho, towhat);
  440. if (retval == PAM_SUCCESS)
  441. if (!is_pwd_shadowed(pwd))
  442. retval = unix_update_passwd(pamh, forwho, "x");
  443. } else {
  444. retval = unix_update_passwd(pamh, forwho, towhat);
  445. }
  446. }
  447. done:
  448. unlock_pwdf();
  449. return retval;
  450. }
  451. static int _unix_verify_shadow(pam_handle_t *pamh, const char *user, unsigned long long ctrl)
  452. {
  453. struct passwd *pwent = NULL; /* Password and shadow password */
  454. struct spwd *spent = NULL; /* file entries for the user */
  455. int daysleft;
  456. int retval;
  457. retval = get_account_info(pamh, user, &pwent, &spent);
  458. if (retval == PAM_USER_UNKNOWN) {
  459. return retval;
  460. }
  461. if (retval == PAM_SUCCESS && spent == NULL)
  462. return PAM_SUCCESS;
  463. if (retval == PAM_UNIX_RUN_HELPER) {
  464. retval = _unix_run_verify_binary(pamh, ctrl, user, &daysleft);
  465. if (retval == PAM_AUTH_ERR || retval == PAM_USER_UNKNOWN)
  466. return retval;
  467. }
  468. else if (retval == PAM_SUCCESS)
  469. retval = check_shadow_expiry(pamh, spent, &daysleft);
  470. if (on(UNIX__IAMROOT, ctrl) || retval == PAM_NEW_AUTHTOK_REQD)
  471. return PAM_SUCCESS;
  472. return retval;
  473. }
  474. static int _pam_unix_approve_pass(pam_handle_t * pamh
  475. ,unsigned long long ctrl
  476. ,const char *pass_old
  477. ,const char *pass_new,
  478. int pass_min_len)
  479. {
  480. const void *user;
  481. const char *remark = NULL;
  482. int retval = PAM_SUCCESS;
  483. D(("&new=%p, &old=%p", pass_old, pass_new));
  484. D(("new=[%s]", pass_new));
  485. D(("old=[%s]", pass_old));
  486. if (pass_new == NULL || (pass_old && !strcmp(pass_old, pass_new))) {
  487. if (on(UNIX_DEBUG, ctrl)) {
  488. pam_syslog(pamh, LOG_DEBUG, "bad authentication token");
  489. }
  490. _make_remark(pamh, ctrl, PAM_ERROR_MSG, pass_new == NULL ?
  491. _("No password has been supplied.") :
  492. _("The password has not been changed."));
  493. return PAM_AUTHTOK_ERR;
  494. }
  495. /*
  496. * if one wanted to hardwire authentication token strength
  497. * checking this would be the place - AGM
  498. */
  499. retval = pam_get_item(pamh, PAM_USER, &user);
  500. if (retval != PAM_SUCCESS) {
  501. if (on(UNIX_DEBUG, ctrl)) {
  502. pam_syslog(pamh, LOG_ERR, "Can not get username");
  503. return PAM_AUTHTOK_ERR;
  504. }
  505. }
  506. if (strlen(pass_new) > PAM_MAX_RESP_SIZE) {
  507. remark = _("You must choose a shorter password.");
  508. D(("length exceeded [%s]", remark));
  509. } else if (off(UNIX__IAMROOT, ctrl)) {
  510. if ((int)strlen(pass_new) < pass_min_len)
  511. remark = _("You must choose a longer password.");
  512. D(("length check [%s]", remark));
  513. if (on(UNIX_REMEMBER_PASSWD, ctrl)) {
  514. if ((retval = check_old_password(user, pass_new)) == PAM_AUTHTOK_ERR)
  515. remark = _("Password has been already used. Choose another.");
  516. if (retval == PAM_ABORT) {
  517. pam_syslog(pamh, LOG_ERR, "can't open %s file to check old passwords",
  518. OLD_PASSWORDS_FILE);
  519. return retval;
  520. }
  521. }
  522. }
  523. if (remark) {
  524. _make_remark(pamh, ctrl, PAM_ERROR_MSG, remark);
  525. retval = PAM_AUTHTOK_ERR;
  526. }
  527. return retval;
  528. }
  529. int
  530. pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv)
  531. {
  532. unsigned long long ctrl, lctrl;
  533. int retval;
  534. int remember = -1;
  535. int rounds = 0;
  536. int pass_min_len = 0;
  537. /* <DO NOT free() THESE> */
  538. const char *user;
  539. const void *item;
  540. const char *pass_old, *pass_new;
  541. /* </DO NOT free() THESE> */
  542. D(("called."));
  543. ctrl = _set_ctrl(pamh, flags, &remember, &rounds, &pass_min_len,
  544. argc, argv);
  545. /*
  546. * First get the name of a user
  547. */
  548. retval = pam_get_user(pamh, &user, NULL);
  549. if (retval == PAM_SUCCESS) {
  550. /*
  551. * Various libraries at various times have had bugs related to
  552. * '+' or '-' as the first character of a user name. Don't
  553. * allow them.
  554. */
  555. if (user[0] == '-' || user[0] == '+') {
  556. pam_syslog(pamh, LOG_NOTICE, "bad username [%s]", user);
  557. return PAM_USER_UNKNOWN;
  558. }
  559. if (retval == PAM_SUCCESS && on(UNIX_DEBUG, ctrl))
  560. pam_syslog(pamh, LOG_DEBUG, "username [%s] obtained",
  561. user);
  562. } else {
  563. if (on(UNIX_DEBUG, ctrl))
  564. pam_syslog(pamh, LOG_DEBUG,
  565. "password - could not identify user");
  566. return retval;
  567. }
  568. D(("Got username of %s", user));
  569. /*
  570. * Before we do anything else, check to make sure that the user's
  571. * info is in one of the databases we can modify from this module,
  572. * which currently is 'files' and 'nis'. We have to do this because
  573. * getpwnam() doesn't tell you *where* the information it gives you
  574. * came from, nor should it. That's our job.
  575. */
  576. if (_unix_comesfromsource(pamh, user, 1, on(UNIX_NIS, ctrl)) == 0) {
  577. pam_syslog(pamh, LOG_DEBUG,
  578. "user \"%s\" does not exist in /etc/passwd%s",
  579. user, on(UNIX_NIS, ctrl) ? " or NIS" : "");
  580. return PAM_USER_UNKNOWN;
  581. } else {
  582. struct passwd *pwd;
  583. _unix_getpwnam(pamh, user, 1, 1, &pwd);
  584. if (pwd == NULL) {
  585. pam_syslog(pamh, LOG_DEBUG,
  586. "user \"%s\" has corrupted passwd entry",
  587. user);
  588. return PAM_USER_UNKNOWN;
  589. }
  590. }
  591. /*
  592. * This is not an AUTH module!
  593. */
  594. if (on(UNIX__NONULL, ctrl))
  595. set(UNIX__NULLOK, ctrl);
  596. if (on(UNIX__PRELIM, ctrl)) {
  597. /*
  598. * obtain and verify the current password (OLDAUTHTOK) for
  599. * the user.
  600. */
  601. D(("prelim check"));
  602. if (_unix_blankpasswd(pamh, ctrl, user)) {
  603. return PAM_SUCCESS;
  604. } else if (off(UNIX__IAMROOT, ctrl) ||
  605. (on(UNIX_NIS, ctrl) && _unix_comesfromsource(pamh, user, 0, 1))) {
  606. /* instruct user what is happening */
  607. if (off(UNIX__QUIET, ctrl)) {
  608. retval = pam_info(pamh, _("Changing password for %s."), user);
  609. if (retval != PAM_SUCCESS)
  610. return retval;
  611. }
  612. retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &pass_old, NULL);
  613. if (retval != PAM_SUCCESS) {
  614. pam_syslog(pamh, LOG_NOTICE,
  615. "password - (old) token not obtained");
  616. return retval;
  617. }
  618. /* verify that this is the password for this user */
  619. retval = _unix_verify_password(pamh, user, pass_old, ctrl);
  620. } else {
  621. D(("process run by root so do nothing this time around"));
  622. pass_old = NULL;
  623. retval = PAM_SUCCESS; /* root doesn't have too */
  624. }
  625. if (retval != PAM_SUCCESS) {
  626. D(("Authentication failed"));
  627. pass_old = NULL;
  628. return retval;
  629. }
  630. pass_old = NULL;
  631. retval = _unix_verify_shadow(pamh,user, ctrl);
  632. if (retval == PAM_AUTHTOK_ERR) {
  633. if (off(UNIX__IAMROOT, ctrl))
  634. _make_remark(pamh, ctrl, PAM_ERROR_MSG,
  635. _("You must wait longer to change your password."));
  636. else
  637. retval = PAM_SUCCESS;
  638. }
  639. } else if (on(UNIX__UPDATE, ctrl)) {
  640. /*
  641. * tpass is used below to store the _pam_md() return; it
  642. * should be _pam_delete()'d.
  643. */
  644. char *tpass = NULL;
  645. int retry = 0;
  646. /*
  647. * obtain the proposed password
  648. */
  649. D(("do update"));
  650. /*
  651. * get the old token back. NULL was ok only if root [at this
  652. * point we assume that this has already been enforced on a
  653. * previous call to this function].
  654. */
  655. retval = pam_get_item(pamh, PAM_OLDAUTHTOK, &item);
  656. if (retval != PAM_SUCCESS) {
  657. pam_syslog(pamh, LOG_NOTICE, "user not authenticated");
  658. return retval;
  659. }
  660. pass_old = item;
  661. D(("pass_old [%s]", pass_old));
  662. D(("get new password now"));
  663. lctrl = ctrl;
  664. if (on(UNIX_USE_AUTHTOK, lctrl)) {
  665. set(UNIX_USE_FIRST_PASS, lctrl);
  666. }
  667. if (on(UNIX_USE_FIRST_PASS, lctrl)) {
  668. retry = MAX_PASSWD_TRIES-1;
  669. }
  670. retval = PAM_AUTHTOK_ERR;
  671. while ((retval != PAM_SUCCESS) && (retry++ < MAX_PASSWD_TRIES)) {
  672. /*
  673. * use_authtok is to force the use of a previously entered
  674. * password -- needed for pluggable password strength checking
  675. */
  676. retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass_new, NULL);
  677. if (retval != PAM_SUCCESS) {
  678. if (on(UNIX_DEBUG, ctrl)) {
  679. pam_syslog(pamh, LOG_ERR,
  680. "password - new password not obtained");
  681. }
  682. pass_old = NULL; /* tidy up */
  683. return retval;
  684. }
  685. D(("returned to _unix_chauthtok"));
  686. /*
  687. * At this point we know who the user is and what they
  688. * propose as their new password. Verify that the new
  689. * password is acceptable.
  690. */
  691. if (*(const char *)pass_new == '\0') { /* "\0" password = NULL */
  692. pass_new = NULL;
  693. }
  694. retval = _pam_unix_approve_pass(pamh, ctrl, pass_old,
  695. pass_new, pass_min_len);
  696. if (retval != PAM_SUCCESS) {
  697. pam_set_item(pamh, PAM_AUTHTOK, NULL);
  698. }
  699. }
  700. if (retval != PAM_SUCCESS) {
  701. pam_syslog(pamh, LOG_NOTICE,
  702. "new password not acceptable");
  703. pass_new = pass_old = NULL; /* tidy up */
  704. return retval;
  705. }
  706. if (lock_pwdf() != PAM_SUCCESS) {
  707. return PAM_AUTHTOK_LOCK_BUSY;
  708. }
  709. if (pass_old) {
  710. retval = _unix_verify_password(pamh, user, pass_old, ctrl);
  711. if (retval != PAM_SUCCESS) {
  712. pam_syslog(pamh, LOG_NOTICE, "user password changed by another process");
  713. unlock_pwdf();
  714. return retval;
  715. }
  716. }
  717. retval = _unix_verify_shadow(pamh, user, ctrl);
  718. if (retval != PAM_SUCCESS) {
  719. pam_syslog(pamh, LOG_NOTICE, "user shadow entry expired");
  720. unlock_pwdf();
  721. return retval;
  722. }
  723. retval = _pam_unix_approve_pass(pamh, ctrl, pass_old, pass_new,
  724. pass_min_len);
  725. if (retval != PAM_SUCCESS) {
  726. pam_syslog(pamh, LOG_NOTICE,
  727. "new password not acceptable 2");
  728. pass_new = pass_old = NULL; /* tidy up */
  729. unlock_pwdf();
  730. return retval;
  731. }
  732. /*
  733. * By reaching here we have approved the passwords and must now
  734. * rebuild the password database file.
  735. */
  736. /*
  737. * First we encrypt the new password.
  738. */
  739. tpass = create_password_hash(pamh, pass_new, ctrl, rounds);
  740. if (tpass == NULL) {
  741. pam_syslog(pamh, LOG_CRIT,
  742. "crypt() failure or out of memory for password");
  743. pass_new = pass_old = NULL; /* tidy up */
  744. unlock_pwdf();
  745. return PAM_BUF_ERR;
  746. }
  747. D(("password processed"));
  748. /* update the password database(s) -- race conditions..? */
  749. retval = _do_setpass(pamh, user, pass_old, tpass, ctrl,
  750. remember);
  751. /* _do_setpass has called unlock_pwdf for us */
  752. _pam_delete(tpass);
  753. pass_old = pass_new = NULL;
  754. } else { /* something has broken with the module */
  755. pam_syslog(pamh, LOG_CRIT,
  756. "password received unknown request");
  757. retval = PAM_ABORT;
  758. }
  759. D(("retval was %d", retval));
  760. return retval;
  761. }