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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: pamtest.c 595 2012-04-14 14:28:35Z 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 if ((pame = pam_authenticate(pamh, flags)) != PAM_SUCCESS)
120 pt_error(pame, "pam_authenticate()");
125 * Wrapper for pam_acct_mgmt(3)
128 pt_acct_mgmt(int flags)
133 if ((pame = pam_acct_mgmt(pamh, flags)) != PAM_SUCCESS)
134 pt_error(pame, "pam_acct_mgmt()");
139 * Wrapper for pam_chauthtok(3)
142 pt_chauthtok(int flags)
147 if ((pame = pam_chauthtok(pamh, flags)) != PAM_SUCCESS)
148 pt_error(pame, "pam_chauthtok()");
153 * Wrapper for pam_setcred(3)
156 pt_setcred(int flags)
161 if ((pame = pam_setcred(pamh, flags)) != PAM_SUCCESS)
162 pt_error(pame, "pam_setcred()");
167 * Wrapper for pam_open_session(3)
170 pt_open_session(int flags)
175 if ((pame = pam_open_session(pamh, flags)) != PAM_SUCCESS)
176 pt_error(pame, "pam_open_session()");
181 * Wrapper for pam_close_session(3)
184 pt_close_session(int flags)
189 if ((pame = pam_close_session(pamh, flags)) != PAM_SUCCESS)
190 pt_error(pame, "pam_close_session()");
195 * Wrapper for pam_set_item(3)
198 pt_set_item(int item, const char *p)
210 case PAM_USER_PROMPT:
211 case PAM_AUTHTOK_PROMPT:
212 case PAM_OLDAUTHTOK_PROMPT:
214 pt_verbose("setting %s to %s", pam_item_name[item], p);
217 pt_verbose("setting %s", pam_item_name[item]);
220 if ((pame = pam_set_item(pamh, item, p)) != PAM_SUCCESS)
221 pt_error(pame, "pam_set_item(%s)", pam_item_name[item]);
226 * Wrapper for pam_end(3)
232 if (pamh != NULL && (pame = pam_end(pamh, pame)) != PAM_SUCCESS)
234 pt_error(pame, "pam_end()");
239 * Retrieve and list the PAM environment variables
244 char **pam_envlist, **pam_env;
246 if ((pam_envlist = pam_getenvlist(pamh)) == NULL ||
247 *pam_envlist == NULL) {
248 pt_verbose("no environment variables.");
250 pt_verbose("environment variables:");
251 for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env) {
252 printf(" %s\n", *pam_env);
257 return (PAM_SUCCESS);
261 * Print usage string and exit
267 fprintf(stderr, "usage: pamtest %s service command ...\n",
268 "[-dkMPsv] [-H rhost] [-h host] [-t tty] [-U ruser] [-u user]");
273 * Handle an option that takes a string argument and can be used only once
276 opt_str_once(int opt, const char **p, const char *arg)
280 fprintf(stderr, "The -%c option can only be used once\n", opt);
290 main(int argc, char *argv[])
293 const char *rhost = NULL;
294 const char *host = NULL;
295 const char *ruser = NULL;
296 const char *user = NULL;
297 const char *service = NULL;
298 const char *tty = NULL;
303 while ((opt = getopt(argc, argv, "dH:h:kMPst:U:u:v")) != -1)
309 opt_str_once(opt, &rhost, optarg);
312 opt_str_once(opt, &host, optarg);
318 openpam_set_feature(OPENPAM_RESTRICT_MODULE_NAME, 0);
319 openpam_set_feature(OPENPAM_VERIFY_MODULE_FILE, 0);
322 openpam_set_feature(OPENPAM_RESTRICT_SERVICE_NAME, 0);
323 openpam_set_feature(OPENPAM_VERIFY_POLICY_FILE, 0);
329 opt_str_once(opt, &tty, optarg);
332 opt_str_once(opt, &ruser, optarg);
335 opt_str_once(opt, &user, optarg);
356 if (gethostname(hostname, sizeof(hostname)) == -1)
357 err(1, "gethostname()");
361 tty = ttyname(STDERR_FILENO);
368 if ((pame = pt_start(service, user)) != PAM_SUCCESS)
372 * pam_start(3) sets this to the machine's hostname, but we allow
373 * the user to override it.
376 if ((pame = pt_set_item(PAM_HOST, host)) != PAM_SUCCESS)
380 * The remote host / user / tty are usually set by the
383 if ((pame = pt_set_item(PAM_RHOST, rhost)) != PAM_SUCCESS ||
384 (pame = pt_set_item(PAM_RUSER, ruser)) != PAM_SUCCESS ||
385 (pame = pt_set_item(PAM_TTY, tty)) != PAM_SUCCESS)
389 if (strcmp(*argv, "listenv") == 0 ||
390 strcmp(*argv, "env") == 0) {
392 } else if (strcmp(*argv, "authenticate") == 0 ||
393 strcmp(*argv, "auth") == 0) {
394 pame = pt_authenticate(0);
395 } else if (strcmp(*argv, "acct_mgmt") == 0 ||
396 strcmp(*argv, "account") == 0) {
397 pame = pt_acct_mgmt(0);
398 } else if (strcmp(*argv, "chauthtok") == 0 ||
399 strcmp(*argv, "change") == 0) {
400 pame = pt_chauthtok(PAM_CHANGE_EXPIRED_AUTHTOK);
401 } else if (strcmp(*argv, "forcechauthtok") == 0 ||
402 strcmp(*argv, "forcechange") == 0) {
403 pame = pt_chauthtok(0);
404 } else if (strcmp(*argv, "setcred") == 0 ||
405 strcmp(*argv, "establish_cred") == 0) {
406 pame = pt_setcred(PAM_ESTABLISH_CRED);
407 } else if (strcmp(*argv, "open_session") == 0 ||
408 strcmp(*argv, "open") == 0) {
409 pame = pt_open_session(0);
410 } else if (strcmp(*argv, "close_session") == 0 ||
411 strcmp(*argv, "close") == 0) {
412 pame = pt_close_session(0);
413 } else if (strcmp(*argv, "unsetcred") == 0 ||
414 strcmp(*argv, "delete_cred") == 0) {
415 pame = pt_setcred(PAM_DELETE_CRED);
417 warnx("unknown primitive: %s", *argv);
418 pame = PAM_SYSTEM_ERR;
420 if (pame != PAM_SUCCESS && !keepatit) {
421 warnx("test aborted");
430 exit(pame == PAM_SUCCESS ? 0 : 1);