]> git.sur5r.net Git - openldap/blob - servers/slurpd/sanity.c
ITS#791: fix SASL ctx close
[openldap] / servers / slurpd / sanity.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14
15 /*
16  * sanity.c - perform sanity checks on the environment at startup time,
17  * and report any errors before we disassociate from the controlling tty,
18  * start up our threads, and do other stuff which makes it hard to give
19  * feedback to the users.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/unistd.h>
27 #include <ac/string.h>
28
29 #include "slurp.h"
30 #include "globals.h"
31
32 #define FC_DIRBAD       1
33 #define FC_DIRUNREAD    2
34 #define FC_DIRUNWRITE   4
35 #define FC_FILEBAD      8
36 #define FC_FILEUNREAD   16
37 #define FC_FILEUNWRITE  32
38
39
40 /*
41  * Forward declarations
42  */
43 static unsigned int filecheck LDAP_P(( char * ));
44
45
46
47 /*
48  * Take a look around to catch any fatal errors.  For example, make sure the
49  * destination directory for our working files exists, check that all
50  * pathnames make sense, and so on.  Returns 0 is everything's ok,
51  # -1 if there's something wrong which will keep us from functioning
52  * correctly.
53  *
54  * We do all these checks at startup so we can print a reasonable error
55  * message on stderr before we disassociate from the controlling tty.  This
56  * keeps some fatal error messages from "disappearing" into syslog.
57  */
58
59 int
60 sanity( void )
61 {
62     int err = 0;
63     int rc;
64
65     /*
66      * Are there any replicas listed in the slapd config file?
67      */
68     if ( sglob->replicas == NULL ) {
69         fprintf( stderr, "No replicas in slapd.conf file \"%s\"!\n",
70             sglob->slapd_configfile );
71         err++;
72     }
73
74     /*
75      * Make sure the directory housing the slapd replogfile exists, and
76      * that the slapd replogfile is readable, if it exists.
77      */
78     if ( sglob->slapd_replogfile == NULL ) {
79         fprintf( stderr, "Fatal error: no \"replogfile\" "
80                 "slapd.conf directive given\n" );
81         err++;
82     } else {
83         rc = filecheck( sglob->slapd_replogfile );
84         if ( rc & FC_DIRBAD ) {
85             fprintf( stderr, "Error: %s: directory specified in "
86                         "\"replogfile\" slapd.conf directive does not exist\n", 
87                     sglob->slapd_replogfile );
88             err++;
89         } else if ( rc & FC_DIRUNREAD ) {
90             fprintf( stderr, "Error: %s: directory specified in "
91                         "\"replogfile\" slapd.conf directive is not readable\n", 
92                     sglob->slapd_replogfile );
93             err++;
94         } else if (!( rc & FC_FILEBAD) && ( rc & FC_FILEUNREAD )) {
95             fprintf( stderr, "Error: %s: file specified in "
96                         "\"replogfile\" slapd.conf directive is not readable\n", 
97                     sglob->slapd_replogfile );
98             err++;
99         }
100     }
101
102     /*
103      * Make sure the directory for the slurpd replogfile is there, and
104      * that the slurpd replogfile is readable and writable, if it exists.
105      */
106     if ( sglob->slurpd_replogfile == NULL ) {
107         fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
108         err++;
109     } else {
110         rc = filecheck( sglob->slurpd_replogfile );
111         if ( rc & FC_DIRBAD ) {
112             fprintf( stderr, "Error: %s: slurpd \"replogfile\" "
113                         "directory does not exist\n", 
114                     sglob->slurpd_replogfile );
115             err++;
116         } else if ( rc & FC_DIRUNREAD ) {
117             fprintf( stderr, "Error: %s: slurpd \"replogfile\" "
118                         "directory not readable\n", 
119                     sglob->slurpd_replogfile );
120             err++;
121         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
122             fprintf( stderr, "Error: %s: slurpd \"replogfile\" not readable\n", 
123                     sglob->slurpd_replogfile );
124             err++;
125         } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
126             fprintf( stderr, "Error: %s: slurpd \"replogfile\" not writeable\n", 
127                     sglob->slurpd_replogfile );
128             err++;
129         }
130     }
131
132     /*
133      * Make sure that the directory for the slurpd status file is there, and
134      * that the slurpd status file is writable, if it exists.
135      */
136     rc = filecheck( sglob->slurpd_status_file );
137     if ( rc & FC_DIRBAD ) {
138         fprintf( stderr, "Error: %s: status directory does not exist\n", 
139                 sglob->slurpd_status_file );
140         err++;
141     } else if ( rc & FC_DIRUNREAD ) {
142         fprintf( stderr, "Error: %s: status directory not readable\n", 
143                 sglob->slurpd_status_file );
144         err++;
145     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
146         fprintf( stderr, "Error: %s: status file not readable\n", 
147                 sglob->slurpd_status_file );
148         err++;
149     } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
150         fprintf( stderr, "Error: %s: status file not writeable\n", 
151                 sglob->slurpd_status_file );
152         err++;
153     }
154     
155     return ( err == 0 ? 0 : -1 );
156 }
157
158
159
160 /*
161  * Check for the existence of the file and directory leading to the file.
162  * Returns a bitmask which is the logical OR of the following flags:
163  *
164  *  FC_DIRBAD:          directory containing "f" does not exist.
165  *  FC_DIRUNREAD:       directory containing "f" exists but is not readable.
166  *  FC_DIRUNWRITE:      directory containing "f" exists but is not writable.
167  *  FC_FILEBAD:         "f" does not exist.
168  *  FC_FILEUNREAD:      "f" exists but is unreadable.
169  *  FC_FILEUNWRITE:     "f" exists but is unwritable.
170  *
171  * The calling routine is responsible for determining which, if any, of
172  * the returned flags is a problem for a particular file.
173  */
174 static unsigned int
175 filecheck(
176     char        *f
177 )
178 {
179     char                dir[ MAXPATHLEN ];
180     char                *p;
181     unsigned int        ret = 0;
182
183     strcpy( dir, f );
184     p = strrchr( dir, '/' );
185     if ( p != NULL ) {
186         *p = '\0';
187     }
188     if ( access( dir, F_OK ) < 0 ) {
189         ret |= FC_DIRBAD;
190     }
191     if ( access( dir, R_OK ) < 0 ) {
192         ret |= FC_DIRUNREAD;
193     }
194     if ( access( dir, W_OK ) < 0 ) {
195         ret |= FC_DIRUNWRITE;
196     }
197     if ( access( f, F_OK ) < 0 ) {
198         ret |= FC_FILEBAD;
199     }
200     if ( access( f, R_OK ) < 0 ) {
201         ret |= FC_FILEUNREAD;
202     }
203     if ( access( f, W_OK ) < 0 ) {
204         ret |= FC_FILEUNWRITE;
205     }
206
207     return ret;
208 }