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