auth-pam.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*-
  2. * Copyright (c) 2002 Networks Associates Technology, Inc.
  3. * All rights reserved.
  4. *
  5. * This software was developed for the FreeBSD Project by ThinkSec AS and
  6. * NAI Labs, the Security Research Division of Network Associates, Inc.
  7. * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
  8. * DARPA CHATS research program.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  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. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org>
  33. * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au>
  34. *
  35. * Permission to use, copy, modify, and distribute this software for any
  36. * purpose with or without fee is hereby granted, provided that the above
  37. * copyright notice and this permission notice appear in all copies.
  38. *
  39. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  40. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  41. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  42. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  43. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  44. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  45. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  46. */
  47. /* Based on FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des */
  48. #include "includes.h"
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <sys/wait.h>
  52. #include <errno.h>
  53. #include <signal.h>
  54. #include <stdarg.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #ifdef USE_PAM
  59. #if defined(HAVE_SECURITY_PAM_APPL_H)
  60. #include <security/pam_appl.h>
  61. #elif defined (HAVE_PAM_PAM_APPL_H)
  62. #include <pam/pam_appl.h>
  63. #endif
  64. #if !defined(SSHD_PAM_SERVICE)
  65. extern char *__progname;
  66. # define SSHD_PAM_SERVICE __progname
  67. #endif
  68. /* OpenGroup RFC86.0 and XSSO specify no "const" on arguments */
  69. #ifdef PAM_SUN_CODEBASE
  70. # define sshpam_const /* Solaris, HP-UX, SunOS */
  71. #else
  72. # define sshpam_const const /* LinuxPAM, OpenPAM, AIX */
  73. #endif
  74. /* Ambiguity in spec: is it an array of pointers or a pointer to an array? */
  75. #ifdef PAM_SUN_CODEBASE
  76. # define PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
  77. #else
  78. # define PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
  79. #endif
  80. #include "xmalloc.h"
  81. #include "sshbuf.h"
  82. #include "ssherr.h"
  83. #include "hostfile.h"
  84. #include "auth.h"
  85. #include "auth-pam.h"
  86. #include "canohost.h"
  87. #include "log.h"
  88. #include "msg.h"
  89. #include "packet.h"
  90. #include "misc.h"
  91. #include "servconf.h"
  92. #include "ssh2.h"
  93. #include "auth-options.h"
  94. #include "misc.h"
  95. #ifdef GSSAPI
  96. #include "ssh-gss.h"
  97. #endif
  98. #include "monitor_wrap.h"
  99. extern ServerOptions options;
  100. extern struct sshbuf *loginmsg;
  101. extern u_int utmp_len;
  102. /* so we don't silently change behaviour */
  103. #ifdef USE_POSIX_THREADS
  104. # error "USE_POSIX_THREADS replaced by UNSUPPORTED_POSIX_THREADS_HACK"
  105. #endif
  106. /*
  107. * Formerly known as USE_POSIX_THREADS, using this is completely unsupported
  108. * and generally a bad idea. Use at own risk and do not expect support if
  109. * this breaks.
  110. */
  111. #ifdef UNSUPPORTED_POSIX_THREADS_HACK
  112. #include <pthread.h>
  113. /*
  114. * Avoid namespace clash when *not* using pthreads for systems *with*
  115. * pthreads, which unconditionally define pthread_t via sys/types.h
  116. * (e.g. Linux)
  117. */
  118. typedef pthread_t sp_pthread_t;
  119. #else
  120. typedef pid_t sp_pthread_t;
  121. #define pthread_exit fake_pthread_exit
  122. #define pthread_create fake_pthread_create
  123. #define pthread_cancel fake_pthread_cancel
  124. #define pthread_join fake_pthread_join
  125. #endif
  126. struct pam_ctxt {
  127. sp_pthread_t pam_thread;
  128. int pam_psock;
  129. int pam_csock;
  130. int pam_done;
  131. };
  132. static void sshpam_free_ctx(void *);
  133. static struct pam_ctxt *cleanup_ctxt;
  134. #ifndef UNSUPPORTED_POSIX_THREADS_HACK
  135. /*
  136. * Simulate threads with processes.
  137. */
  138. static int sshpam_thread_status = -1;
  139. static sshsig_t sshpam_oldsig;
  140. static void
  141. sshpam_sigchld_handler(int sig)
  142. {
  143. ssh_signal(SIGCHLD, SIG_DFL);
  144. if (cleanup_ctxt == NULL)
  145. return; /* handler called after PAM cleanup, shouldn't happen */
  146. if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG)
  147. <= 0) {
  148. /* PAM thread has not exitted, privsep slave must have */
  149. kill(cleanup_ctxt->pam_thread, SIGTERM);
  150. while (waitpid(cleanup_ctxt->pam_thread,
  151. &sshpam_thread_status, 0) == -1) {
  152. if (errno == EINTR)
  153. continue;
  154. return;
  155. }
  156. }
  157. if (WIFSIGNALED(sshpam_thread_status) &&
  158. WTERMSIG(sshpam_thread_status) == SIGTERM)
  159. return; /* terminated by pthread_cancel */
  160. if (!WIFEXITED(sshpam_thread_status))
  161. sigdie("PAM: authentication thread exited unexpectedly");
  162. if (WEXITSTATUS(sshpam_thread_status) != 0)
  163. sigdie("PAM: authentication thread exited uncleanly");
  164. }
  165. /* ARGSUSED */
  166. static void
  167. pthread_exit(void *value)
  168. {
  169. _exit(0);
  170. }
  171. /* ARGSUSED */
  172. static int
  173. pthread_create(sp_pthread_t *thread, const void *attr,
  174. void *(*thread_start)(void *), void *arg)
  175. {
  176. pid_t pid;
  177. struct pam_ctxt *ctx = arg;
  178. sshpam_thread_status = -1;
  179. switch ((pid = fork())) {
  180. case -1:
  181. error("fork(): %s", strerror(errno));
  182. return errno;
  183. case 0:
  184. close(ctx->pam_psock);
  185. ctx->pam_psock = -1;
  186. thread_start(arg);
  187. _exit(1);
  188. default:
  189. *thread = pid;
  190. close(ctx->pam_csock);
  191. ctx->pam_csock = -1;
  192. sshpam_oldsig = ssh_signal(SIGCHLD, sshpam_sigchld_handler);
  193. return (0);
  194. }
  195. }
  196. static int
  197. pthread_cancel(sp_pthread_t thread)
  198. {
  199. ssh_signal(SIGCHLD, sshpam_oldsig);
  200. return (kill(thread, SIGTERM));
  201. }
  202. /* ARGSUSED */
  203. static int
  204. pthread_join(sp_pthread_t thread, void **value)
  205. {
  206. int status;
  207. if (sshpam_thread_status != -1)
  208. return (sshpam_thread_status);
  209. ssh_signal(SIGCHLD, sshpam_oldsig);
  210. while (waitpid(thread, &status, 0) == -1) {
  211. if (errno == EINTR)
  212. continue;
  213. fatal("%s: waitpid: %s", __func__, strerror(errno));
  214. }
  215. return (status);
  216. }
  217. #endif
  218. static pam_handle_t *sshpam_handle = NULL;
  219. static int sshpam_err = 0;
  220. static int sshpam_authenticated = 0;
  221. static int sshpam_session_open = 0;
  222. static int sshpam_cred_established = 0;
  223. static int sshpam_account_status = -1;
  224. static int sshpam_maxtries_reached = 0;
  225. static char **sshpam_env = NULL;
  226. static Authctxt *sshpam_authctxt = NULL;
  227. static const char *sshpam_password = NULL;
  228. static char *sshpam_rhost = NULL;
  229. static char *sshpam_laddr = NULL;
  230. static char *sshpam_conninfo = NULL;
  231. /* Some PAM implementations don't implement this */
  232. #ifndef HAVE_PAM_GETENVLIST
  233. static char **
  234. pam_getenvlist(pam_handle_t *pamh)
  235. {
  236. /*
  237. * XXX - If necessary, we can still support environment passing
  238. * for platforms without pam_getenvlist by searching for known
  239. * env vars (e.g. KRB5CCNAME) from the PAM environment.
  240. */
  241. return NULL;
  242. }
  243. #endif
  244. #ifndef HAVE_PAM_PUTENV
  245. static int
  246. pam_putenv(pam_handle_t *pamh, const char *name_value)
  247. {
  248. return PAM_SUCCESS;
  249. }
  250. #endif /* HAVE_PAM_PUTENV */
  251. /*
  252. * Some platforms, notably Solaris, do not enforce password complexity
  253. * rules during pam_chauthtok() if the real uid of the calling process
  254. * is 0, on the assumption that it's being called by "passwd" run by root.
  255. * This wraps pam_chauthtok and sets/restore the real uid so PAM will do
  256. * the right thing.
  257. */
  258. #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID
  259. static int
  260. sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags)
  261. {
  262. int result;
  263. if (sshpam_authctxt == NULL)
  264. fatal("PAM: sshpam_authctxt not initialized");
  265. if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1)
  266. fatal("%s: setreuid failed: %s", __func__, strerror(errno));
  267. result = pam_chauthtok(pamh, flags);
  268. if (setreuid(0, -1) == -1)
  269. fatal("%s: setreuid failed: %s", __func__, strerror(errno));
  270. return result;
  271. }
  272. # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b)))
  273. #endif
  274. static void
  275. sshpam_password_change_required(int reqd)
  276. {
  277. extern struct sshauthopt *auth_opts;
  278. static int saved_port, saved_agent, saved_x11;
  279. debug3("%s %d", __func__, reqd);
  280. if (sshpam_authctxt == NULL)
  281. fatal("%s: PAM authctxt not initialized", __func__);
  282. sshpam_authctxt->force_pwchange = reqd;
  283. if (reqd) {
  284. saved_port = auth_opts->permit_port_forwarding_flag;
  285. saved_agent = auth_opts->permit_agent_forwarding_flag;
  286. saved_x11 = auth_opts->permit_x11_forwarding_flag;
  287. auth_opts->permit_port_forwarding_flag = 0;
  288. auth_opts->permit_agent_forwarding_flag = 0;
  289. auth_opts->permit_x11_forwarding_flag = 0;
  290. } else {
  291. if (saved_port)
  292. auth_opts->permit_port_forwarding_flag = saved_port;
  293. if (saved_agent)
  294. auth_opts->permit_agent_forwarding_flag = saved_agent;
  295. if (saved_x11)
  296. auth_opts->permit_x11_forwarding_flag = saved_x11;
  297. }
  298. }
  299. /* Import regular and PAM environment from subprocess */
  300. static void
  301. import_environments(struct sshbuf *b)
  302. {
  303. char *env;
  304. u_int n, i, num_env;
  305. int r;
  306. debug3("PAM: %s entering", __func__);
  307. #ifndef UNSUPPORTED_POSIX_THREADS_HACK
  308. /* Import variables set by do_pam_account */
  309. if ((r = sshbuf_get_u32(b, &n)) != 0)
  310. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  311. if (n > INT_MAX)
  312. fatal("%s: invalid PAM account status %u", __func__, n);
  313. sshpam_account_status = (int)n;
  314. if ((r = sshbuf_get_u32(b, &n)) != 0)
  315. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  316. sshpam_password_change_required(n != 0);
  317. /* Import environment from subprocess */
  318. if ((r = sshbuf_get_u32(b, &num_env)) != 0)
  319. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  320. if (num_env > 1024)
  321. fatal("%s: received %u environment variables, expected <= 1024",
  322. __func__, num_env);
  323. sshpam_env = xcalloc(num_env + 1, sizeof(*sshpam_env));
  324. debug3("PAM: num env strings %d", num_env);
  325. for(i = 0; i < num_env; i++) {
  326. if ((r = sshbuf_get_cstring(b, &(sshpam_env[i]), NULL)) != 0)
  327. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  328. }
  329. sshpam_env[num_env] = NULL;
  330. /* Import PAM environment from subprocess */
  331. if ((r = sshbuf_get_u32(b, &num_env)) != 0)
  332. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  333. debug("PAM: num PAM env strings %d", num_env);
  334. for (i = 0; i < num_env; i++) {
  335. if ((r = sshbuf_get_cstring(b, &env, NULL)) != 0)
  336. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  337. /* Errors are not fatal here */
  338. if ((r = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) {
  339. error("PAM: pam_putenv: %s",
  340. pam_strerror(sshpam_handle, r));
  341. }
  342. /*
  343. * XXX this possibly leaks env because it is not documented
  344. * what pam_putenv() does with it. Does it copy it? Does it
  345. * take ownweship? We don't know, so it's safest just to leak.
  346. */
  347. }
  348. #endif
  349. }
  350. /*
  351. * Conversation function for authentication thread.
  352. */
  353. static int
  354. sshpam_thread_conv(int n, sshpam_const struct pam_message **msg,
  355. struct pam_response **resp, void *data)
  356. {
  357. struct sshbuf *buffer;
  358. struct pam_ctxt *ctxt;
  359. struct pam_response *reply;
  360. int r, i;
  361. u_char status;
  362. debug3("PAM: %s entering, %d messages", __func__, n);
  363. *resp = NULL;
  364. if (data == NULL) {
  365. error("PAM: conversation function passed a null context");
  366. return (PAM_CONV_ERR);
  367. }
  368. ctxt = data;
  369. if (n <= 0 || n > PAM_MAX_NUM_MSG)
  370. return (PAM_CONV_ERR);
  371. if ((reply = calloc(n, sizeof(*reply))) == NULL)
  372. return PAM_CONV_ERR;
  373. if ((buffer = sshbuf_new()) == NULL) {
  374. free(reply);
  375. return PAM_CONV_ERR;
  376. }
  377. for (i = 0; i < n; ++i) {
  378. switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
  379. case PAM_PROMPT_ECHO_OFF:
  380. case PAM_PROMPT_ECHO_ON:
  381. if ((r = sshbuf_put_cstring(buffer,
  382. PAM_MSG_MEMBER(msg, i, msg))) != 0)
  383. fatal("%s: buffer error: %s",
  384. __func__, ssh_err(r));
  385. if (ssh_msg_send(ctxt->pam_csock,
  386. PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
  387. goto fail;
  388. if (ssh_msg_recv(ctxt->pam_csock, buffer) == -1)
  389. goto fail;
  390. if ((r = sshbuf_get_u8(buffer, &status)) != 0)
  391. fatal("%s: buffer error: %s",
  392. __func__, ssh_err(r));
  393. if (status != PAM_AUTHTOK)
  394. goto fail;
  395. if ((r = sshbuf_get_cstring(buffer,
  396. &reply[i].resp, NULL)) != 0)
  397. fatal("%s: buffer error: %s",
  398. __func__, ssh_err(r));
  399. break;
  400. case PAM_ERROR_MSG:
  401. case PAM_TEXT_INFO:
  402. if ((r = sshbuf_put_cstring(buffer,
  403. PAM_MSG_MEMBER(msg, i, msg))) != 0)
  404. fatal("%s: buffer error: %s",
  405. __func__, ssh_err(r));
  406. if (ssh_msg_send(ctxt->pam_csock,
  407. PAM_MSG_MEMBER(msg, i, msg_style), buffer) == -1)
  408. goto fail;
  409. break;
  410. default:
  411. goto fail;
  412. }
  413. sshbuf_reset(buffer);
  414. }
  415. sshbuf_free(buffer);
  416. *resp = reply;
  417. return (PAM_SUCCESS);
  418. fail:
  419. for(i = 0; i < n; i++) {
  420. free(reply[i].resp);
  421. }
  422. free(reply);
  423. sshbuf_free(buffer);
  424. return (PAM_CONV_ERR);
  425. }
  426. /*
  427. * Authentication thread.
  428. */
  429. static void *
  430. sshpam_thread(void *ctxtp)
  431. {
  432. struct pam_ctxt *ctxt = ctxtp;
  433. struct sshbuf *buffer = NULL;
  434. struct pam_conv sshpam_conv;
  435. int r, flags = (options.permit_empty_passwd == 0 ?
  436. PAM_DISALLOW_NULL_AUTHTOK : 0);
  437. #ifndef UNSUPPORTED_POSIX_THREADS_HACK
  438. extern char **environ;
  439. char **env_from_pam;
  440. u_int i;
  441. const char *pam_user;
  442. const char **ptr_pam_user = &pam_user;
  443. char *tz = getenv("TZ");
  444. sshpam_err = pam_get_item(sshpam_handle, PAM_USER,
  445. (sshpam_const void **)ptr_pam_user);
  446. if (sshpam_err != PAM_SUCCESS)
  447. goto auth_fail;
  448. environ[0] = NULL;
  449. if (tz != NULL)
  450. if (setenv("TZ", tz, 1) == -1)
  451. error("PAM: could not set TZ environment: %s",
  452. strerror(errno));
  453. if (sshpam_authctxt != NULL) {
  454. setproctitle("%s [pam]",
  455. sshpam_authctxt->valid ? pam_user : "unknown");
  456. }
  457. #endif
  458. sshpam_conv.conv = sshpam_thread_conv;
  459. sshpam_conv.appdata_ptr = ctxt;
  460. if (sshpam_authctxt == NULL)
  461. fatal("%s: PAM authctxt not initialized", __func__);
  462. if ((buffer = sshbuf_new()) == NULL)
  463. fatal("%s: sshbuf_new failed", __func__);
  464. sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
  465. (const void *)&sshpam_conv);
  466. if (sshpam_err != PAM_SUCCESS)
  467. goto auth_fail;
  468. sshpam_err = pam_authenticate(sshpam_handle, flags);
  469. if (sshpam_err == PAM_MAXTRIES)
  470. sshpam_set_maxtries_reached(1);
  471. if (sshpam_err != PAM_SUCCESS)
  472. goto auth_fail;
  473. if (!do_pam_account()) {
  474. sshpam_err = PAM_ACCT_EXPIRED;
  475. goto auth_fail;
  476. }
  477. if (sshpam_authctxt->force_pwchange) {
  478. sshpam_err = pam_chauthtok(sshpam_handle,
  479. PAM_CHANGE_EXPIRED_AUTHTOK);
  480. if (sshpam_err != PAM_SUCCESS)
  481. goto auth_fail;
  482. sshpam_password_change_required(0);
  483. }
  484. if ((r = sshbuf_put_cstring(buffer, "OK")) != 0)
  485. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  486. #ifndef UNSUPPORTED_POSIX_THREADS_HACK
  487. /* Export variables set by do_pam_account */
  488. if ((r = sshbuf_put_u32(buffer, sshpam_account_status)) != 0 ||
  489. (r = sshbuf_put_u32(buffer, sshpam_authctxt->force_pwchange)) != 0)
  490. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  491. /* Export any environment strings set in child */
  492. for (i = 0; environ[i] != NULL; i++) {
  493. /* Count */
  494. if (i > INT_MAX)
  495. fatal("%s: too many environment strings", __func__);
  496. }
  497. if ((r = sshbuf_put_u32(buffer, i)) != 0)
  498. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  499. for (i = 0; environ[i] != NULL; i++) {
  500. if ((r = sshbuf_put_cstring(buffer, environ[i])) != 0)
  501. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  502. }
  503. /* Export any environment strings set by PAM in child */
  504. env_from_pam = pam_getenvlist(sshpam_handle);
  505. for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
  506. /* Count */
  507. if (i > INT_MAX)
  508. fatal("%s: too many PAM environment strings", __func__);
  509. }
  510. if ((r = sshbuf_put_u32(buffer, i)) != 0)
  511. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  512. for (i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) {
  513. if ((r = sshbuf_put_cstring(buffer, env_from_pam[i])) != 0)
  514. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  515. }
  516. #endif /* UNSUPPORTED_POSIX_THREADS_HACK */
  517. /* XXX - can't do much about an error here */
  518. ssh_msg_send(ctxt->pam_csock, sshpam_err, buffer);
  519. sshbuf_free(buffer);
  520. pthread_exit(NULL);
  521. auth_fail:
  522. if ((r = sshbuf_put_cstring(buffer,
  523. pam_strerror(sshpam_handle, sshpam_err))) != 0)
  524. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  525. /* XXX - can't do much about an error here */
  526. if (sshpam_err == PAM_ACCT_EXPIRED)
  527. ssh_msg_send(ctxt->pam_csock, PAM_ACCT_EXPIRED, buffer);
  528. else if (sshpam_maxtries_reached)
  529. ssh_msg_send(ctxt->pam_csock, PAM_MAXTRIES, buffer);
  530. else
  531. ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, buffer);
  532. sshbuf_free(buffer);
  533. pthread_exit(NULL);
  534. return (NULL); /* Avoid warning for non-pthread case */
  535. }
  536. void
  537. sshpam_thread_cleanup(void)
  538. {
  539. struct pam_ctxt *ctxt = cleanup_ctxt;
  540. debug3("PAM: %s entering", __func__);
  541. if (ctxt != NULL && ctxt->pam_thread != 0) {
  542. pthread_cancel(ctxt->pam_thread);
  543. pthread_join(ctxt->pam_thread, NULL);
  544. close(ctxt->pam_psock);
  545. close(ctxt->pam_csock);
  546. memset(ctxt, 0, sizeof(*ctxt));
  547. cleanup_ctxt = NULL;
  548. }
  549. }
  550. static int
  551. sshpam_null_conv(int n, sshpam_const struct pam_message **msg,
  552. struct pam_response **resp, void *data)
  553. {
  554. debug3("PAM: %s entering, %d messages", __func__, n);
  555. return (PAM_CONV_ERR);
  556. }
  557. static struct pam_conv null_conv = { sshpam_null_conv, NULL };
  558. static int
  559. sshpam_store_conv(int n, sshpam_const struct pam_message **msg,
  560. struct pam_response **resp, void *data)
  561. {
  562. struct pam_response *reply;
  563. int r, i;
  564. debug3("PAM: %s called with %d messages", __func__, n);
  565. *resp = NULL;
  566. if (n <= 0 || n > PAM_MAX_NUM_MSG)
  567. return (PAM_CONV_ERR);
  568. if ((reply = calloc(n, sizeof(*reply))) == NULL)
  569. return (PAM_CONV_ERR);
  570. for (i = 0; i < n; ++i) {
  571. switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
  572. case PAM_ERROR_MSG:
  573. case PAM_TEXT_INFO:
  574. if ((r = sshbuf_putf(loginmsg, "%s\n",
  575. PAM_MSG_MEMBER(msg, i, msg))) != 0)
  576. fatal("%s: buffer error: %s",
  577. __func__, ssh_err(r));
  578. reply[i].resp_retcode = PAM_SUCCESS;
  579. break;
  580. default:
  581. goto fail;
  582. }
  583. }
  584. *resp = reply;
  585. return (PAM_SUCCESS);
  586. fail:
  587. for(i = 0; i < n; i++) {
  588. free(reply[i].resp);
  589. }
  590. free(reply);
  591. return (PAM_CONV_ERR);
  592. }
  593. static struct pam_conv store_conv = { sshpam_store_conv, NULL };
  594. void
  595. sshpam_cleanup(void)
  596. {
  597. if (sshpam_handle == NULL || (use_privsep && !mm_is_monitor()))
  598. return;
  599. debug("PAM: cleanup");
  600. pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv);
  601. if (sshpam_session_open) {
  602. debug("PAM: closing session");
  603. pam_close_session(sshpam_handle, PAM_SILENT);
  604. sshpam_session_open = 0;
  605. }
  606. if (sshpam_cred_established) {
  607. debug("PAM: deleting credentials");
  608. pam_setcred(sshpam_handle, PAM_DELETE_CRED);
  609. sshpam_cred_established = 0;
  610. }
  611. sshpam_authenticated = 0;
  612. pam_end(sshpam_handle, sshpam_err);
  613. sshpam_handle = NULL;
  614. }
  615. static int
  616. sshpam_init(struct ssh *ssh, Authctxt *authctxt)
  617. {
  618. const char *pam_user, *user = authctxt->user;
  619. const char **ptr_pam_user = &pam_user;
  620. #if defined(PAM_SUN_CODEBASE) && defined(PAM_MAX_RESP_SIZE)
  621. /* Protect buggy PAM implementations from excessively long usernames */
  622. if (strlen(user) >= PAM_MAX_RESP_SIZE)
  623. fatal("Username too long from %s port %d",
  624. ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
  625. #endif
  626. if (sshpam_handle == NULL) {
  627. if (ssh == NULL) {
  628. fatal("%s: called initially with no "
  629. "packet context", __func__);
  630. }
  631. } if (sshpam_handle != NULL) {
  632. /* We already have a PAM context; check if the user matches */
  633. sshpam_err = pam_get_item(sshpam_handle,
  634. PAM_USER, (sshpam_const void **)ptr_pam_user);
  635. if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0)
  636. return (0);
  637. pam_end(sshpam_handle, sshpam_err);
  638. sshpam_handle = NULL;
  639. }
  640. debug("PAM: initializing for \"%s\"", user);
  641. sshpam_err =
  642. pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle);
  643. sshpam_authctxt = authctxt;
  644. if (sshpam_err != PAM_SUCCESS) {
  645. pam_end(sshpam_handle, sshpam_err);
  646. sshpam_handle = NULL;
  647. return (-1);
  648. }
  649. if (ssh != NULL && sshpam_rhost == NULL) {
  650. /*
  651. * We need to cache these as we don't have packet context
  652. * during the kbdint flow.
  653. */
  654. sshpam_rhost = xstrdup(auth_get_canonical_hostname(ssh,
  655. options.use_dns));
  656. sshpam_laddr = get_local_ipaddr(
  657. ssh_packet_get_connection_in(ssh));
  658. xasprintf(&sshpam_conninfo, "SSH_CONNECTION=%.50s %d %.50s %d",
  659. ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
  660. sshpam_laddr, ssh_local_port(ssh));
  661. }
  662. if (sshpam_rhost != NULL) {
  663. debug("PAM: setting PAM_RHOST to \"%s\"", sshpam_rhost);
  664. sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST,
  665. sshpam_rhost);
  666. if (sshpam_err != PAM_SUCCESS) {
  667. pam_end(sshpam_handle, sshpam_err);
  668. sshpam_handle = NULL;
  669. return (-1);
  670. }
  671. /* Put SSH_CONNECTION in the PAM environment too */
  672. pam_putenv(sshpam_handle, sshpam_conninfo);
  673. }
  674. #ifdef PAM_TTY_KLUDGE
  675. /*
  676. * Some silly PAM modules (e.g. pam_time) require a TTY to operate.
  677. * sshd doesn't set the tty until too late in the auth process and
  678. * may not even set one (for tty-less connections)
  679. */
  680. debug("PAM: setting PAM_TTY to \"ssh\"");
  681. sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh");
  682. if (sshpam_err != PAM_SUCCESS) {
  683. pam_end(sshpam_handle, sshpam_err);
  684. sshpam_handle = NULL;
  685. return (-1);
  686. }
  687. #endif
  688. return (0);
  689. }
  690. static void
  691. expose_authinfo(const char *caller)
  692. {
  693. char *auth_info;
  694. /*
  695. * Expose authentication information to PAM.
  696. * The environment variable is versioned. Please increment the
  697. * version suffix if the format of session_info changes.
  698. */
  699. if (sshpam_authctxt->session_info == NULL)
  700. auth_info = xstrdup("");
  701. else if ((auth_info = sshbuf_dup_string(
  702. sshpam_authctxt->session_info)) == NULL)
  703. fatal("%s: sshbuf_dup_string failed", __func__);
  704. debug2("%s: auth information in SSH_AUTH_INFO_0", caller);
  705. do_pam_putenv("SSH_AUTH_INFO_0", auth_info);
  706. free(auth_info);
  707. }
  708. static void *
  709. sshpam_init_ctx(Authctxt *authctxt)
  710. {
  711. struct pam_ctxt *ctxt;
  712. int result, socks[2];
  713. debug3("PAM: %s entering", __func__);
  714. /*
  715. * Refuse to start if we don't have PAM enabled or do_pam_account
  716. * has previously failed.
  717. */
  718. if (!options.use_pam || sshpam_account_status == 0)
  719. return NULL;
  720. /* Initialize PAM */
  721. if (sshpam_init(NULL, authctxt) == -1) {
  722. error("PAM: initialization failed");
  723. return (NULL);
  724. }
  725. expose_authinfo(__func__);
  726. ctxt = xcalloc(1, sizeof *ctxt);
  727. /* Start the authentication thread */
  728. if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) {
  729. error("PAM: failed create sockets: %s", strerror(errno));
  730. free(ctxt);
  731. return (NULL);
  732. }
  733. ctxt->pam_psock = socks[0];
  734. ctxt->pam_csock = socks[1];
  735. result = pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt);
  736. if (result != 0) {
  737. error("PAM: failed to start authentication thread: %s",
  738. strerror(result));
  739. close(socks[0]);
  740. close(socks[1]);
  741. free(ctxt);
  742. return (NULL);
  743. }
  744. cleanup_ctxt = ctxt;
  745. return (ctxt);
  746. }
  747. static int
  748. sshpam_query(void *ctx, char **name, char **info,
  749. u_int *num, char ***prompts, u_int **echo_on)
  750. {
  751. struct sshbuf *buffer;
  752. struct pam_ctxt *ctxt = ctx;
  753. size_t plen;
  754. u_char type;
  755. char *msg;
  756. size_t len, mlen;
  757. int r;
  758. debug3("PAM: %s entering", __func__);
  759. if ((buffer = sshbuf_new()) == NULL)
  760. fatal("%s: sshbuf_new failed", __func__);
  761. *name = xstrdup("");
  762. *info = xstrdup("");
  763. *prompts = xmalloc(sizeof(char *));
  764. **prompts = NULL;
  765. plen = 0;
  766. *echo_on = xmalloc(sizeof(u_int));
  767. while (ssh_msg_recv(ctxt->pam_psock, buffer) == 0) {
  768. if ((r = sshbuf_get_u8(buffer, &type)) != 0 ||
  769. (r = sshbuf_get_cstring(buffer, &msg, &mlen)) != 0)
  770. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  771. switch (type) {
  772. case PAM_PROMPT_ECHO_ON:
  773. case PAM_PROMPT_ECHO_OFF:
  774. *num = 1;
  775. len = plen + mlen + 1;
  776. **prompts = xreallocarray(**prompts, 1, len);
  777. strlcpy(**prompts + plen, msg, len - plen);
  778. plen += mlen;
  779. **echo_on = (type == PAM_PROMPT_ECHO_ON);
  780. free(msg);
  781. sshbuf_free(buffer);
  782. return (0);
  783. case PAM_ERROR_MSG:
  784. case PAM_TEXT_INFO:
  785. /* accumulate messages */
  786. len = plen + mlen + 2;
  787. **prompts = xreallocarray(**prompts, 1, len);
  788. strlcpy(**prompts + plen, msg, len - plen);
  789. plen += mlen;
  790. strlcat(**prompts + plen, "\n", len - plen);
  791. plen++;
  792. free(msg);
  793. break;
  794. case PAM_ACCT_EXPIRED:
  795. case PAM_MAXTRIES:
  796. if (type == PAM_ACCT_EXPIRED)
  797. sshpam_account_status = 0;
  798. if (type == PAM_MAXTRIES)
  799. sshpam_set_maxtries_reached(1);
  800. /* FALLTHROUGH */
  801. case PAM_AUTH_ERR:
  802. debug3("PAM: %s", pam_strerror(sshpam_handle, type));
  803. if (**prompts != NULL && strlen(**prompts) != 0) {
  804. *info = **prompts;
  805. **prompts = NULL;
  806. *num = 0;
  807. **echo_on = 0;
  808. ctxt->pam_done = -1;
  809. free(msg);
  810. sshbuf_free(buffer);
  811. return 0;
  812. }
  813. /* FALLTHROUGH */
  814. case PAM_SUCCESS:
  815. if (**prompts != NULL) {
  816. /* drain any accumulated messages */
  817. debug("PAM: %s", **prompts);
  818. if ((r = sshbuf_put(loginmsg, **prompts,
  819. strlen(**prompts))) != 0)
  820. fatal("%s: buffer error: %s",
  821. __func__, ssh_err(r));
  822. free(**prompts);
  823. **prompts = NULL;
  824. }
  825. if (type == PAM_SUCCESS) {
  826. if (!sshpam_authctxt->valid ||
  827. (sshpam_authctxt->pw->pw_uid == 0 &&
  828. options.permit_root_login != PERMIT_YES))
  829. fatal("Internal error: PAM auth "
  830. "succeeded when it should have "
  831. "failed");
  832. import_environments(buffer);
  833. *num = 0;
  834. **echo_on = 0;
  835. ctxt->pam_done = 1;
  836. free(msg);
  837. sshbuf_free(buffer);
  838. return (0);
  839. }
  840. error("PAM: %s for %s%.100s from %.100s", msg,
  841. sshpam_authctxt->valid ? "" : "illegal user ",
  842. sshpam_authctxt->user, sshpam_rhost);
  843. /* FALLTHROUGH */
  844. default:
  845. *num = 0;
  846. **echo_on = 0;
  847. free(msg);
  848. ctxt->pam_done = -1;
  849. sshbuf_free(buffer);
  850. return (-1);
  851. }
  852. }
  853. sshbuf_free(buffer);
  854. return (-1);
  855. }
  856. /*
  857. * Returns a junk password of identical length to that the user supplied.
  858. * Used to mitigate timing attacks against crypt(3)/PAM stacks that
  859. * vary processing time in proportion to password length.
  860. */
  861. static char *
  862. fake_password(const char *wire_password)
  863. {
  864. const char junk[] = "\b\n\r\177INCORRECT";
  865. char *ret = NULL;
  866. size_t i, l = wire_password != NULL ? strlen(wire_password) : 0;
  867. if (l >= INT_MAX)
  868. fatal("%s: password length too long: %zu", __func__, l);
  869. ret = malloc(l + 1);
  870. if (ret == NULL)
  871. return NULL;
  872. for (i = 0; i < l; i++)
  873. ret[i] = junk[i % (sizeof(junk) - 1)];
  874. ret[i] = '\0';
  875. return ret;
  876. }
  877. /* XXX - see also comment in auth-chall.c:verify_response */
  878. static int
  879. sshpam_respond(void *ctx, u_int num, char **resp)
  880. {
  881. struct sshbuf *buffer;
  882. struct pam_ctxt *ctxt = ctx;
  883. char *fake;
  884. int r;
  885. debug2("PAM: %s entering, %u responses", __func__, num);
  886. switch (ctxt->pam_done) {
  887. case 1:
  888. sshpam_authenticated = 1;
  889. return (0);
  890. case 0:
  891. break;
  892. default:
  893. return (-1);
  894. }
  895. if (num != 1) {
  896. error("PAM: expected one response, got %u", num);
  897. return (-1);
  898. }
  899. if ((buffer = sshbuf_new()) == NULL)
  900. fatal("%s: sshbuf_new failed", __func__);
  901. if (sshpam_authctxt->valid &&
  902. (sshpam_authctxt->pw->pw_uid != 0 ||
  903. options.permit_root_login == PERMIT_YES)) {
  904. if ((r = sshbuf_put_cstring(buffer, *resp)) != 0)
  905. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  906. } else {
  907. fake = fake_password(*resp);
  908. if ((r = sshbuf_put_cstring(buffer, fake)) != 0)
  909. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  910. free(fake);
  911. }
  912. if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, buffer) == -1) {
  913. sshbuf_free(buffer);
  914. return (-1);
  915. }
  916. sshbuf_free(buffer);
  917. return (1);
  918. }
  919. static void
  920. sshpam_free_ctx(void *ctxtp)
  921. {
  922. struct pam_ctxt *ctxt = ctxtp;
  923. debug3("PAM: %s entering", __func__);
  924. sshpam_thread_cleanup();
  925. free(ctxt);
  926. /*
  927. * We don't call sshpam_cleanup() here because we may need the PAM
  928. * handle at a later stage, e.g. when setting up a session. It's
  929. * still on the cleanup list, so pam_end() *will* be called before
  930. * the server process terminates.
  931. */
  932. }
  933. KbdintDevice sshpam_device = {
  934. "pam",
  935. sshpam_init_ctx,
  936. sshpam_query,
  937. sshpam_respond,
  938. sshpam_free_ctx
  939. };
  940. KbdintDevice mm_sshpam_device = {
  941. "pam",
  942. mm_sshpam_init_ctx,
  943. mm_sshpam_query,
  944. mm_sshpam_respond,
  945. mm_sshpam_free_ctx
  946. };
  947. /*
  948. * This replaces auth-pam.c
  949. */
  950. void
  951. start_pam(struct ssh *ssh)
  952. {
  953. Authctxt *authctxt = (Authctxt *)ssh->authctxt;
  954. if (!options.use_pam)
  955. fatal("PAM: initialisation requested when UsePAM=no");
  956. if (sshpam_init(ssh, authctxt) == -1)
  957. fatal("PAM: initialisation failed");
  958. }
  959. void
  960. finish_pam(void)
  961. {
  962. sshpam_cleanup();
  963. }
  964. u_int
  965. do_pam_account(void)
  966. {
  967. debug("%s: called", __func__);
  968. if (sshpam_account_status != -1)
  969. return (sshpam_account_status);
  970. expose_authinfo(__func__);
  971. sshpam_err = pam_acct_mgmt(sshpam_handle, 0);
  972. debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err,
  973. pam_strerror(sshpam_handle, sshpam_err));
  974. if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) {
  975. sshpam_account_status = 0;
  976. return (sshpam_account_status);
  977. }
  978. if (sshpam_err == PAM_NEW_AUTHTOK_REQD)
  979. sshpam_password_change_required(1);
  980. sshpam_account_status = 1;
  981. return (sshpam_account_status);
  982. }
  983. void
  984. do_pam_setcred(int init)
  985. {
  986. sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
  987. (const void *)&store_conv);
  988. if (sshpam_err != PAM_SUCCESS)
  989. fatal("PAM: failed to set PAM_CONV: %s",
  990. pam_strerror(sshpam_handle, sshpam_err));
  991. if (init) {
  992. debug("PAM: establishing credentials");
  993. sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED);
  994. } else {
  995. debug("PAM: reinitializing credentials");
  996. sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED);
  997. }
  998. if (sshpam_err == PAM_SUCCESS) {
  999. sshpam_cred_established = 1;
  1000. return;
  1001. }
  1002. if (sshpam_authenticated)
  1003. fatal("PAM: pam_setcred(): %s",
  1004. pam_strerror(sshpam_handle, sshpam_err));
  1005. else
  1006. debug("PAM: pam_setcred(): %s",
  1007. pam_strerror(sshpam_handle, sshpam_err));
  1008. }
  1009. static int
  1010. sshpam_tty_conv(int n, sshpam_const struct pam_message **msg,
  1011. struct pam_response **resp, void *data)
  1012. {
  1013. char input[PAM_MAX_MSG_SIZE];
  1014. struct pam_response *reply;
  1015. int i;
  1016. debug3("PAM: %s called with %d messages", __func__, n);
  1017. *resp = NULL;
  1018. if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO))
  1019. return (PAM_CONV_ERR);
  1020. if ((reply = calloc(n, sizeof(*reply))) == NULL)
  1021. return (PAM_CONV_ERR);
  1022. for (i = 0; i < n; ++i) {
  1023. switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
  1024. case PAM_PROMPT_ECHO_OFF:
  1025. reply[i].resp =
  1026. read_passphrase(PAM_MSG_MEMBER(msg, i, msg),
  1027. RP_ALLOW_STDIN);
  1028. reply[i].resp_retcode = PAM_SUCCESS;
  1029. break;
  1030. case PAM_PROMPT_ECHO_ON:
  1031. fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
  1032. if (fgets(input, sizeof input, stdin) == NULL)
  1033. input[0] = '\0';
  1034. if ((reply[i].resp = strdup(input)) == NULL)
  1035. goto fail;
  1036. reply[i].resp_retcode = PAM_SUCCESS;
  1037. break;
  1038. case PAM_ERROR_MSG:
  1039. case PAM_TEXT_INFO:
  1040. fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg));
  1041. reply[i].resp_retcode = PAM_SUCCESS;
  1042. break;
  1043. default:
  1044. goto fail;
  1045. }
  1046. }
  1047. *resp = reply;
  1048. return (PAM_SUCCESS);
  1049. fail:
  1050. for(i = 0; i < n; i++) {
  1051. free(reply[i].resp);
  1052. }
  1053. free(reply);
  1054. return (PAM_CONV_ERR);
  1055. }
  1056. static struct pam_conv tty_conv = { sshpam_tty_conv, NULL };
  1057. /*
  1058. * XXX this should be done in the authentication phase, but ssh1 doesn't
  1059. * support that
  1060. */
  1061. void
  1062. do_pam_chauthtok(void)
  1063. {
  1064. if (use_privsep)
  1065. fatal("Password expired (unable to change with privsep)");
  1066. sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
  1067. (const void *)&tty_conv);
  1068. if (sshpam_err != PAM_SUCCESS)
  1069. fatal("PAM: failed to set PAM_CONV: %s",
  1070. pam_strerror(sshpam_handle, sshpam_err));
  1071. debug("PAM: changing password");
  1072. sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK);
  1073. if (sshpam_err != PAM_SUCCESS)
  1074. fatal("PAM: pam_chauthtok(): %s",
  1075. pam_strerror(sshpam_handle, sshpam_err));
  1076. }
  1077. void
  1078. do_pam_session(struct ssh *ssh)
  1079. {
  1080. debug3("PAM: opening session");
  1081. expose_authinfo(__func__);
  1082. sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
  1083. (const void *)&store_conv);
  1084. if (sshpam_err != PAM_SUCCESS)
  1085. fatal("PAM: failed to set PAM_CONV: %s",
  1086. pam_strerror(sshpam_handle, sshpam_err));
  1087. sshpam_err = pam_open_session(sshpam_handle, 0);
  1088. if (sshpam_err == PAM_SUCCESS)
  1089. sshpam_session_open = 1;
  1090. else {
  1091. sshpam_session_open = 0;
  1092. auth_restrict_session(ssh);
  1093. error("PAM: pam_open_session(): %s",
  1094. pam_strerror(sshpam_handle, sshpam_err));
  1095. }
  1096. }
  1097. int
  1098. is_pam_session_open(void)
  1099. {
  1100. return sshpam_session_open;
  1101. }
  1102. /*
  1103. * Set a PAM environment string. We need to do this so that the session
  1104. * modules can handle things like Kerberos/GSI credentials that appear
  1105. * during the ssh authentication process.
  1106. */
  1107. int
  1108. do_pam_putenv(char *name, const char *value)
  1109. {
  1110. int ret = 1;
  1111. char *compound;
  1112. size_t len;
  1113. len = strlen(name) + strlen(value) + 2;
  1114. compound = xmalloc(len);
  1115. snprintf(compound, len, "%s=%s", name, value);
  1116. ret = pam_putenv(sshpam_handle, compound);
  1117. free(compound);
  1118. return (ret);
  1119. }
  1120. char **
  1121. fetch_pam_child_environment(void)
  1122. {
  1123. return sshpam_env;
  1124. }
  1125. char **
  1126. fetch_pam_environment(void)
  1127. {
  1128. return (pam_getenvlist(sshpam_handle));
  1129. }
  1130. void
  1131. free_pam_environment(char **env)
  1132. {
  1133. char **envp;
  1134. if (env == NULL)
  1135. return;
  1136. for (envp = env; *envp; envp++)
  1137. free(*envp);
  1138. free(env);
  1139. }
  1140. /*
  1141. * "Blind" conversation function for password authentication. Assumes that
  1142. * echo-off prompts are for the password and stores messages for later
  1143. * display.
  1144. */
  1145. static int
  1146. sshpam_passwd_conv(int n, sshpam_const struct pam_message **msg,
  1147. struct pam_response **resp, void *data)
  1148. {
  1149. struct pam_response *reply;
  1150. int r, i;
  1151. size_t len;
  1152. debug3("PAM: %s called with %d messages", __func__, n);
  1153. *resp = NULL;
  1154. if (n <= 0 || n > PAM_MAX_NUM_MSG)
  1155. return (PAM_CONV_ERR);
  1156. if ((reply = calloc(n, sizeof(*reply))) == NULL)
  1157. return (PAM_CONV_ERR);
  1158. for (i = 0; i < n; ++i) {
  1159. switch (PAM_MSG_MEMBER(msg, i, msg_style)) {
  1160. case PAM_PROMPT_ECHO_OFF:
  1161. if (sshpam_password == NULL)
  1162. goto fail;
  1163. if ((reply[i].resp = strdup(sshpam_password)) == NULL)
  1164. goto fail;
  1165. reply[i].resp_retcode = PAM_SUCCESS;
  1166. break;
  1167. case PAM_ERROR_MSG:
  1168. case PAM_TEXT_INFO:
  1169. len = strlen(PAM_MSG_MEMBER(msg, i, msg));
  1170. if (len > 0) {
  1171. if ((r = sshbuf_putf(loginmsg, "%s\n",
  1172. PAM_MSG_MEMBER(msg, i, msg))) != 0)
  1173. fatal("%s: buffer error: %s",
  1174. __func__, ssh_err(r));
  1175. }
  1176. if ((reply[i].resp = strdup("")) == NULL)
  1177. goto fail;
  1178. reply[i].resp_retcode = PAM_SUCCESS;
  1179. break;
  1180. default:
  1181. goto fail;
  1182. }
  1183. }
  1184. *resp = reply;
  1185. return (PAM_SUCCESS);
  1186. fail:
  1187. for(i = 0; i < n; i++) {
  1188. free(reply[i].resp);
  1189. }
  1190. free(reply);
  1191. return (PAM_CONV_ERR);
  1192. }
  1193. static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL };
  1194. /*
  1195. * Attempt password authentication via PAM
  1196. */
  1197. int
  1198. sshpam_auth_passwd(Authctxt *authctxt, const char *password)
  1199. {
  1200. int flags = (options.permit_empty_passwd == 0 ?
  1201. PAM_DISALLOW_NULL_AUTHTOK : 0);
  1202. char *fake = NULL;
  1203. if (!options.use_pam || sshpam_handle == NULL)
  1204. fatal("PAM: %s called when PAM disabled or failed to "
  1205. "initialise.", __func__);
  1206. sshpam_password = password;
  1207. sshpam_authctxt = authctxt;
  1208. /*
  1209. * If the user logging in is invalid, or is root but is not permitted
  1210. * by PermitRootLogin, use an invalid password to prevent leaking
  1211. * information via timing (eg if the PAM config has a delay on fail).
  1212. */
  1213. if (!authctxt->valid || (authctxt->pw->pw_uid == 0 &&
  1214. options.permit_root_login != PERMIT_YES))
  1215. sshpam_password = fake = fake_password(password);
  1216. sshpam_err = pam_set_item(sshpam_handle, PAM_CONV,
  1217. (const void *)&passwd_conv);
  1218. if (sshpam_err != PAM_SUCCESS)
  1219. fatal("PAM: %s: failed to set PAM_CONV: %s", __func__,
  1220. pam_strerror(sshpam_handle, sshpam_err));
  1221. sshpam_err = pam_authenticate(sshpam_handle, flags);
  1222. sshpam_password = NULL;
  1223. free(fake);
  1224. if (sshpam_err == PAM_MAXTRIES)
  1225. sshpam_set_maxtries_reached(1);
  1226. if (sshpam_err == PAM_SUCCESS && authctxt->valid) {
  1227. debug("PAM: password authentication accepted for %.100s",
  1228. authctxt->user);
  1229. return 1;
  1230. } else {
  1231. debug("PAM: password authentication failed for %.100s: %s",
  1232. authctxt->valid ? authctxt->user : "an illegal user",
  1233. pam_strerror(sshpam_handle, sshpam_err));
  1234. return 0;
  1235. }
  1236. }
  1237. int
  1238. sshpam_get_maxtries_reached(void)
  1239. {
  1240. return sshpam_maxtries_reached;
  1241. }
  1242. void
  1243. sshpam_set_maxtries_reached(int reached)
  1244. {
  1245. if (reached == 0 || sshpam_maxtries_reached)
  1246. return;
  1247. sshpam_maxtries_reached = 1;
  1248. options.password_authentication = 0;
  1249. options.kbd_interactive_authentication = 0;
  1250. options.challenge_response_authentication = 0;
  1251. }
  1252. #endif /* USE_PAM */