]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
3801899ceba76c077f8fbd809be377f4f4b09558
[openldap] / libraries / libldap / init.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/socket.h>
7 #include <ac/string.h>
8 #include <ac/ctype.h>
9 #include <ac/time.h>
10
11 #include "ldap-int.h"
12 #include "ldapconfig.h"
13
14 struct ldapoptions openldap_ldap_global_options; 
15
16 #undef gopts
17 #define gopts openldap_ldap_global_options
18
19 int     openldap_ldap_initialized = 0;
20
21 #define ATTR_NONE       0
22 #define ATTR_BOOL       1
23 #define ATTR_INT        2
24 #define ATTR_KV         3
25 #define ATTR_STRING     4
26
27 struct ol_keyvalue {
28         char*           key;
29         int                     value;
30 };
31
32 static struct ol_keyvalue deref_kv[] = {
33         {"never", LDAP_DEREF_NEVER},
34         {"searching", LDAP_DEREF_SEARCHING},
35         {"finding", LDAP_DEREF_FINDING},
36         {"always", LDAP_DEREF_ALWAYS},
37         {NULL, 0}
38 };
39
40 static struct ol_attribute {
41         int                     type;
42         char*           name;
43         void*           data;
44         size_t          offset;
45 } attrs[] = {
46         {ATTR_KV,               "DEREF",        deref_kv, /* or &deref_kv[0] */
47                 offsetof(struct ldapoptions, ldo_deref)},
48         {ATTR_INT,              "SIZELIMIT",    NULL,
49                 offsetof(struct ldapoptions, ldo_sizelimit)},
50         {ATTR_INT,              "TIMELIMIT",    NULL,
51                 offsetof(struct ldapoptions, ldo_timelimit)},
52         {ATTR_STRING,   "BASE",                 NULL,
53                 offsetof(struct ldapoptions, ldo_defbase)},
54         {ATTR_STRING,   "HOST",                 NULL,
55                 offsetof(struct ldapoptions, ldo_defhost)},
56         {ATTR_INT,              "PORT",                 NULL,
57                 offsetof(struct ldapoptions, ldo_defport)},
58         {ATTR_BOOL,             "REFERRALS",    (void *) LDAP_BOOL_REFERRALS, 0},
59         {ATTR_BOOL,             "RESTART",              (void *) LDAP_BOOL_RESTART, 0},
60         {ATTR_BOOL,             "DNS",                  (void *) LDAP_BOOL_DNS, 0},
61         {ATTR_NONE,             NULL,           NULL,   0}
62 };
63
64 #define MAX_LDAP_ATTR_LEN  sizeof("SIZELIMIT")
65 #define MAX_LDAP_ENV_PREFIX_LEN 8
66
67 static void openldap_ldap_init_w_conf(const char *file)
68 {
69         char linebuf[128];
70         FILE *fp;
71         int i;
72         char *cmd, *opt;
73         char *start, *end;
74
75         fp = fopen(file, "r");
76         if(fp == NULL) {
77                 /* could not open file */
78                 return;
79         }
80
81         while((start = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {
82                 /* skip lines starting with '#' */
83                 if(*start == '#') continue;
84
85                 /* trim leading white space */
86                 while((*start != '\0') && isspace(*start)) start++;
87
88                 /* anything left? */
89                 if(*start == '\0') continue;
90
91                 /* trim trailing white space */
92                 end = &start[strlen(start)-1];
93                 while(isspace(*end)) end--;
94                 end[1] = '\0';
95
96                 /* anything left? */
97                 if(*start == '\0') continue;
98                 
99
100                 /* parse the command */
101                 cmd=start;
102                 while((*start != '\0') && !isspace(*start)) {
103                         start++;
104                 }
105                 if(*start == '\0') {
106                         /* command has no argument */
107                         continue;
108                 } 
109
110                 *start++ = '\0';
111
112                 /* we must have some non-whitespace to skip */
113                 while(isspace(*start)) start++;
114                 opt = start;
115
116                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
117                         void *p;
118
119                         if(strcasecmp(cmd, attrs[i].name) != 0) {
120                                 continue;
121                         }
122
123                         p = &((char *) &gopts)[attrs[i].offset];
124
125                         switch(attrs[i].type) {
126                         case ATTR_BOOL:
127                                 if((strcasecmp(opt, "on") == 0) 
128                                         || (strcasecmp(opt, "yes") == 0)
129                                         || (strcasecmp(opt, "true") == 0))
130                                 {
131                                         LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
132
133                                 } else {
134                                         LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
135                                 }
136
137                                 break;
138
139                         case ATTR_INT:
140                                 * (int*) p = atoi(opt);
141                                 break;
142
143                         case ATTR_KV: {
144                                         struct ol_keyvalue *kv;
145
146                                         for(kv = (struct ol_keyvalue *) attrs[i].data;
147                                                 kv->key != NULL;
148                                                 kv++) {
149
150                                                 if(strcasecmp(opt, kv->key) == 0) {
151                                                         * (int*) p = kv->value;
152                                                         break;
153                                                 }
154                                         }
155                                 } break;
156
157                         case ATTR_STRING:
158                                 if (* (char**) p != NULL) free(* (char**) p);
159                                 * (char**) p = ldap_strdup(opt);
160                                 break;
161                         }
162                 }
163         }
164 }
165
166 static void openldap_ldap_init_w_userconf(const char *file)
167 {
168         char *home = getenv("HOME");
169         char *path;
170         
171         if (home != NULL) {
172                 path = malloc(strlen(home) + strlen(file) + 3);
173         } else {
174                 path = malloc(strlen(file) + 3);
175         }
176
177
178         if(home != NULL && path != NULL) {
179                 /* try ~/file */
180                 sprintf(path, "%s/%s", home, file);
181                 openldap_ldap_init_w_conf(path);
182
183                 /* try ~/.file */
184                 sprintf(path, "%s/.%s", home, file);
185                 openldap_ldap_init_w_conf(path);
186         }
187
188         /* try file */
189         openldap_ldap_init_w_conf(file);
190 }
191
192 static void openldap_ldap_init_w_env(const char *prefix)
193 {
194         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
195         int len;
196         int i;
197         void *p;
198         char *value;
199
200         if (prefix == NULL) {
201                 prefix = DEFAULT_LDAP_ENV_PREFIX;
202         }
203
204         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
205         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
206         len = strlen(buf);
207
208         for(i=0; attrs[i].type != ATTR_NONE; i++) {
209                 strcpy(&buf[len], attrs[i].name);
210                 value = getenv(buf);
211
212                 if(value == NULL) {
213                         continue;
214                 }
215
216                 p = &((char *) &gopts)[attrs[i].offset];
217
218                 switch(attrs[i].type) {
219                 case ATTR_BOOL:
220                         if((strcasecmp(value, "on") == 0) 
221                                 || (strcasecmp(value, "yes") == 0)
222                                 || (strcasecmp(value, "true") == 0))
223                         {
224                                 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
225
226                         } else {
227                                 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
228                         }
229                         break;
230
231                 case ATTR_INT:
232                         * (int*) p = atoi(value);
233                         break;
234
235                 case ATTR_KV: {
236                                 struct ol_keyvalue *kv;
237
238                                 for(kv = (struct ol_keyvalue *) attrs[i].data;
239                                         kv->key != NULL;
240                                         kv++) {
241
242                                         if(strcasecmp(value, kv->key) == 0) {
243                                                 * (int*) p = kv->value;
244                                                 break;
245                                         }
246                                 }
247                         } break;
248
249                 case ATTR_STRING:
250                         if (* (char**) p != NULL) free(* (char**) p);
251                         if (*value == '\0') {
252                                 * (char**) p = NULL;
253                         } else {
254                                 * (char**) p = ldap_strdup(value);
255                         }
256                         break;
257                 }
258         }
259 }
260
261 void openldap_ldap_initialize( void )
262 {
263         if ( openldap_ldap_initialized ) {
264                 return;
265         }
266
267         gopts.ldo_version =     LDAP_VERSION2;
268         gopts.ldo_deref =       LDAP_DEREF_NEVER;
269         gopts.ldo_timelimit = LDAP_NO_LIMIT;
270         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
271
272         gopts.ldo_defhost = ldap_strdup("localhost");
273         gopts.ldo_defport = LDAP_PORT;
274
275         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
276
277         LDAP_BOOL_ZERO(&gopts);
278
279 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
280         LDAP_VERSION_MAX > LDAP_VERSION2
281         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
282 #endif
283
284         openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
285         openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
286
287         {
288                 char *altfile = getenv("LDAPRC");
289
290                 if( altfile != NULL ) {
291                         openldap_ldap_init_w_conf( altfile );
292                 }
293         }
294
295         openldap_ldap_init_w_env(NULL);
296
297         openldap_ldap_initialized = 1;
298 }