search.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * search.c -- CGI program to generate smart search form -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. static void get_request(char *docname);
  44. static void do_searchtype_popup( struct ldap_searchobj *sop );
  45. int main( argc, argv, env )
  46. int argc;
  47. char *argv[];
  48. #ifdef DSGW_DEBUG
  49. char *env[];
  50. #endif
  51. {
  52. auto int reqmethod;
  53. char *docname = NULL;
  54. char *qs = NULL;
  55. /* Parse out the file=blah.html */
  56. if (( qs = getenv( "QUERY_STRING" )) != NULL && *qs != '\0' ) {
  57. /* parse the query string: */
  58. auto char *p, *iter = NULL;
  59. qs = dsgw_ch_strdup( qs );
  60. for ( p = ldap_utf8strtok_r( qs, "&", &iter ); p != NULL;
  61. p = ldap_utf8strtok_r( NULL, "&", &iter )) {
  62. /*
  63. * Get the conf file name. It'll be translated
  64. * into /dsgw/context/CONTEXT.conf if
  65. * CONTEXT is all alphanumeric (no slahes,
  66. * or dots). CONTEXT is passed into the cgi.
  67. * if context=CONTEXT is not there, or PATH_INFO
  68. * was used, then use dsgw.conf
  69. */
  70. if ( !strncasecmp( p, "context=", 8 )) {
  71. context = dsgw_ch_strdup( p + 8 );
  72. dsgw_form_unescape( context );
  73. continue;
  74. }
  75. /*Get the filename and check it for naughtiness -RJP*/
  76. if ( !strncasecmp( p, "file=", 5 )) {
  77. docname = dsgw_ch_strdup( p + 5 );
  78. dsgw_form_unescape( docname );
  79. /*
  80. * Make sure the person isn't trying to get
  81. * some file not in the gateway.
  82. */
  83. if (! dsgw_valid_docname(docname)) {
  84. dsgw_error( DSGW_ERR_BADFILEPATH, docname,
  85. DSGW_ERROPT_EXIT, 0, NULL );
  86. }
  87. continue;
  88. }
  89. }
  90. free( qs ); qs = NULL;
  91. }
  92. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_GET );
  93. dsgw_send_header();
  94. #ifdef DSGW_DEBUG
  95. dsgw_logstringarray( "env", env );
  96. {
  97. char buf[ 1024 ];
  98. getcwd( buf, sizeof(buf));
  99. dsgw_log( "cwd: \"%s\"\n", buf );
  100. }
  101. #endif
  102. if ( reqmethod == DSGW_METHOD_GET ) {
  103. get_request(docname);
  104. }
  105. exit( 0 );
  106. }
  107. static void
  108. get_request(char *docname)
  109. {
  110. auto char* filename = NULL;
  111. auto struct ldap_searchobj* sop = NULL;
  112. if (docname != NULL && *docname == '/') {
  113. docname++;
  114. }
  115. if ( docname == NULL || *docname == '\0' ) {
  116. filename = "search.html";
  117. } else if ( !strcmp( docname, "string" )) {
  118. filename = "searchString.html";
  119. dsgw_init_searchprefs( &sop );
  120. }
  121. if (filename) {
  122. auto FILE* html = dsgw_open_html_file( filename, DSGW_ERROPT_EXIT );
  123. auto char line[ BIG_LINE ];
  124. auto int argc;
  125. auto char **argv;
  126. while ( dsgw_next_html_line( html, line )) {
  127. if ( dsgw_parse_line( line, &argc, &argv, 0, dsgw_simple_cond_is_true, NULL )) {
  128. if ( dsgw_directive_is( line, "HEAD" )) {
  129. dsgw_head_begin();
  130. dsgw_emits ("\n");
  131. } else if ( dsgw_directive_is( line, "DS_SEARCH_SCRIPT" )) {
  132. dsgw_emits ("<SCRIPT LANGUAGE=\"JavaScript\">\n"
  133. "<!-- Hide from non-JavaScript-capable browsers\n"
  134. "\n"
  135. "function validate(sform)\n"
  136. "{\n"
  137. " if (sform.searchstring.value == '') {\n");
  138. /*
  139. * It would have been nice to detect when the user pressed return without
  140. * typing anything into the searchstring area, but on Navigator 2.x, the
  141. * form variable's value seems to get set *after* the onSubmit handler
  142. * executes, which is unfortunate.
  143. */
  144. dsgw_emit_alert ("searchFrame", NULL, /* "%s<br>(search base %s)", */
  145. XP_GetClientStr (DBT_youDidNotSupplyASearchString_),
  146. gc->gc_ldapsearchbase);
  147. dsgw_emits (" return false;\n"
  148. " }\n"
  149. " sform.searchstring.select();\n"
  150. " sform.searchstring.focus();\n"
  151. " return true;\n"
  152. "}\n"
  153. "\n"
  154. "function init()\n"
  155. "{}\n"
  156. "// End hiding -->\n"
  157. "</SCRIPT>\n");
  158. } else if ( dsgw_directive_is( line, "DS_SEARCH_BODY" )) {
  159. dsgw_emitf ("<BODY onLoad=\""
  160. "document.searchForm.searchstring.select();"
  161. "document.searchForm.searchstring.focus();\" %s>\n",
  162. dsgw_html_body_colors );
  163. dsgw_emit_alertForm();
  164. } else if ( dsgw_directive_is( line, "DS_SEARCH_FORM" )) {
  165. dsgw_form_begin ("searchForm", "action=\"%s\" %s %s",
  166. dsgw_getvp( DSGW_CGINUM_DOSEARCH ),
  167. "onSubmit=\"return top.validate(this)\"",
  168. argc > 0 ? argv[0] : "");
  169. dsgw_emitf ("\n"
  170. "<INPUT TYPE=hidden NAME=\"mode\" VALUE=\"smart\">\n"
  171. "<INPUT TYPE=hidden NAME=\"base\" VALUE=\"%s\">\n"
  172. "<INPUT TYPE=hidden NAME=\"ldapserver\" VALUE=\"%s\">\n"
  173. "<INPUT TYPE=hidden NAME=\"ldapport\" VALUE=\"%d\">\n",
  174. gc->gc_ldapsearchbase, gc->gc_ldapserver, gc->gc_ldapport );
  175. } else if ( dsgw_directive_is( line, "DS_SEARCH_BASE" )) {
  176. #ifdef NOTFORNOW
  177. /* ldap_dn2ufn currently gobbles up 'dc' so don't use */
  178. /* it for now */
  179. auto char* ufn = ldap_dn2ufn( gc->gc_ldapsearchbase );
  180. dsgw_emits( ufn );
  181. free( ufn );
  182. #else
  183. dsgw_emits( gc->gc_ldapsearchbase );
  184. #endif
  185. } else if ( dsgw_directive_is( line, "DS_SEARCH_TYPE" )) {
  186. do_searchtype_popup( sop );
  187. } else if ( dsgw_directive_is( line, "DS_HELP_BUTTON" )) {
  188. dsgw_emit_helpbutton (argc > 0 ? argv[0] : "");
  189. } else {
  190. dsgw_emits (line);
  191. }
  192. dsgw_argv_free( argv );
  193. }
  194. }
  195. fclose (html);
  196. }
  197. }
  198. static void
  199. do_searchtype_popup(
  200. struct ldap_searchobj *sop
  201. )
  202. {
  203. int first = 1;
  204. struct ldap_searchobj *so;
  205. dsgw_emits( "<SELECT NAME=\"type\">\n" );
  206. for ( so = ldap_first_searchobj( sop ); so != NULL;
  207. so = ldap_next_searchobj( sop, so ), first = 0) {
  208. /* Skip any marked "internal-only" */
  209. if ( LDAP_IS_SEARCHOBJ_OPTION_SET( so, LDAP_SEARCHOBJ_OPT_INTERNAL )) {
  210. continue;
  211. }
  212. dsgw_emitf( "<OPTION%s value=\"%s\">%s</OPTION>\n",
  213. first ? " selected" : "",
  214. so->so_objtypeprompt,
  215. dsgw_get_translation( so->so_objtypeprompt ));
  216. }
  217. dsgw_emits( "</SELECT>\n" );
  218. }