dsconfig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * dsconfig.c -- CGI configuration update handler -- directory gateway
  40. */
  41. #include "dsgw.h"
  42. static void handle_request( int reqmethod );
  43. static void handle_post();
  44. main( argc, argv, env )
  45. int argc;
  46. char *argv[];
  47. #ifdef DSGW_DEBUG
  48. char *env[];
  49. #endif
  50. {
  51. int reqmethod;
  52. context= dsgw_ch_strdup("pb");
  53. /*CHANGE THIS*/
  54. reqmethod = dsgw_init( argc, argv, DSGW_METHOD_POST | DSGW_METHOD_GET );
  55. dsgw_send_header();
  56. #ifdef DSGW_DEBUG
  57. dsgw_logstringarray( "env", env );
  58. #endif
  59. handle_request( reqmethod );
  60. exit( 0 );
  61. }
  62. #define DSGWCONFIG_EMPTY_IF_NULL( s ) ( (s) == NULL ? "" : (s) )
  63. static void
  64. handle_request( int reqmethod )
  65. {
  66. FILE *fp;
  67. char **argv, *buf, line[ BIG_LINE ];
  68. char *checked = " CHECKED ", *qs = NULL;
  69. char *str_valuefmt = " VALUE=\"%s\" ";
  70. char *int_valuefmt = " VALUE=\"%d\" ";
  71. int did_post, argc, switch_mode = 0, is_localdb = 0;
  72. buf = dsgw_ch_malloc( strlen( progname ) + 6 ); /* room for ".html\0" */
  73. sprintf( buf, "%s.html", progname );
  74. fp = dsgw_open_html_file( buf, DSGW_ERROPT_EXIT );
  75. free( buf );
  76. did_post = 0;
  77. qs = getenv( "QUERY_STRING" );
  78. if (( reqmethod == DSGW_METHOD_GET ) && ( qs != NULL ) &&
  79. !strcasecmp( qs, "CHANGE" )) {
  80. switch_mode = 1;
  81. }
  82. is_localdb = gc->gc_localdbconf != NULL;
  83. while ( dsgw_next_html_line( fp, line )) {
  84. if ( dsgw_parse_line( line, &argc, &argv, 0, dsgw_simple_cond_is_true,
  85. NULL )) {
  86. if ( dsgw_directive_is( line, DRCT_DS_INLINE_POST_RESULTS )) {
  87. if ( !did_post && reqmethod == DSGW_METHOD_POST ) {
  88. handle_post();
  89. did_post = 1;
  90. /* We re-read the config file, so re-calculate is_localdb */
  91. is_localdb = ( gc->gc_localdbconf != NULL );
  92. }
  93. } else if ( dsgw_directive_is( line, DRCT_DS_CHECKED_IF_LOCAL )) {
  94. if (( is_localdb && !switch_mode ) ||
  95. ( !is_localdb && switch_mode )) {
  96. dsgw_emits( checked );
  97. }
  98. } else if ( dsgw_directive_is( line, DRCT_DS_CONFIG_INFO )) {
  99. dsgw_emits( "<FONT SIZE=\"+1\"><B>" );
  100. if (( is_localdb && !switch_mode ) ||
  101. ( !is_localdb && switch_mode )) {
  102. dsgw_emits( "Local Directory Configuration" );
  103. } else {
  104. dsgw_emits( "LDAP Directory Server Configuration" );
  105. }
  106. dsgw_emits( "</FONT>\n" );
  107. } else if ( dsgw_directive_is( line, DRCT_DS_CHECKED_IF_REMOTE )) {
  108. if (( !is_localdb && !switch_mode ) ||
  109. ( is_localdb && switch_mode )) {
  110. dsgw_emits( checked );
  111. }
  112. } else if ( dsgw_directive_is( line, DRCT_DS_HOSTNAME_VALUE ) &&
  113. (( !is_localdb && !switch_mode ) ||
  114. ( is_localdb && switch_mode ))) {
  115. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP><B>Host Name:</B></TD>"
  116. "<TD><INPUT TYPE=\"text\" NAME=\"host\"" );
  117. dsgw_emitf( str_valuefmt,
  118. DSGWCONFIG_EMPTY_IF_NULL( gc->gc_ldapserver ));
  119. dsgw_emits( "SIZE=40></TD>\n</TR>\n\n" );
  120. } else if ( dsgw_directive_is( line, DRCT_DS_PORT_VALUE ) &&
  121. (( !is_localdb && !switch_mode ) ||
  122. ( is_localdb && switch_mode ))) {
  123. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP><B>Port:</B></TD>\n"
  124. "<TD><INPUT TYPE=\"text\" NAME=\"port\" " );
  125. if ( !is_localdb ) {
  126. dsgw_emitf( int_valuefmt, gc->gc_ldapport );
  127. }
  128. dsgw_emits( "SIZE=5></TD>\n</TR>\n\n" );
  129. #ifndef DSGW_NO_SSL
  130. } else if ( dsgw_directive_is( line, DRCT_DS_SSL_CONFIG_VALUE ) &&
  131. (( !is_localdb && !switch_mode ) ||
  132. ( is_localdb && switch_mode ))) {
  133. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP>\n"
  134. "<B>Use Secure<BR>Sockets Layer (SSL)<BR>for "
  135. "connections?:</B></TD>\n"
  136. "<TD><INPUT TYPE=\"radio\" NAME=\"ssl\" "
  137. "VALUE=\"true\" onClick=\"selectedSSL(true)\"" );
  138. if ( gc->gc_ldapssl ) {
  139. dsgw_emits( checked );
  140. }
  141. dsgw_HTML_emits( ">Yes" DSGW_UTF8_NBSP "\n<INPUT TYPE=\"radio\" NAME=\"ssl\" "
  142. "VALUE=\"false\" onClick=\"selectedSSL(false)\"" );
  143. if ( !gc->gc_ldapssl ) {
  144. dsgw_emits( checked );
  145. }
  146. dsgw_emits( ">No\n</TD>\n</TR>\n\n" );
  147. #endif
  148. } else if ( dsgw_directive_is( line, DRCT_DS_BASEDN_VALUE )) {
  149. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP><B>Base DN" );
  150. if (( is_localdb && !switch_mode ) ||
  151. ( !is_localdb && switch_mode )) {
  152. dsgw_emits( " (optional)" );
  153. }
  154. dsgw_emits( ":</B></TD>\n<TD><INPUT TYPE=\"text\" "
  155. "NAME=\"basedn\" " );
  156. dsgw_emitf( str_valuefmt,
  157. DSGWCONFIG_EMPTY_IF_NULL( gc->gc_ldapsearchbase ));
  158. dsgw_emits( "SIZE=50></TD>\n</TR>\n\n" );
  159. } else if ( dsgw_directive_is( line, DRCT_DS_BINDDN_VALUE ) &&
  160. (( !is_localdb && !switch_mode ) ||
  161. ( is_localdb && switch_mode ))) {
  162. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP><B>"
  163. "Bind DN (optional):</B></TD>\n"
  164. "<TD><INPUT TYPE=\"text\" NAME=\"binddn\" " );
  165. if ( gc->gc_binddn == NULL || strlen( gc->gc_binddn ) == 0 ) {
  166. dsgw_emits( "VALUE=\"\"" );
  167. } else {
  168. dsgw_emitf( "VALUE=\"%s\" ", gc->gc_binddn );
  169. }
  170. dsgw_emits( " SIZE=50></TD>\n</TR>\n\n" );
  171. } else if ( dsgw_directive_is( line, DRCT_DS_BINDPASSWD_VALUE ) &&
  172. (( !is_localdb && !switch_mode ) ||
  173. ( is_localdb && switch_mode ))) {
  174. dsgw_emits( "<TR>\n<TD ALIGN=\"right\" NOWRAP><B>"
  175. "Bind Password (optional):</B></TD>\n"
  176. "<TD><INPUT TYPE=\"password\" NAME=\"bindpw\" " );
  177. if ( gc->gc_bindpw != NULL && ( strlen( gc->gc_bindpw ) > 0 )) {
  178. dsgw_emitf( str_valuefmt, gc->gc_bindpw );
  179. }
  180. dsgw_emits( "SIZE=20></TD>\n</TR>\n\n" );
  181. } else if ( dsgw_directive_is( line, DRCT_DS_NOCERTFILE_WARNING )
  182. && ( gc->gc_securitypath == NULL )
  183. && !is_localdb && gc->gc_ldapssl && argc > 0 ) {
  184. /*
  185. * using LDAP over SSL but no CertFile in ns-admin.conf:
  186. * show a warning message
  187. */
  188. dsgw_emits( argv[ 0 ] );
  189. }
  190. }
  191. }
  192. fclose( fp );
  193. }
  194. static void
  195. handle_post()
  196. {
  197. char *dirsvctype, *dbhandle;
  198. dsgwconfig cfg;
  199. memset( &cfg, 0, sizeof( cfg ));
  200. dirsvctype = dsgw_get_cgi_var( "dirsvctype", DSGW_CGIVAR_REQUIRED );
  201. dbhandle = dsgw_get_cgi_var( "dbhandle", DSGW_CGIVAR_OPTIONAL );
  202. cfg.gc_ldapsearchbase = dsgw_get_cgi_var( "basedn", DSGW_CGIVAR_OPTIONAL );
  203. if ( strcasecmp( dirsvctype, "local" ) == 0 ) {
  204. char *userdb_path;
  205. if (( userdb_path = get_userdb_dir()) == NULL ) {
  206. dsgw_error( DSGW_ERR_USERDB_PATH, NULL, DSGW_ERROPT_INLINE, 0,
  207. NULL );
  208. return;
  209. }
  210. cfg.gc_localdbconf = dsgw_ch_malloc( strlen( userdb_path ) +
  211. strlen( DSGW_LCACHECONF_PPATH ) +
  212. strlen( DSGW_LCACHECONF_FILE ) + 2 );
  213. sprintf( cfg.gc_localdbconf, "%s/%s%s", userdb_path,
  214. DSGW_LCACHECONF_PPATH, DSGW_LCACHECONF_FILE );
  215. } else if ( strcasecmp( dirsvctype, "remote" ) == 0 ) {
  216. cfg.gc_ldapserver = dsgw_get_cgi_var( "host", DSGW_CGIVAR_REQUIRED );
  217. cfg.gc_ldapport = atoi( dsgw_get_cgi_var( "port",
  218. DSGW_CGIVAR_REQUIRED ));
  219. #ifndef DSGW_NO_SSL
  220. cfg.gc_ldapssl =
  221. dsgw_get_boolean_var( "ssl", DSGW_CGIVAR_OPTIONAL, 0 );
  222. #endif
  223. cfg.gc_binddn = dsgw_get_escaped_cgi_var( "escapedbinddn", "binddn",
  224. DSGW_CGIVAR_OPTIONAL );
  225. cfg.gc_bindpw = dsgw_get_cgi_var( "bindpw", DSGW_CGIVAR_OPTIONAL );
  226. } else {
  227. dsgw_error( DSGW_ERR_SERVICETYPE, dirsvctype, DSGW_ERROPT_INLINE, 0,
  228. NULL );
  229. return;
  230. }
  231. if ( cfg.gc_ldapsearchbase == NULL ) {
  232. cfg.gc_ldapsearchbase = "";
  233. }
  234. if ( dsgw_update_dbswitch( &cfg, dbhandle, DSGW_ERROPT_INLINE ) == 0 ) {
  235. /*
  236. * success: display status message and then re-read config. file
  237. */
  238. dsgw_emits( "<FONT SIZE=\"+1\">\n<P>The Directory Service configuration" );
  239. if ( dbhandle != NULL ) {
  240. dsgw_emitf( " for <B>%s</B>", dbhandle );
  241. }
  242. dsgw_emits( " has been updated.\n</FONT>\n" );
  243. (void)dsgw_read_config(NULL);
  244. }
  245. dsgw_emits( "<HR>\n" );
  246. }