| 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", __extension__ __FUNCTION__); |
| 72 |
return _PAM_CHROOT_INTERNALERR; |
| 73 |
} |
| 74 |
|
| 75 |
opts->flags = _PAM_OPTS_NOOPTS; |
| 76 |
opts->chroot_dir = NULL; |
| 77 |
|
| •78 |
opts->conf = 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", __extension__ __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] + 6, "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 = 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", __extension__ __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) { 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, __extension__ __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, __extension__ __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, __extension__ __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, __extension__ __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] = strdup(scratch); |
| 295 |
} else { |
| •296 |
groups[i] = 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 = 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 = 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, __extension__ __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 const *user; |
| 643 |
|
| 644 |
debug = opts->flags & _PAM_OPTS_DEBUG; |
| 645 |
|
| 646 |
err = pam_get_user(pamh, &user, NULL); |
| 647 |
if(PAM_CONV_AGAIN == err) { |
| 648 |
_pam_log(LOG_NOTICE, "$s: retry username lookup later", opts->module); |
| 649 |
return _PAM_CHROOT_INCOMPLETE; |
| 650 |
} else if(PAM_SUCCESS != err) { |
| 651 |
_pam_log(LOG_ERR, "%s: can't get username", opts->module); |
| 652 |
return _PAM_CHROOT_SYSERR; |
| 653 |
} |
| 654 |
|
| 655 |
if(opts->chroot_dir) { /* overrides the conf file */ |
| 656 |
if(debug) { |
| 657 |
_pam_log(LOG_NOTICE, |
| 658 |
"%s: chrootdir (%s) specified, ignoring conf file", |
| 659 |
opts->module, opts->chroot_dir); |
| 660 |
} |
| 661 |
err = _PAM_CHROOT_OK; |
| 662 |
} else { |
| 663 |
if(debug) { |
| 664 |
_pam_log(LOG_NOTICE, |
| 665 |
"%s: reading config file (%s)", opts->module, opts->conf); |
| 666 |
} |
| 667 |
err = _pam_get_chrootdir(user, opts); |
| 668 |
} |
| 669 |
|
| 670 |
if(_PAM_CHROOT_OK == err) { |
| 671 |
if(debug) { |
| 672 |
_pam_log(LOG_NOTICE, "%s: preparing to chroot()", opts->module); |
| 673 |
} |
| 674 |
|
| 675 |
if(NULL == opts->chroot_dir) { |
| 676 |
/* This is a state that I should never see. If the user wasn't in |
| 677 |
* the conf file, then USERNOTFOUND should have been returned. */ |
| 678 |
_pam_log(LOG_ERR, |
| 679 |
"%s: no chroot_dir set for \"%s\"", opts->module, user); |
| 680 |
return _PAM_CHROOT_INTERNALERR; |
| 681 |
} |
| 682 |
|
| 683 |
if(opts->flags & _PAM_OPTS_SECCHECKS) { |
| 684 |
if(0 != _pam_check_path_perms(opts->chroot_dir, opts)) { |
| 685 |
_pam_log(LOG_ERR, |
| 686 |
"%s: chroot_dir \"%s\" failed security check", |
| 687 |
opts->module, opts->chroot_dir); |
| 688 |
return _PAM_CHROOT_SYSERR; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if(opts->flags & _PAM_OPTS_NO_CHROOT) { |
| 693 |
if(debug) { |
| 694 |
_pam_log(LOG_NOTICE, |
| 695 |
"%s: no_chroot is set, skipping chroot(%s)", |
| 696 |
opts->module, opts->chroot_dir); |
| 697 |
} |
| 698 |
} else if(chdir(opts->chroot_dir) != 0) { |
| 699 |
_pam_log(LOG_ERR, |
| 700 |
"%s: chdir(%s): %s", |
| 701 |
opts->module, opts->chroot_dir, strerror(errno)); |
| 702 |
return _PAM_CHROOT_SYSERR; |
| 703 |
} else if(chroot(opts->chroot_dir) != 0) { |
| 704 |
_pam_log(LOG_ERR, |
| 705 |
"%s: chroot(%s): %s", |
| 706 |
opts->module, opts->chroot_dir, strerror(errno)); |
| 707 |
return _PAM_CHROOT_SYSERR; |
| 708 |
} else { |
| 709 |
if(debug) { |
| 710 |
_pam_log(LOG_NOTICE, |
| 711 |
"%s: chroot(%s) ok", opts->module, opts->chroot_dir); |
| 712 |
} |
| 713 |
} |
| 714 |
return _PAM_CHROOT_OK; |
| 715 |
} else if(_PAM_CHROOT_USERNOTFOUND == err) { |
| 716 |
if(debug) { |
| 717 |
_pam_log(LOG_NOTICE, "%s: no match for %s in %s", |
| 718 |
opts->module, user, opts->conf); |
| 719 |
} |
| 720 |
return _PAM_CHROOT_USERNOTFOUND; |
| 721 |
} else { |
| 722 |
_pam_log(LOG_ERR, |
| 723 |
"%s: error determining chrootdir: user=\"%s\", dir=\"%s\"", |
| 724 |
opts->module, user, opts->chroot_dir); |
| 725 |
return err; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, |
| 730 |
int argc, const char **argv) |
| 731 |
{ |
| 732 |
int err; |
| 733 |
struct _pam_opts opts; |
| 734 |
|
| 735 |
_pam_opts_init(&opts); |
| 736 |
_pam_opts_config(&opts, flags, argc, argv); |
| 737 |
opts.module = "auth"; |
| 738 |
|
| 739 |
err = _pam_do_chroot(pamh, &opts); |
| 740 |
switch(err) { |
| 741 |
case _PAM_CHROOT_OK: |
| 742 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 743 |
_pam_log(LOG_NOTICE, "%s: returning success", opts.module); |
| 744 |
} |
| 745 |
err = PAM_SUCCESS; |
| 746 |
break; |
| 747 |
|
| 748 |
case _PAM_CHROOT_USERNOTFOUND: |
| 749 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 750 |
_pam_log(LOG_NOTICE, "%s: unknown user", opts.module); |
| 751 |
} |
| 752 |
err = PAM_USER_UNKNOWN; |
| 753 |
break; |
| 754 |
|
| 755 |
case _PAM_CHROOT_INCOMPLETE: |
| 756 |
_pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module); |
| 757 |
err = PAM_INCOMPLETE; |
| 758 |
break; |
| 759 |
|
| 760 |
case _PAM_CHROOT_INTERNALERR: |
| 761 |
_pam_log(LOG_ERR, "%s: internal error encountered", opts.module); |
| 762 |
err = PAM_AUTH_ERR; |
| 763 |
break; |
| 764 |
|
| 765 |
default: |
| 766 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 767 |
_pam_log(LOG_NOTICE, "%s: returning failure", opts.module); |
| 768 |
} |
| 769 |
err = PAM_AUTH_ERR; |
| 770 |
break; |
| 771 |
} |
| 772 |
_pam_opts_free(&opts); |
| 773 |
return err; |
| 774 |
} |
| 775 |
|
| 776 |
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, |
| 777 |
int argc, const char **argv) |
| 778 |
{ |
| 779 |
_pam_log(LOG_ERR, "not a credentialator"); |
| 780 |
return PAM_IGNORE; |
| 781 |
} |
| 782 |
|
| 783 |
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, |
| 784 |
int argc, const char **argv) |
| 785 |
{ |
| 786 |
int err; |
| 787 |
struct _pam_opts opts; |
| 788 |
|
| 789 |
_pam_opts_init(&opts); |
| 790 |
_pam_opts_config(&opts, flags, argc, argv); |
| 791 |
opts.module = "account"; |
| 792 |
|
| 793 |
err = _pam_do_chroot(pamh, &opts); |
| 794 |
switch(err) { |
| 795 |
case _PAM_CHROOT_OK: |
| 796 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 797 |
_pam_log(LOG_NOTICE, "%s: returning success", opts.module); |
| 798 |
} |
| 799 |
err = PAM_SUCCESS; |
| 800 |
break; |
| 801 |
|
| 802 |
case _PAM_CHROOT_USERNOTFOUND: |
| 803 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 804 |
_pam_log(LOG_NOTICE, "%s: unknown user", opts.module); |
| 805 |
} |
| 806 |
err = PAM_USER_UNKNOWN; |
| 807 |
break; |
| 808 |
|
| 809 |
case _PAM_CHROOT_INCOMPLETE: |
| 810 |
_pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module); |
| 811 |
err = PAM_INCOMPLETE; |
| 812 |
break; |
| 813 |
|
| 814 |
case _PAM_CHROOT_INTERNALERR: |
| 815 |
_pam_log(LOG_ERR, "%s: internal error encountered", opts.module); |
| 816 |
err = PAM_AUTH_ERR; |
| 817 |
break; |
| 818 |
|
| 819 |
default: |
| 820 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 821 |
_pam_log(LOG_NOTICE, "%s: returning failure", opts.module); |
| 822 |
} |
| 823 |
err = PAM_AUTH_ERR; |
| 824 |
break; |
| 825 |
} |
| 826 |
_pam_opts_free(&opts); |
| 827 |
return err; |
| 828 |
} |
| 829 |
|
| 830 |
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, |
| 831 |
int argc, const char **argv) |
| 832 |
{ |
| 833 |
int err; |
| 834 |
struct _pam_opts opts; |
| 835 |
|
| 836 |
_pam_opts_init(&opts); |
| 837 |
_pam_opts_config(&opts, flags, argc, argv); |
| 838 |
opts.module = "session"; |
| 839 |
|
| 840 |
err = _pam_do_chroot(pamh, &opts); |
| 841 |
switch(err) { |
| 842 |
case _PAM_CHROOT_OK: |
| 843 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 844 |
_pam_log(LOG_NOTICE, "%s: returning success", opts.module); |
| 845 |
} |
| 846 |
err = PAM_SUCCESS; |
| 847 |
break; |
| 848 |
|
| 849 |
case _PAM_CHROOT_USERNOTFOUND: |
| 850 |
if(opts.flags & _PAM_OPTS_NOTFOUNDFAILS) { |
| 851 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 852 |
_pam_log(LOG_NOTICE, |
| 853 |
"%s: notfound=failure is set, returning failure", |
| 854 |
opts.module); |
| 855 |
} |
| 856 |
err = PAM_SESSION_ERR; |
| 857 |
} else { |
| 858 |
err = PAM_SUCCESS; |
| 859 |
} |
| 860 |
break; |
| 861 |
|
| 862 |
case _PAM_CHROOT_INCOMPLETE: |
| 863 |
_pam_log(LOG_NOTICE, "%s: returning incomplete", opts.module); |
| 864 |
err = PAM_INCOMPLETE; |
| 865 |
break; |
| 866 |
|
| 867 |
case _PAM_CHROOT_INTERNALERR: |
| 868 |
_pam_log(LOG_ERR, "%s: internal error encountered", opts.module); |
| 869 |
err = PAM_SESSION_ERR; |
| 870 |
break; |
| 871 |
|
| 872 |
default: |
| •873 |
if(opts.flags & _PAM_OPTS_DEBUG) { |
| 874 |
_pam_log(LOG_NOTICE, "%s: returning failure", opts.module); |
| 875 |
} |
| 876 |
err = PAM_SESSION_ERR; |
| 877 |
break; |
| 878 |
} |
| 879 |
_pam_opts_free(&opts); |
| 880 |
return err; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, |
| 885 |
int argc, const char **argv) |
| 886 |
{ |
| 887 |
return PAM_SUCCESS; |
| 888 |
} |
| 889 |
|
| 890 |
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, |
| 891 |
int argc, const char **argv) |
| 892 |
{ |
| 893 |
_pam_log(LOG_ERR, "password management group is unsupported"); |
| 894 |
return PAM_SERVICE_ERR; |
| 895 |
} |
| 896 |
|