Debian Codegraph @

contrib/mod_sftp_pam.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_sftp_pam -- a module which provides an SSH2
3 * "keyboard-interactive" driver using PAM
4 * Copyright (c) 2008-2025 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_sftp_pam, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 *
28 * -----DO NOT EDIT BELOW THIS LINE-----
29 * $Libraries: -lpam$
30 */
31
32 #include "conf.h"
33 #include "privs.h"
34 #include "mod_sftp.h"
35
36 #ifndef HAVE_PAM
37 # error "mod_sftp_pam requires PAM support on your system"
38 #endif
39
40 #define MOD_SFTP_PAM_VERSION "mod_sftp_pam/0.3"
41
42 /* Make sure the version of proftpd is as necessary. */
43 #if PROFTPD_VERSION_NUMBER < 0x0001030202
44 # error "ProFTPD 1.3.2rc2 or later required"
45 #endif
46
47 #ifdef HAVE_SECURITY_PAM_APPL_H
48 # ifdef HPUX11
49 # ifndef COMSEC
50 # define COMSEC 1
51 # endif
52 # endif /* HPUX11 */
53 # include <security/pam_appl.h>
54 #endif /* HAVE_SECURITY_PAM_APPL_H */
55
56 #ifdef HAVE_SECURITY_PAM_MODULES_H
57 # include <security/pam_modules.h>
58 #endif /* HAVE_SECURITY_PAM_MODULES_H */
59
60 /* Needed for the MAXLOGNAME restriction. */
61 #ifdef HAVE_SYS_PARAM_H
62 # include <sys/param.h>
63 #endif
64
65 #ifdef HAVE_PAM_PAM_APPL_H
66 #include <pam/pam_appl.h>
67 #endif /* HAVE_PAM_PAM_APPL_H */
68
69 /* There is ambiguity in the PAM spec, with regard to the list of
70 * struct pam_message that is passed to the conversation callback. Is it
71 * a pointer to an array, or an array of pointers?
72 */
73 #if defined(SOLARIS2) || defined(HPUX11)
74 # define SFTP_PAM_MSG_MEMBER(msg, n, member) ((*(msg))[(n)].member)
75 #else
76 # define SFTP_PAM_MSG_MEMBER(msg, n, member) ((msg)[(n)]->member)
77 #endif
78
79 /* On non-Solaris systems, the struct pam_message argument of pam_conv is
80 * declared const, but on Solaris, it isn't. To avoid compiler warnings about
81 * incompatible pointer types, we need to use const or not as appropriate.
82 */
83 #ifndef SOLARIS2
84 # define PR_PAM_CONST const
85 #else
86 # define PR_PAM_CONST
87 #endif
88
89 #define SFTP_PAM_OPT_NO_TTY 0x001
90 #define SFTP_PAM_OPT_NO_INFO_MSGS 0x002
91 #define SFTP_PAM_OPT_NO_RADIO_MSGS 0x004
92
93 module sftp_pam_module;
94
95 static void sftppam_exit_ev(const void *, void *);
96 MODRET sftppam_auth(cmd_rec *);
97
98 static sftp_kbdint_driver_t sftppam_driver;
99 static authtable sftppam_authtab[];
100
101 static pam_handle_t *sftppam_pamh = NULL;
102 static const char *sftppam_service = "sshd";
103
104 static int sftppam_authoritative = FALSE;
105 static int sftppam_auth_code = PR_AUTH_OK;
106 static int sftppam_handle_auth = FALSE;
107 static unsigned long sftppam_opts = 0UL;
108 static char *sftppam_user = NULL;
109 static size_t sftppam_userlen = 0;
110 static char sftppam_tty[32];
111
112 static const char *trace_channel = "ssh2";
113
114 /* PAM interaction
115 */
116
117 static int sftppam_converse(int nmsgs, PR_PAM_CONST struct pam_message **msgs,
118 struct pam_response **resps, void *app_data) {
119 register int i = 0, j = 0;
120 array_header *list;
121 uint32_t recvd_count = 0;
122 const char **recvd_responses = NULL;
123 struct pam_response *res = NULL;
124
125 if (nmsgs <= 0 ||
126 nmsgs > PAM_MAX_NUM_MSG) {
127 pr_trace_msg(trace_channel, 3, "bad number of PAM messages (%d)", nmsgs);
128 return PAM_CONV_ERR;
129 }
130
131 pr_trace_msg(trace_channel, 9, "handling %d PAM %s", nmsgs,
132 nmsgs == 1 ? "message" : "messages");
133
134 /* First, send these messages to the client. */
135
136 list = make_array(sftppam_driver.driver_pool, 1,
137 sizeof(sftp_kbdint_challenge_t));
138
139 for (i = 0; i < nmsgs; i++) {
140 sftp_kbdint_challenge_t *challenge;
141
142 /* Skip PAM_ERROR_MSG messages; we don't want to send these to the client.
143 */
144 if (SFTP_PAM_MSG_MEMBER(msgs, i, msg_style) == PAM_TEXT_INFO) {
145 if (sftppam_opts & SFTP_PAM_OPT_NO_INFO_MSGS) {
146 pr_trace_msg(trace_channel, 9,
147 "skipping sending of PAM_TEXT_INFO '%s' to client",
148 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
149
150 } else {
151 pr_trace_msg(trace_channel, 9, "sending PAM_TEXT_INFO '%s' to client",
152 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
153
154 sftp_auth_send_banner(SFTP_PAM_MSG_MEMBER(msgs, i, msg));
155 }
156
157 continue;
158
159 #ifdef PAM_RADIO_TYPE
160 } else if (SFTP_PAM_MSG_MEMBER(msgs, i, msg_style) == PAM_RADIO_TYPE) {
161 if (sftppam_opts & SFTP_PAM_OPT_NO_RADIO_MSGS) {
162 pr_trace_msg(trace_channel, 9,
163 "skipping sending of PAM_RADIO_TYPE '%s' to client",
164 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
165
166 } else {
167 pr_trace_msg(trace_channel, 9, "sending PAM_RADIO_TYPE '%s' to client",
168 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
169
170 sftp_auth_send_banner(SFTP_PAM_MSG_MEMBER(msgs, i, msg));
171 }
172
173 continue;
174 #endif /* PAM_RADIO_TYPE */
175
176 } else if (SFTP_PAM_MSG_MEMBER(msgs, i, msg_style) == PAM_ERROR_MSG) {
177 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
178 "received PAM_ERROR_MSG '%s'", SFTP_PAM_MSG_MEMBER(msgs, i, msg));
179 continue;
180 }
181
182 challenge = push_array(list);
183 challenge->challenge = pstrdup(sftppam_driver.driver_pool,
184 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
185 challenge->display_response = FALSE;
186 }
187
188 if (list->nelts == 0) {
189 /* Nothing to see here, move along. */
190 return PAM_SUCCESS;
191 }
192
193 if (sftp_kbdint_send_challenge(NULL, NULL, list->nelts, list->elts) < 0) {
194 pr_trace_msg(trace_channel, 3,
195 "error sending keyboard-interactive challenges: %s", strerror(errno));
196 return PAM_CONV_ERR;
197 }
198
199 if (sftp_kbdint_recv_response(sftppam_driver.driver_pool, list->nelts,
200 &recvd_count, &recvd_responses) < 0) {
201 pr_trace_msg(trace_channel, 3,
202 "error receiving keyboard-interactive responses: %s", strerror(errno));
203 return PAM_CONV_ERR;
204 }
205
206 res = calloc(nmsgs, sizeof(struct pam_response));
207 if (res == NULL) {
208 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_PAM_VERSION ": Out of memory!");
209 return PAM_BUF_ERR;
210 }
211
212 for (i = 0; i < nmsgs; i++) {
213 res[i].resp_retcode = 0;
214
215 switch (SFTP_PAM_MSG_MEMBER(msgs, i, msg_style)) {
216 case PAM_PROMPT_ECHO_ON:
217 pr_trace_msg(trace_channel, 9,
218 "received PAM_PROMPT_ECHO_ON message '%s', responding with '%s'",
219 SFTP_PAM_MSG_MEMBER(msgs, i, msg), recvd_responses[i]);
220 res[i].resp = strdup(recvd_responses[i]);
221 break;
222
223 case PAM_PROMPT_ECHO_OFF:
224 pr_trace_msg(trace_channel, 9,
225 "received PAM_PROMPT_ECHO_OFF message '%s', responding with text",
226 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
227 res[i].resp = strdup(recvd_responses[i]);
228 break;
229
230 case PAM_TEXT_INFO:
231 pr_trace_msg(trace_channel, 9, "received PAM_TEXT_INFO message: %s",
232 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
233 res[i].resp = NULL;
234 break;
235
236 case PAM_ERROR_MSG:
237 pr_trace_msg(trace_channel, 9, "received PAM_ERROR_MSG message: %s",
238 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
239 res[i].resp = NULL;
240 break;
241
242 #ifdef PAM_RADIO_TYPE
243 case PAM_RADIO_TYPE:
244 pr_trace_msg(trace_channel, 9, "received PAM_RADIO_TYPE message: %s",
245 SFTP_PAM_MSG_MEMBER(msgs, i, msg));
246 res[i].resp = NULL;
247 break;
248 #endif /* PAM_RADIO_TYPE */
249
250 default:
251 pr_trace_msg(trace_channel, 3,
252 "received unknown PAM message style (%d), treating it as an error",
253 SFTP_PAM_MSG_MEMBER(msgs, i, msg_style));
254 for (j = 0; j < nmsgs; j++) {
255 if (res[i].resp != NULL) {
256 free(res[i].resp);
257 res[i].resp = NULL;
258 }
259 }
260
261 free(res);
262
263 return PAM_CONV_ERR;
264 }
265 }
266
267 *resps = res;
268 return PAM_SUCCESS;
269 }
270
271 static const struct pam_conv sftppam_conv = { &sftppam_converse, NULL };
272
273 /* Driver callbacks
274 */
275
276 static int sftppam_driver_open(sftp_kbdint_driver_t *driver, const char *user) {
277 int res;
278 config_rec *c;
279
280 /* XXX Should we pay attention to AuthOrder here? I.e. if AuthOrder
281 * does not include mod_sftp_pam or mod_auth_pam, should we fail to
282 * open this driver, since the AuthOrder indicates that no PAM check is
283 * desired? For this to work, AuthOrder needs to have been processed
284 * prior to this callback being invoked...
285 */
286
287 /* Figure out our default return style: whether or not PAM should allow
288 * other auth modules a shot at this user or not is controlled by adding
289 * '*' to a module name in the AuthOrder directive. By default, auth
290 * modules are not authoritative, and allow other auth modules a chance at
291 * authenticating the user. This is not the most secure configuration, but
292 * it allows things like AuthUserFile to work "out of the box".
293 */
294 if (sftppam_authtab[0].auth_flags & PR_AUTH_FL_REQUIRED) {
295 sftppam_authoritative = TRUE;
296 }
297
298 sftppam_userlen = strlen(user) + 1;
299 if (sftppam_userlen > (PAM_MAX_MSG_SIZE + 1)) {
300 sftppam_userlen = PAM_MAX_MSG_SIZE + 1;
301 }
302
303 #ifdef MAXLOGNAME
304 /* Some platforms' PAM libraries do not handle login strings that exceed
305 * this length.
306 */
307 if (sftppam_userlen > MAXLOGNAME) {
308 pr_log_pri(PR_LOG_NOTICE,
309 "PAM(%s): Name exceeds maximum login length (%u)", user, MAXLOGNAME);
310 pr_trace_msg(trace_channel, 1,
311 "user name '%s' exceeds maximum login length %u, declining", user,
312 MAXLOGNAME);
313 errno = EPERM;
314 return -1;
315 }
316 #endif
317
318 sftppam_user = malloc(sftppam_userlen);
319 if (sftppam_user == NULL) {
320 pr_log_pri(PR_LOG_ALERT, MOD_SFTP_PAM_VERSION ": Out of memory!");
321 exit(1);
322 }
323
324 memset(sftppam_user, '\0', sftppam_userlen);
325 sstrncpy(sftppam_user, user, sftppam_userlen);
326
327 c = find_config(main_server->conf, CONF_PARAM, "SFTPPAMOptions", FALSE);
328 while (c != NULL) {
329 unsigned long opts;
330
331 pr_signals_handle();
332
333 opts = *((unsigned long *) c->argv[0]);
334 sftppam_opts |= opts;
335
336 c = find_config_next(c, c->next, CONF_PARAM, "SFTPPAMOptions", FALSE);
337 }
338
339 #ifdef SOLARIS2
340 /* For Solaris environments, the TTY environment will always be set,
341 * in order to workaround a bug (Solaris Bug ID 4250887) where
342 * pam_open_session() will crash unless both PAM_RHOST and PAM_TTY are
343 * set, and the PAM_TTY setting is at least greater than the length of
344 * the string "/dev/".
345 */
346 sftppam_opts &= ~SFTP_PAM_OPT_NO_TTY;
347 #endif /* SOLARIS2 */
348
349 pr_signals_block();
350 PRIVS_ROOT
351
352 res = pam_start(sftppam_service, sftppam_user, &sftppam_conv, &sftppam_pamh);
353 if (res != PAM_SUCCESS) {
354 PRIVS_RELINQUISH
355 pr_signals_unblock();
356
357 free(sftppam_user);
358 sftppam_user = NULL;
359 sftppam_userlen = 0;
360
361 switch (res) {
362 case PAM_SYSTEM_ERR:
363 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
364 "error starting PAM service: %s", strerror(errno));
365 break;
366
367 case PAM_BUF_ERR:
368 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
369 "error starting PAM service: Memory buffer error");
370 break;
371 }
372
373 return -1;
374 }
375
376 pam_set_item(sftppam_pamh, PAM_RUSER, sftppam_user);
377 pam_set_item(sftppam_pamh, PAM_RHOST, session.c->remote_name);
378
379 if (!(sftppam_opts & SFTP_PAM_OPT_NO_TTY)) {
380 memset(sftppam_tty, '\0', sizeof(sftppam_tty));
381 pr_snprintf(sftppam_tty, sizeof(sftppam_tty), "/dev/ftpd%02lu",
382 (unsigned long) (session.pid ? session.pid : getpid()));
383 sftppam_tty[sizeof(sftppam_tty)-1] = '\0';
384
385 pr_trace_msg(trace_channel, 9, "setting PAM_TTY to '%s'", sftppam_tty);
386 pam_set_item(sftppam_pamh, PAM_TTY, sftppam_tty);
387 }
388
389 PRIVS_RELINQUISH
390 pr_signals_unblock();
391
392 /* We need to disable mod_auth_pam, since both mod_auth_pam and us want
393 * to talk to the PAM API, just in different fashions.
394 */
395
396 c = add_config_param_set(&(main_server->conf), "AuthPAM", 1, NULL);
397 c->argv[0] = palloc(c->pool, sizeof(unsigned char));
398 *((unsigned char *) c->argv[0]) = FALSE;
399
400 if (pr_auth_remove_auth_only_module("mod_auth_pam.c") < 0) {
401 if (errno != ENOENT) {
402 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_PAM_VERSION
403 ": error removing 'mod_auth_pam.c' from the auth-only module list: %s",
404 strerror(errno));
405 }
406 }
407
408 if (pr_auth_add_auth_only_module("mod_sftp_pam.c") < 0) {
409 if (errno != EEXIST) {
410 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_PAM_VERSION
411 ": error adding 'mod_sftp_pam.c' to the auth-only module list: %s",
412 strerror(errno));
413 }
414 }
415
416 sftppam_handle_auth = TRUE;
417
418 driver->driver_pool = make_sub_pool(permanent_pool);
419 pr_pool_tag(driver->driver_pool, "PAM keyboard-interactive driver pool");
420
421 return 0;
422 }
423
424 static int sftppam_driver_authenticate(sftp_kbdint_driver_t *driver,
425 const char *user) {
426 int res;
427
428 pr_signals_block();
429 PRIVS_ROOT
430
431 res = pam_authenticate(sftppam_pamh, 0);
432 if (res != PAM_SUCCESS) {
433 switch (res) {
434 case PAM_USER_UNKNOWN:
435 sftppam_auth_code = PR_AUTH_NOPWD;
436 break;
437
438 default:
439 sftppam_auth_code = PR_AUTH_BADPWD;
440 }
441
442 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
443 "PAM authentication error (%d) for user '%s': %s", res, user,
444 pam_strerror(sftppam_pamh, res));
445 (void) pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_PAM_VERSION
446 ": PAM authentication error (%d) for user '%s': %s", res, user,
447 pam_strerror(sftppam_pamh, res));
448
449 PRIVS_RELINQUISH
450 pr_signals_unblock();
451
452 errno = EPERM;
453 return -1;
454 }
455
456 res = pam_acct_mgmt(sftppam_pamh, 0);
457 if (res != PAM_SUCCESS) {
458 switch (res) {
459 #ifdef PAM_AUTHTOKEN_REQD
460 case PAM_AUTHTOKEN_REQD:
461 pr_trace_msg(trace_channel, 8,
462 "PAM account mgmt error: PAM_AUTHTOKEN_REQD");
463 break;
464 #endif
465
466 case PAM_ACCT_EXPIRED:
467 pr_trace_msg(trace_channel, 8,
468 "PAM account mgmt error: PAM_ACCT_EXPIRED");
469 sftppam_auth_code = PR_AUTH_DISABLEDPWD;
470 break;
471
472 #ifdef PAM_ACCT_DISABLED
473 case PAM_ACCT_DISABLED:
474 pr_trace_msg(trace_channel, 8,
475 "PAM account mgmt error: PAM_ACCT_DISABLED");
476 sftppam_auth_code = PR_AUTH_DISABLEDPWD;
477 break;
478 #endif
479
480 case PAM_USER_UNKNOWN:
481 pr_trace_msg(trace_channel, 8,
482 "PAM account mgmt error: PAM_USER_UNKNOWN");
483 sftppam_auth_code = PR_AUTH_NOPWD;
484 break;
485
486 default:
487 sftppam_auth_code = PR_AUTH_BADPWD;
488 break;
489 }
490
491 pr_trace_msg(trace_channel, 1,
492 "PAM account mgmt error (%d) for user '%s': %s", res, user,
493 pam_strerror(sftppam_pamh, res));
494
495 PRIVS_RELINQUISH
496 pr_signals_unblock();
497
498 errno = EPERM;
499 return -1;
500 }
501
502 res = pam_open_session(sftppam_pamh, 0);
503 if (res != PAM_SUCCESS) {
504 sftppam_auth_code = PR_AUTH_DISABLEDPWD;
505
506 pr_trace_msg(trace_channel, 1,
507 "PAM session error (%d) for user '%s': %s", res, user,
508 pam_strerror(sftppam_pamh, res));
509
510 PRIVS_RELINQUISH
511 pr_signals_unblock();
512
513 errno = EPERM;
514 return -1;
515 }
516
517 #ifdef PAM_CRED_ESTABLISH
518 res = pam_setcred(sftppam_pamh, PAM_CRED_ESTABLISH);
519 #else
520 res = pam_setcred(sftppam_pamh, PAM_ESTABLISH_CRED);
521 #endif /* !PAM_CRED_ESTABLISH */
522 if (res != PAM_SUCCESS) {
523 switch (res) {
524 case PAM_CRED_EXPIRED:
525 pr_trace_msg(trace_channel, 8,
526 "PAM credentials error: PAM_CRED_EXPIRED");
527 sftppam_auth_code = PR_AUTH_AGEPWD;
528 break;
529
530 case PAM_USER_UNKNOWN:
531 pr_trace_msg(trace_channel, 8,
532 "PAM credentials error: PAM_USER_UNKNOWN");
533 sftppam_auth_code = PR_AUTH_NOPWD;
534 break;
535
536 default:
537 sftppam_auth_code = PR_AUTH_BADPWD;
538 break;
539 }
540
541 pr_trace_msg(trace_channel, 1,
542 "PAM credentials error (%d) for user '%s': %s", res, user,
543 pam_strerror(sftppam_pamh, res));
544
545 PRIVS_RELINQUISH
546 pr_signals_unblock();
547
548 errno = EPERM;
549 return -1;
550 }
551
552 /* XXX Not sure why these platforms have different treatment...? */
553 #if defined(SOLARIS2) || defined(HPUX10) || defined(HPUX11)
554 res = pam_close_session(sftppam_pamh, 0);
555 if (sftppam_pamh != NULL) {
556 pam_end(sftppam_pamh, PAM_SUCCESS);
557 sftppam_pamh = NULL;
558 }
559 #endif
560
561 PRIVS_RELINQUISH
562 pr_signals_unblock();
563
564 return 0;
565 }
566
567 static int sftppam_driver_close(sftp_kbdint_driver_t *driver) {
568 if (driver->driver_pool) {
569 destroy_pool(driver->driver_pool);
570 driver->driver_pool = NULL;
571 }
572
573 if (sftppam_user) {
574 free(sftppam_user);
575 sftppam_user = NULL;
576 sftppam_userlen = 0;
577 }
578
579 return 0;
580 }
581
582 /* Auth handlers
583 */
584
585 MODRET sftppam_auth(cmd_rec *cmd) {
586 if (!sftppam_handle_auth) {
587 return PR_DECLINED(cmd);
588 }
589
590 if (sftppam_auth_code != PR_AUTH_OK) {
591 if (sftppam_authoritative) {
592 return PR_ERROR_INT(cmd, sftppam_auth_code);
593 }
594
595 return PR_DECLINED(cmd);
596 }
597
598 session.auth_mech = "mod_sftp_pam.c";
599 pr_event_register(&sftp_pam_module, "core.exit", sftppam_exit_ev, NULL);
600 return PR_HANDLED(cmd);
601 }
602
603 /* Configuration handlers
604 */
605
606 /* usage: SFTPPAMEngine on|off */
607 MODRET set_sftppamengine(cmd_rec *cmd) {
608 int engine = -1;
609 config_rec *c;
610
611 CHECK_ARGS(cmd, 1);
612 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
613
614 engine = get_boolean(cmd, 1);
615 if (engine == -1) {
616 CONF_ERROR(cmd, "expected Boolean parameter");
617 }
618
619 c = add_config_param(cmd->argv[0], 1, NULL);
620 c->argv[0] = pcalloc(c->pool, sizeof(int));
621 *((int *) c->argv[0]) = engine;
622
623 return PR_HANDLED(cmd);
624 }
625
626 /* usage: SFTPPAMOptions opt1 ... */
627 MODRET set_sftppamoptions(cmd_rec *cmd) {
628 config_rec *c = NULL;
629 register unsigned int i = 0;
630 unsigned long opts = 0UL;
631
632 if (cmd->argc-1 == 0) {
633 CONF_ERROR(cmd, "wrong number of parameters");
634 }
635
636 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
637
638 c = add_config_param(cmd->argv[0], 1, NULL);
639
640 for (i = 1; i < cmd->argc; i++) {
641 if (strcmp(cmd->argv[i], "NoTTY") == 0) {
642 opts |= SFTP_PAM_OPT_NO_TTY;
643
644 } else if (strcmp(cmd->argv[i], "NoInfoMsgs") == 0) {
645 opts |= SFTP_PAM_OPT_NO_INFO_MSGS;
646
647 } else if (strcmp(cmd->argv[i], "NoRadioMsgs") == 0) {
648 opts |= SFTP_PAM_OPT_NO_RADIO_MSGS;
649
650 } else {
651 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown SFTPPAMOption: '",
652 cmd->argv[i], "'", NULL));
653 }
654 }
655
656 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
657 *((unsigned long *) c->argv[0]) = opts;
658
659 if (pr_module_exists("mod_ifsession.c")) {
660 /* These are needed in case this directive is used with mod_ifsession
661 * configuration.
662 */
663 c->flags |= CF_MULTI;
664 }
665
666 return PR_HANDLED(cmd);
667 }
668
669 /* usage: SFTPPAMServiceName name */
670 MODRET set_sftppamservicename(cmd_rec *cmd) {
671 CHECK_ARGS(cmd, 1);
672 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
673
674 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
675 return PR_HANDLED(cmd);
676 }
677
678 /* Event Handlers
679 */
680
681 static void sftppam_exit_ev(const void *event_data, void *user_data) {
682
683 /* Close the PAM session */
684
685 if (sftppam_pamh != NULL) {
686 int res;
687
688 #ifdef PAM_CRED_DELETE
689 res = pam_setcred(sftppam_pamh, PAM_CRED_DELETE);
690 #else
691 res = pam_setcred(sftppam_pamh, PAM_DELETE_CRED);
692 #endif
693 if (res != PAM_SUCCESS) {
694 pr_trace_msg(trace_channel, 9, "PAM error setting PAM_DELETE_CRED: %s",
695 pam_strerror(sftppam_pamh, res));
696 }
697
698 pr_trace_msg(trace_channel, 17, "closing PAM session");
699 res = pam_close_session(sftppam_pamh, PAM_SILENT);
700 if (res != PAM_SUCCESS) {
701 pr_trace_msg(trace_channel, 1, "error closing PAM session: %s",
702 pam_strerror(sftppam_pamh, res));
703 }
704
705 pr_trace_msg(trace_channel, 17, "freeing PAM handle");
706 res = pam_end(sftppam_pamh, PAM_SUCCESS);
707 if (res != PAM_SUCCESS) {
708 pr_trace_msg(trace_channel, 1, "error freeing PAM handle: %s",
709 pam_strerror(sftppam_pamh, res));
710 }
711
712 sftppam_pamh = NULL;
713 }
714
715 if (sftppam_user != NULL) {
716 free(sftppam_user);
717 sftppam_user = NULL;
718 sftppam_userlen = 0;
719 }
720 }
721
722 #if defined(PR_SHARED_MODULE)
723 static void sftppam_mod_unload_ev(const void *event_data, void *user_data) {
724 if (strcmp("mod_sftp_pam.c", (const char *) event_data) == 0) {
725 if (sftppam_user) {
726 free(sftppam_user);
727 sftppam_user = NULL;
728 sftppam_userlen = 0;
729 }
730
731 sftp_kbdint_unregister_driver("pam");
732 pr_event_unregister(&sftp_pam_module, NULL, NULL);
733 }
734 }
735 #endif /* !PR_SHARED_MODULE */
736
737 /* Initialization functions
738 */
739
740 static int sftppam_init(void) {
741 #if defined(PR_SHARED_MODULE)
742 pr_event_register(&sftp_pam_module, "core.module-unload",
743 sftppam_mod_unload_ev, NULL);
744 #endif /* !PR_SHARED_MODULE */
745
746 /* Prepare our driver. */
747 memset(&sftppam_driver, 0, sizeof(sftppam_driver));
748 sftppam_driver.open = sftppam_driver_open;
749 sftppam_driver.authenticate = sftppam_driver_authenticate;
750 sftppam_driver.close = sftppam_driver_close;
751
752 /* Register ourselves with mod_sftp. */
753 if (sftp_kbdint_register_driver("pam", &sftppam_driver) < 0) {
754 int xerrno = errno;
755
756 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_PAM_VERSION
757 ": notice: error registering 'keyboard-interactive' driver: %s",
758 strerror(xerrno));
759
760 errno = xerrno;
761 return -1;
762 }
763
764 return 0;
765 }
766
767 static int sftppam_sess_init(void) {
768 config_rec *c;
769
770 c = find_config(main_server->conf, CONF_PARAM, "SFTPPAMEngine", FALSE);
771 if (c != NULL) {
772 int engine;
773
774 engine = *((int *) c->argv[0]);
775 if (engine == FALSE) {
776 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
777 "disabled by SFTPPAMEngine setting, unregistered 'pam' driver");
778 sftp_kbdint_unregister_driver("pam");
779 return 0;
780 }
781 }
782
783 /* To preserve the principle of least surprise, also check for the AuthPAM
784 * directive.
785 */
786 c = find_config(main_server->conf, CONF_PARAM, "AuthPAM", FALSE);
787 if (c != NULL) {
788 unsigned char auth_pam;
789
790 auth_pam = *((unsigned char *) c->argv[0]);
791 if (auth_pam == FALSE) {
792 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_PAM_VERSION,
793 "disabled by AuthPAM setting, unregistered 'pam' driver");
794 sftp_kbdint_unregister_driver("pam");
795 return 0;
796 }
797 }
798
799 c = find_config(main_server->conf, CONF_PARAM, "SFTPPAMServiceName", FALSE);
800 if (c != NULL) {
801 sftppam_service = c->argv[0];
802 }
803
804 pr_trace_msg(trace_channel, 8, "using PAM service name '%s'",
805 sftppam_service);
806
807 return 0;
808 }
809
810 /* Module API tables
811 */
812
813 static conftable sftppam_conftab[] = {
814 { "SFTPPAMEngine", set_sftppamengine, NULL },
815 { "SFTPPAMOptions", set_sftppamoptions, NULL },
816 { "SFTPPAMServiceName", set_sftppamservicename, NULL },
817 { NULL }
818 };
819
820 static authtable sftppam_authtab[] = {
821 { 0, "auth", sftppam_auth },
822 { 0, NULL, NULL }
823 };
824
825 module sftp_pam_module = {
826 NULL, NULL,
827
828 /* Module API version 2.0 */
829 0x20,
830
831 /* Module name */
832 "sftp_pam",
833
834 /* Module configuration handler table */
835 sftppam_conftab,
836
837 /* Module command handler table */
838 NULL,
839
840 /* Module authentication handler table */
841 sftppam_authtab,
842
843 /* Module initialization function */
844 sftppam_init,
845
846 /* Session initialization function */
847 sftppam_sess_init,
848
849 /* Module version */
850 MOD_SFTP_PAM_VERSION
851 };