]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/main.c
cleanup
[openldap] / servers / slapd / main.c
index 609e184365ccf35026df9a32478a8c2359d07f7f..6d4096b9bc5cbbad3d1817a73e0d8be5add5939d 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Copyright 1998-2004 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -64,6 +64,20 @@ static struct sockaddr_in    bind_addr;
 #define MAIN_RETURN(x) return(x)
 #endif
 
+typedef int (MainFunc) LDAP_P(( int argc, char *argv[] ));
+extern MainFunc slapadd, slapcat, slapindex, slappasswd;
+
+static struct {
+       char *name;
+       MainFunc *func;
+} tools[] = {
+       {"slapadd", slapadd},
+       {"slapcat", slapcat},
+       {"slapindex", slapindex},
+       {"slappasswd", slappasswd},
+       {NULL, NULL}
+};
+
 /*
  * when more than one slapd is running on one machine, each one might have
  * it's own LOCAL for syslogging and must have its own pid/args files
@@ -102,7 +116,11 @@ static int   cnvt_str2int( char *, STRDISP_P, int );
 
 #endif /* LOG_LOCAL4 */
 
-static int check_config = 0;
+#define CHECK_NONE     0x00
+#define        CHECK_CONFIG    0x01
+#define CHECK_DN       0x02
+static int check = CHECK_NONE;
+static struct berval check_dn = BER_BVC("");
 static int version = 0;
 
 static void
