summaryrefslogtreecommitdiffstats
path: root/servers/slapd/slaptest.c
blob: 0fcb10e139acadad258f8a6dff144f7f706c1d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2004-2018 The OpenLDAP Foundation.
 * Portions Copyright 2004 Pierangelo Masarati.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion
 * in OpenLDAP Software.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/stdlib.h>

#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ac/unistd.h>
#include <ac/errno.h>

#include <lber.h>
#include <ldif.h>
#include <lutil.h>

#include "slapcommon.h"

#ifndef S_IWRITE
#define S_IWRITE	S_IWUSR
#endif

static int
test_file( const char *fname, const char *ftype )
{
	struct stat	st;
	int		save_errno;

	switch ( stat( fname, &st ) ) {
	case 0:
		if ( !( st.st_mode & S_IWRITE ) ) {
			Debug( LDAP_DEBUG_ANY, "%s file "
				"\"%s\" exists, but user does not have access\n",
				ftype, fname, 0 );
			return -1;
		}
		break;

	case -1:
	default:
		save_errno = errno;
		if ( save_errno == ENOENT ) {
			FILE		*fp = fopen( fname, "w" );

			if ( fp == NULL ) {
				save_errno = errno;

				Debug( LDAP_DEBUG_ANY, "unable to open file "
					"\"%s\": %d (%s)\n",
					fname,
					save_errno, strerror( save_errno ) );

				return -1;
			}
			fclose( fp );
			unlink( fname );
			break;
		}

		Debug( LDAP_DEBUG_ANY, "unable to stat file "
			"\"%s\": %d (%s)\n",
			slapd_pid_file,
			save_errno, strerror( save_errno ) );
		return -1;
	}

	return 0;
}

int
slaptest( int argc, char **argv )
{
	int			rc = EXIT_SUCCESS;
	const char		*progname = "slaptest";

	slap_tool_init( progname, SLAPTEST, argc, argv );

	if ( slapd_pid_file != NULL ) {
		if ( test_file( slapd_pid_file, "pid" ) ) {
			return EXIT_FAILURE;
		}
	}

	if ( slapd_args_file != NULL ) {
		if ( test_file( slapd_args_file, "args" ) ) {
			return EXIT_FAILURE;
		}
	}

	if ( !quiet ) {
		fprintf( stderr, "config file testing succeeded\n");
	}

	if ( slap_tool_destroy())
		rc = EXIT_FAILURE;

	return rc;
}