Back to index

openldap  2.4.31
slaptest.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2004-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2004 Pierangelo Masarati.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Pierangelo Masarati for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/stdlib.h>
00026 
00027 #include <ac/ctype.h>
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <ac/unistd.h>
00033 #include <ac/errno.h>
00034 
00035 #include <lber.h>
00036 #include <ldif.h>
00037 #include <lutil.h>
00038 
00039 #include "slapcommon.h"
00040 
00041 #ifndef S_IWRITE
00042 #define S_IWRITE     S_IWUSR
00043 #endif
00044 
00045 static int
00046 test_file( const char *fname, const char *ftype )
00047 {
00048        struct stat   st;
00049        int           save_errno;
00050 
00051        switch ( stat( fname, &st ) ) {
00052        case 0:
00053               if ( !( st.st_mode & S_IWRITE ) ) {
00054                      Debug( LDAP_DEBUG_ANY, "%s file "
00055                             "\"%s\" exists, but user does not have access\n",
00056                             ftype, fname, 0 );
00057                      return -1;
00058               }
00059               break;
00060 
00061        case -1:
00062        default:
00063               save_errno = errno;
00064               if ( save_errno == ENOENT ) {
00065                      FILE          *fp = fopen( fname, "w" );
00066 
00067                      if ( fp == NULL ) {
00068                             save_errno = errno;
00069 
00070                             Debug( LDAP_DEBUG_ANY, "unable to open file "
00071                                    "\"%s\": %d (%s)\n",
00072                                    fname,
00073                                    save_errno, strerror( save_errno ) );
00074 
00075                             return -1;
00076                      }
00077                      fclose( fp );
00078                      unlink( fname );
00079                      break;
00080               }
00081 
00082               Debug( LDAP_DEBUG_ANY, "unable to stat file "
00083                      "\"%s\": %d (%s)\n",
00084                      slapd_pid_file,
00085                      save_errno, strerror( save_errno ) );
00086               return -1;
00087        }
00088 
00089        return 0;
00090 }
00091 
00092 int
00093 slaptest( int argc, char **argv )
00094 {
00095        int                  rc = EXIT_SUCCESS;
00096        const char           *progname = "slaptest";
00097 
00098        slap_tool_init( progname, SLAPTEST, argc, argv );
00099 
00100        if ( slapd_pid_file != NULL ) {
00101               if ( test_file( slapd_pid_file, "pid" ) ) {
00102                      return EXIT_FAILURE;
00103               }
00104        }
00105 
00106        if ( slapd_args_file != NULL ) {
00107               if ( test_file( slapd_args_file, "args" ) ) {
00108                      return EXIT_FAILURE;
00109               }
00110        }
00111 
00112        if ( !quiet ) {
00113               fprintf( stderr, "config file testing succeeded\n");
00114        }
00115 
00116        if ( slap_tool_destroy())
00117               rc = EXIT_FAILURE;
00118 
00119        return rc;
00120 }