@@ -113,6 +131,7 @@ usage( char *name )
        fprintf( stderr,
                "\t-4\t\tIPv4 only\n"
                "\t-6\t\tIPv6 only\n"
+               "\t-T (a|c|i|p)\tRun in Tool mode\n"
                "\t-c cookie\tSync cookie of consumer\n"
                "\t-d level\tDebug level" "\n"
                "\t-f filename\tConfiguration file\n"
@@ -129,6 +148,7 @@ usage( char *name )
 #endif
                "\t-s level\tSyslog level\n"
                "\t-t\t\tCheck configuration file and exit\n"
+               "\t-D dn\tCheck dn and exit\n"
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
                "\t-u user\t\tUser (id or name) to run as\n"
                "\t-V\t\tprint version info (-VV only)\n"
@@ -164,7 +184,7 @@ int main( int argc, char **argv )
 #else
        char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
 #endif
-       char        *serverName = NULL;
+       char        *serverName;
        int         serverMode = SLAP_SERVER_MODE;
 
        struct berval cookie = { 0, NULL };
@@ -180,6 +200,17 @@ int main( int argc, char **argv )
 
        sl_mem_init();
 
+       serverName = lutil_progname( "slapd", argc, argv );
+
+       if ( strcmp( serverName, "slapd" ) ) {
+               for (i=0; tools[i].name; i++) {
+                       if ( !strcmp( serverName, tools[i].name ) ) {
+                               rc = tools[i].func(argc, argv);
+                               MAIN_RETURN(rc);
+                       }
+               }
+       }
+
 #ifdef HAVE_NT_SERVICE_MANAGER
        {
                int *i;
@@ -188,7 +219,6 @@ int main( int argc, char **argv )
                char *regService = NULL;
 
                if ( is_NT_Service ) {
-                       serverName = argv[0];
                        lutil_CommenceStartupProcessing( serverName, slap_sig_shutdown );
                        if ( strcmp(serverName, SERVICE_NAME) )
                            regService = serverName;
@@ -241,7 +271,7 @@ int main( int argc, char **argv )
 #endif
 
        while ( (i = getopt( argc, argv,
-                            "c:d:f:h:s:n:t:V"
+                            "c:d:D:f:h:s:n:tT:V"
 #if LDAP_PF_INET6
                                "46"
 #endif
@@ -341,17 +371,31 @@ int main( int argc, char **argv )
 #endif /* SETUID && GETUID */
 
                case 'n':  /* NT service name */
-                       if( serverName != NULL ) free( serverName );
                        serverName = ch_strdup( optarg );
                        break;
 
                case 't':
-                       check_config++;
+                       check |= CHECK_CONFIG;
                        break;
+
+               case 'D':
+                       check |= CHECK_DN;
+                       check_dn.bv_val = optarg;
+                       check_dn.bv_len = strlen( optarg );
+                       break;
+
                case 'V':
                        version++;
                        break;
 
+               case 'T':
+                       for (i=0; tools[i].name; i++) {
+                               if ( optarg[0] == tools[i].name[4] ) {
+                                       rc = tools[i].func(argc, argv);
+                                       MAIN_RETURN(rc);
+                               }
+                       }
+                       /* FALLTHRU */
                default:
                        usage( argv[0] );
                        rc = 1;
@@ -374,19 +418,24 @@ int main( int argc, char **argv )
                if ( version > 1 ) goto stop;
        }
 
-       if( serverName == NULL ) {
-               if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
-                       serverName = argv[0];
-               } else {
-                       serverName = serverName + 1;
-               }
-       }
+       {
+               char *logName;
+#ifdef HAVE_EBCDIC
+               logName = ch_strdup( serverName );
+               __atoe( logName );
+#else
+               logName = serverName;
+#endif
 
 #ifdef LOG_LOCAL4
-       openlog( serverName, OPENLOG_OPTIONS, syslogUser );
+               openlog( logName, OPENLOG_OPTIONS, syslogUser );
 #elif LOG_DEBUG
-       openlog( serverName, OPENLOG_OPTIONS );
+               openlog( logName, OPENLOG_OPTIONS );
+#endif
+#ifdef HAVE_EBCDIC
+               free( logName );
 #endif
+       }
 
 #ifdef NEW_LOGGING
        LDAP_LOG( SLAPD, INFO, "%s", Versionstr, 0, 0 );
@@ -394,7 +443,7 @@ int main( int argc, char **argv )
        Debug( LDAP_DEBUG_ANY, "%s", Versionstr, 0, 0 );
 #endif
 
-       if( !check_config && slapd_daemon_init( urls ) != 0 ) {
+       if( check == CHECK_NONE && slapd_daemon_init( urls ) != 0 ) {
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
                goto stop;
@@ -495,17 +544,21 @@ int main( int argc, char **argv )
                rc = 1;
                SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
 
-               if ( check_config ) {
+               if ( check & CHECK_CONFIG ) {
                        fprintf( stderr, "config check failed\n" );
                }
 
                goto destroy;
        }
 
-       if ( check_config ) {
-               rc = 0;
+       if ( check & CHECK_CONFIG ) {
                fprintf( stderr, "config check succeeded\n" );
-               goto destroy;
+
+               check &= ~CHECK_CONFIG;
+               if ( check == CHECK_NONE ) {
+                       rc = 0;
+                       goto destroy;
+               }
        }
 
        if ( glue_sub_init( ) != 0 ) {
@@ -531,6 +584,32 @@ int main( int argc, char **argv )
                goto destroy;
        }
 
+       if ( check & CHECK_DN ) {
+               struct berval   pdn, ndn;
+               
+               rc = dnPrettyNormal( NULL, &check_dn,
+                                       &pdn, &ndn, NULL );
+               if ( rc != LDAP_SUCCESS ) {
+                       fprintf( stderr, "DN: <%s> check failed %d (%s)\n",
+                                       check_dn.bv_val, rc,
+                                       ldap_err2string( rc ) );
+                       rc = 1;
+                       
+               } else {
+                       fprintf( stderr, "DN: <%s> check succeeded\n"
+                                       "normalized: <%s>\n"
+                                       "pretty:     <%s>\n",
+                                       check_dn.bv_val,
+                                       ndn.bv_val, pdn.bv_val );
+                       rc = 0;
+               }
+
+               check &= ~CHECK_DN;
+               if ( check == CHECK_NONE ) {
+                       goto destroy;
+               }
+       }
+
 #ifdef HAVE_TLS
        rc = ldap_pvt_tls_init();
        if( rc != 0) {