1 /* pam_misc.c -- This is random stuff */
3 /* $Id: pam_misc.c,v 1.9 1997/04/05 06:56:19 morgan Exp $
4 * $FreeBSD: src/contrib/libpam/libpam/pam_misc.c,v 1.1.1.1.6.3 2001/09/13 08:24:14 roam Exp $
5 * $DragonFly: src/contrib/libpam/libpam/Attic/pam_misc.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
8 * Revision 1.9 1997/04/05 06:56:19 morgan
9 * enforce AUTHTOK restrictions
11 * Revision 1.8 1997/02/15 15:59:46 morgan
12 * modified ..strCMP comment
14 * Revision 1.7 1996/12/01 03:14:13 morgan
17 * Revision 1.6 1996/11/10 20:05:52 morgan
18 * name convention _pam_ enforced. Also modified _pam_strdup()
20 * Revision 1.5 1996/07/07 23:57:14 morgan
21 * deleted debuggin function and replaced it with a static function
22 * defined in pam_private.h
24 * Revision 1.4 1996/06/02 08:00:56 morgan
25 * added StrTok function
27 * Revision 1.3 1996/05/21 04:36:58 morgan
28 * added debugging information
29 * replaced the _pam_log need for a local buffer with a call to vsyslog()
30 * [Al Longyear had some segfaulting problems related to this]
32 * Revision 1.2 1996/03/16 21:55:13 morgan
33 * changed pam_mkargv to _pam_mkargv
44 #include "pam_private.h"
46 /* caseless string comparison: POSIX does not define this.. */
47 int _pam_strCMP(const char *s, const char *t)
52 cf = tolower(*s) - tolower(*t);
54 } while (!cf && *s++);
59 char *_pam_StrTok(char *from, const char *format, char **next)
61 * this function is a variant of the standard strtok, it differs in that
62 * it takes an additional argument and doesn't nul terminate tokens until
63 * they are actually reached.
66 char table[256], *end;
69 if (from == NULL && (from = *next) == NULL)
72 /* initialize table */
73 for (i=1; i<256; table[i++] = '\0');
74 for (i=0; format[i] ; table[(int)format[i++]] = 'y');
76 /* look for first non-blank char */
77 while (*from && table[(int)*from]) {
83 * special case, "[...]" is considered to be a single
84 * object. Note, however, if one of the format[] chars is
85 * '[' this single string will not be read correctly.
87 for (end=++from; *end && *end != ']'; ++end) {
88 if (*end == '\\' && end[1] == ']')
91 /* note, this string is stripped of its edges: "..." is what
94 /* simply look for next blank char */
95 for (end=from; *end && !table[(int)*end]; ++end);
97 return (*next = NULL); /* no tokens left */
100 /* now terminate what we have */
104 /* indicate what it left */
108 *next = NULL; /* have found last token */
111 /* return what we have */
116 * Safe duplication of character strings. "Paranoid"; don't leave
117 * evidence of old token around for later stack analysis.
120 char *_pam_strdup(const char *x)
122 register char *new=NULL;
127 for (i=0; x[i]; ++i); /* length of string */
128 if ((new = malloc(++i)) == NULL) {
130 pam_system_log(NULL, NULL, LOG_CRIT,
131 "_pam_strdup: failed to get memory");
140 return new; /* return the duplicate or NULL on error */
143 /* Generate argv, argc from s */
144 /* caller must free(argv) */
146 int _pam_mkargv(char *s, char ***argv, int *argc)
150 char *sbuf, *sbuf_start;
151 char **our_argv = NULL;
158 D(("_pam_mkargv called: %s",s));
164 if ((sbuf = sbuf_start = _pam_strdup(s)) == NULL) {
165 pam_system_log(NULL, NULL, LOG_CRIT,
166 "pam_mkargv: null returned by _pam_strdup");
169 /* Overkill on the malloc, but not large */
170 argvlen = (l + 1) * ((sizeof(char)) + sizeof(char *));
171 if ((our_argv = argvbuf = malloc(argvlen)) == NULL) {
172 pam_system_log(NULL, NULL, LOG_CRIT,
173 "pam_mkargv: null returned by malloc");
177 argvbufp = (char *) argvbuf + (l * sizeof(char *));
179 while ((sbuf = _pam_StrTok(sbuf, " \n\t", &tmp))) {
180 D(("arg #%d",++count));
182 strcpy(argvbufp, sbuf);
185 argvbufp += strlen(argvbufp) + 1;
186 D(("stepped in argvbufp"));
192 _pam_drop(sbuf_start);
199 D(("_pam_mkargv returned"));
205 * this function is used to protect the modules from accidental or
206 * semi-mallicious harm that an application may do to confuse the API.
209 void _pam_sanitize(pam_handle_t *pamh)
212 * this is for security. We reset the auth-tokens here.
214 pam_set_item(pamh,PAM_AUTHTOK,NULL);
215 pam_set_item(pamh,PAM_OLDAUTHTOK,NULL);
219 * This function scans the array and replaces the _PAM_ACTION_UNDEF
220 * entries with the default action.
223 void _pam_set_default_control(int *control_array, int default_action)
227 for (i=0; i<_PAM_RETURN_VALUES; ++i) {
228 if (control_array[i] == _PAM_ACTION_UNDEF) {
229 control_array[i] = default_action;
235 * This function is used to parse a control string. This string is a
236 * series of tokens of the following form:
238 * "[ ]*return_code[ ]*=[ ]*action/[ ]".
241 #include "pam_tokens.h"
243 void _pam_parse_control(int *control_array, char *tok)
251 /* skip leading space */
252 while (isspace(*tok) && *++tok);
256 /* identify return code */
257 for (ret=0; ret<=_PAM_RETURN_VALUES; ++ret) {
258 len = strlen(_pam_token_returns[ret]);
259 if (!strncmp(_pam_token_returns[ret], tok, len)) {
263 if (ret > _PAM_RETURN_VALUES || !*(tok += len)) {
264 error = "expecting return value";
269 while (isspace(*tok) && *++tok);
270 if (!*tok || *tok++ != '=') {
271 error = "expecting '='";
275 /* skip leading space */
276 while (isspace(*tok) && *++tok);
278 error = "expecting action";
282 /* observe action type */
283 for (act=0; act < (-(_PAM_ACTION_UNDEF)); ++act) {
284 len = strlen(_pam_token_actions[act]);
285 if (!strncmp(_pam_token_actions[act], tok, len)) {
293 * Either we have a number or we have hit an error. In
294 * principle, there is nothing to stop us accepting
295 * negative offsets. (Although we would have to think of
296 * another way of encoding the tokens.) However, I really
297 * think this would be both hard to administer and easily
298 * cause looping problems. So, for now, we will just
299 * allow forward jumps. (AGM 1998/1/7)
301 if (!isdigit(*tok)) {
302 error = "expecting jump number";
309 act += *tok - '0'; /* XXX - this assumes ascii behavior */
310 } while (*++tok && isdigit(*tok));
312 /* we do not allow 0 jumps. There is a token ('ignore')
314 error = "expecting non-zero";
319 /* set control_array element */
320 if (ret != _PAM_RETURN_VALUES) {
321 control_array[ret] = act;
323 /* set the default to 'act' */
324 _pam_set_default_control(control_array, act);
328 /* that was a success */
332 /* treat everything as bad */
333 pam_system_log(NULL, NULL, LOG_ERR, "pam_parse: %s; [...%s]", error, tok);
334 for (ret=0; ret<_PAM_RETURN_VALUES; control_array[ret++]=_PAM_ACTION_BAD);