]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
Fix ldap_send_initial_request() to open connection if not already
[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         char*           key;
33         int                     value;
34 };
35
36 static 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 struct ol_attribute {
45         int                     type;
46         char*           name;
47         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",    (void *) LDAP_BOOL_REFERRALS, 0},
63         {ATTR_BOOL,             "RESTART",              (void *) LDAP_BOOL_RESTART, 0},
64         {ATTR_BOOL,             "DNS",                  (void *) LDAP_BOOL_DNS, 0},
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(*start)) start++;
96
97                 /* anything left? */
98                 if(*start == '\0') continue;
99
100                 /* trim trailing white space */
101                 end = &start[strlen(start)-1];
102                 while(isspace(*end)) end--;
103                 end[1] = '\0';
104
105                 /* anything left? */
106                 if(*start == '\0') continue;
107                 
108
109                 /* parse the command */
110                 cmd=start;
111                 while((*start != '\0') && !isspace(*start)) {
112                         start++;
113                 }
114                 if(*start == '\0') {
115                         /* command has no argument */
116                         continue;
117                 } 
118
119                 *start++ = '\0';
120
121                 /* we must have some non-whitespace to skip */
122                 while(isspace(*start)) start++;
123                 opt = start;
124
125                 for(i=0; attrs[i].type != ATTR_NONE; i++) {
126                         void *p;
127
128                         if(strcasecmp(cmd, attrs[i].name) != 0) {
129                                 continue;
130                         }
131
132                         p = &((char *) &gopts)[attrs[i].offset];
133
134                         switch(attrs[i].type) {
135                         case ATTR_BOOL:
136                                 if((strcasecmp(opt, "on") == 0) 
137                                         || (strcasecmp(opt, "yes") == 0)
138                                         || (strcasecmp(opt, "true") == 0))
139                                 {
140                                         LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
141
142                                 } else {
143                                         LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
144                                 }
145
146                                 break;
147
148                         case ATTR_INT:
149                                 * (int*) p = atoi(opt);
150                                 break;
151
152                         case ATTR_KV: {
153                                         struct ol_keyvalue *kv;
154
155                                         for(kv = (struct ol_keyvalue *) attrs[i].data;
156                                                 kv->key != NULL;
157                                                 kv++) {
158
159                                                 if(strcasecmp(opt, kv->key) == 0) {
160                                                         * (int*) p = kv->value;
161                                                         break;
162                                                 }
163                                         }
164                                 } break;
165
166                         case ATTR_STRING:
167                                 if (* (char**) p != NULL) free(* (char**) p);
168                                 * (char**) p = strdup(opt);
169                                 break;
170                         }
171                 }
172         }
173 }
174
175 static void openldap_ldap_init_w_userconf(const char *file)
176 {
177         char *home;
178         char *path;
179
180         if (file == NULL) {
181                 /* no file name */
182                 return;
183         }
184
185         home = getenv("HOME");
186
187         if (home != NULL) {
188                 path = malloc(strlen(home) + strlen(file) + 3);
189         } else {
190                 path = malloc(strlen(file) + 3);
191         }
192
193         if(home != NULL && path != NULL) {
194                 /* we assume UNIX path syntax is used... */
195
196                 /* try ~/file */
197                 sprintf(path, "%s/%s", home, file);
198                 openldap_ldap_init_w_conf(path);
199
200                 /* try ~/.file */
201                 sprintf(path, "%s/.%s", home, file);
202                 openldap_ldap_init_w_conf(path);
203         }
204
205         if(path != NULL) {
206                 free(path);
207         }
208
209         /* try file */
210         openldap_ldap_init_w_conf(file);
211 }
212
213 static void openldap_ldap_init_w_env(const char *prefix)
214 {
215         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
216         int len;
217         int i;
218         void *p;
219         char *value;
220
221         if (prefix == NULL) {
222                 prefix = DEFAULT_LDAP_ENV_PREFIX;
223         }
224
225         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
226         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
227         len = strlen(buf);
228
229         for(i=0; attrs[i].type != ATTR_NONE; i++) {
230                 strcpy(&buf[len], attrs[i].name);
231                 value = getenv(buf);
232
233                 if(value == NULL) {
234                         continue;
235                 }
236
237                 p = &((char *) &gopts)[attrs[i].offset];
238
239                 switch(attrs[i].type) {
240                 case ATTR_BOOL:
241                         if((strcasecmp(value, "on") == 0) 
242                                 || (strcasecmp(value, "yes") == 0)
243                                 || (strcasecmp(value, "true") == 0))
244                         {
245                                 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
246
247                         } else {
248                                 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
249                         }
250                         break;
251
252                 case ATTR_INT:
253                         * (int*) p = atoi(value);
254                         break;
255
256                 case ATTR_KV: {
257                                 struct ol_keyvalue *kv;
258
259                                 for(kv = (struct ol_keyvalue *) attrs[i].data;
260                                         kv->key != NULL;
261                                         kv++) {
262
263                                         if(strcasecmp(value, kv->key) == 0) {
264                                                 * (int*) p = kv->value;
265                                                 break;
266                                         }
267                                 }
268                         } break;
269
270                 case ATTR_STRING:
271                         if (* (char**) p != NULL) free(* (char**) p);
272                         if (*value == '\0') {
273                                 * (char**) p = NULL;
274                         } else {
275                                 * (char**) p = strdup(value);
276                         }
277                         break;
278                 }
279         }
280 }
281
282 void openldap_ldap_initialize( void )
283 {
284         if ( openldap_ldap_initialized ) {
285                 return;
286         }
287
288         ldap_pvt_init_utils();
289
290         gopts.ldo_version =     LDAP_VERSION2;
291         gopts.ldo_deref =       LDAP_DEREF_NEVER;
292         gopts.ldo_timelimit = LDAP_NO_LIMIT;
293         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
294
295         gopts.ldo_debug = 0;
296
297         gopts.ldo_defhost = strdup("localhost");
298         gopts.ldo_defport = LDAP_PORT;
299
300         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
301
302         LDAP_BOOL_ZERO(&gopts);
303
304 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
305         LDAP_VERSION_MAX > LDAP_VERSION2
306         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
307 #endif
308
309         openldap_ldap_initialized = 1;
310
311         if( getenv("LDAPNOINIT") != NULL ) {
312                 return;
313         }
314
315         openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
316         openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
317
318         {
319                 char *altfile = getenv("LDAPCONF");
320
321                 if( altfile != NULL ) {
322                         openldap_ldap_init_w_conf( altfile );
323                 }
324         }
325
326         {
327                 char *altfile = getenv("LDAPRC");
328
329                 if( altfile != NULL ) {
330                         openldap_ldap_init_w_userconf( altfile );
331                 }
332         }
333
334         openldap_ldap_init_w_env(NULL);
335 }