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