]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
Added the functions ldap_rename2() and ldap_rename2_s() to support LDAP
[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((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                         p = &((char *) &gopts)[attrs[i].offset];
134
135                         switch(attrs[i].type) {
136                         case ATTR_BOOL:
137                                 if((strcasecmp(opt, "on") == 0) 
138                                         || (strcasecmp(opt, "yes") == 0)
139                                         || (strcasecmp(opt, "true") == 0))
140                                 {
141                                         LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
142
143                                 } else {
144                                         LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
145                                 }
146
147                                 break;
148
149                         case ATTR_INT:
150                                 * (int*) p = atoi(opt);
151                                 break;
152
153                         case ATTR_KV: {
154                                         struct ol_keyvalue *kv;
155
156                                         for(kv = (struct ol_keyvalue *) attrs[i].data;
157                                                 kv->key != NULL;
158                                                 kv++) {
159
160                                                 if(strcasecmp(opt, kv->key) == 0) {
161                                                         * (int*) p = kv->value;
162                                                         break;
163                                                 }
164                                         }
165                                 } break;
166
167                         case ATTR_STRING:
168                                 if (* (char**) p != NULL) free(* (char**) p);
169                                 * (char**) p = strdup(opt);
170                                 break;
171                         }
172                 }
173         }
174 }
175
176 static void openldap_ldap_init_w_userconf(const char *file)
177 {
178         char *home;
179         char *path;
180
181         if (file == NULL) {
182                 /* no file name */
183                 return;
184         }
185
186         home = getenv("HOME");
187
188         if (home != NULL) {
189                 path = malloc(strlen(home) + strlen(file) + 3);
190         } else {
191                 path = malloc(strlen(file) + 3);
192         }
193
194         if(home != NULL && path != NULL) {
195                 /* we assume UNIX path syntax is used... */
196
197                 /* try ~/file */
198                 sprintf(path, "%s/%s", home, file);
199                 openldap_ldap_init_w_conf(path);
200
201                 /* try ~/.file */
202                 sprintf(path, "%s/.%s", home, file);
203                 openldap_ldap_init_w_conf(path);
204         }
205
206         if(path != NULL) {
207                 free(path);
208         }
209
210         /* try file */
211         openldap_ldap_init_w_conf(file);
212 }
213
214 static void openldap_ldap_init_w_env(const char *prefix)
215 {
216         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
217         int len;
218         int i;
219         void *p;
220         char *value;
221
222         if (prefix == NULL) {
223                 prefix = DEFAULT_LDAP_ENV_PREFIX;
224         }
225
226         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
227         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
228         len = strlen(buf);
229
230         for(i=0; attrs[i].type != ATTR_NONE; i++) {
231                 strcpy(&buf[len], attrs[i].name);
232                 value = getenv(buf);
233
234                 if(value == NULL) {
235                         continue;
236                 }
237
238                 p = &((char *) &gopts)[attrs[i].offset];
239
240                 switch(attrs[i].type) {
241                 case ATTR_BOOL:
242                         if((strcasecmp(value, "on") == 0) 
243                                 || (strcasecmp(value, "yes") == 0)
244                                 || (strcasecmp(value, "true") == 0))
245                         {
246                                 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
247
248                         } else {
249                                 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
250                         }
251                         break;
252
253                 case ATTR_INT:
254                         * (int*) p = atoi(value);
255                         break;
256
257                 case ATTR_KV: {
258                                 struct ol_keyvalue *kv;
259
260                                 for(kv = (struct ol_keyvalue *) attrs[i].data;
261                                         kv->key != NULL;
262                                         kv++) {
263
264                                         if(strcasecmp(value, kv->key) == 0) {
265                                                 * (int*) p = kv->value;
266                                                 break;
267                                         }
268                                 }
269                         } break;
270
271                 case ATTR_STRING:
272                         if (* (char**) p != NULL) free(* (char**) p);
273                         if (*value == '\0') {
274                                 * (char**) p = NULL;
275                         } else {
276                                 * (char**) p = strdup(value);
277                         }
278                         break;
279                 }
280         }
281 }
282
283 void openldap_ldap_initialize( void )
284 {
285         if ( openldap_ldap_initialized ) {
286                 return;
287         }
288
289         ldap_pvt_init_utils();
290
291         gopts.ldo_version =     LDAP_VERSION2;
292         gopts.ldo_deref =       LDAP_DEREF_NEVER;
293         gopts.ldo_timelimit = LDAP_NO_LIMIT;
294         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
295
296         gopts.ldo_debug = 0;
297
298         gopts.ldo_defhost = strdup("localhost");
299         gopts.ldo_defport = LDAP_PORT;
300
301         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
302
303         LDAP_BOOL_ZERO(&gopts);
304
305 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
306         LDAP_VERSION_MAX > LDAP_VERSION2
307         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
308 #endif
309
310         openldap_ldap_initialized = 1;
311
312         if( getenv("LDAPNOINIT") != NULL ) {
313                 return;
314         }
315
316         openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
317         openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
318
319         {
320                 char *altfile = getenv("LDAPCONF");
321
322                 if( altfile != NULL ) {
323                         openldap_ldap_init_w_conf( altfile );
324                 }
325         }
326
327         {
328                 char *altfile = getenv("LDAPRC");
329
330                 if( altfile != NULL ) {
331                         openldap_ldap_init_w_userconf( altfile );
332                 }
333         }
334
335         openldap_ldap_init_w_env(NULL);
336 }