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