1/* 2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com> 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13*/ 14 15#ifdef HAVE_CONFIG_H 16#include "config.h" 17#endif /* HAVE_CONFIG_H */ 18 19#ifdef HAVE_LDAP 20 21#include <stdio.h> 22#include <stdlib.h> 23#include <errno.h> 24#include <string.h> 25#include <ctype.h> 26#include <ldap.h> 27 28#include <atalk/ldapconfig.h> 29#include <atalk/logger.h> 30 31#define LINESIZE 1024 32 33/* Parse one line. Return result in pref and val */ 34static int getpref(char *buf, char **R_pref, char **R_val) 35{ 36 char *p, *pref, *val; 37 38 /* a little pre-processing to get rid of spaces and end-of-lines */ 39 p = buf; 40 while (p && isspace(*p)) 41 p++; 42 if (!p || (*p == '\0')) 43 return -1; 44 45 if ((val = strchr(p, '=')) == NULL) 46 return -1; 47 while ((*val == '=') || (*val == ' ')) 48 val++; 49 if ((val = strtok(val, " \n")) == NULL) 50 return -1; 51 if ((val = strdup(val)) == NULL) 52 return -1; 53 if ((pref = strtok(p, " =")) == NULL) 54 return -1; 55 56 *R_pref = pref; 57 *R_val = val; 58 return 0; 59} 60 61/* Parse the afp_ldap.conf file */ 62int acl_ldap_readconfig(char *name) 63{ 64 int i, j; 65 FILE *f; 66 char buf[LINESIZE]; 67 char *pref, *val; 68 69 f = fopen(name,"r"); 70 if (!f) { 71 perror("fopen"); 72 return -1; 73 } 74 75 while (!feof(f)) { 76 /* read a line from file */ 77 if (!fgets(buf, LINESIZE, f) || buf[0] == '#') 78 continue; 79 80 /* parse and return pref and value */ 81 if ((getpref(buf, &pref, &val)) != 0) 82 continue; 83 84 i = 0; 85 /* now see if its a correct pref */ 86 while(ldap_prefs[i].pref != NULL) { 87 if ((strcmp(ldap_prefs[i].name, pref)) == 0) { 88 /* ok, found a valid pref */ 89 90 /* check if we have pre-defined values */ 91 if (0 == ldap_prefs[i].intfromarray) { 92 /* no, its just a string */ 93 ldap_prefs[i].valid = 0; 94 if (0 == ldap_prefs[i].strorint) 95 /* store string as string */ 96 *((char **)(ldap_prefs[i].pref)) = val; 97 else 98 /* store as int */ 99 *((int *)(ldap_prefs[i].pref)) = atoi(val); 100 } else { 101 /* ok, we have string to int mapping for this pref 102 eg. "none", "simple", "sasl" map to 0, 128, 129 */ 103 j = 0; 104 while(prefs_array[j].pref != NULL) { 105 if (((strcmp(prefs_array[j].pref, pref)) == 0) && 106 ((strcmp(prefs_array[j].valuestring, val)) == 0)) { 107 ldap_prefs[i].valid = 0; 108 *((int *)(ldap_prefs[i].pref)) = prefs_array[j].value; 109 } 110 j++; 111 } /* while j*/ 112 } /* if else 0 == ldap_prefs*/ 113 break; 114 } /* if strcmp */ 115 i++; 116 } /* while i */ 117 if (ldap_prefs[i].pref == NULL) 118 LOG(log_error, logtype_afpd,"afp_ldap.conf: Unknown option: \"%s\"", pref); 119 } /* EOF */ 120 121 /* check if the config is sane and complete */ 122 i = 0; 123 ldap_config_valid = 1; 124 125 while(ldap_prefs[i].pref != NULL) { 126 if ( ldap_prefs[i].valid != 0) { 127 LOG(log_debug, logtype_afpd,"afp_ldap.conf: Missing option: \"%s\"", ldap_prefs[i].name); 128 ldap_config_valid = 0; 129 break; 130 } 131 i++; 132 } 133 134 if (ldap_config_valid) { 135 if (ldap_auth_method == LDAP_AUTH_NONE) 136 LOG(log_debug, logtype_afpd,"afp_ldap.conf: Using anonymous bind."); 137 else if (ldap_auth_method == LDAP_AUTH_SIMPLE) 138 LOG(log_debug, logtype_afpd,"afp_ldap.conf: Using simple bind."); 139 else { 140 ldap_config_valid = 0; 141 LOG(log_error, logtype_afpd,"afp_ldap.conf: SASL not yet supported."); 142 } 143 } else 144 LOG(log_info, logtype_afpd,"afp_ldap.conf: not used"); 145 fclose(f); 146 return 0; 147} 148#endif /* HAVE_LDAP */ 149