xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/slaptest.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: slaptest.c,v 1.3 2021/08/14 16:14:58 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2004-2021 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 <sys/cdefs.h>
24 __RCSID("$NetBSD: slaptest.c,v 1.3 2021/08/14 16:14:58 christos Exp $");
25 
26 #include "portable.h"
27 
28 #include <stdio.h>
29 
30 #include <ac/stdlib.h>
31 
32 #include <ac/ctype.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <ac/unistd.h>
38 #include <ac/errno.h>
39 
40 #include <lber.h>
41 #include <ldif.h>
42 #include <lutil.h>
43 
44 #include "slapcommon.h"
45 
46 #ifndef S_IWRITE
47 #define S_IWRITE	S_IWUSR
48 #endif
49 
50 static int
test_file(const char * fname,const char * ftype)51 test_file( const char *fname, const char *ftype )
52 {
53 	struct stat	st;
54 	char ebuf[128];
55 	int		save_errno;
56 
57 	switch ( stat( fname, &st ) ) {
58 	case 0:
59 		if ( !( st.st_mode & S_IWRITE ) ) {
60 			Debug( LDAP_DEBUG_ANY, "%s file "
61 				"\"%s\" exists, but user does not have access\n",
62 				ftype, fname );
63 			return -1;
64 		}
65 		break;
66 
67 	case -1:
68 	default:
69 		save_errno = errno;
70 		if ( save_errno == ENOENT ) {
71 			FILE		*fp = fopen( fname, "w" );
72 
73 			if ( fp == NULL ) {
74 				save_errno = errno;
75 
76 				Debug( LDAP_DEBUG_ANY, "unable to open file "
77 					"\"%s\": %d (%s)\n",
78 					fname,
79 					save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) );
80 
81 				return -1;
82 			}
83 			fclose( fp );
84 			unlink( fname );
85 			break;
86 		}
87 
88 		Debug( LDAP_DEBUG_ANY, "unable to stat file "
89 			"\"%s\": %d (%s)\n",
90 			slapd_pid_file,
91 			save_errno, AC_STRERROR_R( save_errno, ebuf, sizeof(ebuf) ) );
92 		return -1;
93 	}
94 
95 	return 0;
96 }
97 
98 int
slaptest(int argc,char ** argv)99 slaptest( int argc, char **argv )
100 {
101 	int			rc = EXIT_SUCCESS;
102 	const char		*progname = "slaptest";
103 
104 	slap_tool_init( progname, SLAPTEST, argc, argv );
105 
106 	if ( slapd_pid_file != NULL ) {
107 		if ( test_file( slapd_pid_file, "pid" ) ) {
108 			return EXIT_FAILURE;
109 		}
110 	}
111 
112 	if ( slapd_args_file != NULL ) {
113 		if ( test_file( slapd_args_file, "args" ) ) {
114 			return EXIT_FAILURE;
115 		}
116 	}
117 
118 	if ( !quiet ) {
119 		fprintf( stderr, "config file testing succeeded\n");
120 	}
121 
122 	if ( slap_tool_destroy())
123 		rc = EXIT_FAILURE;
124 
125 	return rc;
126 }
127