edit.c 9.3 KB

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