]> git.sur5r.net Git - openldap/blob - servers/slapd/slaptest.c
Happy New Year (belated)
[openldap] / servers / slapd / slaptest.c
1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2  *
3  * Copyright 2004-2008 The OpenLDAP Foundation.
4  * Portions Copyright 2004 Pierangelo Masarati.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Pierangelo Masarati for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <ac/unistd.h>
32 #include <ac/errno.h>
33
34 #include <lber.h>
35 #include <ldif.h>
36 #include <lutil.h>
37
38 #include "slapcommon.h"
39
40 static int
41 test_file( const char *fname, const char *ftype )
42 {
43         struct stat     st;
44         int             save_errno;
45
46         switch ( stat( fname, &st ) ) {
47         case 0:
48                 if ( !( st.st_mode & S_IWRITE ) ) {
49                         Debug( LDAP_DEBUG_ANY, "%s file "
50                                 "\"%s\" exists, but user does not have access\n",
51                                 ftype, fname, 0 );
52                         return -1;
53                 }
54                 break;
55
56         case -1:
57         default:
58                 save_errno = errno;
59                 if ( save_errno == ENOENT ) {
60                         FILE            *fp = fopen( fname, "w" );
61
62                         if ( fp == NULL ) {
63                                 save_errno = errno;
64
65                                 Debug( LDAP_DEBUG_ANY, "unable to open file "
66                                         "\"%s\": %d (%s)\n",
67                                         fname,
68                                         save_errno, strerror( save_errno ) );
69
70                                 return -1;
71                         }
72                         fclose( fp );
73                         unlink( fname );
74                         break;
75                 }
76
77                 Debug( LDAP_DEBUG_ANY, "unable to stat file "
78                         "\"%s\": %d (%s)\n",
79                         slapd_pid_file,
80                         save_errno, strerror( save_errno ) );
81                 return -1;
82         }
83
84         return 0;
85 }
86
87 int
88 slaptest( int argc, char **argv )
89 {
90         int                     rc = EXIT_SUCCESS;
91         const char              *progname = "slaptest";
92
93         slap_tool_init( progname, SLAPTEST, argc, argv );
94
95         if ( slapd_pid_file != NULL ) {
96                 if ( test_file( slapd_pid_file, "pid" ) ) {
97                         return EXIT_FAILURE;
98                 }
99         }
100
101         if ( slapd_args_file != NULL ) {
102                 if ( test_file( slapd_args_file, "args" ) ) {
103                         return EXIT_FAILURE;
104                 }
105         }
106
107         if ( !quiet ) {
108                 fprintf( stderr, "config file testing succeeded\n");
109         }
110
111         slap_tool_destroy();
112
113         return rc;
114 }