Debian Codegraph @

pam_chroot.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Linux-PAM session chroot()er
3 * account, session, authentication
4 *
5 * $Id: pam_chroot.c,v 1.3 2004/06/24 02:01:44 schmolli Exp $
6 */
7
8 #include <syslog.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <regex.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <ctype.h>
16 #include <stdarg.h>
17 #include <grp.h>
18 #include <pwd.h>
19 #include <sys/stat.h>
20
21 #define PAM_SM_AUTH
22 #define PAM_SM_ACCOUNT
23 #define PAM_SM_SESSION
24 #include <security/pam_modules.h>
25 #include <security/_pam_macros.h>
26
27 #define CONFIG "/etc/security/chroot.conf"
28 #define LINELEN 1024 /* max length (bytes) of line in config file */
29 #define SCRATCH 32 /* max length (bytes) for scribble space */
30 #define MAX_GROUPS 64 /* maximum number of groups we handle */
31
32 /* defines for flags */
33 #define _PAM_OPTS_NOOPTS 0x0000
34 #define _PAM_OPTS_DEBUG 0x0001
35 #define _PAM_OPTS_SILENT 0x0002
36 #define _PAM_OPTS_NOTFOUNDFAILS 0x0004
37 #define _PAM_OPTS_NO_CHROOT 0x0008
38 #define _PAM_OPTS_USE_REGEX 0x0010
39 #define _PAM_OPTS_USE_EXT_REGEX 0x0030 /* includes _PAM_OPTS_USE_REGEX */
40 #define _PAM_OPTS_USE_GROUPS 0x0040
41 #define _PAM_OPTS_SECCHECKS 0x0080
42
43 /* defines for (internal) return values */
44 #define _PAM_CHROOT_INTERNALERR -2
45 #define _PAM_CHROOT_SYSERR -1
46 #define _PAM_CHROOT_OK 0
47 #define _PAM_CHROOT_USERNOTFOUND 1
48 #define _PAM_CHROOT_INCOMPLETE 2
49
50
51 struct _pam_opts {
52 int16_t flags; /* combined option flags */
53 char* chroot_dir; /* where to chroot to */
54 char* conf; /* name of pam_chroot config file */
55 char* module; /* module currently being processed */
56 };
57
58 static void _pam_log(int err, const char *format, ...) {
59 va_list args;
60
61 va_start(args, format);
62 openlog("pam_chroot", LOG_PID, LOG_AUTHPRIV);
63 vsyslog(err, format, args);
64 va_end(args);
65 closelog();
66 }
67
68 /* initialize opts to a standard known state */
69 int _pam_opts_init(struct _pam_opts* opts) {
70 if(NULL == opts) {
71 _pam_log(LOG_ERR, "%s: NULL opts pointer", __FUNCTION__);
72 return _PAM_CHROOT_INTERNALERR;
73 }
74
75 opts->flags = _PAM_OPTS_NOOPTS;
76 opts->chroot_dir = NULL;
77
78 opts->conf = x_strdup(CONFIG);
79 if(NULL == opts->conf) {
80 _pam_log(LOG_ERR, "strdup: %s", strerror(errno));
81 return _PAM_CHROOT_SYSERR;
82 }
83
84 return _PAM_CHROOT_OK;
85 }
86
87 /* configure opts per the passed flags and cmd line args */
88 int _pam_opts_config(struct _pam_opts* opts, int flags,
89 int argc, const char** argv)
90 {
91 int i;
92
93 if(NULL == opts) {
94 _pam_log(LOG_ERR, "%s: NULL opts pointer", __FUNCTION__);
95 return _PAM_CHROOT_INTERNALERR;
96 }
97
98 if(flags & PAM_SILENT) { opts->flags = opts->flags | _PAM_OPTS_SILENT; }
99 if((flags & PAM_DISALLOW_NULL_AUTHTOK) &&
100 (!strcmp(opts->module, "auth") || !strcmp(opts->module, "account")))
101 {
102 opts->flags = opts->flags | _PAM_OPTS_NOTFOUNDFAILS;
103 }
104
105 /* parse command line args */
106 for(i = 0; i < argc; i++) {
107 if(!strcmp(argv[i], "debug")) {
108 opts->flags = opts->flags | _PAM_OPTS_DEBUG;
109 } else if(!strcmp(argv[i], "no_warn")) {
110 opts->flags = opts->flags | _PAM_OPTS_SILENT;
111 } else if(!strcmp(argv[i], "use_first_pass") ||
112 !strcmp(argv[i], "try_first_pass") ||
113 !strcmp(argv[i], "use_mapped_pass")) {
114 /* ignore these, pam_chroot doesn't care about passwds */
115 } else if(!strcmp(argv[i], "no_chroot")) {
116 opts->flags = opts->flags | _PAM_OPTS_NO_CHROOT;
117 } else if(!strcmp(argv[i], "use_regex")) {
118 opts->flags = opts->flags | _PAM_OPTS_USE_REGEX;
119 } else if(!strcmp(argv[i], "use_ext_regex")) {
120 opts->flags = opts->flags | _PAM_OPTS_USE_EXT_REGEX;
121 } else if(!strcmp(argv[i], "use_groups")) {
122 opts->flags = opts->flags | _PAM_OPTS_USE_GROUPS;
123 } else if(!strcmp(argv[i], "sec_checks")) {
124 opts->flags = opts->flags | _PAM_OPTS_SECCHECKS;
125 } else if(!strncmp(argv[i], "notfound=", 9)) {
126 if(!strcmp(argv[i] + 9, "success")) {
127 opts->flags = opts->flags & (~_PAM_OPTS_NOTFOUNDFAILS);
128 } else if(!strcmp(argv[i] + 9, "failure")) {
129 opts->flags = opts->flags | _PAM_OPTS_NOTFOUNDFAILS;
130 } else {
131 _pam_log(LOG_ERR, "bad config option: \"%s\"", argv[i]);
132 }
133 } else if(!strncmp(argv[i], "onerr=", 6)) {
134 if(!strcmp(argv[i] + 6, "succeed")) {
135 opts->flags = opts->flags & (~_PAM_OPTS_NOTFOUNDFAILS);
136 } else if(!strcmp(argv[i] + 9, "fail")) {
137 opts->flags = opts->flags | _PAM_OPTS_NOTFOUNDFAILS;
138 } else {
139 _pam_log(LOG_ERR, "bad config option: \"%s\"", argv[i]);
140 }
141 } else if(!strncmp(argv[i], "chroot_dir=", 11)) {
142 if(*(argv[i] + 11) == '\0') {
143 _pam_log(LOG_ERR,
144 "bad config option: \"%s\": specify a directory", argv[i]);
145 } else if(NULL != opts->chroot_dir) {
146 _pam_log(LOG_ERR,
147 "bad config option: \"%s\": chroot dir already set", argv[i]);
148 } else {
149 opts->chroot_dir = x_strdup(argv[i] + 11);
150 if(NULL == opts->chroot_dir) {
151 _pam_log(LOG_ERR, "strdup: %s", strerror(errno));
152 }
153 }
154 } else {
155 _pam_log(LOG_ERR, "unrecognized config option: \"%s\"", argv[i]);
156 }
157 }
158
159 return _PAM_CHROOT_OK;
160 }
161
162 /* free the allocated memory of a struct _pam_opts */
163 int _pam_opts_free(struct _pam_opts* opts) {
164 if(NULL == opts) {
165 _pam_log(LOG_ERR, "%s: NULL opts pointer", __FUNCTION__);
166 }
167 _pam_drop(opts->chroot_dir);
168 _pam_drop(opts->conf);
169
170 return _PAM_CHROOT_OK;
171 }
172
173 /* if the system doesn't have getgrouplist(), then I have to do it myself */
174 #ifndef HAVE_GETGROUPLIST
175 #define _PAM_GETUGROUPS _pam_getugroups
176 /* *user is the user to collect info on
177 * gid is a gid to include in the grplist
178 * *grps is the array of gid_t to return the grplist in (if not NULL)
179 * *ngrps is the max number of gid_t to return in grplist AND where to
180 * store the actual number of gid_t returned
181 *
182 * return -1 if *ngrps is too small
183 */
184 int _pam_getugroups(const char *user, gid_t gid, gid_t *grps, int *ngrps) {
185 struct group *grp;
186 int gcount = 0;
187 char **uptr;
188
189 if(NULL == user || NULL == ngrps) { return -1; }
190 if(0 > *ngrps || 0 > gid) { return -1; }
191
192 if(gcount < *ngrps) {
193 if(NULL != grps) { grps[gcount] = gid; }
194 gcount++;
195 } else {
196 *ngrps = gcount;
197 endgrent();
198 return -1;
199 }
200
201 setgrent();
202 while(NULL != (grp = getgrent())) {
203 if(NULL == grp->gr_name || NULL == grp->gr_mem) { continue; }
204
205 /* don't add gids more than once, don't do this check if grps is NULL */
206 /* yes, that's incorrect, but it's harder to do it correctly, and since
207 * we're just counting, for the NULL case, it's unlikely to cause a
208 * real problem in reasonable circumstances */
209 if(NULL != grps) {
210 int i;
211 for(i = 0 ; i < gcount ; i++) {
212 if(grp->gr_gid == grps[i]) { i = -1; break; }
213 }
214 if(-1 == i) { continue; }
215 }
216
217 for(uptr = grp->gr_mem ; NULL != *uptr ; uptr++) {
218 if(0 != strcmp(*uptr, user)) { continue; }
219
220 if(gcount < *ngrps) {
221 if(NULL != grps) { grps[gcount] = grp->gr_gid; }
222 gcount++;
223 } else {
224 *ngrps = gcount;
225 endgrent();
226 return -1;
227 }
228 }
229 }
230 endgrent();
231 *ngrps = gcount;
232 return gcount;
233 }
234 #else
235 #define _PAM_GETUGROUPS getgrouplist
236 #endif
237
238 /* generate a list of group names from a list of gids */
239 char** _pam_get_groups(const char* user, struct _pam_opts* opts) {
240 int i, ret, ngroups = MAX_GROUPS;
241 struct group* grp;
242 struct passwd* pwd;
243 gid_t *gids;
244 char **groups;
245 char scratch[SCRATCH];
246
247 pwd = getpwnam(user);
248 if(pwd == NULL) {
249 _pam_log(LOG_ERR, "%s: user \"%s\" not a valid username",
250 opts->module, user);
251 return NULL;
252 }
253
254 /* get a list of all the gids for this username */
255 _PAM_GETUGROUPS(pwd->pw_name, pwd->pw_gid, NULL, &ngroups);
256 if(0 >= ngroups) {
257 _pam_log(LOG_ERR, "%s: error fetching groups for user \"%s\"",
258 opts->module, user);
259 return NULL;
260 }
261 gids = (gid_t*) malloc(ngroups*sizeof(gid_t));
262 if(NULL == gids) {
263 _pam_log(LOG_ERR, "%s: %s: malloc: %s",
264 opts->module, __FUNCTION__, strerror(errno));
265 return NULL;
266 }
267 ret = _PAM_GETUGROUPS(pwd->pw_name, pwd->pw_gid, gids, &ngroups);
268 if(-1 == ret) {
269 _pam_log(LOG_WARNING,
270 "%s: %s: _PAM_GETUGROUPS found more gids on second run",
271 opts->module, __FUNCTION__);
272 }
273 if(0 >= ngroups) {
274 _pam_log(LOG_ERR,
275 "%s: %s: _PAM_GETUGROUPS returned no groups for user \"%s\"",
276 opts->module, __FUNCTION__, user);
277 _pam_drop(gids);
278 return NULL;
279 }
280
281 /* resolve gids into grpnams */
282 groups = (char**)malloc((ngroups+1)*sizeof(char*));
283 if(NULL == groups) {
284 _pam_log(LOG_ERR, "%s: %s: malloc: %s",
285 opts->module, __FUNCTION__, strerror(errno));
286 _pam_drop(gids);
287 return NULL;
288 }
289 for(i = 0; i < ngroups ; i++) {
290 grp = getgrgid(gids[i]);
291 if(NULL == grp) {
292 _pam_log(LOG_DEBUG, "%s: no grnam for gid %d", opts->module, gids[i]);
293 snprintf(scratch, SCRATCH, "%d", gids[i]);
294 groups[i] = x_strdup(scratch);
295 } else {
296 groups[i] = x_strdup(grp->gr_name);
297 }
298 }
299 groups[i] = NULL;
300 _pam_drop(gids);
301
302 return groups;
303 }
304
305 /* helper function to free group list */
306 void _pam_free_groups(char **groups) {
307 int i = 0;
308
309 if(NULL == groups) { return; }
310 while(NULL != groups[i]) {
311 _pam_drop(groups[i]);
312 i++;
313 }
314 _pam_drop(groups);
315 }
316
317 /* verify that the arguement path is root owned and not writable by
318 * group or other
319 * return 0 if ok, 1 if not, -1 on system error */
320 int _pam_check_path_perms(char *path, struct _pam_opts* opts) {
321 int i = 0, rslt = 0;
322 char save;
323 struct stat st;
324
325 path = x_strdup(path);
326 if(NULL == path) {
327 _pam_log(LOG_ERR, "strdup: %s", strerror(errno));
328 return -1;
329 }
330
331 while('\0' != path[i]) {
332 if('/' == path[i]) {
333 save = path[i + 1];
334 path[i + 1] = '\0';
335
336 if(-1 == stat(path, &st)) {
337 _pam_log(LOG_ERR, "stat(%s): %s", path, strerror(errno));
338 rslt = -1;
339 break;
340 }
341
342 if((0 != st.st_uid) || (st.st_mode & (S_IWGRP | S_IWOTH))) {
343 _pam_log(LOG_ERR, "bad ownership/perms on %s", path);
344 rslt = 1;
345 break;
346 }
347 path[i + 1] = save;
348 }
349 i++;
350 }
351 if((NULL != opts) && (opts->flags & _PAM_OPTS_DEBUG)) {
352 _pam_log(LOG_NOTICE,
353 "%s: ownership/perms ok on %s", opts->module, path);
354 }
355
356 _pam_drop(path);
357 return rslt;
358 }
359
360 /* expand chroot path */
361 /* path - string to expand
362 * user - username, for %u expansion
363 * grp - group, for %g expansion
364 * match - name entry that was matched
365 * matchptr - array of regmatch_t containing matched substrings (assume
366 * at least 10 items in array)
367 */
368 char* _pam_expand_chroot_dir(const char* path, const char* user,
369 const char* grp, const char* match, regmatch_t* matchptr,
370 struct _pam_opts* opts)
371 {
372 char ref; /* ref char */
373 char *pos; /* ref position */
374 char *exp; /* expanded path */
375 int reflen; /* length of expanded path */
376 int refnum; /* number of regex backreference */
377 int offset = 0; /* search offset */
378
379 if(NULL == path || NULL == user || NULL == opts) {
380 return NULL;
381 } else if(NULL == grp && (opts->flags & _PAM_OPTS_USE_GROUPS)) {
382 return NULL;
383 }
384
385 exp = x_strdup(path);
386 if(NULL == exp) {
387 _pam_log(LOG_ERR, "%s: strdup: %s", opts->module, strerror(errno));
388 return NULL;
389 }
390
391 while(NULL != (pos = strchr(exp + offset, '%'))) {
392 offset = pos - exp; /* save for post-realloc */
393 reflen = 0;
394 ref = *(pos + 1);
395
396 if('u' == tolower(ref)) { /* %u found */
397 reflen = strlen(user);
398 exp = (char*) realloc(exp, strlen(exp) + reflen - 1);
399 if(NULL == exp) { break; }
400 pos = exp + offset;
401 memmove(pos + reflen, pos + 2, strlen(pos + 2) + 1);
402 memcpy(pos, user, reflen);
403
404 } else if ('g' == tolower(ref)) { /* %g found */
405 reflen = strlen(grp);
406 exp = (char*) realloc(exp, strlen(exp) + reflen - 1);
407 if(NULL == exp) { break; }
408 pos = exp + offset;
409 memmove(pos + reflen, pos + 2, strlen(pos + 2) + 1);
410 memcpy(pos, grp, reflen);
411
412 } else if (isdigit(ref)) { /* backreference (%1...%9) found */
413 if(NULL == match) {
414 _pam_log(LOG_ERR,
415 "%s: backreference \"%%%c\" found, but subject of match is NULL",
416 opts->module, ref);
417 _pam_drop(exp);
418 return NULL;
419 }
420
421 refnum = ref - '0';
422 if(-1 != matchptr[refnum].rm_so) { /* submatch exists */
423 reflen = matchptr[refnum].rm_eo - matchptr[refnum].rm_so;
424 exp = (char*) realloc(exp, strlen(exp) + reflen + 1);
425 if(NULL == exp) { break; }
426 pos = exp + offset;
427 memmove(pos + reflen, pos + 2, strlen(pos + 2) + 1);
428 memcpy (pos, match + matchptr[refnum].rm_so, reflen);
429 } else {
430 _pam_log(LOG_ERR,
431 "%s: no submatch corresponding to backreference \"%%%c\"",
432 opts->module, ref);
433 _pam_drop(exp);
434 return NULL;
435 }
436
437 } else if ('%' == ref) { /* %% found */
438 memmove(pos, pos + 1, strlen(pos) + 1);
439 reflen = 1;
440
441 } else { /* unknown ref */
442 _pam_log(LOG_ERR, "%s: unknown reference \"%%%c\"",
443 opts->module, ref);
444 _pam_drop(exp);
445 return NULL;
446 }
447
448 offset += reflen; /* skip past what we just copied */
449 }
450
451 if(NULL == exp) { /* catch realloc errors - don't forget to free old exp! */
452 _pam_log(LOG_ERR, "%s: realloc: %s", opts->module, strerror(errno));
453 free(pos - offset);
454 return NULL;
455 }
456
457 if(opts->flags & _PAM_OPTS_DEBUG) {
458 _pam_log(LOG_NOTICE, "%s: expanded path \"%s\" -> \"%s\"",
459 opts->module, path, exp);
460 }
461 return exp;
462 }
463
464 /* parse the chroot.conf to find chroot_dir */
465 int _pam_get_chrootdir(const char* user, struct _pam_opts* opts) {
466 FILE* conf;
467 char conf_line[LINELEN];
468 int lineno, err, name_is_group, regflags = REG_ICASE, i;
469 char *name, *mark, *group = NULL, **group_list = NULL;
470
471 if(opts->flags & _PAM_OPTS_SECCHECKS) {
472 /* don't need to distinguish the errors for now */
473 if(0 != _pam_check_path_perms(opts->conf, opts)) {
474 return _PAM_CHROOT_SYSERR;
475 }
476 }
477 if(!(conf = fopen(opts->conf, "r"))) {
478 _pam_log(LOG_ERR,
479 "%s: fopen(%s): %s", opts->module, opts->conf, strerror(errno));
480 opts->chroot_dir = NULL;
481 return _PAM_CHROOT_SYSERR;
482 }
483
484 if(opts->flags & _PAM_OPTS_USE_GROUPS) {
485 group_list = _pam_get_groups(user, opts);
486 if(NULL == group_list) { /* probably some kind of malloc error */
487 fclose(conf);
488 return _PAM_CHROOT_SYSERR;
489 } else {
490 group = group_list[0];
491 }
492 }
493
494 lineno = 0; err = 0;
495 while(fgets(conf_line, LINELEN, conf)) {
496 ++lineno;
497
498 /* ignore comments and blank lines */
499 if((mark = strchr(conf_line, '#'))) { *mark = 0; }
500 if(!(name = strtok(conf_line, " \t\r\n"))) { continue; }
501
502 /* ignore lines that contain usernames/regexps but not directories */
503 if(!(mark = strtok(NULL, " \t\r\n"))) {
504 _pam_log(LOG_ERR, "%s: %s %d: no directory",
505 opts->module, opts->conf, lineno);
506 continue;
507 }
508
509 /* is it a group? */
510 name_is_group = 0;
511 if('@' == name[0]) {
512 if(opts->flags & _PAM_OPTS_USE_GROUPS) {
513 name_is_group = 1;
514 name++;
515 } else {
516 _pam_log(LOG_ERR, "%s: %s %d: found @group style syntax, but use_groups has not been turned on",
517 opts->module, opts->conf, lineno);
518 fclose(conf);
519 return _PAM_CHROOT_SYSERR;
520 }
521 }
522
523 if(opts->flags & _PAM_OPTS_USE_REGEX) {
524 regex_t name_regex;
525 regmatch_t matchptr[10];
526 char const* match = NULL;
527 if(opts->flags & _PAM_OPTS_USE_EXT_REGEX) {
528 regflags |= REG_EXTENDED;
529 }
530
531 if(0 != (err = regcomp(&name_regex, name, regflags))) {
532 size_t len = regerror(err, &name_regex, NULL, 0);
533 char *errbuf = malloc(len);
534 if(NULL == errbuf) {
535 _pam_log(LOG_ERR, "%s: %s: malloc: %s",
536 opts->module, __FUNCTION__, strerror(errno));
537 if(opts->flags & _PAM_OPTS_USE_GROUPS) {
538 _pam_free_groups(group_list);
539 }
540 regfree(&name_regex);
541 fclose(conf);
542 return _PAM_CHROOT_SYSERR;
543 }
544 regerror(err, &name_regex, errbuf, len);
545 _pam_log(LOG_ERR, "%s: %s %d: illegal regex \"%s\": %s",
546 opts->module, opts->conf, lineno, name, errbuf);
547
548 free(errbuf);
549 regfree(&name_regex);
550 continue; /* with the next line */
551 }
552
553 if(1 == name_is_group) {
554 for(i = 0; NULL != group_list[i]; i++) {
555 match = group_list[i];
556 err = regexec(&name_regex, match, 10, matchptr, 0);
557 if(0 == err) { break; }
558 }
559 } else {
560 match = user;
561 err = regexec(&name_regex, match, 10, matchptr, 0);
562 }
563 regfree(&name_regex);
564
565 if(0 == err) {
566 fclose(conf);
567
568 opts->chroot_dir = _pam_expand_chroot_dir(mark, user,
569 group, match, matchptr, opts);
570 if(NULL == opts->chroot_dir) {
571 _pam_log(LOG_ERR, "%s: unable to expand chroot_dir",
572 opts->module);
573 _pam_free_groups(group_list);
574 return _PAM_CHROOT_SYSERR;
575 }
576 if (opts->flags & _PAM_OPTS_DEBUG) {
577 _pam_log(LOG_NOTICE,
578 "%s: found chroot_dir \"%s\" for user \"%s\"",
579 opts->module, opts->chroot_dir, user);
580 }
581 _pam_free_groups(group_list);
582
583 return _PAM_CHROOT_OK;
584 }
585 } else {
586 char* tmp = name;
587
588 /* tack a NULL at the end of the name field */
589 while(('\0' != *tmp) && !isspace(*tmp)) { tmp++; }
590 *tmp = '\0';
591
592 if(1 == name_is_group) {
593 for(i = 0; NULL != group_list[i]; i++) {
594 if(0 == (err = strcmp(group_list[i], name))) { break; }
595 }
596 } else {
597 err = strcmp(user, name);
598 }
599
600 if(0 == err) {
601 fclose(conf);
602
603 opts->chroot_dir = _pam_expand_chroot_dir(mark, user,
604 group, NULL, NULL, opts);
605 if(NULL == opts->chroot_dir) {
606 _pam_log(LOG_ERR, "%s: unable to expand chroot_dir",
607 opts->module);
608 return _PAM_CHROOT_SYSERR;
609 }
610 if (opts->flags & _PAM_OPTS_DEBUG) {
611 _pam_log(LOG_NOTICE,
612 "%s: found chroot_dir \"%s\" for user \"%s\"",
613 opts->module, opts->chroot_dir, user);
614 }
615 _pam_free_groups(group_list);
616 return _PAM_CHROOT_OK;
617 }
618 }
619 if(opts->flags & _PAM_OPTS_DEBUG) {
620 _pam_log(LOG_NOTICE,
621 "%s: \"%s\" does not match \"%s\"",
622 opts->module, user, conf_line);
623 }
624 } /* end while(fgets(conf_line, LINELEN, conf)) */
625
626 if(opts->flags & _PAM_OPTS_DEBUG) {
627 _pam_log(LOG_NOTICE,
628 "%s: no match found for user \"%s\" in conf file \"%s\"",
629 opts->module, user, opts->conf);
630 }
631 fclose(conf);
632 _pam_free_groups(group_list);
633 opts->chroot_dir = NULL;
634 return _PAM_CHROOT_USERNOTFOUND;
635 }
636
637 /* This is the workhorse function. All of the pam_sm_* functions should
638 * initialize a _pam_opts struct with the command line args and flags,
639 * then pass it to this function */
640 int _pam_do_chroot(pam_handle_t *pamh, struct _pam_opts *opts) {
641 int err,debug;
642 char *name;
643 char const *user;
644
645 name = NULL;
646 debug = opts->flags & _PAM_OPTS_DEBUG;
647
648 err = pam_get_user(pamh, &user, NULL);
649 if(PAM_CONV_AGAIN == err) {
650 _pam_log(LOG_NOTICE, "$s: retry username lookup later", opts->module);
651 return _PAM_CHROOT_INCOMPLETE;
652 } else if(PAM_SUCCESS != err) {
653 _pam_log(LOG_ERR, "%s: can't get username", opts->module);
654 return _PAM_CHROOT_SYSERR;
655 }
656
657 if(opts->chroot_dir) { /* overrides the conf file */
658 if(debug) {
659 _pam_log(LOG_NOTICE,
660 "%s: chrootdir (%s) specified, ignoring conf file",
661 opts->module, opts->chroot_dir);
662 }
663 err = _PAM_CHROOT_OK;
664 } else {
665 if(debug) {
666 _pam_log(LOG_NOTICE,
667 "%s: reading config file (%s)", opts->module, opts->conf);
668 }
669 err = _pam_get_chrootdir(user, opts);
670 }
671
672 if(_PAM_CHROOT_OK == err) {
673 if(debug) {
674 _pam_log(LOG_NOTICE, "%s: preparing to chroot()", opts->module);
675 }
676
677 if(NULL == opts->chroot_dir) {
678 /* This is a state that I should never see. If the user wasn't in
679 * the conf file, then USERNOTFOUND should have been returned. */
680 _pam_log(LOG_ERR,
681 "%s: no chroot_dir set for \"%s\"", opts->module, user);
682 return _PAM_CHROOT_INTERNALERR;
683 }
684
685 if(opts->flags & _PAM_OPTS_SECCHECKS) {
686 if(0 != _pam_check_path_perms(opts->chroot_dir, opts)) {
687 _pam_log(LOG_ERR,
688 "%s: chroot_dir \"%s\" failed security check",
689 opts->module, opts->chroot_dir);
690 return _PAM_CHROOT_SYSERR;
691 }
692 }
693
694 if(opts->flags & _PAM_OPTS_NO_CHROOT) {
695 if(debug) {
696 _pam_log(LOG_NOTICE,
697 "%s: no_chroot is set, skipping chroot(%s)",
698 opts->module, opts->chroot_dir);
699 }
700 } else if(chdir(opts->chroot_dir) != 0) {
701 _pam_log(LOG_ERR,
702 "%s: chdir(%s): %s",
703 opts->module, opts->chroot_dir, strerror(errno));
704 return _PAM_CHROOT_SYSERR;
705 } else if(chroot(opts->chroot_dir) != 0) {
706 _pam_log(LOG_ERR,
707 "%s: chroot(%s): %s",
708 opts->module, opts->chroot_dir, strerror(errno));
709 return _PAM_CHROOT_SYSERR;
710 } else {
711 if(debug) {
712 _pam_log(LOG_NOTICE,
713 "%s: chroot(%s) ok", opts->module, opts->chroot_dir);
714 }
715 }
716 return _PAM_CHROOT_OK;
717 } else if(_PAM_CHROOT_USERNOTFOUND == err) {
718 if(debug) {
719 _pam_log(LOG_NOTICE, "%s: no match for %s in %s",
720 opts->module, user, opts->conf);
721 }
722 return _PAM_CHROOT_USERNOTFOUND;
723 } else {
724 _pam_log(LOG_ERR,
725 "%s: error determining chrootdir: user=\"%s\", dir=\"%s\"",
726 opts->module, user, opts->chroot_dir);
727 return err;
728 }
729 }
730
731 PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
732 int argc, const char **argv)
733 {
734 int err;
735 struct _pam_opts opts;
736
737 _pam_opts_init(&opts);
738 _pam_opts_config(&opts, flags, argc, argv);
739 opts.module = "auth";
740
741 err = _pam_do_chroot(pamh, &opts);
742 switch(err) {
743 case _PAM_CHROOT_OK:
744 if(opts.flags & _PAM_OPTS_DEBUG) {
745 _pam_log(LOG_NOTICE, "%s: returning success", opts.module);
746 }
747 err = PAM_SUCCESS;
748 break;
749
750 case _PAM_CHROOT_USERNOTFOUND:
751 if(opts.flags & _PAM_OPTS_DEBUG) {
752 _pam_log(LOG_NOTICE, "%s: unknown user", opts.module);
753 }
754 err = PAM_USER_UNKNOWN;
755 break;
756
757 case _PAM_CHROOT_INCOMPLETE:
758 _pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module);
759 err = PAM_INCOMPLETE;
760 break;
761
762 case _PAM_CHROOT_INTERNALERR:
763 _pam_log(LOG_ERR, "%s: internal error encountered", opts.module);
764 err = PAM_AUTH_ERR;
765 break;
766
767 default:
768 if(opts.flags & _PAM_OPTS_DEBUG) {
769 _pam_log(LOG_NOTICE, "%s: returning failure", opts.module);
770 }
771 err = PAM_AUTH_ERR;
772 break;
773 }
774 _pam_opts_free(&opts);
775 return err;
776 }
777
778 PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags,
779 int argc, const char **argv)
780 {
781 _pam_log(LOG_ERR, "not a credentialator");
782 return PAM_IGNORE;
783 }
784
785 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
786 int argc, const char **argv)
787 {
788 int err;
789 struct _pam_opts opts;
790
791 _pam_opts_init(&opts);
792 _pam_opts_config(&opts, flags, argc, argv);
793 opts.module = "account";
794
795 err = _pam_do_chroot(pamh, &opts);
796 switch(err) {
797 case _PAM_CHROOT_OK:
798 if(opts.flags & _PAM_OPTS_DEBUG) {
799 _pam_log(LOG_NOTICE, "%s: returning success", opts.module);
800 }
801 err = PAM_SUCCESS;
802 break;
803
804 case _PAM_CHROOT_USERNOTFOUND:
805 if(opts.flags & _PAM_OPTS_DEBUG) {
806 _pam_log(LOG_NOTICE, "%s: unknown user", opts.module);
807 }
808 err = PAM_USER_UNKNOWN;
809 break;
810
811 case _PAM_CHROOT_INCOMPLETE:
812 _pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module);
813 err = PAM_INCOMPLETE;
814 break;
815
816 case _PAM_CHROOT_INTERNALERR:
817 _pam_log(LOG_ERR, "%s: internal error encountered", opts.module);
818 err = PAM_AUTH_ERR;
819 break;
820
821 default:
822 if(opts.flags & _PAM_OPTS_DEBUG) {
823 _pam_log(LOG_NOTICE, "%s: returning failure", opts.module);
824 }
825 err = PAM_AUTH_ERR;
826 break;
827 }
828 _pam_opts_free(&opts);
829 return err;
830 }
831
832 PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags,
833 int argc, const char **argv)
834 {
835 int err;
836 struct _pam_opts opts;
837
838 _pam_opts_init(&opts);
839 _pam_opts_config(&opts, flags, argc, argv);
840 opts.module = "session";
841
842 err = _pam_do_chroot(pamh, &opts);
843 switch(err) {
844 case _PAM_CHROOT_OK:
845 if(opts.flags & _PAM_OPTS_DEBUG) {
846 _pam_log(LOG_NOTICE, "%s: returning success", opts.module);
847 }
848 err = PAM_SUCCESS;
849 break;
850
851 case _PAM_CHROOT_USERNOTFOUND:
852 if(opts.flags & _PAM_OPTS_NOTFOUNDFAILS) {
853 if(opts.flags & _PAM_OPTS_DEBUG) {
854 _pam_log(LOG_NOTICE,
855 "%s: notfound=failure is set, returning failure",
856 opts.module);
857 }
858 err = PAM_SESSION_ERR;
859 } else {
860 err = PAM_SUCCESS;
861 }
862 break;
863
864 case _PAM_CHROOT_INCOMPLETE:
865 _pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module);
866 err = PAM_INCOMPLETE;
867 break;
868
869 case _PAM_CHROOT_INTERNALERR:
870 _pam_log(LOG_ERR, "%s: internal error encountered", opts.module);
871 err = PAM_SESSION_ERR;
872 break;
873
874 default:
875 if(opts.flags * _PAM_OPTS_DEBUG) {
876 _pam_log(LOG_NOTICE, "%s: returning failure", opts.module);
877 }
878 err = PAM_SESSION_ERR;
879 break;
880 }
881 _pam_opts_free(&opts);
882 return err;
883 }
884
885
886 PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags,
887 int argc, const char **argv)
888 {
889 return PAM_SUCCESS;
890 }
891
892 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
893 int argc, const char **argv)
894 {
895 _pam_log(LOG_ERR, "password management group is unsupported");
896 return PAM_SERVICE_ERR;
897 }
898