2 * Form Processing Web application that returns html based
3 * LDAP data with definitions from a configuration file.
5 * Jens Moller - Dec 11, 1998
19 #include "maint_form.h" /* for HTML Form manipulations */
23 #define SERVER "ldap.bigfoot.com"
24 #define CONFIG "web_ldap.cfg"
26 #define MAX_ATTRIB 100
28 #define version "v 1.1"
30 entry entries[MAX_ENTRIES];
33 char servername[MAX_CHARS];
34 char query[MAX_CHARS];
35 char searchbase[MAX_CHARS];
36 char htmlfile[MAX_CHARS];
39 int debug; /* if zero, no debug text displayed */
46 /* function Prototypes */
47 void process_cfg(char *config,
49 char *attribute_array[],
50 ATTRIB_TITLE *disp_attrib);
52 int strscn(char *istring,
55 void upcase_string(char *array,
58 int find_comma(char *array);
60 int find_colon(char *array);
62 void squeeze_blanks(char *array);
64 /* Pass in nothing and use the default config file, or
65 * pass in the config file to use */
67 main(int argc, char ** argv) {
70 int i, j, cl, x, how_many;
74 char *read_attrs[MAX_ATTRIB]; /* up to MAX_ATTRIB attribs returned */
75 struct berval **bvals;
76 char attrs_name[MAX_CHARS];
77 char config_file[MAX_CHARS];
79 char passed_in[MAX_CHARS];
81 ATTRIB_TITLE attribute[MAX_ATTRIB];
86 /* html initialization */
87 printf("Content-type: text/html\n\n");
88 printf("<html>\n<head>\n<title>Web Ldap Results</title>\n");
90 printf("<body text=\"#000000\" bgcolor=\"#FFFFFF\">\n");
91 printf("<h2>Web LDAP Results</h2>\n");
93 /* initialize ldap_data structure */
94 memset(ldap_data.servername,0,MAX_CHARS);
95 memset(ldap_data.query,0,MAX_CHARS);
96 memset(ldap_data.searchbase,0,MAX_CHARS);
97 memset(ldap_data.htmlfile,0,MAX_CHARS);
98 ldap_data.ldap_port_num = 0;
99 ldap_data.num_of_attrib = 0;
102 memset(passed_in,0,MAX_CHARS);
104 if (argc > 1) { /* interactive mode */
106 /* To use in this fashion when run from a Unix command line:
108 * > web_ldap DEF "cn=j*moller"
109 * > web_ldap DEF cn=jens moller
111 * NOTE: The quotes are required if a special
112 * character is a part of the LDAP request such
113 * as the asterix (*) in the above example.
116 /* Parameters passed in are
118 * argv[0] = Program Name (argc = 1)
119 * argv[1] = Config File Name (argc = 2)
120 * argv[2] = Ldap Request (argc => 3)
123 /* do we use a different config file ? */
124 strcpy(config_file, CONFIG);
127 if ((strcmp(argv[1],"DEF")) == 0) {
128 strcpy(config_file, CONFIG);
131 strcpy(config_file, argv[1]);
135 /* is there an LDAP request?
136 * if so, it may take up to 3 argv[x] values */
140 strcpy(temp, argv[2]);
144 strcpy(temp, argv[2]);
146 strcat(temp, argv[3]);
150 strcpy(temp, argv[2]);
152 strcat(temp, argv[3]);
154 strcat(temp, argv[4]);
158 for (i=0; i<strlen(temp);i++) {
159 if ((temp[i] != '"') &&
161 passed_in[j] = temp[i];
167 else { /* Non Interactive Mode - read from a form */
168 if (strcompare(getenv("REQUEST_METHOD"),"POST"))
170 printf("<p>++ Error - This script should be referenced with a METHOD of POST.\n");
173 if (strcompare(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded"))
175 printf("<p>++ Error - This script can only be used to decode form results. \n");
178 cl = atoi(getenv("CONTENT_LENGTH"));
180 for(x=0; cl && (!feof(stdin));x++)
182 entries[x].val = fmakeword(stdin,'&',&cl);
183 plustospace(entries[x].val);
184 unescape_url(entries[x].val);
185 entries[x].name = makeword(entries[x].val,'=');
186 how_many = x; /* keep track of how many we got */
189 printf("entries[%d].name=%s - ",x,entries[x].name);
190 printf("entries[%d].val =%s<br>\n",x,entries[x].val);
194 entries[x].name = NULL; /* entry after last valid one */
195 entries[x].val = NULL; /* is set to NULL */
197 if(!getvalue(entries, "FORM", &value))
199 printf("%s%s%s", "This script expected a 'FORM' value returned ",
200 "and did not get one. Make sure the HTML used for this ",
201 "script is correct.");
206 * LDAP_REQUEST - actual LDAP request, ie 'cn=j*moller'
207 * CONFIG = Configuration file
210 strcpy(config_file, CONFIG);
212 if(getvalue(entries, "LDAP_REQUEST", &value)) {
213 strcpy(passed_in,value);
216 if(getvalue(entries, "CONFIG", &value)) {
217 if ((strcmp("DEF",value)) == 0) {
218 strcpy(config_file, CONFIG);
221 strcpy(config_file, value);
227 /* zero out the attribute pointers/data area */
228 for (i = 0; i < MAX_ATTRIB; i++) {
229 read_attrs[i] = NULL;
230 memset(attribute[i].title,0,40);
233 /* read in the config file */
234 process_cfg(config_file, &ldap_data, read_attrs, attribute);
236 if (passed_in[0] != 0) {
237 strcpy(ldap_data.query,passed_in);
240 if (ldap_data.debug != 0) {
241 if ((logfp = fopen("web_ldap.log","w")) == 0) {
242 printf("<font color=red size=5>\n");
243 printf("<p>Unable to open requested log file: web_ldap.log<p>\n");
248 sprintf(temp,"==> web_ldap request made at: %s\n",ctime(&now));
251 sprintf(temp," Interactive Unix Command Line Request:\n\n");
254 sprintf(temp," Browser/Form Request:\n\n");
257 sprintf(temp," Server Name: %s\n", ldap_data.servername);
259 sprintf(temp," Query: %s\n", ldap_data.query);
261 sprintf(temp," Passed In: %s\n", passed_in);
263 sprintf(temp," Searchbase: %s\n",ldap_data.searchbase);
265 if (ldap_data.htmlfile[0] != 0) {
266 sprintf(temp," HTML File: %s\n",ldap_data.htmlfile);
269 sprintf(temp," HTML File: Non Provided - Use Default Processing\n");
272 sprintf(temp," LDAP Port: %d\n",ldap_data.ldap_port_num);
274 sprintf(temp," Number of Attributes: %d\n",ldap_data.num_of_attrib);
276 if (ldap_data.num_of_attrib > 0) {
277 for (i = 0; i < ldap_data.num_of_attrib; i++) {
278 sprintf(temp," - %s\n",read_attrs[i]);
283 sprintf(temp,"\n==< Process Arguments: %d >==\n\n argv[0]: %s\n",
287 sprintf(temp," argv[1]: %s\n",argv[1]);
291 sprintf(temp," argv[2]: %s\n",argv[2]);
295 sprintf(temp," argv[3]: %s\n",argv[3]);
299 sprintf(temp," argv[4]: %s\n",argv[4]);
307 if (ldap_data.debug != 0) {
308 if ((logfp = fopen("web_ldap.log","a")) == 0) {
312 sprintf(temp,"\n==< Results >==\n\n");
314 sprintf(temp,"** performing ldap_open at %s\n", ctime(&now));
320 if ( (ld = ldap_open(ldap_data.servername, ldap_data.ldap_port_num) ) == NULL)
322 printf("<font color=red><b>ldap_open error</b></font>\n");
323 if (ldap_data.debug != 0) {
324 if ((logfp = fopen("web_ldap.log","a")) == 0) {
327 sprintf(temp,"++ ldap_open error\n");
333 printf("</body>\n</html>\n");
337 /*authenticate as nobody */
338 if (ldap_data.debug != 0) {
339 if ((logfp = fopen("web_ldap.log","a")) == 0) {
343 sprintf(temp,"** performing ldap_bind_s at %s\n",ctime(&now));
349 if(ldap_bind_s(ld, "", "", LDAP_AUTH_SIMPLE) != 0)
351 printf("<font color=red><b>");
352 ldap_perror (ld, "ldap_simple_bind_s");
353 printf("</b></font>\n");
355 if (ldap_data.debug != 0) {
356 if ((logfp = fopen("web_ldap.log","a")) == 0) {
359 sprintf(temp,"++ ldap_bind_s error\n");
365 printf("</body>\n</html>\n");
369 printf("<b>Directory Lookup Results</b>\n<pre>\n");
370 printf("<hr><p>\n<pre>\n");
373 if (ldap_data.debug != 0) {
374 if ((logfp = fopen("web_ldap.log","a")) == 0) {
378 sprintf(temp,"** performing ldap_search_s at %s\n",ctime(&now));
384 if(ldap_search_s(ld, ldap_data.searchbase, LDAP_SCOPE_SUBTREE,
385 ldap_data.query, read_attrs, 0, &res)
388 ldap_perror(ld, "ldap_search_s");
391 for (e=ldap_first_entry(ld, res); e != NULL; e=ldap_next_entry(ld, e))
393 dn = ldap_get_dn(ld, e);
395 if (ldap_data.debug != 0) {
396 if ((logfp = fopen("web_ldap.log","a")) == 0) {
399 sprintf(temp," dn=%s\n", dn);
406 /*print each attribute */
407 for (a = ldap_first_attribute(ld, e, &ptr);
409 a = ldap_next_attribute(ld, e, ptr) )
411 strcpy(attrs_name, a);
412 /* print attribute name */
413 printf("%s: ", attrs_name);
415 /*print each value */
417 vals = ldap_get_values(ld, e, a);
419 for(i=0; vals[i] != NULL; i++)
420 /* print value of attribute */
421 printf("%s\n", vals[i],strlen(vals[i]));
423 ldap_value_free(vals);
429 /*free the search results */
436 if (ldap_data.debug != 0) {
437 if ((logfp = fopen("web_ldap.log","a")) == 0) {
441 sprintf(temp,"\nFinished gathering results at %s\n",ctime(&now));
443 sprintf(temp,"==< Done >==\n");
449 printf("</body>\n</html>\n");
452 /* Process the user passed in configuration file */
453 void process_cfg(char *config,
454 LDAP_INFO *ldap_data,
455 char *attribute_array[],
456 ATTRIB_TITLE *disp_attrib) {
458 char file_data[1024];
459 char upfile_data[1024];
461 int lcomma, lcolon, attrib_pos;
464 strcpy(ldap_data->servername,SERVER);
465 ldap_data->ldap_port_num = LDAP_PORT;
466 strcpy(ldap_data->query,"cn=jens*moller");
468 /* config file needs to be in the cgi-bin directory */
469 if ((fp = fopen(config,"r")) == 0) {
475 for (;;) { /* read until eof */
476 fgets (file_data,1024,fp);
478 if (file_data[0] != '#') { /* skip commented lines */
479 upcase_string(file_data,upfile_data);
481 /* get the server specific data */
482 if (strscn(upfile_data,"SERVER:") == 0) {
483 lcolon = find_colon(file_data) + 1;
484 lcomma = find_comma(file_data);
486 memset(ldap_data->servername,0,MAX_CHARS);
487 strncpy(ldap_data->servername,&file_data[lcolon],
489 ldap_data->ldap_port_num = atoi(&file_data[lcomma + 1]);
492 strcpy(ldap_data->servername,&file_data[lcolon]);
494 squeeze_blanks(ldap_data->servername);
496 else if (strscn(upfile_data,"SEARCHBASE:") == 0) {
497 lcolon = find_colon(file_data) + 1;
498 strcpy(ldap_data->searchbase,&file_data[lcolon]);
499 squeeze_blanks(ldap_data->searchbase);
501 else if (strscn(upfile_data,"HTMLFILE:") == 0) {
502 lcolon = find_colon(file_data) + 1;
503 strcpy(ldap_data->htmlfile,&file_data[lcolon]);
505 else if (strscn(upfile_data,"DEBUG:") == 0) {
506 lcolon = find_colon(file_data) + 1;
507 ldap_data->debug = atoi(&file_data[lcolon]);
510 /* get the attribute list */
511 else if ((file_data[0] != ' ') && (file_data[0] != 0)) {
513 /* data appears as a comma delimited list, where:
515 * attrib_name (char),display_length (int)
517 * (default length = 20 if display_length undefined)
519 * is how each record is defined */
520 lcomma = find_comma(file_data);
522 strcpy(temp,file_data);
523 squeeze_blanks(temp);
526 strncpy(temp,file_data,lcomma);
527 squeeze_blanks(temp);
529 attribute_array[attrib_pos] = malloc(strlen(temp));
530 strcpy(attribute_array[attrib_pos],temp);
532 ldap_data->num_of_attrib = attrib_pos;
538 /* find character substring matches */
539 int strscn(char * istring,
541 int i, status, icmp, len;
543 len = (strlen(istring) + 1) - strlen(tstring);
544 if (len < 1) len = 1;
545 for (i=0;i<len ;i++) {
546 icmp = memcmp(&istring[i],tstring,strlen(tstring));
555 /* convert the array to uparray, where uparray contains upper
557 void upcase_string(char *array,
560 for (i=0; i < strlen(array); i++) {
561 uparray[i] = toupper(array[i]);
567 /* return the position of the first comma - ',' - from within a string */
568 int find_comma(char *array){
570 for (i = 0; i < strlen(array); i++) {
571 if (array[i] == ',') return(i);
576 /* return the position of the first colon - '.' - from within a string */
577 int find_colon(char *array){
579 for (i = 0; i < strlen(array); i++) {
580 if (array[i] == ':') return(i);
585 /* Remove unneeded blanks from a character array. Don't leave
586 * any at the end & throw away any newline characters */
587 void squeeze_blanks(char *array){
592 pos = 0; /* location within temp array */
593 blank = 0; /* # of blanks written in a row */
595 for (i = 0; i < strlen(array); i++) {
596 if (array[i] != ' ') {
597 temp[pos] = array[i];
601 else if ((blank == 0) &&
604 temp[pos] = array[i];
610 if (array[strlen(array) - 1] <= ' ')
611 array[strlen(array) - 1] = 0;