dsconfig.c 7.7 KB

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