xref: /illumos-gate/usr/src/lib/libsasl/lib/config.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
2004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /* SASL Config file API
77c478bd9Sstevel@tonic-gate  * Rob Siemborski
87c478bd9Sstevel@tonic-gate  * Tim Martin (originally in Cyrus distribution)
97c478bd9Sstevel@tonic-gate  * $Id: config.c,v 1.13 2003/02/13 19:55:54 rjs3 Exp $
107c478bd9Sstevel@tonic-gate  */
11*1da57d55SToomas Soome /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
157c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
167c478bd9Sstevel@tonic-gate  * are met:
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
19*1da57d55SToomas Soome  *    notice, this list of conditions and the following disclaimer.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
227c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
237c478bd9Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
247c478bd9Sstevel@tonic-gate  *    distribution.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * 3. The name "Carnegie Mellon University" must not be used to
277c478bd9Sstevel@tonic-gate  *    endorse or promote products derived from this software without
287c478bd9Sstevel@tonic-gate  *    prior written permission. For permission or any other legal
29*1da57d55SToomas Soome  *    details, please contact
307c478bd9Sstevel@tonic-gate  *      Office of Technology Transfer
317c478bd9Sstevel@tonic-gate  *      Carnegie Mellon University
327c478bd9Sstevel@tonic-gate  *      5000 Forbes Avenue
337c478bd9Sstevel@tonic-gate  *      Pittsburgh, PA  15213-3890
347c478bd9Sstevel@tonic-gate  *      (412) 268-4387, fax: (412) 268-7395
357c478bd9Sstevel@tonic-gate  *      tech-transfer@andrew.cmu.edu
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * 4. Redistributions of any form whatsoever must retain the following
387c478bd9Sstevel@tonic-gate  *    acknowledgment:
397c478bd9Sstevel@tonic-gate  *    "This product includes software developed by Computing Services
407c478bd9Sstevel@tonic-gate  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
437c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
447c478bd9Sstevel@tonic-gate  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
457c478bd9Sstevel@tonic-gate  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
467c478bd9Sstevel@tonic-gate  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
477c478bd9Sstevel@tonic-gate  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
487c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Current Valid keys:
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * canon_user_plugin: <string>
557c478bd9Sstevel@tonic-gate  * pwcheck_method: <string>
567c478bd9Sstevel@tonic-gate  * auto_transition: <boolean>
577c478bd9Sstevel@tonic-gate  * plugin_list: <string>
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * srvtab: <string>
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include "sasl.h"
647c478bd9Sstevel@tonic-gate #include "saslint.h"
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <stdio.h>
677c478bd9Sstevel@tonic-gate #include <stdlib.h>
687c478bd9Sstevel@tonic-gate #include <ctype.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include "config.h"	/* _SUN_SDK_ */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct configlist {
737c478bd9Sstevel@tonic-gate     char *key;
747c478bd9Sstevel@tonic-gate     char *value;
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
787c478bd9Sstevel@tonic-gate static struct configlist *configlist;
797c478bd9Sstevel@tonic-gate static int nconfiglist;
807c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define CONFIGLISTGROWSIZE 100
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
sasl_config_init(_sasl_global_context_t * gctx,const char * filename)857c478bd9Sstevel@tonic-gate int sasl_config_init(_sasl_global_context_t *gctx, const char *filename)
867c478bd9Sstevel@tonic-gate #else
877c478bd9Sstevel@tonic-gate int sasl_config_init(const char *filename)
887c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate     FILE *infile;
917c478bd9Sstevel@tonic-gate     int lineno = 0;
927c478bd9Sstevel@tonic-gate     int alloced = 0;
937c478bd9Sstevel@tonic-gate     char buf[4096];
947c478bd9Sstevel@tonic-gate     char *p, *key;
957c478bd9Sstevel@tonic-gate     int result;
967c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
977c478bd9Sstevel@tonic-gate     int invalid_line = 0;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     gctx->nconfiglist=0;
1007c478bd9Sstevel@tonic-gate #else
1017c478bd9Sstevel@tonic-gate     nconfiglist=0;
1027c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1037c478bd9Sstevel@tonic-gate 
104004388ebScasper     infile = fopen(filename, "rF");
1057c478bd9Sstevel@tonic-gate     if (!infile) {
1067c478bd9Sstevel@tonic-gate       return SASL_CONTINUE;
1077c478bd9Sstevel@tonic-gate     }
1087c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1097c478bd9Sstevel@tonic-gate     result = _sasl_strdup(filename, &gctx->config_path, NULL);
1107c478bd9Sstevel@tonic-gate     if (result != SASL_OK)
1117c478bd9Sstevel@tonic-gate 	goto done;
1127c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
113*1da57d55SToomas Soome 
1147c478bd9Sstevel@tonic-gate     while (fgets(buf, sizeof(buf), infile)) {
1157c478bd9Sstevel@tonic-gate 	lineno++;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
1187c478bd9Sstevel@tonic-gate 	for (p = buf; *p && isspace((int) *p); p++);
1197c478bd9Sstevel@tonic-gate 	if (!*p || *p == '#') continue;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	key = p;
1227c478bd9Sstevel@tonic-gate 	while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) {
1237c478bd9Sstevel@tonic-gate 	    if (isupper((int) *p)) *p = tolower(*p);
1247c478bd9Sstevel@tonic-gate 	    p++;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	if (*p != ':') {
1277c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1287c478bd9Sstevel@tonic-gate 	  invalid_line = 1;
1297c478bd9Sstevel@tonic-gate 	  goto done;
1307c478bd9Sstevel@tonic-gate #else
1317c478bd9Sstevel@tonic-gate 	  return SASL_FAIL;
1327c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	*p++ = '\0';
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	while (*p && isspace((int) *p)) p++;
137*1da57d55SToomas Soome 
1387c478bd9Sstevel@tonic-gate 	if (!*p) {
1397c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1407c478bd9Sstevel@tonic-gate 	  invalid_line = 1;
1417c478bd9Sstevel@tonic-gate 	  goto done;
1427c478bd9Sstevel@tonic-gate #else
1437c478bd9Sstevel@tonic-gate 	  return SASL_FAIL;
1447c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1487c478bd9Sstevel@tonic-gate 	if (gctx->nconfiglist == alloced) {
1497c478bd9Sstevel@tonic-gate #else
1507c478bd9Sstevel@tonic-gate 	if (nconfiglist == alloced) {
1517c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1527c478bd9Sstevel@tonic-gate 	    alloced += CONFIGLISTGROWSIZE;
1537c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
154*1da57d55SToomas Soome 	    gctx->configlist=sasl_REALLOC((char *)gctx->configlist,
1557c478bd9Sstevel@tonic-gate 				    alloced * sizeof(struct configlist));
1567c478bd9Sstevel@tonic-gate 	    if (gctx->configlist==NULL) {
1577c478bd9Sstevel@tonic-gate 		result = SASL_NOMEM;
1587c478bd9Sstevel@tonic-gate 		goto done;
1597c478bd9Sstevel@tonic-gate 	    }
1607c478bd9Sstevel@tonic-gate #else
161*1da57d55SToomas Soome 	    configlist=sasl_REALLOC((char *)configlist,
1627c478bd9Sstevel@tonic-gate 				    alloced * sizeof(struct configlist));
1637c478bd9Sstevel@tonic-gate 	    if (configlist==NULL) return SASL_NOMEM;
1647c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1707c478bd9Sstevel@tonic-gate 	result = _sasl_strdup(key,
1717c478bd9Sstevel@tonic-gate 			      &(((struct configlist *)(gctx->configlist))
1727c478bd9Sstevel@tonic-gate 				[gctx->nconfiglist].key),
1737c478bd9Sstevel@tonic-gate 			      NULL);
1747c478bd9Sstevel@tonic-gate 	if (result!=SASL_OK)
1757c478bd9Sstevel@tonic-gate 	  goto done;
1767c478bd9Sstevel@tonic-gate #else
1777c478bd9Sstevel@tonic-gate 	result = _sasl_strdup(key,
1787c478bd9Sstevel@tonic-gate 			      &(configlist[nconfiglist].key),
1797c478bd9Sstevel@tonic-gate 			      NULL);
1807c478bd9Sstevel@tonic-gate 	if (result!=SASL_OK) return result;
1817c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1827c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1837c478bd9Sstevel@tonic-gate 	result = _sasl_strdup(p,
1847c478bd9Sstevel@tonic-gate 			      &(((struct configlist *)(gctx->configlist))
1857c478bd9Sstevel@tonic-gate 				[gctx->nconfiglist].value),
1867c478bd9Sstevel@tonic-gate 			      NULL);
1877c478bd9Sstevel@tonic-gate 	if (result!=SASL_OK) {
1887c478bd9Sstevel@tonic-gate 	    sasl_FREE(((struct configlist *)(gctx->configlist))
1897c478bd9Sstevel@tonic-gate 				[gctx->nconfiglist].key);
1907c478bd9Sstevel@tonic-gate 	    goto done;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate 	result = _sasl_strdup(p,
1947c478bd9Sstevel@tonic-gate 			      &(configlist[nconfiglist].value),
1957c478bd9Sstevel@tonic-gate 			      NULL);
1967c478bd9Sstevel@tonic-gate 	if (result!=SASL_OK) return result;
1977c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2007c478bd9Sstevel@tonic-gate 	(gctx->nconfiglist)++;
2017c478bd9Sstevel@tonic-gate #else
2027c478bd9Sstevel@tonic-gate 	nconfiglist++;
2037c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2047c478bd9Sstevel@tonic-gate     }
2057c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2067c478bd9Sstevel@tonic-gate     result = SASL_OK;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate done:
2097c478bd9Sstevel@tonic-gate     fclose(infile);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate     if (invalid_line) {
2127c478bd9Sstevel@tonic-gate 	__sasl_log(gctx, gctx->server_global_callbacks.callbacks,
2137c478bd9Sstevel@tonic-gate 		   SASL_LOG_ERR, "%s: bad config line: '%s'", filename, buf);
2147c478bd9Sstevel@tonic-gate 	result = SASL_FAIL;
2157c478bd9Sstevel@tonic-gate     }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate     return result;
2187c478bd9Sstevel@tonic-gate #else
2197c478bd9Sstevel@tonic-gate     fclose(infile);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate     return SASL_OK;
2227c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2267c478bd9Sstevel@tonic-gate /* Releases the resources acquired in sasl_config_init() */
2277c478bd9Sstevel@tonic-gate void sasl_config_free(_sasl_global_context_t *gctx)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate     int i;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate     if (gctx->config_path != NULL)
2327c478bd9Sstevel@tonic-gate 	sasl_FREE(gctx->config_path);
2337c478bd9Sstevel@tonic-gate     gctx->config_path = NULL;
2347c478bd9Sstevel@tonic-gate     if (gctx->configlist == NULL)
2357c478bd9Sstevel@tonic-gate 	return;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate     for (i = 0; i < gctx->nconfiglist; i++) {
2387c478bd9Sstevel@tonic-gate 	if ((((struct configlist *)gctx->configlist))[i].key)
2397c478bd9Sstevel@tonic-gate 	    sasl_FREE(((struct configlist *)gctx->configlist)[i].key);
2407c478bd9Sstevel@tonic-gate 	if (((struct configlist *)gctx->configlist)[i].value)
2417c478bd9Sstevel@tonic-gate 	    sasl_FREE(((struct configlist *)gctx->configlist)[i].value);
2427c478bd9Sstevel@tonic-gate     }
2437c478bd9Sstevel@tonic-gate     sasl_FREE(gctx->configlist);
2447c478bd9Sstevel@tonic-gate     gctx->configlist = NULL;
2457c478bd9Sstevel@tonic-gate     gctx->nconfiglist = 0;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate const char *sasl_config_getstring(_sasl_global_context_t *gctx,
2497c478bd9Sstevel@tonic-gate 	const char *key, const char *def)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate     int opt;
2527c478bd9Sstevel@tonic-gate     struct configlist *clist = (struct configlist *)gctx->configlist;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate     for (opt = 0; opt < gctx->nconfiglist; opt++) {
2557c478bd9Sstevel@tonic-gate 	if (*key == clist[opt].key[0] &&
2567c478bd9Sstevel@tonic-gate 	    !strcmp(key, clist[opt].key))
2577c478bd9Sstevel@tonic-gate 	  return clist[opt].value;
2587c478bd9Sstevel@tonic-gate     }
2597c478bd9Sstevel@tonic-gate     return def;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate #else
2627c478bd9Sstevel@tonic-gate const char *sasl_config_getstring(const char *key,const char *def)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate     int opt;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate     for (opt = 0; opt < nconfiglist; opt++) {
2677c478bd9Sstevel@tonic-gate 	if (*key == configlist[opt].key[0] &&
2687c478bd9Sstevel@tonic-gate 	    !strcmp(key, configlist[opt].key))
2697c478bd9Sstevel@tonic-gate 	  return configlist[opt].value;
2707c478bd9Sstevel@tonic-gate     }
2717c478bd9Sstevel@tonic-gate     return def;
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2767c478bd9Sstevel@tonic-gate int sasl_config_getint(_sasl_global_context_t *gctx, const char *key,int def)
2777c478bd9Sstevel@tonic-gate #else
2787c478bd9Sstevel@tonic-gate int sasl_config_getint(const char *key,int def)
2797c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2827c478bd9Sstevel@tonic-gate     const char *val = sasl_config_getstring(gctx, key, (char *)0);
2837c478bd9Sstevel@tonic-gate #else
2847c478bd9Sstevel@tonic-gate     const char *val = sasl_config_getstring(key, (char *)0);
2857c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate     if (!val) return def;
2887c478bd9Sstevel@tonic-gate     if (!isdigit((int) *val) && (*val != '-' || !isdigit((int) val[1]))) return def;
2897c478bd9Sstevel@tonic-gate     return atoi(val);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2937c478bd9Sstevel@tonic-gate int sasl_config_getswitch(_sasl_global_context_t *gctx,const char *key,int def)
2947c478bd9Sstevel@tonic-gate #else
2957c478bd9Sstevel@tonic-gate int sasl_config_getswitch(const char *key,int def)
2967c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2997c478bd9Sstevel@tonic-gate     const char *val = sasl_config_getstring(gctx, key, (char *)0);
3007c478bd9Sstevel@tonic-gate #else
3017c478bd9Sstevel@tonic-gate     const char *val = sasl_config_getstring(key, (char *)0);
3027c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate     if (!val) return def;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate     if (*val == '0' || *val == 'n' ||
3077c478bd9Sstevel@tonic-gate 	(*val == 'o' && val[1] == 'f') || *val == 'f') {
3087c478bd9Sstevel@tonic-gate 	return 0;
3097c478bd9Sstevel@tonic-gate     }
3107c478bd9Sstevel@tonic-gate     else if (*val == '1' || *val == 'y' ||
3117c478bd9Sstevel@tonic-gate 	     (*val == 'o' && val[1] == 'n') || *val == 't') {
3127c478bd9Sstevel@tonic-gate 	return 1;
3137c478bd9Sstevel@tonic-gate     }
3147c478bd9Sstevel@tonic-gate     return def;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
317