Debian Codegraph @

extra/pam_shishi/pam_shishi.c

raw symbols SCIP
1 /* pam_shishi.c PAM module using Shishi.
2 * Copyright (C) 2002-2021 Simon Josefsson
3 *
4 * This file is part of Shishi.
5 *
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
20 *
21 */
22
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #ifdef STDC_HEADERS
28 # include <stdio.h>
29 # include <stdlib.h>
30 # include <stdarg.h>
31 # include <ctype.h>
32 # include <string.h>
33 #endif
34
35 #include <shishi.h>
36
37 /* These #defines must be present according to PAM documentation. */
38 #define PAM_SM_AUTH
39 #define PAM_SM_ACCOUNT
40 #define PAM_SM_SESSION
41 #define PAM_SM_PASSWORD
42
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #ifdef HAVE_SECURITY_PAM_APPL_H
48 # include <security/pam_appl.h>
49 #endif
50 #ifdef HAVE_SECURITY_PAM_MODULES_H
51 # include <security/pam_modules.h>
52 #endif
53
54 #ifdef HAVE_SECURITY_PAM_EXT_H
55 # include <syslog.h>
56 # include <security/pam_ext.h>
57 # define SHISHI_LINUXPAM_LOGGING 1
58 #endif
59 #ifdef HAVE_SECURITY_OPENPAM_H
60 # include <security/openpam.h>
61 # define SHISHI_OPENPAM_LOGGING 1
62 #endif
63
64 #if defined DEBUG_PAM && defined HAVE_SECURITY__PAM_MACROS_H
65 # define DEBUG
66 # include <security/_pam_macros.h>
67 #else
68 # define D(x) /* nothing */
69 #endif
70
71 /* Rely on <security/pam_modules.h>
72 * for settings in general, as PAM_EXTERN
73 * is not universal among PAM implementations.
74 */
75 #ifndef PAM_EXTERN
76 # define PAM_EXTERN
77 #endif /* !PAM_EXTERN */
78
79 /* Flagging of options. */
80 static int opt_debug;
81 static const char *opt_principal = NULL;
82 static const char *opt_realm = NULL;
83
84 static char *servername = NULL;
85 static char *principal = NULL;
86
87 void
88 parse_argv (int argc, const char **argv)
89 {
90 int i;
91
92 for (i = 0; i < argc; i++)
93 {
94 if (!strcmp ("debug", argv[i]))
95 opt_debug++;
96 else if (!strncmp ("principal=", argv[i], strlen ("principal=")))
97 opt_principal = argv[i] + strlen ("principal=");
98 else if (!strncmp ("realm=", argv[i], strlen ("realm=")))
99 opt_realm = argv[i] + strlen ("realm=");
100 }
101 }
102
103 PAM_EXTERN int
104 pam_sm_authenticate (pam_handle_t * pamh,
105 int flags, int argc, const char **argv)
106 {
107 Shishi *h = NULL;
108 Shishi_key *key = NULL;
109 Shishi_tkt *tkt = NULL;
110 Shishi_tkts_hint hint;
111 int retval, rc;
112 const char *user = NULL;
113 const char *password = NULL;
114 char *realm = NULL;
115 int i;
116 struct pam_conv *conv;
117 struct pam_message *pmsg[1], msg[1];
118 struct pam_response *resp;
119 int nargs = 1;
120
121 D (("called."));
122 D (("flags %d argc %d", flags, argc));
123 for (i = 0; i < argc; i++)
124 D (("argv[%d]=%s", i, argv[i]));
125
126 parse_argv (argc, argv);
127
128 rc = shishi_init (&h);
129 if (rc != SHISHI_OK)
130 {
131 h = NULL;
132 D (("shishi_init() failed: %s", shishi_strerror (retval)));
133 retval = PAM_AUTHINFO_UNAVAIL;
134 goto done;
135 }
136
137 /* Extract overriding realm setting. */
138 if (opt_realm && *opt_realm)
139 shishi_realm_default_set (h, opt_realm);
140
141 /* Extract overriding host principal name. */
142 if (opt_principal && *opt_principal)
143 {
144 rc = shishi_parse_name (h, opt_principal, &principal, &realm);
145 if (rc != SHISHI_OK)
146 {
147 D (("Could not parse name: %s\n", shishi_strerror (rc)));
148 retval = PAM_AUTHINFO_UNAVAIL;
149 goto done;
150 }
151
152 /* The present REALM is allowed to override OPT_REALM.
153 * PRINCIPAL is available for later use in the ticket.
154 */
155 if (realm && *realm)
156 shishi_realm_default_set (h, realm);
157 }
158
159 /* Detect the calling user client. */
160 retval = pam_get_user (pamh, &user, NULL);
161 if (retval != PAM_SUCCESS)
162 {
163 D (("get user returned error: %s", pam_strerror (pamh, retval)));
164 goto done;
165 }
166 D (("get user returned: %s", user));
167
168 shishi_principal_default_set (h, user);
169
170 if (opt_debug)
171 {
172 #if defined SHISHI_LINUXPAM_LOGGING
173 pam_syslog (pamh, LOG_INFO, "Request from `%s@%s'.",
174 shishi_principal_default (h), shishi_realm_default (h));
175 #elif defined SHISHI_OPENPAM_LOGGING
176 openpam_log (PAM_LOG_VERBOSE, "Request from `%s@%s'.",
177 shishi_principal_default (h), shishi_realm_default (h));
178 #endif
179 }
180
181 retval = pam_get_item (pamh, PAM_AUTHTOK, (const void **) &password);
182 if (retval != PAM_SUCCESS)
183 {
184 D (("get password returned error: %s", pam_strerror (pamh, retval)));
185 goto done;
186 }
187 D (("get password returned: %s", password));
188
189 if (password == NULL)
190 {
191 retval = pam_get_item (pamh, PAM_CONV, (const void **) &conv);
192 if (retval != PAM_SUCCESS)
193 {
194 D (("get conv returned error: %s", pam_strerror (pamh, retval)));
195 goto done;
196 }
197
198 pmsg[0] = &msg[0];
199 asprintf ((char **) &msg[0].msg, "Password for `%s@%s': ",
200 shishi_principal_default (h), shishi_realm_default (h));
201 msg[0].msg_style = PAM_PROMPT_ECHO_OFF;
202 resp = NULL;
203
204 retval = conv->conv (nargs, (const struct pam_message **) pmsg,
205 &resp, conv->appdata_ptr);
206
207 free ((char *) msg[0].msg);
208
209 if (retval != PAM_SUCCESS)
210 {
211 D (("conv returned error: %s", pam_strerror (pamh, retval)));
212 goto done;
213 }
214
215 D (("conv returned: %s", resp->resp));
216
217 password = resp->resp;
218
219 retval = pam_set_item (pamh, PAM_AUTHTOK, password);
220 if (retval != PAM_SUCCESS)
221 {
222 D (("set_item returned error: %s", pam_strerror (pamh, retval)));
223 goto done;
224 }
225 }
226
227 /* Is the service name "host" being overridden? */
228 if (principal && *principal && strchr (principal, '/'))
229 {
230 servername = strdup (principal);
231 if (!servername)
232 {
233 retval = PAM_BUF_ERR;
234 D (("failed at duplicating name: %s", principal));
235 goto done;
236 }
237 }
238
239 if (!servername)
240 servername= shishi_server_for_local_service (h, "host");
241
242 memset (&hint, 0, sizeof (hint));
243 hint.client = (char *) shishi_principal_default (h);
244 hint.server = servername;
245 hint.passwd = (char *) password;
246
247 tkt = shishi_tkts_get (shishi_tkts_default (h), &hint);
248 if (tkt == NULL)
249 {
250 free (servername);
251 D (("TGS exchange failed: %s\n", shishi_error (h)));
252 retval = PAM_AUTHINFO_UNAVAIL;
253 goto done;
254 }
255
256 key = shishi_hostkeys_for_serverrealm (h, servername,
257 shishi_realm_default (h));
258 if (key == NULL)
259 {
260 free (servername);
261 D (("Key not found: %s\n", shishi_error (h)));
262 retval = PAM_AUTHINFO_UNAVAIL;
263 goto done;
264 }
265
266 if (opt_debug)
267 {
268 #if defined SHISHI_LINUXPAM_LOGGING
269 pam_syslog (pamh, LOG_INFO, "Requested server `%s@%s'.",
270 servername, shishi_realm_default (h));
271 #elif defined SHISHI_OPENPAM_LOGGING
272 openpam_log (PAM_LOG_VERBOSE, "Requested server `%s@%s'.",
273 servername, shishi_realm_default (h));
274 #endif
275 }
276
277 free (servername);
278
279 rc = shishi_tkt_decrypt (tkt, key);
280 if (rc != SHISHI_OK)
281 {
282 D (("Could not decrypt ticket: %s\n", shishi_strerror (rc)));
283 retval = PAM_AUTHINFO_UNAVAIL;
284 goto done;
285 }
286
287 retval = PAM_SUCCESS;
288
289 done:
290 if (h)
291 shishi_done (h);
292 pam_set_data (pamh, "shishi_setcred_return", (void *) (intptr_t) retval, NULL);
293 D (("done. [%s]", pam_strerror (pamh, retval)));
294
295 return retval;
296 }
297
298 PAM_EXTERN int
299 pam_sm_setcred (pam_handle_t * pamh, int flags, int argc, const char **argv)
300 {
301 int retval;
302 int auth_retval;
303
304 D (("called."));
305
306 retval = pam_get_data (pamh, "shishi_setcred_return",
307 (const void **) &auth_retval);
308 if (retval != PAM_SUCCESS)
309 return PAM_CRED_UNAVAIL;
310
311 /* XXX save ticket in user's file here
312 XXX support CRED_EXPIRED */
313
314 switch (auth_retval)
315 {
316 case PAM_SUCCESS:
317 retval = PAM_SUCCESS;
318 break;
319
320 case PAM_USER_UNKNOWN:
321 retval = PAM_USER_UNKNOWN;
322 break;
323
324 case PAM_AUTH_ERR:
325 default:
326 retval = PAM_CRED_ERR;
327 break;
328 }
329
330 D (("done. [%s]", pam_strerror (pamh, retval)));
331
332 return retval;
333 }
334
335 PAM_EXTERN int
336 pam_sm_acct_mgmt (pam_handle_t * pamh, int flags, int argc, const char **argv)
337 {
338 int retval;
339
340 D (("called."));
341
342 /* TODO: check if password expired? */
343 retval = PAM_SUCCESS;
344
345 D (("done. [%s]", pam_strerror (pamh, retval)));
346
347 return retval;
348 }
349
350 PAM_EXTERN int
351 pam_sm_open_session (pam_handle_t * pamh,
352 int flags, int argc, const char **argv)
353 {
354 int retval;
355
356 D (("called."));
357
358 /* TODO: afslog()? */
359 retval = PAM_SUCCESS;
360
361 D (("done. [%s]", pam_strerror (pamh, retval)));
362
363 return retval;
364 }
365
366 PAM_EXTERN int
367 pam_sm_close_session (pam_handle_t * pamh,
368 int flags, int argc, const char **argv)
369 {
370 int retval;
371
372 D (("called."));
373
374 /* TODO: destroy tickets? destroy AFS tokens? */
375 retval = PAM_SUCCESS;
376
377 D (("done. [%s]", pam_strerror (pamh, retval)));
378
379 return retval;
380 }
381
382 PAM_EXTERN int
383 pam_sm_chauthtok (pam_handle_t * pamh, int flags, int argc, const char **argv)
384 {
385 int retval;
386
387 D (("called."));
388
389 /* TODO: Change password */
390 retval = PAM_SUCCESS;
391
392 D (("done. [%s]", pam_strerror (pamh, retval)));
393
394 return retval;
395 }
396
397 /* Linux-PAM. */
398 #ifdef PAM_STATIC
399
400 struct pam_module _pam_shishi_modstruct = {
401 "pam_shishi",
402 pam_sm_authenticate,
403 pam_sm_setcred,
404 pam_sm_acct_mgmt,
405 pam_sm_open_session,
406 pam_sm_close_session,
407 pam_sm_chauthtok
408 };
409
410 #endif /* PAM_STATIC */
411
412 /* OpenPAM */
413 #ifdef PAM_MODULE_ENTRY
414
415 PAM_MODULE_ENTRY("pam_shishi");
416
417 #endif /* PAM_MODULE_ENTRY */