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