edit.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /** --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. --- END COPYRIGHT BLOCK --- */
  6. /*
  7. * edit.c -- CGI editable entry display -- HTTP gateway
  8. */
  9. #include "dsgw.h"
  10. #include "dbtdsgw.h"
  11. static void get_request(char *dn, char *tmplname,
  12. char *parent, unsigned long options);
  13. int main( argc, argv, env )
  14. int argc;
  15. char *argv[];
  16. #ifdef DSGW_DEBUG
  17. char *env[];
  18. #endif
  19. {
  20. char *dn, *tmplname, *p, *parent;
  21. unsigned long options;
  22. /*
  23. * If the QUERY_STRING is non-NULL, it looks like this:
  24. *
  25. * template [&CONTEXT=context] [ &INFO=infostring ] [ &ADD ] [ &DN=dn ] \
  26. * [&DNATTR=attrname&DNDESC=description]
  27. *
  28. * where:
  29. * "template" is the name of the edit template to use for display,
  30. * "dn" is escaped dn,
  31. * "infostring" is a message used to replace DS_LAST_OP_INFO directives
  32. * "attrname" is the name of a DN-valued attribute
  33. * "dndesc" is the destriptive name of the above DN-valued attribute
  34. *
  35. * If "&ADD" is present, we check to make sure the entry
  36. * does not exist, then we check that the parent entry exists, and then
  37. * we present an "add entry" form.
  38. *
  39. * Note: original form http://host/edit/dn[/...]?template[&...] is
  40. * supported for keeping backward compatibility.
  41. * But passing DN as PATH_INFO is NOT recommended.
  42. * Since PATH_INFO is passed to CGI as is (non-escaped),
  43. * the content has a risk to get broken especially when
  44. * it contains 8-bit UTF-8 data. (This is a known problem
  45. * on localized Windows machines.)
  46. */
  47. options = DSGW_DISPLAY_OPT_EDITABLE;
  48. dn = NULL;
  49. #ifndef __LP64__
  50. #ifdef HPUX
  51. /* call the static constructors in libnls */
  52. _main();
  53. #endif
  54. #endif
  55. if (( tmplname = getenv( "QUERY_STRING" )) != NULL && *tmplname != '\0' ) {
  56. tmplname = dsgw_ch_strdup( tmplname );
  57. while ( tmplname != NULL && ((( p = strrchr( tmplname, '&' )) != NULL ) || (p=tmplname) != NULL )) {
  58. if (p == tmplname) {
  59. tmplname = NULL;
  60. } else {
  61. *p++ = '\0';
  62. }
  63. if ( strcasecmp( p, "add" ) == 0 ) {
  64. options |= DSGW_DISPLAY_OPT_ADDING;
  65. if (( p = strrchr( tmplname, '&' )) != NULL ) {
  66. *p++ = '\0';
  67. }
  68. }
  69. if ( p != NULL && strncasecmp( p, "info=", 5 ) == 0 ) {
  70. dsgw_last_op_info = dsgw_ch_strdup( p + 5 );
  71. dsgw_form_unescape( dsgw_last_op_info );
  72. continue;
  73. }
  74. if ( p != NULL && strncasecmp( p, "dn=", 3 ) == 0 ) {
  75. dn = dsgw_ch_strdup( p + 3 );
  76. dsgw_form_unescape( dn );
  77. continue;
  78. }
  79. if ( p != NULL && strncasecmp( p, "dnattr=", 7 ) == 0 ) {
  80. dsgw_dnattr = dsgw_ch_strdup( p + 7 );
  81. dsgw_form_unescape( dsgw_dnattr );
  82. continue;
  83. }
  84. if ( p != NULL && strncasecmp( p, "dndesc=", 7 ) == 0 ) {
  85. dsgw_dndesc = dsgw_ch_strdup( p + 7 );
  86. dsgw_form_unescape( dsgw_dndesc );
  87. continue;
  88. }
  89. if ( p != NULL && strncasecmp( p, "context=", 8 ) == 0) {
  90. context = dsgw_ch_strdup( p + 8 );
  91. dsgw_form_unescape( context );
  92. continue;
  93. }
  94. /*
  95. * If none of the if-statements above matched,
  96. * then it's the template name
  97. */
  98. tmplname = p;
  99. break;
  100. }
  101. } else {
  102. tmplname = NULL;
  103. }
  104. (void)dsgw_init( argc, argv, DSGW_METHOD_GET );
  105. dsgw_send_header();
  106. #ifdef DSGW_DEBUG
  107. dsgw_logstringarray( "env", env );
  108. #endif
  109. get_request(dn, tmplname, parent, options);
  110. exit( 0 );
  111. }
  112. static void
  113. get_request(char *dn, char *tmplname, char *parent, unsigned long options)
  114. {
  115. LDAP *ld;
  116. if ( dn == NULL ) { /* not found in QUERY_STRING */
  117. dsgw_error( DSGW_ERR_MISSINGINPUT, NULL, DSGW_ERROPT_EXIT, 0, NULL );
  118. }
  119. #ifdef DSGW_DEBUG
  120. dsgw_log( "get_request: dn: \"%s\", tmplname: \"%s\" "
  121. "dnattr: \"%s\", dndesc: \"%s\"\n", dn,
  122. ( tmplname == NULL ) ? "(null)" : tmplname,
  123. ( dsgw_dnattr == NULL ) ? "(null)" : dsgw_dnattr,
  124. ( dsgw_dndesc == NULL ) ? "(null)" : dsgw_dndesc );
  125. #endif
  126. (void)dsgw_init_ldap( &ld, NULL, 0, 0);
  127. if (( options & DSGW_DISPLAY_OPT_ADDING ) == 0 ) {
  128. /*
  129. * editing an existing entry -- if no DN is provided and we are running
  130. * under the admin server, try to get DN from admin. server
  131. */
  132. if ( *dn == '\0' ) {
  133. (void)dsgw_get_adm_identity( ld, NULL, &dn, NULL,
  134. DSGW_ERROPT_EXIT );
  135. }
  136. dsgw_read_entry( ld, dn, NULL, tmplname, NULL, options );
  137. } else {
  138. dsgwtmplinfo *tip;
  139. char *matched;
  140. /*
  141. * new entry -- check to make sure it doesn't exist
  142. */
  143. if ( dsgw_ldap_entry_exists( ld, dn, &matched, DSGW_ERROPT_EXIT )) {
  144. char **rdns;
  145. dsgw_html_begin( XP_GetClientStr(DBT_entryAlreadyExists_), 1 );
  146. dsgw_emits( XP_GetClientStr(DBT_anEntryNamed_) );
  147. rdns = ldap_explode_dn( dn, 1 );
  148. dsgw_html_href(
  149. dsgw_build_urlprefix(),
  150. dn, ( rdns == NULL || rdns[ 0 ] == NULL ) ? dn : rdns[ 0 ],
  151. NULL, XP_GetClientStr(DBT_onmouseoverWindowStatusClickHere_) );
  152. if ( rdns != NULL ) {
  153. ldap_value_free( rdns );
  154. }
  155. dsgw_emits( XP_GetClientStr(DBT_alreadyExistsPPleaseChooseAnothe_) );
  156. dsgw_form_begin( NULL, NULL );
  157. dsgw_emits( "\n<CENTER><TABLE border=2 width=\"100%\"><TR>\n" );
  158. dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
  159. dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  160. "onClick=\"parent.close()\">", XP_GetClientStr(DBT_closeWindow_1) );
  161. dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
  162. dsgw_emit_helpbutton( "ENTRYEXISTS" );
  163. dsgw_emits( "\n</TABLE></CENTER></FORM>\n" );
  164. dsgw_html_end();
  165. } else if ( !dsgw_is_dnparent( matched, dn ) &&
  166. !dsgw_dn_cmp( dn, gc->gc_ldapsearchbase )) {
  167. /*
  168. * The parent entry does not exist, and the dn being added is not
  169. * the same as the suffix for which the gateway is configured.
  170. */
  171. dsgw_html_begin( XP_GetClientStr(DBT_parentEntryDoesNotExist_), 1 );
  172. dsgw_emitf( XP_GetClientStr(DBT_youCannotAddAnEntryByTheNamePBSB_),
  173. dn );
  174. parent = dsgw_dn_parent( dn );
  175. if ( parent == NULL || strlen( parent ) == 0 ) {
  176. dsgw_emits( XP_GetClientStr(DBT_itsParentN_) );
  177. } else {
  178. dsgw_emitf( XP_GetClientStr(DBT_anEntryNamedPBSBN_), parent );
  179. free( parent );
  180. }
  181. dsgw_form_begin( NULL, NULL );
  182. dsgw_emits( "\n<CENTER><TABLE border=2 width=\"100%\"><TR>\n" );
  183. dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
  184. dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  185. "onClick=\"parent.close()\">", XP_GetClientStr(DBT_closeWindow_2) );
  186. dsgw_emits( "<TD WIDTH=\"50%\" ALIGN=\"center\">\n" );
  187. dsgw_emit_helpbutton( "ADD_NOPARENT" );
  188. dsgw_emits( "\n</TABLE></CENTER></FORM>\n" );
  189. dsgw_html_end();
  190. } else {
  191. /*
  192. * The parent exists, or the user is adding the entry whose DN
  193. * is the same as the suffix for which the gateway is configured.
  194. * Display the "add entry" form.
  195. */
  196. if ( tmplname == NULL ) {
  197. #ifdef DSGW_DEBUG
  198. dsgw_log( "NULL tmplname\n" );
  199. #endif
  200. dsgw_error( DSGW_ERR_MISSINGINPUT,
  201. XP_GetClientStr(DBT_missingTemplate_),
  202. DSGW_ERROPT_EXIT, 0, NULL );
  203. }
  204. tip = dsgw_display_init( DSGW_TMPLTYPE_DISPLAY, tmplname, options );
  205. dsgw_display_entry( tip, ld, NULL, NULL, dn );
  206. dsgw_display_done( tip );
  207. }
  208. }
  209. ldap_unbind( ld );
  210. }