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