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/globals.h>
29#include <atalk/ldapconfig.h>
30#include <atalk/logger.h>
31#include <atalk/iniparser.h>
32
33void acl_ldap_freeconfig(void)
34{
35    for (int i = 0; ldap_prefs[i].name != NULL; i++) {
36        if (ldap_prefs[i].intfromarray == 0 && ldap_prefs[i].strorint == 0) {
37            free(*((char **)(ldap_prefs[i].pref)));
38            *((char **)(ldap_prefs[i].pref)) = NULL;
39        }
40        ldap_prefs[i].valid = ldap_prefs[i].valid_save;
41    }
42}
43
44int acl_ldap_readconfig(dictionary *iniconfig)
45{
46    int i, j;
47    const char *val;
48
49    i = 0;
50    /* now see if its a correct pref */
51    for (i = 0; ldap_prefs[i].name != NULL; i++) {
52        if ((val = atalk_iniparser_getstring(iniconfig, INISEC_GLOBAL, ldap_prefs[i].name, NULL))) {
53            /* check if we have pre-defined values */
54            if (ldap_prefs[i].intfromarray == 0) {
55                /* no, its just a string */
56                ldap_prefs[i].valid = 0;
57                if (ldap_prefs[i].strorint)
58                    /* store as int */
59                    *((int *)(ldap_prefs[i].pref)) = atoi(val);
60                else
61                    /* store string as string */
62                    *((const char **)(ldap_prefs[i].pref)) = strdup(val);
63            } else {
64                /* ok, we have string to int mapping for this pref
65                   eg. "none", "simple", "sasl" map to 0, 128, 129 */
66                for (j = 0; prefs_array[j].pref != NULL; j++) {
67                    if ((strcmp(prefs_array[j].pref, ldap_prefs[i].name) == 0)
68                        && (strcmp(prefs_array[j].valuestring, val) == 0)) {
69                        ldap_prefs[i].valid = 0;
70                        *((int *)(ldap_prefs[i].pref)) = prefs_array[j].value;
71                        break;
72                    }
73                }
74            }
75        }
76    }
77
78    /* check if the config is sane and complete */
79    i = 0;
80    ldap_config_valid = 1;
81
82    while(ldap_prefs[i].pref != NULL) {
83        if ( ldap_prefs[i].valid != 0) {
84            LOG(log_debug, logtype_afpd,"LDAP: Missing option: \"%s\"", ldap_prefs[i].name);
85            ldap_config_valid = 0;
86            break;
87        }
88        i++;
89    }
90
91    if (ldap_config_valid) {
92        if (ldap_auth_method == LDAP_AUTH_NONE)
93            LOG(log_debug, logtype_afpd,"LDAP: Using anonymous bind.");
94        else if (ldap_auth_method == LDAP_AUTH_SIMPLE)
95            LOG(log_debug, logtype_afpd,"LDAP: Using simple bind.");
96        else {
97            ldap_config_valid = 0;
98            LOG(log_error, logtype_afpd,"LDAP: SASL not yet supported.");
99        }
100    } else
101        LOG(log_info, logtype_afpd,"LDAP: not used");
102    return 0;
103}
104#endif /* HAVE_LDAP */
105