2 * Copyright 1998 Juniper Networks, Inc.
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <security/pam_modules.h>
33 #include "pam_mod_misc.h"
35 /* Everyone has to have these options. It is not an error to
36 * specify them and then not use them.
38 struct opttab std_options[PAM_MAX_OPTIONS] = {
39 { "debug", PAM_OPT_DEBUG },
40 { "no_warn", PAM_OPT_NO_WARN },
41 { "echo_pass", PAM_OPT_ECHO_PASS },
42 { "use_first_pass", PAM_OPT_USE_FIRST_PASS },
43 { "try_first_pass", PAM_OPT_TRY_FIRST_PASS },
44 { "use_mapped_pass", PAM_OPT_USE_MAPPED_PASS },
45 { "expose_account", PAM_OPT_EXPOSE_ACCOUNT },
49 /* Populate the options structure, syslogging all errors */
51 pam_std_option(struct options *options, struct opttab other_options[],
52 int argc, const char **argv)
55 int i, j, arglen, found;
58 for (i = 0; i < PAM_MAX_OPTIONS; i++) {
59 if (std_options[i].name == NULL) {
63 /* XXX Bad juju happens if loop exits with j == 0 */
66 for (oo = other_options; oo->name != NULL; oo++) {
68 for (i = 0; std_options[i].name; i++)
69 if (strcmp((char *)oo->name,
70 std_options[i].name) == 0)
73 std_options[j].name = oo->name;
74 std_options[j].value = oo->value;
78 for (i = 0; i < PAM_MAX_OPTIONS; i++) {
79 options->opt[i].bool = 0;
80 options->opt[i].arg = NULL;
82 if (j < PAM_MAX_OPTIONS) {
83 std_options[j].name = NULL;
84 std_options[j].value = 0;
86 for (j = 0; j < argc; j++) {
88 syslog(LOG_DEBUG, "Doing arg %s ", argv[j]);
91 for (i = 0; i < PAM_MAX_OPTIONS; i++) {
92 if (std_options[i].name == NULL)
94 if (strcmp(argv[j], std_options[i].name) == 0) {
95 options->opt[std_options[i].value].bool = 1;
100 arglen = strlen(std_options[i].name);
101 if (strncmp(argv[j], std_options[i].name,
103 && argv[j][arglen] == '=') {
104 options->opt[std_options[i].value].bool
106 options->opt[std_options[i].value].arg =
107 strdup(&argv[j][arglen + 1]);
114 syslog(LOG_WARNING, "PAM option: %s invalid", argv[j]);
118 /* Test if option is set in options */
120 pam_test_option(struct options *options, enum opt option, char **arg)
123 *arg = options->opt[option].arg;
124 return options->opt[option].bool;
127 /* Set option in options, errors to syslog */
129 pam_set_option(struct options *options, enum opt option)
131 if (option < PAM_OPT_STD_MAX)
132 options->opt[option].bool = 1;
135 syslog(LOG_DEBUG, "PAM options: attempt to set option %d",
140 /* Clear option in options, errors to syslog */
142 pam_clear_option(struct options *options, enum opt option)
144 if (option < PAM_OPT_STD_MAX)
145 options->opt[option].bool = 0;
148 syslog(LOG_DEBUG, "PAM options: attempt to clear option %d",
154 enum { PAM_OPT_FOO=PAM_OPT_STD_MAX, PAM_OPT_BAR, PAM_OPT_BAZ, PAM_OPT_QUX };
156 struct opttab other_options[] = {
157 { "foo", PAM_OPT_FOO },
158 { "bar", PAM_OPT_BAR },
159 { "baz", PAM_OPT_BAZ },
160 { "qux", PAM_OPT_QUX },
165 main(int argc, const char *argv[])
167 struct options options;
171 pam_std_option(&options, other_options, argc, argv);
172 for (i = 0; i < PAM_MAX_OPTIONS; i++) {
173 opt = pam_test_option(&options, i, &arg);
176 printf("%d []\n", i);
178 printf("%d [%s]\n", i, arg);