templateindex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. --- END COPYRIGHT BLOCK --- */
  38. /*
  39. * templateindex.c -- CGI template indexer -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #if defined( XP_WIN32 )
  43. #include <io.h>
  44. struct dirent {
  45. char d_name[1];
  46. };
  47. #else
  48. #include <dirent.h>
  49. #endif
  50. static void build_index();
  51. #if defined( XP_WIN32 )
  52. char **ds_get_file_list( char *dir )
  53. {
  54. char szWildcardFileSpec[MAX_PATH];
  55. char **ret = NULL;
  56. long hFile;
  57. struct _finddata_t fileinfo;
  58. int nfiles = 0;
  59. if( ( dir == NULL ) || (strlen( dir ) == 0) )
  60. return NULL;
  61. if( ( ret = malloc( sizeof( char * ) ) ) == NULL )
  62. return NULL;
  63. strcpy(szWildcardFileSpec, dir);
  64. strcat(szWildcardFileSpec, "/*");
  65. hFile = _findfirst( szWildcardFileSpec, &fileinfo);
  66. if( hFile == -1 )
  67. return NULL;
  68. if( ( strcmp( fileinfo.name, "." ) != 0 ) &&
  69. ( strcmp( fileinfo.name, ".." ) != 0 ) )
  70. {
  71. ret[ nfiles++ ] = strdup( fileinfo.name );
  72. }
  73. while( _findnext( hFile, &fileinfo ) == 0 )
  74. {
  75. if( ( strcmp( fileinfo.name, "." ) != 0 ) &&
  76. ( strcmp( fileinfo.name, ".." ) != 0 ) )
  77. {
  78. if( ( ret = (char **) realloc( ret, sizeof( char * ) * ( nfiles + 1 ) ) ) != NULL )
  79. ret[ nfiles++ ] = strdup( fileinfo.name);
  80. }
  81. }
  82. _findclose( hFile );
  83. ret[ nfiles ] = NULL;
  84. return ret;
  85. }
  86. #endif ( XP_WIN32 )
  87. main( argc, argv, env )
  88. int argc;
  89. char *argv[];
  90. #ifdef DSGW_DEBUG
  91. char *env[];
  92. #endif
  93. {
  94. int reqmethod;
  95. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_GET );
  96. dsgw_send_header();
  97. #ifdef DSGW_DEBUG
  98. dsgw_logstringarray( "env", env );
  99. #endif
  100. dsgw_html_begin( "Directory Server Gateway Template Indexer", 1 );
  101. build_index();
  102. dsgw_html_end();
  103. exit( 0 );
  104. }
  105. static void
  106. build_index()
  107. {
  108. FILE *htmlfp;
  109. #if !defined( XP_WIN32 )
  110. DIR *dirp;
  111. #endif
  112. struct dirent *dep;
  113. char *path, **argv, *classes, *p, line[ BIG_LINE ];
  114. char **filelist;
  115. int errcount, prefixlen, count, argc, filecount = 0;
  116. path = dsgw_file2path( gc->gc_tmpldir, "" );
  117. #if defined( XP_WIN32 )
  118. if (( filelist = ds_get_file_list( path )) == NULL ) {
  119. #else
  120. if (( dirp = opendir( path )) == NULL ) {
  121. #endif
  122. dsgw_error( DSGW_ERR_OPENDIR, path, DSGW_ERROPT_EXIT, 0, NULL );
  123. }
  124. free( path );
  125. prefixlen = strlen( DSGW_CONFIG_DISPLAYPREFIX );
  126. errcount = count = 0;
  127. dsgw_emitf( "Remove any lines that begin with \"template\" from \n" );
  128. dsgw_emitf( "your dsgw.conf file and add these lines:<BR><PRE>\n" );
  129. #if defined( XP_WIN32 )
  130. while( filelist != NULL && filelist[filecount] != NULL ) {
  131. dep = (struct dirent *)filelist[filecount];
  132. #else
  133. while (( dep = readdir( dirp )) != NULL ) {
  134. #endif
  135. if ( strlen( dep->d_name ) > prefixlen && strncasecmp( dep->d_name,
  136. DSGW_CONFIG_DISPLAYPREFIX, prefixlen ) == 0 && strcmp(
  137. ".html", dep->d_name + strlen( dep->d_name ) - 5 ) == 0 ) {
  138. ++count;
  139. htmlfp = dsgw_open_html_file( dep->d_name, DSGW_ERROPT_EXIT );
  140. while ( dsgw_next_html_line( htmlfp, line )) {
  141. if ( dsgw_parse_line( line, &argc, &argv, 1,
  142. dsgw_simple_cond_is_true, NULL )) {
  143. if ( dsgw_directive_is( line, DRCT_DS_OBJECTCLASS )) {
  144. if (( classes = get_arg_by_name( "value", argc, argv ))
  145. == NULL ) {
  146. dsgw_emitf(
  147. "Missing \"value=objectclass\" on line &lt%s<BR>\n", line+1 );
  148. ++errcount;
  149. continue;
  150. }
  151. dsgw_emitf( "template %.*s",
  152. strlen( dep->d_name ) - prefixlen - 5,
  153. dep->d_name + prefixlen );
  154. for ( ; classes != NULL && *classes != '\0';
  155. classes = p ) {
  156. if (( p = strchr( classes, ',' )) != NULL ) {
  157. *p++ = '\0';
  158. while ( ldap_utf8isspace( p )) {
  159. LDAP_UTF8INC(p);
  160. }
  161. }
  162. dsgw_emitf( " %s", classes );
  163. }
  164. dsgw_emits( "\n" );
  165. }
  166. }
  167. }
  168. fclose( htmlfp );
  169. filecount++;
  170. }
  171. }
  172. #if !defined( XP_WIN32 )
  173. closedir( dirp );
  174. #endif
  175. dsgw_emits( "</PRE><H3>Template indexing " );
  176. if ( errcount == 0 ) {
  177. dsgw_emitf( "complete (%d files).<H3>\n", count );
  178. } else {
  179. dsgw_emitf( "failed (%d errors).<H3>\n", errcount );
  180. }
  181. }