2 * Copyright (c) 2011 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: pamtest.c 685 2013-07-11 16:33:34Z des $
45 #include <security/pam_appl.h>
46 #include <security/openpam.h> /* for openpam_ttyconv() */
48 /* OpenPAM internals */
49 extern const char *pam_item_name[PAM_NUM_ITEMS];
50 extern int openpam_debug;
52 static pam_handle_t *pamh;
53 static struct pam_conv pamc;
58 static void pt_verbose(const char *, ...)
59 OPENPAM_FORMAT ((__printf__, 1, 2));
60 static void pt_error(int, const char *, ...)
61 OPENPAM_FORMAT ((__printf__, 2, 3));
64 * Print an information message if -v was specified at least once
67 pt_verbose(const char *fmt, ...)
73 vfprintf(stderr, fmt, ap);
75 fprintf(stderr, "\n");
80 * Print an error message
83 pt_error(int e, const char *fmt, ...)
87 if (e == PAM_SUCCESS && !verbose)
90 vfprintf(stderr, fmt, ap);
92 fprintf(stderr, ": %s\n", pam_strerror(NULL, e));
96 * Wrapper for pam_start(3)
99 pt_start(const char *service, const char *user)
103 pamc.conv = &openpam_ttyconv;
104 pt_verbose("pam_start(%s, %s)", service, user);
105 if ((pame = pam_start(service, user, &pamc, &pamh)) != PAM_SUCCESS)
106 pt_error(pame, "pam_start(%s)", service);
111 * Wrapper for pam_authenticate(3)
114 pt_authenticate(int flags)
119 pt_verbose("pam_authenticate()");
120 if ((pame = pam_authenticate(pamh, flags)) != PAM_SUCCESS)
121 pt_error(pame, "pam_authenticate()");
126 * Wrapper for pam_acct_mgmt(3)
129 pt_acct_mgmt(int flags)
134 pt_verbose("pam_acct_mgmt()");
135 if ((pame = pam_acct_mgmt(pamh, flags)) != PAM_SUCCESS)
136 pt_error(pame, "pam_acct_mgmt()");
141 * Wrapper for pam_chauthtok(3)
144 pt_chauthtok(int flags)
149 pt_verbose("pam_chauthtok()");
150 if ((pame = pam_chauthtok(pamh, flags)) != PAM_SUCCESS)
151 pt_error(pame, "pam_chauthtok()");
156 * Wrapper for pam_setcred(3)
159 pt_setcred(int flags)
164 pt_verbose("pam_setcred()");
165 if ((pame = pam_setcred(pamh, flags)) != PAM_SUCCESS)
166 pt_error(pame, "pam_setcred()");
171 * Wrapper for pam_open_session(3)
174 pt_open_session(int flags)
179 pt_verbose("pam_open_session()");
180 if ((pame = pam_open_session(pamh, flags)) != PAM_SUCCESS)
181 pt_error(pame, "pam_open_session()");
186 * Wrapper for pam_close_session(3)
189 pt_close_session(int flags)
194 pt_verbose("pam_close_session()");
195 if ((pame = pam_close_session(pamh, flags)) != PAM_SUCCESS)
196 pt_error(pame, "pam_close_session()");
201 * Wrapper for pam_set_item(3)
204 pt_set_item(int item, const char *p)
216 case PAM_USER_PROMPT:
217 case PAM_AUTHTOK_PROMPT:
218 case PAM_OLDAUTHTOK_PROMPT:
220 pt_verbose("setting %s to %s", pam_item_name[item], p);
223 pt_verbose("setting %s", pam_item_name[item]);
226 if ((pame = pam_set_item(pamh, item, p)) != PAM_SUCCESS)
227 pt_error(pame, "pam_set_item(%s)", pam_item_name[item]);
232 * Wrapper for pam_end(3)
238 if (pamh != NULL && (pame = pam_end(pamh, pame)) != PAM_SUCCESS)
240 pt_error(pame, "pam_end()");
245 * Retrieve and list the PAM environment variables
250 char **pam_envlist, **pam_env;
252 if ((pam_envlist = pam_getenvlist(pamh)) == NULL ||
253 *pam_envlist == NULL) {
254 pt_verbose("no environment variables.");
256 pt_verbose("environment variables:");
257 for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env) {
258 printf(" %s\n", *pam_env);
263 return (PAM_SUCCESS);
267 * Print usage string and exit
273 fprintf(stderr, "usage: pamtest %s service command ...\n",
274 "[-dkMPsv] [-H rhost] [-h host] [-t tty] [-U ruser] [-u user]");
279 * Handle an option that takes an int argument and can be used only once
282 opt_num_once(int opt, long *num, const char *arg)
287 l = strtol(arg, &end, 0);
288 if (end == optarg || *end != '\0') {
290 "The -%c option expects a numeric argument\n", opt);
297 * Handle an option that takes a string argument and can be used only once
300 opt_str_once(int opt, const char **p, const char *arg)
304 fprintf(stderr, "The -%c option can only be used once\n", opt);
314 main(int argc, char *argv[])
317 const char *rhost = NULL;
318 const char *host = NULL;
319 const char *ruser = NULL;
320 const char *user = NULL;
321 const char *service = NULL;
322 const char *tty = NULL;
328 while ((opt = getopt(argc, argv, "dH:h:kMPsT:t:U:u:v")) != -1)
334 opt_str_once(opt, &rhost, optarg);
337 opt_str_once(opt, &host, optarg);
343 openpam_set_feature(OPENPAM_RESTRICT_MODULE_NAME, 0);
344 openpam_set_feature(OPENPAM_VERIFY_MODULE_FILE, 0);
347 openpam_set_feature(OPENPAM_RESTRICT_SERVICE_NAME, 0);
348 openpam_set_feature(OPENPAM_VERIFY_POLICY_FILE, 0);
354 opt_num_once(opt, &timeout, optarg);
355 if (timeout < 0 || timeout > INT_MAX) {
357 "Invalid conversation timeout\n");
360 openpam_ttyconv_timeout = (int)timeout;
363 opt_str_once(opt, &tty, optarg);
366 opt_str_once(opt, &ruser, optarg);
369 opt_str_once(opt, &user, optarg);
392 if (gethostname(hostname, sizeof(hostname)) == -1)
393 err(1, "gethostname()");
397 tty = ttyname(STDERR_FILENO);
404 if ((pame = pt_start(service, user)) != PAM_SUCCESS)
408 * pam_start(3) sets this to the machine's hostname, but we allow
409 * the user to override it.
412 if ((pame = pt_set_item(PAM_HOST, host)) != PAM_SUCCESS)
416 * The remote host / user / tty are usually set by the
419 if ((pame = pt_set_item(PAM_RHOST, rhost)) != PAM_SUCCESS ||
420 (pame = pt_set_item(PAM_RUSER, ruser)) != PAM_SUCCESS ||
421 (pame = pt_set_item(PAM_TTY, tty)) != PAM_SUCCESS)
425 if (strcmp(*argv, "listenv") == 0 ||
426 strcmp(*argv, "env") == 0) {
428 } else if (strcmp(*argv, "authenticate") == 0 ||
429 strcmp(*argv, "auth") == 0) {
430 pame = pt_authenticate(0);
431 } else if (strcmp(*argv, "acct_mgmt") == 0 ||
432 strcmp(*argv, "account") == 0) {
433 pame = pt_acct_mgmt(0);
434 } else if (strcmp(*argv, "chauthtok") == 0 ||
435 strcmp(*argv, "change") == 0) {
436 pame = pt_chauthtok(PAM_CHANGE_EXPIRED_AUTHTOK);
437 } else if (strcmp(*argv, "forcechauthtok") == 0 ||
438 strcmp(*argv, "forcechange") == 0) {
439 pame = pt_chauthtok(0);
440 } else if (strcmp(*argv, "setcred") == 0 ||
441 strcmp(*argv, "establish_cred") == 0) {
442 pame = pt_setcred(PAM_ESTABLISH_CRED);
443 } else if (strcmp(*argv, "open_session") == 0 ||
444 strcmp(*argv, "open") == 0) {
445 pame = pt_open_session(0);
446 } else if (strcmp(*argv, "close_session") == 0 ||
447 strcmp(*argv, "close") == 0) {
448 pame = pt_close_session(0);
449 } else if (strcmp(*argv, "unsetcred") == 0 ||
450 strcmp(*argv, "delete_cred") == 0) {
451 pame = pt_setcred(PAM_DELETE_CRED);
453 warnx("unknown primitive: %s", *argv);
454 pame = PAM_SYSTEM_ERR;
456 if (pame != PAM_SUCCESS && !keepatit) {
457 warnx("test aborted");
466 exit(pame == PAM_SUCCESS ? 0 : 1);