liblattutil.h 5.88 KB
Newer Older
Shawn Webb's avatar
Shawn Webb committed
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
/*-
 * Copyright (c) 2021 Shawn Webb <shawn.webb@hardenedbsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _LIBLATTUTIL_H
#define	_LIBLATTUTIL_H

#include <stdbool.h>
#include <sys/queue.h>
#include <sys/stat.h>
Shawn Webb's avatar
Shawn Webb committed
31
#include <stdarg.h>
Shawn Webb's avatar
Shawn Webb committed
32
33
34

#include <ucl.h>

Shawn Webb's avatar
Shawn Webb committed
35
36
struct _lllog;

Shawn Webb's avatar
Shawn Webb committed
37
38
#define	EXPORTED_SYM	__attribute__((visibility("default")))

Shawn Webb's avatar
Shawn Webb committed
39
40
41
42
43
44
#define LATTUTIL_LOG_VERBOSITY_DEFAULT	1000
#define LATTUTIL_LOG_DEFAULT_NAME	"lattutil"

typedef ssize_t (*log_cb)(struct _lllog *, int, const char *, ...);
typedef void (*log_close)(struct _lllog *);

Shawn Webb's avatar
Shawn Webb committed
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Though we export the underlying data structure, using the API is
 * preferred over directly accessing the ABI.
 */
typedef struct _llconfig {
	struct stat		 l_sb;
	char			*l_path;
	int			 l_fd;
	struct ucl_parser	*l_parser;
	const ucl_object_t	*l_rootobj;
} lattutil_config_path_t;

Shawn Webb's avatar
Shawn Webb committed
57
58
59
typedef struct _lllog {
	int		 ll_verbosity;
	char		*ll_path;
60
61

	log_cb		 ll_log_err;
Shawn Webb's avatar
Shawn Webb committed
62
	log_cb		 ll_log_info;
63
64
	log_cb		 ll_log_warn;

Shawn Webb's avatar
Shawn Webb committed
65
66
67
	log_close	 ll_log_close;
} lattutil_log_t;

Shawn Webb's avatar
Shawn Webb committed
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
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * Find a configuration file in a set of given paths
 *
 * This function iterates through the set of paths, looking for the
 * passed-in configuration file name. If the configuration file is
 * found, a UCL parser instance is created and the file is loaded by
 * UCL.
 *
 * @param Set of paths
 * @param Number of paths in set
 * @param Configuration file name
 * @return Pointer to struct that contains information about the
 * 	config file on success, NULL on error.
 */
lattutil_config_path_t *lattutil_find_config(const char **, size_t, const char *,
    int);

/**
 * Free a given lattutil_config_path_t object
 *
 * In order to prevent UAF vulnerabilities, the structure is zeroed
 * after all its members have been freed. Additionally, the pointer to
 * the structure is set to NULL.
 *
 * @param A pointer to the to-be-freed object
 */
void lattutil_free_config_path(lattutil_config_path_t **);

/**
 * Return the fully-qualified path of the config file
 *
 * @param A pointer to the lattutil_config_path_t object
 * @return The path of the config file
 */
char *lattutil_config_path_path(lattutil_config_path_t *);

/**
 * Return the file descriptor of the opened config
 *
 * @param A pointer to the lattutil_config_path_t object
 * @return The file descriptor
 */
int lattutil_config_path_fd(lattutil_config_path_t *);

/**
 * Look for a string-typed variable in a UCL configuration tree.
 *
 * @param UCL object root
 * @param Path within the UCL object root
 * @param Default value to set if path is not found
 * @return The value if found, default value or zero-length string on
 * error.
 */
char *lattutil_find_config_string(const ucl_object_t *, const char *,
    const char *);

/**
 * Look for an integer-typed variable in a UCL configuration tree.
 *
 * @param UCL object root
 * @param Path within the UCL object root
 * @param Default value to set if path is not found
 * @return The value if found, default value if not
 */
int64_t find_config_int(const ucl_object_t *, const char *, int64_t);

Shawn Webb's avatar
Shawn Webb committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * Initialize logger
 *
 * @param Path
 * @param Verbosity
 * @return Pointer to the logging object
 */
lattutil_log_t *lattutil_log_init(char *, int);

/**
 * Free logger
 *
 * The underlying variable is set to NULL.
 *
 * @param Double pointer to the logging object
 */
void lattutil_log_free(lattutil_log_t **);

/**
 * Initialize syslog-based logging
 *
 * @param Logging object
 * @param syslog(3) logopt
 * @param syslog(3) facility
 * @return True on success, False otherwise
 */
bool lattutil_log_syslog_init(lattutil_log_t *, int, int);

Shawn Webb's avatar
Shawn Webb committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * Initialize NULL logging
 *
 * This is the default logger. It simply discards all logging
 * messages.
 *
 * @param Logging object
 */
void lattutil_log_dummy_init(lattutil_log_t *);

/**
 * Determine if the logging subsystem is ready to receive messages
 *
 * @param Pointer to the logging object
 */
bool lattutil_log_ready(lattutil_log_t *);

/**
 * Get the configured minimum verbosity level
 *
 * @param Logging object
 * @return Verbosity level
 */
int lattutil_log_verbosity(lattutil_log_t *);

int lattutil_log_set_verbosity(lattutil_log_t *, int);

Shawn Webb's avatar
Shawn Webb committed
189
#ifdef _lattutil_internal
190
191
ssize_t lattutil_log_syslog_err(lattutil_log_t *, int,
    const char *, ...);
Shawn Webb's avatar
Shawn Webb committed
192
193
ssize_t lattutil_log_syslog_info(lattutil_log_t *, int,
    const char *, ...);
194
195
ssize_t lattutil_log_syslog_warn(lattutil_log_t *, int,
    const char *, ...);
Shawn Webb's avatar
Shawn Webb committed
196
void lattutil_log_syslog_close(lattutil_log_t *);
Shawn Webb's avatar
Shawn Webb committed
197
198
199
200
201
202
203
204

ssize_t lattutil_log_dummy_err(lattutil_log_t *, int,
    const char *, ...);
ssize_t lattutil_log_dummy_info(lattutil_log_t *, int,
    const char *, ...);
ssize_t lattutil_log_dummy_warn(lattutil_log_t *, int,
    const char *, ...);
void lattutil_log_dummy_close(lattutil_log_t *);
Shawn Webb's avatar
Shawn Webb committed
205
206
#endif /* _lattutil_internal */

Shawn Webb's avatar
Shawn Webb committed
207
#endif /* !_LIBLATTUTIL_H */