1 /*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
5 * Function:..WorldWideWeb-X.500-Gateway - Supporting Routines *
6 * Based on web500gw.c 1.3 written by Frank Richter, TU Chemmniz *
7 * which is based on go500gw by Tim Howes, University of *
8 * Michigan - All rights reserved *
10 * Authors:...Dr. Kurt Spanier & Bernhard Winkler, *
11 * Zentrum fuer Datenverarbeitung, Bereich Entwicklung *
12 * neuer Dienste, Universitaet Tuebingen, GERMANY *
15 * Creation date: Z D D V V *
16 * August 16 1995 Z D D V V *
17 * Last modification: Z D D V V *
18 * September 7 1999 ZZZZ DDD V *
20 _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_*/
23 * $Id: support.c,v 1.6 1999/09/10 15:01:20 zrnsk01 Exp $
33 * Utilities for dealing with HTML junk
36 char hex[17] = "0123456789abcdef";
39 PUBLIC char * hex_decode (in)
47 if (*in == '?') /* start search */
49 if (*in == '%') { /* Hex escape */
51 if(!(c = *in++)) break;
53 if(!(c = *in++)) break;
54 *out++ = (b<<4) + from_hex(c);
55 } else if (q && *in == '+') {
56 /* '+' is legal in path, in search it's a ' ' */
66 /* end of function: hex_decode */
68 /* decode in search (for do_modify) */
70 PUBLIC char * hex_qdecode (in)
77 if (*in == '%') { /* Hex escape */
79 if(!(c = *in++)) break;
81 if(!(c = *in++)) break;
82 *out++ = (b<<4) + from_hex(c);
83 } else if (*in == '+') { /* we are in search, so: '+' -> ' ' */
93 /* end of function: hex_qdecode */
95 PUBLIC char * form_encode (in)
100 /* bzero(out, 1024); */
102 if (*in == '"' || *in == '>' ) {
109 /* fprintf( stderr, "returning - esc: %s.\n", buffer); */
112 /* end of function: form_encode */
114 /* gtime(): the inverse of localtime().
115 This routine was supplied by Mike Accetta at CMU many years ago.
119 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
123 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
125 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
127 PRIVATE time_t gtime (tm)
137 register long result;
139 if ((sec = tm -> tm_sec) < 0 || sec > 59
140 || (mins = tm -> tm_min) < 0 || mins > 59
141 || (hour = tm -> tm_hour) < 0 || hour > 24
142 || (mday = tm -> tm_mday) < 1 || mday > 31
143 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
149 year = YEAR (tm -> tm_year);
151 for (i = 1970; i < year; i++)
152 result += dysize (i);
153 if (dysize (year) == 366 && mon >= 3)
156 result += dmsize[mon - 1];
158 result = 24 * result + hour;
159 result = 60 * result + mins;
160 result = 60 * result + sec;
163 /* end of function: gtime */
165 PUBLIC char * format_date (s, format)
169 /* PATCHED by /KSp, 94/04/29 */
170 static char date[256];
178 tm.tm_year = 10*((*s)[0] - '0') + ((*s)[1] - '0');
179 tm.tm_mon = 10*((*s)[2] - '0') + ((*s)[3] - '0') - 1;
180 tm.tm_mday = 10*((*s)[4] - '0') + ((*s)[5] - '0');
181 tm.tm_hour = 10*((*s)[6] - '0') + ((*s)[7] - '0');
182 tm.tm_min = 10*((*s)[8] - '0') + ((*s)[9] - '0');
183 tm.tm_sec = 10*((*s)[10] - '0') + ((*s)[11] - '0');
185 /* PATCHED for HPUX by /KSp, 94/04/28 */
189 #if !defined(__hpux) && !defined(__linux__) && !defined(__sun)
197 strftime(date, 256, format, ntm);
200 /* end of function: format_date */
202 PUBLIC char * friendly_dn (dn, glob)
206 /* PATCHED BY /KSp, 94/04/29 */
207 /* static char fufn[1024], **s; */
211 /* Again patched by /KSp, 97/01/25: dynamic mem-allocation */
214 if ( ( fufn = calloc( 1, BUFSIZ )) ) {
216 if (strlen(dn) == 0) {
217 strcpy( fufn, glob->la[77] );
223 s = ldap_explode_dn( dn, 1 );
225 strcat( fufn, s[i++] );
226 strcat( fufn, ", " );
228 strcat( fufn, ldap_friendly_name( glob->friendlyfile, s[i], &fm ));
234 /* end of function: friendly_dn */
237 PUBLIC char * format_time (whatTime)
241 static char theTime[_TIMEOUT_LEN+1];
243 timer = whatTime ? whatTime : time (&timer);
244 strftime (theTime, (_TIMEOUT_LEN + 1), _LOG_TIME, localtime (&timer));
248 } /* end of function: format_time */
250 PUBLIC char * strQuoteChr(string, c)
260 inQuote = ( (inQuote == TRUE) ? FALSE : TRUE );
261 if( (*cPtr == c) && (inQuote == FALSE) )
268 /* end of function: strQuoteChr */
270 PUBLIC char * strrQuoteChr(string, c)
277 cPtr = string + strlen(string) - 1;
278 while(cPtr >= string) {
280 inQuote = ( (inQuote == TRUE) ? FALSE : TRUE );
281 if( (*cPtr == c) && (inQuote == FALSE) )
288 /* end of function: strrQuoteChr */
291 PUBLIC void disp_file(glob, filename, fp)
298 if(filename && (fp2 = fopen(filename, "r" ))) {
299 while ( fgets(buf, 4095, fp2) != NULL )
300 fprintf( fp, "%s",buf);
304 /* Copyright-Zeile */
305 if(filename == glob->footer ||
306 (glob->basedn && filename == glob->basedn->foot))
307 fprintf( fp, glob->la[99],glob->la[101], copyright );
309 /* end of function: disp_file */
312 PUBLIC int dn_cmp(dn1, dn2)
316 while(*dn1 == ' ' || *dn1 == '"')
318 while(*dn2 == ' ' || *dn2 == '"')
321 return(FALSE); /* equality */
322 } while(tolower(*dn1++) == tolower(*dn2++));
325 /* end of function: dn_cmp */
331 * comparison of dns by rdn parts. in case of unmatched the part(s) which
332 * matched can be returned (matched not NULL)
336 * - dn1 (in url format)
338 * - matched (pointer to dynamically allocatable string, or NULL)
342 * - DN_EQUAL | DN_LESS | DN_GREATER | DN_UNMATCHED (with matched allocated)
346 dn_cmp_parts( dn1, dn2, matched )
351 char **dn1arr = dn2charray( dn1 );
352 char **dn2arr = dn2charray( dn2 );
355 int result = DN_EQUAL;
357 for ( idx = 0; dn1arr[idx] && dn2arr[idx]; idx++ ) {
359 if ( strcasecmp( dn1arr[idx], dn2arr[idx] )) {
367 /* what was the result */
378 for ( idx2 = 0; idx2 < idx; idx2++ ) {
381 sprintf( buf, "%s,%s", dn1arr[idx2], buf2 );
384 trimright( buf, "," );
386 *matched = strdup( buf );
390 result = DN_UNMATCHED;
392 } else if ( dn1arr[idx] ) result = DN_GREATER;
393 else if ( dn2arr[idx] ) result = DN_LESS;
395 charray_free( dn1arr ); charray_free( dn2arr );
404 * Comparison of substring lists
407 PUBLIC int strlstcmp (s1, s2, sep)
420 target = str_tolower (strdup (s1));
421 source = str_tolower (strdup (s2));
424 while (idx && (idx2 = strchr (idx+1, sep))) {
426 tmpChar = *(++idx2); *idx2 = '\0';
427 if (strstr (target, idx)) {
444 /* end of function: strlstcmp */
446 PUBLIC char *hex_html_encode(string, flag)
448 int flag; /* 0->hex 1->html */
450 static char strbuf[10*BUFSIZ];
454 for(strptr = string; *strptr; strptr++){
455 /* 200 a -> ä fuer aouAOU */
456 if((flag==1) && (((int)*strptr&255)==200)) {
457 if(strchr("aouAOU", *(strptr+1))) {
458 sprintf(strbuf, "%s&%cuml;", strbuf, *++strptr);
463 /* &#xxx; Handling-Patch */
464 if((flag==1) && (((int)*strptr&255)>=160)) {
465 sprintf(strbuf, "%s&#%d;", strbuf, ((int)*strptr&255));
470 if(!encoding_tbl[(int)*strptr&255][flag]) {
471 sprintf(strbuf, "%s%c", strbuf, *strptr);
473 strcat(strbuf, encoding_tbl[(int)*strptr&255][flag]);
478 /* end of function: hex_html_encode */
480 /* Strips basecount+1 characters of type target at the end of an RDN */
481 PUBLIC char *dnrcut(rdn, target, basecount)
486 static char buf[BUFSIZ];
488 int rdncount, morecount;
490 rdncount = chrcnt(rdn, target);
491 if( (morecount = (rdncount - basecount)) > 0) {
495 strptr = strpbrk(strptr+1, target);
501 /* end of function: dnrcut */
503 PUBLIC char **dn2charray(dn)
506 char *dnbuf, *strptr, **a=NULL;
510 a = (char **) ch_calloc( 1, sizeof( char ** ));
517 strptr = strrQuoteChr(dnbuf, ',');
521 while(*strptr == ' ')
524 strptr = trim( strptr, " " );
526 charray_add( &a, strptr ? strptr : trim( dnbuf, " " ));
531 /* end of function: dn2charray */
534 /* get the parent DN for a given one */
542 a = dn2charray( dn );
543 *tmp = *(tmp + 1) = '\0';
548 /* we have to re-build the DN beginning at the last array element */
549 for ( rdnH = a; *rdnH; rdnH++ )
552 /* re-build DN from it's parts */
554 for ( ; rdnH >= a; rdnH-- ) {
556 sprintf( tmp, "%s,%s", tmp, *rdnH );
564 /* ignore a leading ',' */
565 return( strdup( tmp + 1 ));
570 PUBLIC char *elapsed(firsttime, secondtime)
571 struct timeval firsttime;
572 struct timeval secondtime;
574 long int elapsedmicrosec, elapsedsec;
575 char elapsed_string[BUFSIZ];
577 elapsedsec = secondtime.tv_sec - firsttime.tv_sec;
578 elapsedmicrosec = secondtime.tv_usec - firsttime.tv_usec;
579 if(elapsedmicrosec < 0) {
580 elapsedmicrosec += 1000000;
583 if(elapsedsec > 1000){
587 sprintf(elapsed_string, "%ld.%.6ld", elapsedsec, elapsedmicrosec);
588 return(strdup(elapsed_string));
590 /* end of function: elapsed */
593 /* performance-log on exit */
594 PUBLIC int exit_tweb(rc)
597 struct timeval secondtime;
599 gettimeofday(&secondtime, NULL);
600 if(!secondtime.tv_sec || !timestore[0].tv_sec) exit(rc);
603 syslog (LOG_INFO, "performance: %s#%s#%s#%s#%s#%s#%d seconds <%08d>",
604 elapsed(timestore[0], secondtime), elapsed(timestore[0],
605 timestore[1]), elapsed(timestore[1], timestore[2]),
606 elapsed(timestore[2], timestore[3]),
607 elapsed(timestore[3], items_displayed ? timestore[4] : secondtime),
608 items_displayed ? elapsed(timestore[4], secondtime) : "",
609 items_displayed, globP->svc_cnt);
612 /* end of function: exit_tweb */