dosearch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * dosearch.c -- CGI search handler -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. static void get_request(char* hostport, char *dn, char *ldapquery);
  43. static void post_request();
  44. int main( argc, argv, env )
  45. int argc;
  46. char *argv[];
  47. #ifdef DSGW_DEBUG
  48. char *env[];
  49. #endif
  50. {
  51. int reqmethod;
  52. char *qs = NULL;
  53. char *dn = NULL;
  54. char *hostport = NULL;
  55. char *ldapquery = NULL;
  56. #ifndef __LP64__
  57. #ifdef HPUX
  58. #ifndef __ia64
  59. /* call the static constructors in libnls */
  60. _main();
  61. #endif
  62. #endif
  63. #endif
  64. /*
  65. * Parse out the GET args, if any. See the comments under
  66. * get_request for an explanation of what's going on here
  67. */
  68. if (( qs = getenv( "QUERY_STRING" )) != NULL && *qs != '\0' ) {
  69. /* parse the query string: */
  70. auto char *p, *iter = NULL;
  71. qs = dsgw_ch_strdup( qs );
  72. for ( p = ldap_utf8strtok_r( qs, "&", &iter ); p != NULL;
  73. p = ldap_utf8strtok_r( NULL, "&", &iter )) {
  74. /*
  75. * Get the conf file name. It'll be translated
  76. * into /dsgw/context/CONTEXT.conf if
  77. * CONTEXT is all alphanumeric (no slahes,
  78. * or dots). CONTEXT is passed into the cgi.
  79. * if context=CONTEXT is not there, or PATH_INFO
  80. * was used, then use dsgw.conf
  81. */
  82. if ( !strncasecmp( p, "context=", 8 )) {
  83. context = dsgw_ch_strdup( p + 8 );
  84. dsgw_form_unescape( context );
  85. continue;
  86. }
  87. if ( !strncasecmp( p, "hp=", 3 )) {
  88. hostport = dsgw_ch_strdup( p + 3 );
  89. dsgw_form_unescape( hostport );
  90. continue;
  91. }
  92. if ( !strncasecmp( p, "ldq=", 4 )) {
  93. ldapquery = dsgw_ch_strdup( p + 4 );
  94. dsgw_form_unescape( ldapquery );
  95. continue;
  96. }
  97. if ( !strncasecmp( p, "dn=", 3 )) {
  98. dn = dsgw_ch_strdup( p + 3 );
  99. dsgw_form_unescape( dn );
  100. continue;
  101. }
  102. /*
  103. * If it doesn't match any of the above, then
  104. * tack it onto the end of ldapquery.
  105. */
  106. if (ldapquery != NULL) {
  107. ldapquery = dsgw_ch_realloc(ldapquery, sizeof(char *) * (strlen(ldapquery) + strlen(p) + 2));
  108. sprintf( ldapquery, "%s&%s", ldapquery, p );
  109. }
  110. }
  111. free( qs ); qs = NULL;
  112. }
  113. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_POST | DSGW_METHOD_GET );
  114. /*
  115. * Note: we don't call dsgw_send_header() here like we usually do because
  116. * on a GET we may be asked to return a MIME type other than the default
  117. * of text/html. For GET requests, we send the headers inside
  118. * ldaputil.c:dsgw_ldapurl_search(). For POST requests, we send them
  119. * below in post_request().
  120. */
  121. #ifdef DSGW_DEBUG
  122. dsgw_logstringarray( "env", env );
  123. #endif
  124. if ( reqmethod == DSGW_METHOD_GET ) {
  125. get_request(hostport, dn, ldapquery);
  126. } else {
  127. post_request();
  128. }
  129. exit( 0 );
  130. }
  131. static void
  132. get_request(char* hostport, char *dn, char *ldapquery)
  133. {
  134. int urllen = 0;
  135. int argslen = 0;
  136. char *p = NULL;
  137. char *ldapurl = NULL;
  138. /*
  139. * The following comment is kept here only as a reminder of the past.
  140. * It is no longer relevant. See the next comment. - RJP
  141. *
  142. * On a GET request, we do an LDAP URL search (which will just display
  143. * a single entry if all that is included is "host:port/DN").
  144. * The HTTP URL should be:
  145. * .../dosearch[/host[:port]][?[dn=baseDN&][LDAPquery]]
  146. * This will be converted to the LDAP URL:
  147. * ldap://[host[:port]]/[baseDN][?LDAPquery]
  148. *
  149. * For compatibility with prior versions, the HTTP URL may be:
  150. * .../dosearch/host[:port]/[baseDN][?LDAPquery]
  151. * In this case, the host:port is required, since PATH_INFO can't
  152. * start with a '/' (web server sees that as a different program).
  153. * This older HTTP URL format is deprecated, because PATH_INFO is
  154. * not 8-bit clean on Japanese Windows NT.
  155. */
  156. /*
  157. * The only form supported now is:
  158. * .../dosearch?context=BLAH[&hp=host[:port]][&dn=baseDN][&ldq=LDAPquery]]
  159. * -RJP
  160. */
  161. argslen = 0;
  162. /* get the length of all the args (dn, hostport, ldapquery)*/
  163. if (hostport != NULL) {
  164. argslen += strlen(hostport);
  165. }
  166. if (dn != NULL) {
  167. argslen += strlen(dn);
  168. }
  169. if (ldapquery != NULL) {
  170. argslen += strlen(ldapquery);
  171. }
  172. /* If nothing was supplied, exit*/
  173. if ( argslen == 0 ) {
  174. dsgw_error( DSGW_ERR_MISSINGINPUT, NULL, DSGW_ERROPT_EXIT, 0, NULL );
  175. }
  176. /* Malloc the ldapurl*/
  177. urllen = LDAP_URL_PREFIX_LEN + argslen + 3;
  178. p = ldapurl = (char *)dsgw_ch_malloc( urllen );
  179. /*Slap on ldap:// */
  180. strcpy( p, LDAP_URL_PREFIX );
  181. p += LDAP_URL_PREFIX_LEN;
  182. /*Slap on host:port if there is one*/
  183. if ( hostport != NULL ) {
  184. strcpy( p, hostport );
  185. }
  186. strcat( ldapurl, "/" );
  187. /*Slap on /dn, if there is a dn */
  188. if ( dn != NULL ) {
  189. strcat( ldapurl, dn );
  190. }
  191. /*Slap on ?ldapquery */
  192. if ( ldapquery != NULL ) {
  193. sprintf( ldapurl + strlen( ldapurl ), "?%s", ldapquery );
  194. }
  195. #ifdef DSGW_DEBUG
  196. dsgw_log( "get_request: processing LDAP URL \"%s\"\n", ldapurl );
  197. #endif
  198. dsgw_ldapurl_search( NULL, ldapurl);
  199. }
  200. static void
  201. post_request()
  202. {
  203. char *modestr, *searchstring, *type, *base;
  204. LDAP *ld;
  205. LDAPFiltDesc *lfdp;
  206. struct ldap_searchobj *solistp, *sop;
  207. int authmode, mode, options;
  208. dsgw_send_header();
  209. options = 0;
  210. modestr = dsgw_get_cgi_var( "mode", DSGW_CGIVAR_REQUIRED );
  211. searchstring = dsgw_get_cgi_var( "searchstring", DSGW_CGIVAR_OPTIONAL );
  212. dsgw_remove_leading_and_trailing_spaces( &searchstring );
  213. #ifdef DSGW_DEBUG
  214. if (searchstring) {
  215. dsgw_log ("searchstring=\"%s\"\n", searchstring);
  216. } else {
  217. dsgw_log ("searchstring=NULL");
  218. }
  219. #endif
  220. authmode = 0;
  221. if ( strcasecmp( modestr, DSGW_SRCHMODE_AUTH ) == 0 ) {
  222. /*
  223. * treat authenticate as a variant of the smart search mode
  224. */
  225. authmode = 1;
  226. mode = DSGW_SRCHMODE_SMART_ID;
  227. options |= DSGW_DISPLAY_OPT_AUTH;
  228. } else if ( strcasecmp( modestr, DSGW_SRCHMODE_SMART ) == 0 ) {
  229. mode = DSGW_SRCHMODE_SMART_ID;
  230. } else if ( strcasecmp( modestr, DSGW_SRCHMODE_COMPLEX ) == 0 ) {
  231. mode = DSGW_SRCHMODE_COMPLEX_ID;
  232. } else if ( strcasecmp( modestr, DSGW_SRCHMODE_PATTERN ) == 0 ) {
  233. mode = DSGW_SRCHMODE_PATTERN_ID;
  234. } else {
  235. dsgw_error( DSGW_ERR_SEARCHMODE, modestr, 0, 0, NULL );
  236. }
  237. if ( mode != DSGW_SRCHMODE_PATTERN_ID
  238. && ( searchstring == NULL || *searchstring == '\0' )) {
  239. dsgw_error( DSGW_ERR_NOSEARCHSTRING, NULL, DSGW_ERROPT_EXIT, 0, NULL );
  240. }
  241. if (( type = dsgw_get_cgi_var( "type", authmode ? DSGW_CGIVAR_OPTIONAL :
  242. DSGW_CGIVAR_REQUIRED )) == NULL ) {
  243. type = DSGW_SRCHTYPE_AUTH;
  244. }
  245. if (( base = dsgw_get_cgi_var( "base", DSGW_CGIVAR_OPTIONAL )) == NULL ) {
  246. base = gc->gc_ldapsearchbase;
  247. }
  248. /* check for options (carried in boolean CGI variables) */
  249. if ( dsgw_get_boolean_var( "listifone", DSGW_CGIVAR_OPTIONAL, 0 )) {
  250. options |= DSGW_DISPLAY_OPT_LIST_IF_ONE;
  251. }
  252. if ( dsgw_get_boolean_var( "editable", DSGW_CGIVAR_OPTIONAL, 0 )) {
  253. options |= DSGW_DISPLAY_OPT_EDITABLE;
  254. }
  255. if ( dsgw_get_boolean_var( "link2edit", DSGW_CGIVAR_OPTIONAL, 0 )) {
  256. options |= DSGW_DISPLAY_OPT_LINK2EDIT;
  257. }
  258. if ( dsgw_get_boolean_var( "dnlist_js", DSGW_CGIVAR_OPTIONAL, 0 )) {
  259. options |= DSGW_DISPLAY_OPT_DNLIST_JS;
  260. }
  261. (void) dsgw_init_ldap( &ld, &lfdp, ( authmode == 1 ) ? 1 : 0, 0);
  262. if ( mode != DSGW_SRCHMODE_PATTERN_ID ) {
  263. dsgw_init_searchprefs( &solistp );
  264. if (( sop = dsgw_type2searchobj( solistp, type )) == NULL ) {
  265. ldap_unbind( ld );
  266. dsgw_error( DSGW_ERR_UNKSRCHTYPE, type, DSGW_ERROPT_EXIT, 0, NULL );
  267. }
  268. }
  269. switch( mode ) {
  270. case DSGW_SRCHMODE_SMART_ID:
  271. /*
  272. * smart search mode -- try to do the right kind of search for the
  273. * client based on what the user entered in the search box
  274. */
  275. dsgw_smart_search( ld, sop, lfdp, base, searchstring, options );
  276. break;
  277. case DSGW_SRCHMODE_COMPLEX_ID: {
  278. /*
  279. * complex search mode -- construct a specific filter based on
  280. * user's form selections
  281. */
  282. int scope;
  283. char *attrlabel, *matchprompt;
  284. struct ldap_searchattr *sap;
  285. struct ldap_searchmatch *smp;
  286. attrlabel = dsgw_get_cgi_var( "attr", DSGW_CGIVAR_REQUIRED );
  287. if (( sap = dsgw_label2searchattr( sop, attrlabel )) == NULL ) {
  288. ldap_unbind( ld );
  289. dsgw_error( DSGW_ERR_UNKATTRLABEL, attrlabel, DSGW_ERROPT_EXIT,
  290. 0, NULL );
  291. }
  292. matchprompt = dsgw_get_cgi_var( "match", DSGW_CGIVAR_REQUIRED );
  293. if (( smp = dsgw_prompt2searchmatch( sop, matchprompt )) == NULL ) {
  294. ldap_unbind( ld );
  295. dsgw_error( DSGW_ERR_UNKMATCHPROMPT, matchprompt,
  296. DSGW_ERROPT_EXIT, 0, NULL );
  297. }
  298. scope = dsgw_get_int_var( "scope", DSGW_CGIVAR_OPTIONAL,
  299. sop->so_defaultscope );
  300. dsgw_pattern_search( ld, sop->so_objtypeprompt,
  301. sap->sa_attrlabel, smp->sm_matchprompt, searchstring,
  302. smp->sm_filter, sop->so_filterprefix, NULL, sap->sa_attr,
  303. base, scope, searchstring, options );
  304. }
  305. break;
  306. case DSGW_SRCHMODE_PATTERN_ID: {
  307. /*
  308. * pattern-based search mode (no searchprefs or filter file used)
  309. */
  310. char *attr, *pattern, *prefix, *suffix, *searchdesc;
  311. int scope;
  312. attr = dsgw_get_cgi_var( "attr", DSGW_CGIVAR_REQUIRED );
  313. pattern = dsgw_get_cgi_var( "filterpattern", DSGW_CGIVAR_REQUIRED );
  314. prefix = dsgw_get_cgi_var( "filterprefix", DSGW_CGIVAR_OPTIONAL );
  315. suffix = dsgw_get_cgi_var( "filtersuffix", DSGW_CGIVAR_OPTIONAL );
  316. scope = dsgw_get_int_var( "scope", DSGW_CGIVAR_OPTIONAL,
  317. LDAP_SCOPE_SUBTREE );
  318. options |= DSGW_DISPLAY_OPT_CUSTOM_SEARCHDESC;
  319. searchdesc = dsgw_get_cgi_var( "searchdesc", DSGW_CGIVAR_OPTIONAL );
  320. dsgw_pattern_search( ld, type, searchdesc, NULL, NULL,
  321. pattern, prefix, suffix, attr,
  322. base, scope, searchstring, options );
  323. }
  324. break;
  325. }
  326. ldap_unbind( ld );
  327. }