1 /*
2 * Copyright (c) 2022 Iker Pedrosa <ipedrosa@redhat.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, and the entire permission notice in its entirety,
9 * including the disclaimer of warranties.
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
15 * written permission.
16 *
17 * ALTERNATIVELY, this product may be distributed under the terms of
18 * the GNU Public License, in which case the provisions of the GPL are
19 * required INSTEAD OF the above restrictions. (This clause is
20 * necessary due to a potential bad interaction between the GPL and
21 * the restrictions contained in a BSD-style copyright.)
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include "config.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <sys/stat.h>
43
44 #include <security/pam_modutil.h>
45
46 #include "pam_inline.h"
47 #include "pwhistory_config.h"
48
49 #define PWHISTORY_DEFAULT_CONF SCONFIGDIR "/pwhistory.conf"
50
51 #ifdef VENDOR_SCONFIGDIR
52 #define VENDOR_PWHISTORY_DEFAULT_CONF (VENDOR_SCONFIGDIR "/pwhistory.conf")
53 #endif
54
55 void
56 parse_config_file(pam_handle_t *pamh, int argc, const char **argv,
57 struct options_t *options)
58 {
59 const char *fname = NULL;
60 int i;
61 char *val;
62
63 for (i = 0; i < argc; ++i) {
64 const char *str = pam_str_skip_prefix(argv[i], "conf=");
65
66 if (str != NULL) {
67 fname = str;
68 }
69 }
70
71 if (fname == NULL) {
72 fname = PWHISTORY_DEFAULT_CONF;
73
74 #ifdef VENDOR_PWHISTORY_DEFAULT_CONF
75 /*
76 * Check whether PWHISTORY_DEFAULT_CONF file is available.
77 * If it does not exist, fall back to VENDOR_PWHISTORY_DEFAULT_CONF file.
78 */
79 struct stat buffer;
80 if (stat(fname, &buffer) != 0 && errno == ENOENT) {
81 fname = VENDOR_PWHISTORY_DEFAULT_CONF;
82 }
83 #endif
84 }
85
86 val = pam_modutil_search_key (pamh, fname, "debug");
87 if (val != NULL) {
88 options->debug = 1;
89 free(val);
90 }
91
92 val = pam_modutil_search_key (pamh, fname, "enforce_for_root");
93 if (val != NULL) {
94 options->enforce_for_root = 1;
95 free(val);
96 }
97
98 val = pam_modutil_search_key (pamh, fname, "remember");
99 if (val != NULL) {
100 unsigned int temp;
101 if (sscanf(val, "%u", &temp) != 1) {
102 pam_syslog(pamh, LOG_ERR,
103 "Bad number supplied for remember argument");
104 } else {
105 options->remember = temp;
106 }
107 free(val);
108 }
109
110 val = pam_modutil_search_key (pamh, fname, "retry");
111 if (val != NULL) {
112 unsigned int temp;
113 if (sscanf(val, "%u", &temp) != 1) {
114 pam_syslog(pamh, LOG_ERR,
115 "Bad number supplied for retry argument");
116 } else {
117 options->tries = temp;
118 }
119 free(val);
120 }
121
122 val = pam_modutil_search_key (pamh, fname, "file");
123 if (val != NULL) {
124 if (*val != '/') {
125 pam_syslog (pamh, LOG_ERR,
126 "File path should be absolute: %s", val);
127 } else {
128 options->filename = val;
129 }
130 }
131 }