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