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