]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/acl/ldap_config.c
Fix SIGHUP config reloading
[netatalk.git] / libatalk / acl / ldap_config.c
index b7c160c169712249818ae65b3eb4c3d200cdb712..26a63fa9c76dab72f94af1fb812943d50f2ba804 100644 (file)
@@ -1,5 +1,4 @@
 /*
-  $Id: ldap_config.c,v 1.4 2009-11-28 11:10:37 franklahm Exp $
   Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
 
   This program is free software; you can redistribute it and/or modify
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#ifdef HAVE_ACLS
+#ifdef HAVE_LDAP
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <string.h>
 #include <ctype.h>
 #include <ldap.h>
 
+#include <atalk/globals.h>
 #include <atalk/ldapconfig.h>
 #include <atalk/logger.h>
+#include <atalk/iniparser.h>
 
-#define LINESIZE 1024
-
-/* Parse one line. Return result in pref and val */
-static int getpref(char *buf, char **R_pref, char **R_val)
+void acl_ldap_freeconfig(void)
 {
-    char *p, *pref, *val;
-
-    /* a little pre-processing to get rid of spaces and end-of-lines */
-    p = buf;
-    while (p && isspace(*p))
-        p++;
-    if (!p || (*p == '\0'))
-        return -1;
-
-    if ((val = strchr(p, '=')) == NULL)
-        return -1;
-    while ((*val == '=') || (*val == ' '))
-        val++;
-    if ((val = strtok(val, " \n")) == NULL)
-        return -1;
-    if ((val = strdup(val)) == NULL)
-        return -1;
-    if ((pref = strtok(p, " =")) == NULL)
-        return -1;
-
-    *R_pref = pref;
-    *R_val = val;
-    return 0;
+    for (int i = 0; ldap_prefs[i].name != NULL; i++) {
+        if (ldap_prefs[i].intfromarray == 0 && ldap_prefs[i].strorint == 0) {
+            free(*((char **)(ldap_prefs[i].pref)));
+            *((char **)(ldap_prefs[i].pref)) = NULL;
+        }
+        ldap_prefs[i].valid = ldap_prefs[i].valid_save;
+    }
 }
 
-/* Parse the afp_ldap.conf file */
-int acl_ldap_readconfig(char *name)
+int acl_ldap_readconfig(dictionary *iniconfig)
 {
     int i, j;
-    FILE *f;
-    char buf[LINESIZE];
-    char *pref, *val;
+    const char *val;
 
-    f = fopen(name,"r");
-    if (!f) {
-        perror("fopen");
-        return -1;
+    i = 0;
+    /* now see if its a correct pref */
+    for (i = 0; ldap_prefs[i].name != NULL; i++) {
+        if ((val = iniparser_getstring(iniconfig, INISEC_GLOBAL, ldap_prefs[i].name, NULL))) {
+            /* check if we have pre-defined values */
+            if (ldap_prefs[i].intfromarray == 0) {
+                /* no, its just a string */
+                ldap_prefs[i].valid = 0;
+                if (ldap_prefs[i].strorint)
+                    /* store as int */
+                    *((int *)(ldap_prefs[i].pref)) = atoi(val);
+                else
+                    /* store string as string */
+                    *((const char **)(ldap_prefs[i].pref)) = strdup(val);
+            } else {
+                /* ok, we have string to int mapping for this pref
+                   eg. "none", "simple", "sasl" map to 0, 128, 129 */
+                for (j = 0; prefs_array[j].pref != NULL; j++) {
+                    if ((strcmp(prefs_array[j].pref, ldap_prefs[i].name) == 0)
+                        && (strcmp(prefs_array[j].valuestring, val) == 0)) {
+                        ldap_prefs[i].valid = 0;
+                        *((int *)(ldap_prefs[i].pref)) = prefs_array[j].value;
+                        break;
+                    }
+                }
+            }
+        }
     }
 
-    while (!feof(f)) {
-        /* read a line from file */
-        if (!fgets(buf, LINESIZE, f) || buf[0] == '#')
-            continue;
-
-        /* parse and return pref and value */
-        if ((getpref(buf, &pref, &val)) != 0)
-            continue;
-
-        i = 0;
-        /* now see if its a correct pref */
-        while(ldap_prefs[i].pref != NULL) {
-            if ((strcmp(ldap_prefs[i].name, pref)) == 0) {
-                /* ok, found a valid pref */
-
-                /* check if we have pre-defined values */
-                if (0 == ldap_prefs[i].intfromarray) {
-                    /* no, its just a string */
-                    ldap_prefs[i].valid = 0;
-                    if (0 == ldap_prefs[i].strorint)
-                        /* store string as string */
-                        *((char **)(ldap_prefs[i].pref)) = val;
-                    else
-                        /* store as int */
-                        *((int *)(ldap_prefs[i].pref)) = atoi(val);
-                } else {
-                    /* ok, we have string to int mapping for this pref
-                       eg. "none", "simple", "sasl" map to 0, 128, 129 */
-                    j = 0;
-                    while(prefs_array[j].pref != NULL) {
-                        if (((strcmp(prefs_array[j].pref, pref)) == 0) &&
-                            ((strcmp(prefs_array[j].valuestring, val)) == 0)) {
-                            ldap_prefs[i].valid = 0;
-                            *((int *)(ldap_prefs[i].pref)) = prefs_array[j].value;
-                        }
-                        j++;
-                    } /* while j*/
-                } /* if else 0 == ldap_prefs*/
-                break;
-            } /* if strcmp */
-            i++;
-        } /* while i */
-        if (ldap_prefs[i].pref == NULL)
-            LOG(log_error, logtype_afpd,"afp_ldap.conf: Unknown option: \"%s\"", pref);
-    }  /*  EOF */
-
     /* check if the config is sane and complete */
     i = 0;
     ldap_config_valid = 1;
 
     while(ldap_prefs[i].pref != NULL) {
         if ( ldap_prefs[i].valid != 0) {
-            LOG(log_error, logtype_afpd,"afp_ldap.conf: Missing option: \"%s\"", ldap_prefs[i].name);
+            LOG(log_debug, logtype_afpd,"LDAP: Missing option: \"%s\"", ldap_prefs[i].name);
             ldap_config_valid = 0;
             break;
         }
@@ -133,16 +90,15 @@ int acl_ldap_readconfig(char *name)
 
     if (ldap_config_valid) {
         if (ldap_auth_method == LDAP_AUTH_NONE)
-            LOG(log_debug, logtype_afpd,"ldappref: Pref is ok. Using anonymous bind.");
+            LOG(log_debug, logtype_afpd,"LDAP: Using anonymous bind.");
         else if (ldap_auth_method == LDAP_AUTH_SIMPLE)
-            LOG(log_debug, logtype_afpd,"ldappref: Pref is ok. Using simple bind.");
+            LOG(log_debug, logtype_afpd,"LDAP: Using simple bind.");
         else {
             ldap_config_valid = 0;
-            LOG(log_error, logtype_afpd,"ldappref: Pref not ok. SASL not yet supported.");
+            LOG(log_error, logtype_afpd,"LDAP: SASL not yet supported.");
         }
     } else
-        LOG(log_error, logtype_afpd,"ldappref: Pref is not ok.");
-    fclose(f);
+        LOG(log_info, logtype_afpd,"LDAP: not used");
     return 0;
 }
-#endif /* HAVE_ACLS */
+#endif /* HAVE_LDAP */