dsimpldif.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * dsimpldif.c -- CGI import ldif file handler -- directory gateway
  8. */
  9. #include "dsgw.h"
  10. static void handle_request( int reqmethod );
  11. static void handle_post();
  12. static char *ldiffile;
  13. static int erase = 0, stop = 1;
  14. main( argc, argv, env )
  15. int argc;
  16. char *argv[];
  17. #ifdef DSGW_DEBUG
  18. char *env[];
  19. #endif
  20. {
  21. int reqmethod;
  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 ";
  37. char *str_valuefmt = " VALUE=\"%s\" ";
  38. int did_post, argc;
  39. buf = dsgw_ch_malloc( strlen( progname ) + 6 ); /* room for ".html\0" */
  40. sprintf( buf, "%s.html", progname );
  41. fp = dsgw_open_html_file( buf, DSGW_ERROPT_EXIT );
  42. free( buf );
  43. did_post = 0;
  44. while ( dsgw_next_html_line( fp, line )) {
  45. if ( dsgw_parse_line( line, &argc, &argv, 0, dsgw_simple_cond_is_true,
  46. NULL )) {
  47. if ( dsgw_directive_is( line, DRCT_DS_INLINE_POST_RESULTS )) {
  48. if ( !did_post && reqmethod == DSGW_METHOD_POST ) {
  49. handle_post();
  50. did_post = 1;
  51. }
  52. } else if ( dsgw_directive_is( line, DS_LDIF_FILE )) {
  53. dsgw_emitf( str_valuefmt,
  54. DSGWCONFIG_EMPTY_IF_NULL( ldiffile ));
  55. } else if ( dsgw_directive_is( line, DS_CHECKED_IF_ERASE )) {
  56. if ( erase ) {
  57. dsgw_emits( checked );
  58. }
  59. } else if ( dsgw_directive_is( line, DS_CHECKED_IF_NOTERASE )) {
  60. if ( !erase ) {
  61. dsgw_emits( checked );
  62. }
  63. } else if ( dsgw_directive_is( line, DS_CHECKED_IF_STOP )) {
  64. if ( stop ) {
  65. dsgw_emits( checked );
  66. }
  67. } else if ( dsgw_directive_is( line, DS_CHECKED_IF_NOTSTOP )) {
  68. if ( !stop ) {
  69. dsgw_emits( checked );
  70. }
  71. }
  72. }
  73. }
  74. fclose( fp );
  75. }
  76. static void
  77. handle_post()
  78. {
  79. char cmd[ BIG_LINE ], path[BIG_LINE ];
  80. char *userdb_path;
  81. ldiffile = dsgw_get_cgi_var( "ldif", DSGW_CGIVAR_REQUIRED );
  82. erase = dsgw_get_boolean_var( "erase", DSGW_CGIVAR_REQUIRED, 0 );
  83. stop = dsgw_get_boolean_var( "stop", DSGW_CGIVAR_REQUIRED, 0 );
  84. if (erase) {
  85. if ( gc->gc_localdbconf == NULL) {
  86. /* don't erase the real ldap database */
  87. dsgw_error( DSGW_ERR_DB_ERASE, NULL, DSGW_ERROPT_EXIT, 0, NULL );
  88. }
  89. /* erase the local database */
  90. if ( erase_db() != 0 ) {
  91. return;
  92. }
  93. }
  94. if (( userdb_path = get_userdb_dir()) == NULL ) {
  95. dsgw_error( DSGW_ERR_USERDB_PATH, NULL, DSGW_ERROPT_EXIT, 0, NULL );
  96. }
  97. if (gc->gc_localdbconf == NULL) {
  98. /* remote */
  99. PR_snprintf (cmd, BIG_LINE, "./%s -a %s -h %s -p %d -f %s > %s 2>&1",
  100. DSGW_LDAPMODIFY, stop?"":"-c",gc->gc_ldapserver,
  101. gc->gc_ldapport, ldiffile, DSGW_NULL_DEVICE);
  102. }
  103. else {
  104. /* local database */
  105. PR_snprintf (cmd, BIG_LINE, "./%s -a %s -C %s -f %s > %s 2>&1",
  106. DSGW_LDAPMODIFY, stop?"":"-c", gc->gc_localdbconf, ldiffile,
  107. DSGW_NULL_DEVICE);
  108. }
  109. PR_snprintf (path, BIG_LINE, "%s%s", userdb_path, DSGW_TOOLSDIR);
  110. chdir ( path );
  111. fflush (stdout);
  112. if (system (cmd) == 0) {
  113. /*
  114. * success: display status message
  115. */
  116. dsgw_emits(
  117. "<FONT SIZE=\"+1\">\n<P>The ldif file has been added.\n</FONT>\n " );
  118. }
  119. else {
  120. dsgw_emits(
  121. "<FONT SIZE=\"+1\">\n<P>The ldif file could not be added.\n</FONT>\n " );
  122. }
  123. dsgw_emits( "<HR>\n" );
  124. }