csearch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * csearch.c -- CGI program to generate complex search form -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. static void get_request(char *fname);
  44. static void emit_file(char* filename, 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. int reqmethod;
  53. char *qs = NULL;
  54. char *fname = NULL;
  55. /* Parse out the file=blah.html from the query string*/
  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. if ( !strncasecmp( p, "file=", 5 )) {
  76. fname = dsgw_ch_strdup( p + 5 );
  77. dsgw_form_unescape( fname );
  78. continue;
  79. }
  80. }
  81. free( qs ); qs = NULL;
  82. }
  83. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_POST | DSGW_METHOD_GET );
  84. dsgw_send_header();
  85. #ifdef DSGW_DEBUG
  86. dsgw_logstringarray( "env", env );
  87. {
  88. char buf[ 1024 ];
  89. getcwd( buf, sizeof(buf));
  90. dsgw_log( "cwd: \"%s\"\n", buf );
  91. }
  92. #endif
  93. if ( reqmethod == DSGW_METHOD_POST || reqmethod == DSGW_METHOD_GET ) {
  94. get_request(fname);
  95. }
  96. exit( 0 );
  97. }
  98. static void
  99. get_request(char *fname)
  100. {
  101. auto char* filename = NULL;
  102. struct ldap_searchobj* sop = NULL;
  103. if ( fname == NULL || *fname == '\0' ) {
  104. filename = "csearch.html";
  105. dsgw_init_searchprefs( &sop );
  106. } else if ( !strcmp( fname, "type" )) {
  107. filename = "csearchType.html";
  108. } else if ( !strcmp( fname, "attr" )) {
  109. filename = "csearchAttr.html";
  110. } else if ( !strcmp( fname, "match" )) {
  111. filename = "csearchMatch.html";
  112. } else if ( !strcmp( fname, "string" )) {
  113. filename = "csearchString.html";
  114. } else if ( !strcmp( fname, "base" )) {
  115. filename = "csearchBase.html";
  116. }
  117. if (filename) {
  118. emit_file (filename, sop);
  119. }
  120. fflush(stdout);
  121. }
  122. static void
  123. dsgw_emit_options (struct ldap_searchobj** sop, char* searchType, char* searchAttr)
  124. /* Emit HTML <OPTION> tags corresponding to search preferences.
  125. If searchType==NULL, emit searchType options; otherwise
  126. if searchAttr==NULL, emit searchAttr options for the given searchType;
  127. otherwise emit searchMatch options for the given searchType and searchAttr.
  128. */
  129. {
  130. auto struct ldap_searchobj *so;
  131. if (!*sop) dsgw_init_searchprefs (sop);
  132. for (so = ldap_first_searchobj(*sop); so != NULLSEARCHOBJ;
  133. so = ldap_next_searchobj (*sop, so)) {
  134. if (LDAP_IS_SEARCHOBJ_OPTION_SET (so, LDAP_SEARCHOBJ_OPT_INTERNAL)) {
  135. continue; /* Skip any marked "internal-only" */
  136. }
  137. if (!searchType) { /* emit searchType option */
  138. dsgw_emitf ("<OPTION VALUE=\"%s\">%s</OPTION>\n",
  139. so->so_objtypeprompt,
  140. dsgw_get_translation( so->so_objtypeprompt ));
  141. } else if (!*searchType || !strcmp (searchType, so->so_objtypeprompt)) {
  142. auto struct ldap_searchattr *sa;
  143. for (sa = so->so_salist; sa != NULL;
  144. sa = sa->sa_next) {
  145. if (!searchAttr) { /* emit searchAttr option */
  146. dsgw_emitf ("<OPTION VALUE=\"%1$s\">%1$s</OPTION>\n",
  147. sa->sa_attrlabel);
  148. } else if (!*searchAttr || !strcmp (searchAttr, sa->sa_attrlabel)) {
  149. auto int mi;
  150. auto struct ldap_searchmatch *sm;
  151. for (mi=0, sm = so->so_smlist; sm != NULL;
  152. ++mi, sm = sm->sm_next) { /* emit searchMatch option */
  153. if (sa->sa_matchtypebitmap & (1L << mi)) {
  154. dsgw_emitf ("<OPTION VALUE=\"%1$s\">%1$s</OPTION>\n",
  155. sm->sm_matchprompt);
  156. }
  157. }
  158. break;
  159. }
  160. }
  161. break;
  162. }
  163. }
  164. }
  165. static void
  166. emit_file (char* filename, struct ldap_searchobj* sop)
  167. {
  168. auto FILE* html = dsgw_open_html_file( filename, DSGW_ERROPT_EXIT );
  169. auto char line[ BIG_LINE ];
  170. auto int argc;
  171. auto char **argv;
  172. while ( dsgw_next_html_line( html, line )) {
  173. if ( dsgw_parse_line( line, &argc, &argv, 0, dsgw_simple_cond_is_true, NULL )) {
  174. if ( dsgw_directive_is( line, "HEAD" )) {
  175. dsgw_head_begin();
  176. dsgw_emits ("\n");
  177. } else if ( dsgw_directive_is( line, "DS_CSEARCH_SCRIPT" )) {
  178. dsgw_emits("<SCRIPT LANGUAGE=\"JavaScript\">\n"
  179. "<!-- Hide from non-JavaScript-capable browsers\n"
  180. "var searchType = '';\n"
  181. "var searchAttr = '';\n"
  182. "var searchMatch = '';\n" );
  183. dsgw_emits ("\n"
  184. "function searchTypeSet(val)\n"
  185. "{\n"
  186. " searchType = val + '';\n"
  187. "}\n"
  188. "\n"
  189. "function searchAttrSet(val)\n"
  190. "{\n"
  191. " searchAttr = val + '';\n"
  192. "}\n"
  193. "\n"
  194. "function searchMatchSet(val)\n"
  195. "{\n"
  196. " searchMatch = val + '';\n"
  197. "}\n"
  198. "\n"
  199. "function setHiddenFields(sform)\n"
  200. "{\n"
  201. /*
  202. * On Navigator 2.x, the form variable's value seems to get set
  203. * *after* the onSumbit handler executes, which is unfortunate.
  204. */
  205. " if (sform.searchstring.value == '') {\n");
  206. dsgw_emit_alert ("searchStringFrame", NULL,
  207. XP_GetClientStr (DBT_youDidNotSupplyASearchString_));
  208. dsgw_emits (" return false;\n"
  209. " }\n"
  210. " sform.type.value = searchType;\n"
  211. " sform.attr.value = searchAttr;\n"
  212. " sform.match.value = searchMatch;\n"
  213. " sform.searchstring.select();\n"
  214. " sform.searchstring.focus();\n"
  215. " return true;\n"
  216. "}\n"
  217. "\n"
  218. "function init()\n"
  219. "{}\n"
  220. "// End hiding -->\n"
  221. "</SCRIPT>\n" );
  222. } else if ( dsgw_directive_is( line, "EVALUATE" )) {
  223. auto int i;
  224. for (i = 0; i < argc; ++i) {
  225. if (!strcmp (argv[i], "parent.searchBase")) {
  226. dsgw_emits (gc->gc_ldapsearchbase);
  227. } else if (!strcmp (argv[i], "parent.UFNsearchBase")) {
  228. #ifdef NOTFORNOW
  229. /* ldap_dn2ufn currently gobbles up 'dc' so don't use */
  230. /* it for now */
  231. auto char* ufn = ldap_dn2ufn (gc->gc_ldapsearchbase);
  232. dsgw_emits (ufn);
  233. free( ufn );
  234. #else
  235. dsgw_emits (gc->gc_ldapsearchbase);
  236. #endif
  237. }
  238. }
  239. } else if ( dsgw_directive_is( line, "DS_CSEARCH_TYPE_BODY" )) {
  240. dsgw_emitf ("<BODY %s %s>\n", dsgw_html_body_colors,
  241. "onLoad=\"parent.searchTypeSet(document.searchTypeForm.searchType.options"
  242. "[document.searchTypeForm.searchType.selectedIndex].value);\"");
  243. } else if ( dsgw_directive_is( line, "DS_CSEARCH_ATTR_BODY" )) {
  244. dsgw_emitf ("<BODY %s %s>\n", dsgw_html_body_colors,
  245. "onLoad=\"parent.searchAttrSet(document.searchAttrForm.searchAttr.options"
  246. "[document.searchAttrForm.searchAttr.selectedIndex].value);\"");
  247. } else if ( dsgw_directive_is( line, "DS_CSEARCH_MATCH_BODY" )) {
  248. dsgw_emitf ("<BODY %s %s>\n", dsgw_html_body_colors,
  249. "onLoad=\"parent.searchMatchSet(document.searchMatchForm.searchMatch.options"
  250. "[document.searchMatchForm.searchMatch.selectedIndex].value);\"");
  251. } else if ( dsgw_directive_is( line, "DS_CSEARCH_STRING_BODY" )) {
  252. dsgw_emitf ("<BODY %s %s>\n", dsgw_html_body_colors,
  253. "onLoad=\"document.searchStringForm.searchstring.select();"
  254. "document.searchStringForm.searchstring.focus();\"");
  255. dsgw_emit_alertForm();
  256. } else if ( dsgw_directive_is( line, "DS_CSEARCH_BASE_BODY" )) {
  257. dsgw_emitf ("<BODY %s>\n", dsgw_html_body_colors);
  258. } else if ( dsgw_directive_is( line, "DS_CSEARCH_TYPE_FORM" )) {
  259. dsgw_form_begin ("searchTypeForm",
  260. "action=\"%s?file=attr\" target=searchAttrFrame",
  261. dsgw_getvp( DSGW_CGINUM_CSEARCH));
  262. dsgw_emits("\n");
  263. } else if ( dsgw_directive_is( line, "DS_CSEARCH_ATTR_FORM" )) {
  264. dsgw_form_begin ("searchAttrForm",
  265. "action=\"%s?file=match\" target=searchMatchFrame",
  266. dsgw_getvp( DSGW_CGINUM_CSEARCH));
  267. dsgw_emits("\n");
  268. {
  269. auto char* searchType = dsgw_get_cgi_var ("searchType", DSGW_CGIVAR_OPTIONAL);
  270. if (searchType && *searchType) {
  271. dsgw_emitf ("<INPUT TYPE=hidden NAME=searchType VALUE=\"%s\">\n",
  272. searchType);
  273. }
  274. }
  275. } else if ( dsgw_directive_is( line, "DS_CSEARCH_MATCH_FORM" )) {
  276. dsgw_form_begin ("searchMatchForm", NULL);
  277. dsgw_emits("\n");
  278. } else if ( dsgw_directive_is( line, "DS_CSEARCH_STRING_FORM" )) {
  279. dsgw_form_begin ("searchStringForm", "action=\"%s\" %s %s",
  280. dsgw_getvp( DSGW_CGINUM_DOSEARCH ),
  281. "onSubmit=\"return parent.setHiddenFields(this)\"",
  282. argc > 0 ? argv[0] : "");
  283. dsgw_emitf ("\n"
  284. "<INPUT TYPE=hidden NAME=mode VALUE=\"complex\">\n"
  285. "<INPUT TYPE=hidden NAME=base VALUE=\"%s\">\n"
  286. "<INPUT TYPE=hidden NAME=ldapserver VALUE=\"%s\">\n"
  287. "<INPUT TYPE=hidden NAME=ldapport VALUE=\"%d\">\n"
  288. "<INPUT TYPE=hidden NAME=type>\n"
  289. "<INPUT TYPE=hidden NAME=attr>\n"
  290. "<INPUT TYPE=hidden NAME=match>\n"
  291. "<INPUT TYPE=hidden NAME=context VALUE=\"%s\">\n",
  292. gc->gc_ldapsearchbase, gc->gc_ldapserver, gc->gc_ldapport, context);
  293. } else if ( dsgw_directive_is( line, "DS_CSEARCH_TYPE_SELECT" )) {
  294. dsgw_emitf ("<SELECT NAME=searchType "
  295. "onChange=\"parent.searchTypeSet(this.options[this.selectedIndex].value);"
  296. "this.form.submit();"
  297. "parent.searchMatchFrame.location='%s?context=%s&file=match&'+this.name+'='"
  298. "+escape(this.options[this.selectedIndex].value);\">\n",
  299. dsgw_getvp( DSGW_CGINUM_CSEARCH), context);
  300. dsgw_emit_options (&sop, NULL, NULL);
  301. dsgw_emits ("</SELECT>\n");
  302. } else if ( dsgw_directive_is( line, "DS_CSEARCH_ATTR_SELECT" )) {
  303. dsgw_emits ("<SELECT NAME=searchAttr"
  304. " onChange=\"parent.searchAttrSet(this.options[this.selectedIndex].value);"
  305. "this.form.submit();\">\n");
  306. {
  307. auto char* searchType = dsgw_get_cgi_var ("searchType", DSGW_CGIVAR_OPTIONAL);
  308. dsgw_emit_options (&sop, searchType ? searchType : "", NULL);
  309. }
  310. dsgw_emits ("</SELECT>\n");
  311. } else if ( dsgw_directive_is( line, "DS_CSEARCH_MATCH_SELECT" )) {
  312. dsgw_emits ("<SELECT NAME=searchMatch"
  313. " onChange=\"parent.searchMatchSet(this.options[this.selectedIndex].value);\">\n");
  314. {
  315. auto char* searchType = dsgw_get_cgi_var ("searchType", DSGW_CGIVAR_OPTIONAL);
  316. auto char* searchAttr = dsgw_get_cgi_var ("searchAttr", DSGW_CGIVAR_OPTIONAL);
  317. dsgw_emit_options (&sop, searchType ? searchType : "", searchAttr ? searchAttr : "");
  318. }
  319. dsgw_emits ("</SELECT>\n");
  320. } else if ( dsgw_directive_is( line, "DS_HELP_BUTTON" ) && argc > 0) {
  321. dsgw_emit_helpbutton (argv[0]);
  322. } else {
  323. dsgw_emits (line);
  324. }
  325. dsgw_argv_free( argv );
  326. }
  327. }
  328. fclose (html);
  329. }