3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
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.
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.
30 #include <ac/unistd.h>
31 #include <ac/string.h>
37 #define FC_DIRUNREAD 2
38 #define FC_DIRUNWRITE 4
40 #define FC_FILEUNREAD 16
41 #define FC_FILEUNWRITE 32
45 * Forward declarations
47 static unsigned int filecheck LDAP_P(( char * ));
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
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.
70 * Are there any replicas listed in the slapd config file?
72 if ( sglob->replicas == NULL ) {
73 fprintf( stderr, "No replicas in slapd.conf file \"%s\"!\n",
74 sglob->slapd_configfile );
79 * Make sure the directory housing the slapd replogfile exists, and
80 * that the slapd replogfile is readable, if it exists.
82 if ( sglob->slapd_replogfile == NULL ) {
83 fprintf( stderr, "Fatal error: no \"replogfile\" "
84 "slapd.conf directive given\n" );
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 );
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 );
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 );
107 * Make sure the directory for the slurpd replogfile is there, and
108 * that the slurpd replogfile is readable and writable, if it exists.
110 if ( sglob->slurpd_replogfile == NULL ) {
111 fprintf( stderr, "Fatal error: no \"replogfile\" directive given\n" );
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 );
120 } else if ( rc & FC_DIRUNREAD ) {
121 fprintf( stderr, "Error: %s: slurpd \"replogfile\" "
122 "directory not readable\n",
123 sglob->slurpd_replogfile );
125 } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
126 fprintf( stderr, "Error: %s: slurpd \"replogfile\" not readable\n",
127 sglob->slurpd_replogfile );
129 } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
130 fprintf( stderr, "Error: %s: slurpd \"replogfile\" not writeable\n",
131 sglob->slurpd_replogfile );
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.
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 );
145 } else if ( rc & FC_DIRUNREAD ) {
146 fprintf( stderr, "Error: %s: status directory not readable\n",
147 sglob->slurpd_status_file );
149 } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNREAD )) {
150 fprintf( stderr, "Error: %s: status file not readable\n",
151 sglob->slurpd_status_file );
153 } else if ( !( rc & FC_FILEBAD ) && ( rc & FC_FILEUNWRITE )) {
154 fprintf( stderr, "Error: %s: status file not writeable\n",
155 sglob->slurpd_status_file );
159 return ( err == 0 ? 0 : -1 );
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:
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.
175 * The calling routine is responsible for determining which, if any, of
176 * the returned flags is a problem for a particular file.
183 char dir[ MAXPATHLEN ];
185 unsigned int ret = 0;
188 p = strrchr( dir, '/' );
192 if ( access( dir, F_OK ) < 0 ) {
195 if ( access( dir, R_OK ) < 0 ) {
198 if ( access( dir, W_OK ) < 0 ) {
199 ret |= FC_DIRUNWRITE;
201 if ( access( f, F_OK ) < 0 ) {
204 if ( access( f, R_OK ) < 0 ) {
205 ret |= FC_FILEUNREAD;
207 if ( access( f, W_OK ) < 0 ) {
208 ret |= FC_FILEUNWRITE;