]> git.sur5r.net Git - openldap/blob - libraries/libldap/init.c
527f4e876b12340f1e6d501e1937d002afda6de8
[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     if (file[file[0] == '.'] != '/') {
169         char *home = getenv("HOME");
170         char *path;
171         
172         if (home != NULL) {
173                 path = malloc(strlen(home) + strlen(file) + 3);
174         } else {
175                 path = malloc(strlen(file) + 3);
176         }
177
178
179         if(home != NULL && path != NULL) {
180                 /* try ~/file */
181                 sprintf(path, "%s/%s", home, file);
182                 openldap_ldap_init_w_conf(path);
183
184                 /* try ~/.file */
185                 sprintf(path, "%s/.%s", home, file);
186                 openldap_ldap_init_w_conf(path);
187         }
188         free(path);
189     }
190
191         /* try file */
192         openldap_ldap_init_w_conf(file);
193 }
194
195 static void openldap_ldap_init_w_env(const char *prefix)
196 {
197         char buf[MAX_LDAP_ATTR_LEN+MAX_LDAP_ENV_PREFIX_LEN];
198         int len;
199         int i;
200         void *p;
201         char *value;
202
203         if (prefix == NULL) {
204                 prefix = DEFAULT_LDAP_ENV_PREFIX;
205         }
206
207         strncpy(buf, prefix, MAX_LDAP_ENV_PREFIX_LEN);
208         buf[MAX_LDAP_ENV_PREFIX_LEN] = '\0';
209         len = strlen(buf);
210
211         for(i=0; attrs[i].type != ATTR_NONE; i++) {
212                 strcpy(&buf[len], attrs[i].name);
213                 value = getenv(buf);
214
215                 if(value == NULL) {
216                         continue;
217                 }
218
219                 p = &((char *) &gopts)[attrs[i].offset];
220
221                 switch(attrs[i].type) {
222                 case ATTR_BOOL:
223                         if((strcasecmp(value, "on") == 0) 
224                                 || (strcasecmp(value, "yes") == 0)
225                                 || (strcasecmp(value, "true") == 0))
226                         {
227                                 LDAP_BOOL_SET(&gopts, (int) attrs[i].data);
228
229                         } else {
230                                 LDAP_BOOL_CLR(&gopts, (int) attrs[i].data);
231                         }
232                         break;
233
234                 case ATTR_INT:
235                         * (int*) p = atoi(value);
236                         break;
237
238                 case ATTR_KV: {
239                                 struct ol_keyvalue *kv;
240
241                                 for(kv = (struct ol_keyvalue *) attrs[i].data;
242                                         kv->key != NULL;
243                                         kv++) {
244
245                                         if(strcasecmp(value, kv->key) == 0) {
246                                                 * (int*) p = kv->value;
247                                                 break;
248                                         }
249                                 }
250                         } break;
251
252                 case ATTR_STRING:
253                         if (* (char**) p != NULL) free(* (char**) p);
254                         if (*value == '\0') {
255                                 * (char**) p = NULL;
256                         } else {
257                                 * (char**) p = ldap_strdup(value);
258                         }
259                         break;
260                 }
261         }
262 }
263
264 void openldap_ldap_initialize( void )
265 {
266         if ( openldap_ldap_initialized ) {
267                 return;
268         }
269
270         gopts.ldo_version =     LDAP_VERSION2;
271         gopts.ldo_deref =       LDAP_DEREF_NEVER;
272         gopts.ldo_timelimit = LDAP_NO_LIMIT;
273         gopts.ldo_sizelimit = LDAP_NO_LIMIT;
274
275         gopts.ldo_defhost = ldap_strdup("localhost");
276         gopts.ldo_defport = LDAP_PORT;
277
278         gopts.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
279
280         LDAP_BOOL_ZERO(&gopts);
281
282 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || \
283         LDAP_VERSION_MAX > LDAP_VERSION2
284         LDAP_BOOL_SET(&gopts, LDAP_BOOL_REFERRALS);
285 #endif
286
287         openldap_ldap_init_w_conf(DEFAULT_LDAP_CONF_FILE);
288         openldap_ldap_init_w_userconf(DEFAULT_LDAP_USERRC_FILE);
289
290         {
291                 char *altfile = getenv("LDAPRC");
292
293                 if( altfile != NULL ) {
294                         openldap_ldap_init_w_conf( altfile );
295                 }
296         }
297
298         openldap_ldap_init_w_env(NULL);
299
300         openldap_ldap_initialized = 1;
301 }