]> arthur.barton.de Git - netatalk.git/blob - libatalk/acl/ldap_config.c
Untabify and reindent
[netatalk.git] / libatalk / acl / ldap_config.c
1 /*
2    $Id: ldap_config.c,v 1.1 2009-02-02 11:55:01 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_NFSv4_ACLS
21
22 #include <stdio.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 */
34 static 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 ldap.conf file */
62 int 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                 } /* else */
113             }
114             i++;
115         } /* while i */
116     }  /*  EOF */
117
118     /* check if the config is sane and complete */
119     i = 0;
120     ldap_config_valid = 1;
121
122     while(ldap_prefs[i].pref != NULL) {
123         if ( ldap_prefs[i].valid != 0) {
124             ldap_config_valid = 0;
125             break;
126         }
127         i++;
128     }
129
130     if (ldap_config_valid) {
131         if (ldap_auth_method == LDAP_AUTH_NONE)
132             LOG(log_debug, logtype_afpd,"ldappref: Pref is ok. Using anonymous bind.");
133         else if (ldap_auth_method == LDAP_AUTH_SIMPLE)
134             LOG(log_debug, logtype_afpd,"ldappref: Pref is ok. Using simple bind.");
135         else {
136             ldap_config_valid = 0;
137             LOG(log_error, logtype_afpd,"ldappref: Pref not ok. SASL not yet supported.");      
138         }
139     } else
140         LOG(log_error, logtype_afpd,"ldappref: Pref is not ok.");       
141     fclose(f);
142     return 0;
143 }
144 #endif