/* pam_handlers.c -- pam config file parsing and module loading */ /* * created by Marc Ewing. * Currently maintained by Andrew G. Morgan * * */ #include #include #include #include #include #include #include #include #include #include #include "pam_conf.h" /* FreeBSD doesn't define this */ #ifndef RTLD_NOW # define RTLD_NOW 1 #endif #define BUF_SIZE 1024 #define MODULE_CHUNK 4 static int _pam_get_line(const char **ptr, char *buf, int buf_len); static void _pam_free_handlers_aux(struct handler **hp); static int _pam_add_handler(pam_handle_t *pamh , int must_fail, int other, int type , int *actions, const char *mod_path , int argc, char **argv, int argvlen); /* Values for module type */ #define PAM_T_AUTH 1 #define PAM_T_SESS 2 #define PAM_T_ACCT 4 #define PAM_T_PASS 8 static int _pam_parse_conf_file(pam_handle_t *pamh, const char *known_service) { char buf[BUF_SIZE]; int other = 0; /* 0 - reading cfg, 1 - reading default cfg */ int x; /* read a line from a cfg? */ char zero = 0; char smfbuf[BUF_SIZE * 4]; const char *dptr = &zero; const char *cptr = smfbuf; struct pam_default_conf *pdcp; extern struct pam_default_conf *_pam_default_conf_list; /* * First, grab the current SMF configuration for the service */ _pam_get_conf(known_service, smfbuf, BUF_SIZE * 4); /* * Then, grab the default configuration for the service */ for (pdcp = _pam_default_conf_list; pdcp != NULL; pdcp = pdcp->next) { if (!strcmp(pdcp->module, known_service)) { dptr = pdcp->conf; break; } } /* * read a line from the configuration */ //lint -e{506} while ( ((x = _pam_get_line(&cptr, buf, BUF_SIZE)) > 0) || ((other = 1) && (x = _pam_get_line(&dptr, buf, BUF_SIZE)) > 0) ) { char *tok, *nexttok=NULL; const char *this_service; const char *mod_path; int module_type, actions[_PAM_RETURN_VALUES]; int res; /* module added successfully? */ int must_fail=0; /* a badly formatted line must fail when used */ int argc; char **argv; int argvlen; D(("_pam_init_handler: LINE: %s", buf)); nexttok = buf; /* No service field: all lines are for the known service. */ this_service = known_service; /* accept "service name" or PAM_DEFAULT_SERVICE modules */ /* This is a service we are looking for */ D(("_pam_init_handlers: Found PAM config entry for: %s" , this_service)); tok = _pam_StrTok(NULL, " \n\t", &nexttok); if (!_pam_strCMP("auth", tok)) { module_type = PAM_T_AUTH; } else if (!_pam_strCMP("session", tok)) { module_type = PAM_T_SESS; } else if (!_pam_strCMP("account", tok)) { module_type = PAM_T_ACCT; } else if (!_pam_strCMP("password", tok)) { module_type = PAM_T_PASS; } else { /* Illegal module type */ D(("_pam_init_handlers: bad module type: %s", tok)); pam_system_log(pamh, NULL, LOG_ERR, "(%s) illegal module type: %s" , this_service, tok); module_type = PAM_T_AUTH; /* most sensitive */ must_fail = 1; /* install as normal but fail when dispatched */ } D(("Using %s config entry: %s", must_fail?"BAD ":"", tok)); /* reset the actions to .._UNDEF's -- this is so that we can work out which entries are not yet set (for default). */ { int i; for (i=0; i<_PAM_RETURN_VALUES; actions[i++] = _PAM_ACTION_UNDEF){ ; } } tok = _pam_StrTok(NULL, " \n\t", &nexttok); if (!_pam_strCMP("required", tok)) { D(("*PAM_F_REQUIRED*")); actions[PAM_SUCCESS] = _PAM_ACTION_OK; actions[PAM_NEW_AUTHTOK_REQD] = _PAM_ACTION_OK; actions[PAM_IGNORE] = _PAM_ACTION_IGNORE; _pam_set_default_control(actions, _PAM_ACTION_BAD); } else if (!_pam_strCMP("requisite", tok)) { D(("*PAM_F_REQUISITE*")); actions[PAM_SUCCESS] = _PAM_ACTION_OK; actions[PAM_NEW_AUTHTOK_REQD] = _PAM_ACTION_OK; actions[PAM_IGNORE] = _PAM_ACTION_IGNORE; _pam_set_default_control(actions, _PAM_ACTION_DIE); } else if (!_pam_strCMP("optional", tok)) { D(("*PAM_F_OPTIONAL*")); actions[PAM_SUCCESS] = _PAM_ACTION_OK; actions[PAM_NEW_AUTHTOK_REQD] = _PAM_ACTION_OK; _pam_set_default_control(actions, _PAM_ACTION_IGNORE); } else if (!_pam_strCMP("sufficient", tok)) { D(("*PAM_F_SUFFICIENT*")); actions[PAM_SUCCESS] = _PAM_ACTION_DONE; actions[PAM_NEW_AUTHTOK_REQD] = _PAM_ACTION_DONE; _pam_set_default_control(actions, _PAM_ACTION_IGNORE); } else if (!_pam_strCMP("available", tok)) { D(("*PAM_F_AVAILABLE*")); actions[PAM_SUCCESS] = _PAM_ACTION_DONE; actions[PAM_NEW_AUTHTOK_REQD] = _PAM_ACTION_DONE; actions[PAM_IGNORE] = _PAM_ACTION_IGNORE; actions[PAM_AUTHINFO_UNAVAIL] = _PAM_ACTION_IGNORE; actions[PAM_TIMEOUT] = _PAM_ACTION_DONE; _pam_set_default_control(actions, _PAM_ACTION_BAD); } else { D(("will need to parse %s", tok)); _pam_parse_control(actions, tok); /* by default the default is to treat as failure */ _pam_set_default_control(actions, _PAM_ACTION_BAD); } tok = _pam_StrTok(NULL, " \n\t", &nexttok); if (tok != NULL) { mod_path = tok; D(("mod_path = %s",mod_path)); } else { /* no module name given */ D(("_pam_init_handlers: no module name supplied")); pam_system_log(pamh, NULL, LOG_ERR, "(%s) no module name supplied", this_service); mod_path = NULL; must_fail = 1; } /* nexttok points to remaining arguments... */ if (nexttok != NULL) { D(("list: %s",nexttok)); argvlen = _pam_mkargv(nexttok, &argv, &argc); D(("argvlen = %d",argvlen)); } else { /* there are no arguments so fix by hand */ D(("_pam_init_handlers: empty argument list")); argvlen = argc = 0; argv = NULL; } #ifdef DEBUG { int y; D(("CONF%s: %s%s %d %s %d" , must_fail?"<*will fail*>":"" , this_service, other ? "(backup)":"" , module_type , mod_path, argc)); for (y = 0; y < argc; y++) { D(("CONF: %s", argv[y])); } for (y = 0; y<_PAM_RETURN_VALUES; ++y) { D(("RETURN %s(%d) -> %d %s", _pam_token_returns[y], y, actions[y], actions[y]>0 ? "jump": _pam_token_actions[-actions[y]])); } fprintf(stderr, "pause to look at debugging: "); getchar(); } #endif res = _pam_add_handler(pamh, must_fail, other , module_type, actions, mod_path , argc, argv, argvlen); if (res != PAM_SUCCESS) { pam_system_log(pamh, NULL, LOG_ERR, "error loading %s", mod_path); D(("failed to load module - aborting")); return PAM_ABORT; } } return ( (x < 0) ? PAM_ABORT:PAM_SUCCESS ); } /* Parse config file, allocate handler structures, dlopen() */ int _pam_init_handlers(pam_handle_t *pamh) { int retval; D(("_pam_init_handlers called")); IF_NO_PAMH("_pam_init_handlers",pamh,PAM_SYSTEM_ERR); /* Return immediately if everything is already loaded */ if (pamh->handlers.handlers_loaded) { return PAM_SUCCESS; } D(("_pam_init_handlers: initializing")); /* First clean the service structure */ (void) _pam_free_handlers(pamh); if (! pamh->handlers.module) { if ((pamh->handlers.module = malloc(MODULE_CHUNK * sizeof(struct loaded_module))) == NULL) { pam_system_log(pamh, NULL, LOG_CRIT, "_pam_init_handlers: no memory loading module"); return PAM_BUF_ERR; } pamh->handlers.modules_allocated = MODULE_CHUNK; pamh->handlers.modules_used = 0; } if (pamh->service_name == NULL) { return PAM_BAD_ITEM; /* XXX - better error? */ } /* * Now parse the config file(s) and add handlers */ retval = _pam_parse_conf_file(pamh, pamh->service_name); if (retval != PAM_SUCCESS) { /* Read error */ pam_system_log(pamh, NULL, LOG_ERR, "error reading PAM configuration file"); return PAM_ABORT; } pamh->handlers.handlers_loaded = 1; D(("_pam_init_handlers exiting")); return PAM_SUCCESS; } /* * This is where we read a line of a PAM config string. The line may be * preceeded by lines of comments and also extended with "\\\n" */ int _pam_get_line(const char **ptr, char *buffer, int buf_len) { char *p = buffer; char *s, *os; int used = 0; /* loop broken with a 'break' when a non-'\\n' ended line is read */ D(("called.")); for (;;) { long cpylength; const char *nptr; if (used >= buf_len) { /* Overflow */ D(("_pam_assemble_line: overflow")); return -1; } if (strlen(*ptr) == 0) { if (used) { /* Incomplete read */ return -1; } else { /* EOF */ return 0; } } nptr = strchr(*ptr, '\n'); nptr = nptr ? nptr + 1 : *ptr + strlen(*ptr); cpylength = ((nptr - *ptr) < (buf_len - used - 1)) ? (nptr - *ptr) : (buf_len - used - 1); strncpy(p, *ptr, cpylength); *(p + cpylength) = (char)0; *ptr = nptr; /* skip leading spaces --- line may be blank */ s = p + strspn(p, " \n\t"); if (*s && (*s != '#')) { os = s; /* * we are only interested in characters before the first '#' * character */ while (*s && *s != '#') { ++s; } if (*s == '#') { *s = '\0'; used += (int) strlen(os); break; /* the line has been read */ } s = os; /* * Check for backslash by scanning back from the end of * the entered line, the '\n' has been included since * normally a line is terminated with this * character. fgets() should only return one though! */ s += strlen(s); while (s > os && ((*--s == ' ') || (*s == '\t') || (*s == '\n'))) { ; } /* check if it ends with a backslash */ if (*s == '\\') { *s++ = ' '; /* replace backslash with ' ' */ *s = '\0'; /* truncate the line here */ used += (int) strlen(os); p = s; /* there is more ... */ } else { /* End of the line! */ used += (int) strlen(os); break; /* this is the complete line */ } } else { /* Nothing in this line */ /* Don't move p */ } } return used; } typedef int (*servicefn)(pam_handle_t *, int, int, char **); int _pam_add_handler(pam_handle_t *pamh , int must_fail, int other, int type , int *actions, const char *mod_path , int argc, char **argv, int argvlen) { struct loaded_module *mod; int x = 0; struct handler **handler_p; struct handler **handler_p2; struct handlers *the_handlers; const char *sym, *sym2; servicefn func, func2; int success; D(("called.")); IF_NO_PAMH("_pam_add_handler",pamh,PAM_SYSTEM_ERR); /* if NULL set to something that can be searched for */ if (mod_path == NULL) { mod_path = "[unknown module]"; } D(("_pam_add_handler: adding type %d, module `%s'",type,mod_path)); mod = pamh->handlers.module; /* First, ensure the module is loaded */ while (x < pamh->handlers.modules_used) { if (!strcmp(mod[x].name, mod_path)) { /* case sensitive ! */ break; } x++; } if (x == pamh->handlers.modules_used) { /* Not found */ if (pamh->handlers.modules_allocated == pamh->handlers.modules_used) { /* will need more memory */ void *tmp = realloc(pamh->handlers.module, (pamh->handlers.modules_allocated+MODULE_CHUNK) *sizeof(struct loaded_module)); if (tmp == NULL) { D(("cannot enlarge module pointer memory")); pam_system_log(pamh, NULL, LOG_ERR, "realloc returned NULL in _pam_add_handler"); return PAM_ABORT; } pamh->handlers.module = tmp; pamh->handlers.modules_allocated += MODULE_CHUNK; } mod = &(pamh->handlers.module[x]); /* Be pessimistic... */ success = PAM_ABORT; #ifdef PAM_STATIC /* Only load static function if function was not found dynamically. * This code should work even if no dynamic loading is available. */ if (success != PAM_SUCCESS) { D(("_pam_add_handler: open static handler %s", mod_path)); mod->dl_handle = _pam_open_static_handler(mod_path); if (mod->dl_handle == NULL) { D(("_pam_add_handler: unable to find static handler %s", mod_path)); pam_system_log(pamh, NULL, LOG_ERR, "unable to open static handler %s", mod_path); /* Didn't find module in dynamic or static..will mark bad */ } else { D(("static module added successfully")); success = PAM_SUCCESS; mod->type = PAM_MT_STATIC_MOD; pamh->handlers.modules_used++; } } #endif if (success != PAM_SUCCESS) { /* add a malformed module */ mod->dl_handle = NULL; mod->type = PAM_MT_FAULTY_MOD; pamh->handlers.modules_used++; pam_system_log(pamh, NULL, LOG_ERR, "adding faulty module: %s", mod_path); success = PAM_SUCCESS; /* We have successfully added a module */ } if (mod->dl_handle && ((struct pam_module *)mod->dl_handle)->descr && type == PAM_T_AUTH) { smf_auth_addmap(pamh->service_name, ((struct pam_module *)mod->dl_handle)->descr); } /* indicate its name - later we will search for it by this */ if ((mod->name = _pam_strdup(mod_path)) == NULL) { D(("_pam_handler: couldn't get memory for mod_path")); pam_system_log(pamh, NULL, LOG_ERR, "no memory for module path", mod_path); success = PAM_ABORT; } } else { /* x != pamh->handlers.modules_used */ mod += x; /* the located module */ success = PAM_SUCCESS; } mod_path = NULL; /* no longer needed or trusted */ /* Now return error if necessary after trying all possible ways... */ if (success != PAM_SUCCESS) { return(success); } /* * At this point 'mod' points to the stored/loaded module. If its * dl_handle is unknown, then we must be able to indicate dispatch * failure with 'must_fail' */ /* Now define the handler(s) based on mod->dlhandle and type */ /* decide which list of handlers to use */ the_handlers = (other) ? &pamh->handlers.other : &pamh->handlers.conf; handler_p = handler_p2 = NULL; func = func2 = NULL; sym2 = NULL; /* point handler_p's at the root addresses of the function stacks */ switch (type) { case PAM_T_AUTH: handler_p = &the_handlers->authenticate; sym = "pam_sm_authenticate"; handler_p2 = &the_handlers->setcred; sym2 = "pam_sm_setcred"; break; case PAM_T_SESS: handler_p = &the_handlers->open_session; sym = "pam_sm_open_session"; handler_p2 = &the_handlers->close_session; sym2 = "pam_sm_close_session"; break; case PAM_T_ACCT: handler_p = &the_handlers->acct_mgmt; sym = "pam_sm_acct_mgmt"; break; case PAM_T_PASS: handler_p = &the_handlers->chauthtok; sym = "pam_sm_chauthtok"; break; default: /* Illegal module type */ D(("_pam_add_handler: illegal module type %d", type)); return PAM_ABORT; } /* are the modules reliable? */ if ( #ifdef PAM_STATIC mod->type != PAM_MT_STATIC_MOD && #endif /* PAM_STATIC */ mod->type != PAM_MT_FAULTY_MOD ) { D(("_pam_add_handlers: illegal module library type; %d", mod->type)); pam_system_log(pamh, NULL, LOG_ERR, "internal error: module library type not known: %s;%d", sym, mod->type); return PAM_ABORT; } /* now identify this module's functions - for non-faulty modules */ #ifdef PAM_STATIC if ((mod->type == PAM_MT_STATIC_MOD) && (func = (servicefn)_pam_get_static_sym(mod->dl_handle, sym)) == NULL) { pam_system_log(pamh, NULL, LOG_ERR, "unable to resolve static symbol: %s", sym); } #endif if (sym2) { #ifdef PAM_STATIC if ((mod->type == PAM_MT_STATIC_MOD) && (func2 = (servicefn)_pam_get_static_sym(mod->dl_handle, sym2)) == NULL) { pam_system_log(pamh, NULL, LOG_ERR, "unable to resolve symbol: %s", sym2); } #endif } /* here func (and perhaps func2) point to the appropriate functions */ /* add new handler to end of existing list */ while (*handler_p != NULL) { handler_p = &((*handler_p)->next); } if ((*handler_p = malloc(sizeof(struct handler))) == NULL) { pam_system_log(pamh, NULL, LOG_CRIT, "cannot malloc struct handler #1"); return (PAM_ABORT); } (*handler_p)->must_fail = must_fail; /* failure forced? */ (*handler_p)->func = func; memcpy((*handler_p)->actions,actions,sizeof((*handler_p)->actions)); (*handler_p)->argc = argc; (*handler_p)->argv = argv; /* not a copy */ (*handler_p)->next = NULL; /* some of the modules have a second calling function */ if (handler_p2) { /* add new handler to end of existing list */ while (*handler_p2) { handler_p2 = &((*handler_p2)->next); } if ((*handler_p2 = malloc(sizeof(struct handler))) == NULL) { pam_system_log(pamh, NULL, LOG_CRIT, "cannot malloc struct handler #2"); return (PAM_ABORT); } (*handler_p2)->must_fail = must_fail; /* failure forced? */ (*handler_p2)->func = func2; memcpy((*handler_p2)->actions,actions,sizeof((*handler_p2)->actions)); (*handler_p2)->argc = argc; if (argv) { if (((*handler_p2)->argv = malloc(argvlen)) == NULL) { pam_system_log(pamh, NULL, LOG_CRIT, "cannot malloc argv for handler #2"); return (PAM_ABORT); } memcpy((*handler_p2)->argv, argv, argvlen); } else { (*handler_p2)->argv = NULL; /* no arguments */ } (*handler_p2)->next = NULL; } D(("_pam_add_handler: returning successfully")); return PAM_SUCCESS; } /* Free various allocated structures and dlclose() the libs */ int _pam_free_handlers(pam_handle_t *pamh) { struct loaded_module *mod; D(("called.")); IF_NO_PAMH("_pam_free_handlers",pamh,PAM_SYSTEM_ERR); mod = pamh->handlers.module; /* Close all loaded modules */ while (pamh->handlers.modules_used) { D(("_pam_free_handlers: dlclose(%s)", mod->name)); free(mod->name); mod++; pamh->handlers.modules_used--; } /* Free all the handlers */ _pam_free_handlers_aux(&(pamh->handlers.conf.authenticate)); _pam_free_handlers_aux(&(pamh->handlers.conf.setcred)); _pam_free_handlers_aux(&(pamh->handlers.conf.acct_mgmt)); _pam_free_handlers_aux(&(pamh->handlers.conf.open_session)); _pam_free_handlers_aux(&(pamh->handlers.conf.close_session)); _pam_free_handlers_aux(&(pamh->handlers.conf.chauthtok)); _pam_free_handlers_aux(&(pamh->handlers.other.authenticate)); _pam_free_handlers_aux(&(pamh->handlers.other.setcred)); _pam_free_handlers_aux(&(pamh->handlers.other.acct_mgmt)); _pam_free_handlers_aux(&(pamh->handlers.other.open_session)); _pam_free_handlers_aux(&(pamh->handlers.other.close_session)); _pam_free_handlers_aux(&(pamh->handlers.other.chauthtok)); /* no more loaded modules */ _pam_drop(pamh->handlers.module); /* Indicate that handlers are not initialized for this pamh */ pamh->handlers.handlers_loaded = 0; return PAM_SUCCESS; } void _pam_start_handlers(pam_handle_t *pamh) { D(("called.")); /* NB. There is no check for a NULL pamh here, since no return * value to communicate the fact! */ /* Indicate that handlers are not initialized for this pamh */ pamh->handlers.handlers_loaded = 0; pamh->handlers.modules_allocated = 0; pamh->handlers.modules_used = 0; pamh->handlers.module = NULL; /* initialize the .conf and .other entries */ pamh->handlers.conf.authenticate = NULL; pamh->handlers.conf.setcred = NULL; pamh->handlers.conf.acct_mgmt = NULL; pamh->handlers.conf.open_session = NULL; pamh->handlers.conf.close_session = NULL; pamh->handlers.conf.chauthtok = NULL; pamh->handlers.other.authenticate = NULL; pamh->handlers.other.setcred = NULL; pamh->handlers.other.acct_mgmt = NULL; pamh->handlers.other.open_session = NULL; pamh->handlers.other.close_session = NULL; pamh->handlers.other.chauthtok = NULL; } void _pam_free_handlers_aux(struct handler **hp) { struct handler *h = *hp; struct handler *last; D(("called.")); while (h) { last = h; _pam_drop(h->argv); /* This is all alocated in a single chunk */ h = h->next; memset(last, 0, sizeof(*last)); free(last); } *hp = NULL; }