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