xref: /freebsd/contrib/ntp/include/ntp_stdlib.h (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*
2  * ntp_stdlib.h - Prototypes for NTP lib.
3  */
4 #ifndef NTP_STDLIB_H
5 #define NTP_STDLIB_H
6 
7 #include <sys/types.h>
8 #ifdef HAVE_SYS_SOCKET_H
9 #include <sys/socket.h>
10 #endif
11 
12 #include "declcond.h"	/* ntpd uses ntpd/declcond.h, others include/ */
13 #include "l_stdlib.h"
14 #include "ntp_md5.h"
15 #include "ntp_net.h"
16 #include "ntp_debug.h"
17 #include "ntp_malloc.h"
18 #include "ntp_string.h"
19 #include "ntp_syslog.h"
20 #include "ntp_keyacc.h"
21 
22 #ifndef PATH_MAX
23 # define PATH_MAX MAX_PATH
24 #endif
25 
26 #ifdef __GNUC__
27 #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
28 #else
29 #define NTP_PRINTF(fmt, args)
30 #endif
31 
32 extern	int	mprintf(const char *, ...) NTP_PRINTF(1, 2);
33 extern	int	mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3);
34 extern	int	mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0);
35 extern	int	mvsnprintf(char *, size_t, const char *, va_list)
36 			NTP_PRINTF(3, 0);
37 extern	int	msnprintf(char *, size_t, const char *, ...)
38 			NTP_PRINTF(3, 4);
39 extern	void	msyslog(int, const char *, ...) NTP_PRINTF(2, 3);
40 extern	void	mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0);
41 extern	void	init_logging	(const char *, u_int32, int);
42 extern	int	change_logfile	(const char *, int);
43 extern	void	setup_logfile	(const char *);
44 #ifndef errno_to_str		/* Windows port defines this */
45 extern	void	errno_to_str(int, char *, size_t);
46 #endif
47 
48 extern	char *	ntp_realpath(const char *fsname);
49 
50 extern	int	xvsbprintf(char **, char * const, char const *, va_list)
51 				NTP_PRINTF(3, 0);
52 extern	int	xsbprintf(char **, char * const, char const *, ...)
53 				NTP_PRINTF(3, 4);
54 
55 #define SAVE_ERRNO(stmt)				\
56 	{						\
57 		int preserved_errno;			\
58 							\
59 		preserved_errno = socket_errno();	\
60 		{					\
61 			stmt				\
62 		}					\
63 		errno = preserved_errno;		\
64 	}
65 
66 typedef void (*ctrl_c_fn)(void);
67 
68 /* authkeys.c */
69 #define AUTHPWD_MAXSECLEN	64	/* max. length of secret blob */
70 
71 enum AuthPwdEnc {
72 	AUTHPWD_UNSPEC,	/* format unspecified, length used for discrimination */
73 	AUTHPWD_PLAIN,	/* plain text, used as is */
74 	AUTHPWD_HEX	/* hex-encoded string */
75 };
76 
77 extern	void	auth_delkeys	(void);
78 extern	int	auth_havekey	(keyid_t);
79 extern	size_t	authdecodepw	(u_char *dst, size_t dstlen, const char *src, enum AuthPwdEnc);
80 extern	int	authdecrypt	(keyid_t, u_int32 *, size_t, size_t);
81 extern	size_t	authencrypt	(keyid_t, u_int32 *, size_t);
82 extern	int	authhavekey	(keyid_t);
83 extern	int	authistrusted	(keyid_t);
84 extern	int	authistrustedip	(keyid_t, sockaddr_u *);
85 extern	int	authreadkeys	(const char *);
86 extern	void	authtrust	(keyid_t, u_long);
87 extern	int	authusekey	(keyid_t, int, const u_char *);
88 
89 /*
90  * Based on the NTP timestamp, calculate the NTP timestamp of
91  * the corresponding calendar unit. Use the pivot time to unfold
92  * the NTP timestamp properly, or the current system time if the
93  * pivot pointer is NULL.
94  */
95 extern	u_int32	calyearstart	(u_int32 ntptime, const time_t *pivot);
96 extern	u_int32	calmonthstart	(u_int32 ntptime, const time_t *pivot);
97 extern	u_int32	calweekstart	(u_int32 ntptime, const time_t *pivot);
98 extern	u_int32	caldaystart	(u_int32 ntptime, const time_t *pivot);
99 
100 extern	const char *clockname	(int);
101 extern	int	clocktime	(int, int, int, int, int, u_int32, u_long *, u_int32 *);
102 extern	int	ntp_getopt	(int, char **, const char *);
103 extern	void	init_auth	(void);
104 extern	void	init_lib	(void);
105 extern	struct savekey *auth_findkey (keyid_t);
106 extern	void	auth_moremem	(int);
107 extern	void	auth_prealloc_symkeys(int);
108 extern	int	ymd2yd		(int, int, int);
109 
110 /* a_md5encrypt.c */
111 extern	size_t	MD5authencrypt	(int type, const u_char *key, size_t klen,
112 				 u_int32 *pkt, size_t length);
113 extern	int	MD5authdecrypt	(int type, const u_char *key, size_t klen,
114 				 u_int32 *pkt, size_t length, size_t size,
115 				 keyid_t keyno);
116 extern	u_int32	addr2refid(sockaddr_u *);
117 
118 /* authkeys.c */
119 extern	void	MD5auth_setkey	(keyid_t, int, const u_char *, size_t,
120 				 KeyAccT *c);
121 
122 /* emalloc.c */
123 #ifndef EREALLOC_CALLSITE	/* ntp_malloc.h defines */
124 extern	void *	ereallocz	(void *, size_t, size_t, int);
125 extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size, size_t extra);
126 #define	erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z))
127 #define	emalloc(n)		ereallocz(NULL, (n), 0, FALSE)
128 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE)
129 #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE)
130 #define erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE)
131 #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0)
132 #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0)
133 #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x))
134 #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x))
135 extern	char *	estrdup_impl(const char *);
136 #define	estrdup(s)		estrdup_impl(s)
137 #else
138 extern	void *	ereallocz	(void *, size_t, size_t, int,
139 				 const char *, int);
140 extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size,
141 				 size_t extra, const char *, int);
142 #define erealloczsite		ereallocz
143 #define	emalloc(c)		ereallocz(NULL, (c), 0, FALSE, \
144 					  __FILE__, __LINE__)
145 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE, \
146 					  __FILE__, __LINE__)
147 #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE, \
148 					  __FILE__, __LINE__)
149 #define	erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE, \
150 					  __FILE__, __LINE__)
151 #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0, \
152 					  __FILE__, __LINE__)
153 #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0, \
154 					  __FILE__, __LINE__)
155 #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x), \
156 					  __FILE__, __LINE__)
157 #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x), \
158 					  __FILE__, __LINE__)
159 extern	char *	estrdup_impl(const char *, const char *, int);
160 #define	estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
161 #endif
162 
163 
164 extern	int	atoint		(const char *, long *);
165 extern	int	atouint		(const char *, u_long *);
166 extern	int	hextoint	(const char *, u_long *);
167 extern	const char *	humanlogtime	(void);
168 extern	const char *	humantime	(time_t);
169 extern int	is_ip_address	(const char *, u_short, sockaddr_u *);
170 extern	char *	mfptoa		(u_int32, u_int32, short);
171 extern	char *	mfptoms		(u_int32, u_int32, short);
172 extern	const char * modetoa	(size_t);
173 extern	const char * eventstr	(int);
174 extern	const char * ceventstr	(int);
175 extern	const char * res_match_flags(u_short);
176 extern	const char * res_access_flags(u_int32);
177 #ifdef KERNEL_PLL
178 extern	const char * k_st_flags	(u_int32);
179 #endif
180 extern	char *	statustoa	(int, int);
181 extern	sockaddr_u * netof	(sockaddr_u *);
182 extern	char *	numtoa		(u_int32);
183 extern	char *	numtohost	(u_int32);
184 extern	const char * socktoa	(const sockaddr_u *);
185 extern	const char * sockporttoa(const sockaddr_u *);
186 extern	u_short	sock_hash	(const sockaddr_u *);
187 extern	int	sockaddr_masktoprefixlen(const sockaddr_u *);
188 extern	const char * socktohost	(const sockaddr_u *);
189 extern	int	octtoint	(const char *, u_long *);
190 extern	u_long	ranp2		(int);
191 extern	const char *refnumtoa	(const sockaddr_u *);
192 extern	const char *refid_str	(u_int32, int);
193 
194 extern	int	decodenetnum	(const char *, sockaddr_u *);
195 
196 extern	const char * FindConfig	(const char *);
197 
198 extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
199 extern	void	set_ctrl_c_hook (ctrl_c_fn);
200 
201 extern	void	getauthkeys 	(const char *);
202 extern	void	auth_agekeys	(void);
203 extern	void	rereadkeys	(void);
204 
205 /*
206  * Variable declarations for libntp.
207  */
208 
209 /* authkeys.c */
210 extern u_long	authkeynotfound;	/* keys not found */
211 extern u_long	authkeylookups;		/* calls to lookup keys */
212 extern u_long	authnumkeys;		/* number of active keys */
213 extern u_long	authkeyexpired;		/* key lifetime expirations */
214 extern u_long	authkeyuncached;	/* cache misses */
215 extern u_long	authencryptions;	/* calls to encrypt */
216 extern u_long	authdecryptions;	/* calls to decrypt */
217 
218 extern int	authnumfreekeys;
219 
220 /*
221  * The key cache. We cache the last key we looked at here.
222  */
223 extern keyid_t	cache_keyid;		/* key identifier */
224 extern int	cache_type;		/* key type */
225 extern u_char *	cache_secret;		/* secret */
226 extern size_t	cache_secretsize;	/* secret octets */
227 extern u_short	cache_flags;		/* KEY_ bit flags */
228 
229 /* getopt.c */
230 extern char *	ntp_optarg;		/* global argument pointer */
231 extern int	ntp_optind;		/* global argv index */
232 
233 /* lib_strbuf.c */
234 extern int	ipv4_works;
235 extern int	ipv6_works;
236 
237 /* machines.c */
238 typedef void (*pset_tod_using)(const char *);
239 extern pset_tod_using	set_tod_using;
240 
241 /* ssl_init.c */
242 #ifdef OPENSSL
243 extern	void	ssl_init		(void);
244 extern	void	ssl_check_version	(void);
245 extern	int	ssl_init_done;
246 #define	INIT_SSL()				\
247 	do {					\
248 		if (!ssl_init_done)		\
249 			ssl_init();		\
250 	} while (0)
251 #else	/* !OPENSSL follows */
252 #define	INIT_SSL()		do {} while (0)
253 #endif
254 extern	int	keytype_from_text	(const char *,	size_t *);
255 extern	const char *keytype_name	(int);
256 extern	char *	getpass_keytype		(int);
257 
258 /* strl-obsd.c */
259 #ifndef HAVE_STRLCPY		/* + */
260 /*
261  * Copy src to string dst of size siz.  At most siz-1 characters
262  * will be copied.  Always NUL terminates (unless siz == 0).
263  * Returns strlen(src); if retval >= siz, truncation occurred.
264  */
265 extern	size_t	strlcpy(char *dst, const char *src, size_t siz);
266 #endif
267 #ifndef HAVE_STRLCAT		/* + */
268 /*
269  * Appends src to string dst of size siz (unlike strncat, siz is the
270  * full size of dst, not space left).  At most siz-1 characters
271  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
272  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
273  * If retval >= siz, truncation occurred.
274  */
275 extern	size_t	strlcat(char *dst, const char *src, size_t siz);
276 #endif
277 
278 
279 
280 /* lib/isc/win32/strerror.c
281  *
282  * To minimize Windows-specific changes to the rest of the NTP code,
283  * particularly reference clocks, we hijack calls to strerror() to deal
284  * with our mixture of error codes from the  C runtime (open, write)
285  * and Windows (sockets, serial ports).  This is an ugly hack because
286  * both use the lowest values differently, but particularly for ntpd,
287  * it's not a problem.
288  */
289 #ifdef NTP_REDEFINE_STRERROR
290 #define	strerror(e)	ntp_strerror(e)
291 extern char *	ntp_strerror	(int e);
292 #endif
293 
294 /* systime.c */
295 extern double	sys_tick;		/* tick size or time to read */
296 extern double	measured_tick;		/* non-overridable sys_tick */
297 extern double	sys_fuzz;		/* min clock read latency */
298 extern int	trunc_os_clock;		/* sys_tick > measured_tick */
299 
300 /* version.c */
301 extern const char *Version;		/* version declaration */
302 
303 #endif	/* NTP_STDLIB_H */
304