xref: /freebsd/contrib/ntp/include/ntp_stdlib.h (revision e6bfd18d)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * ntp_stdlib.h - Prototypes for NTP lib.
3c0b746e5SOllivier Robert  */
42b15cb3dSCy Schubert #ifndef NTP_STDLIB_H
52b15cb3dSCy Schubert #define NTP_STDLIB_H
6c0b746e5SOllivier Robert 
72b15cb3dSCy Schubert #include <sys/types.h>
82b15cb3dSCy Schubert #ifdef HAVE_SYS_SOCKET_H
92b15cb3dSCy Schubert #include <sys/socket.h>
102b15cb3dSCy Schubert #endif
112b15cb3dSCy Schubert 
122b15cb3dSCy Schubert #include "declcond.h"	/* ntpd uses ntpd/declcond.h, others include/ */
13c0b746e5SOllivier Robert #include "l_stdlib.h"
14e6bfd18dSCy Schubert #include "ntp_md5.h"
152b15cb3dSCy Schubert #include "ntp_net.h"
162b15cb3dSCy Schubert #include "ntp_debug.h"
172b15cb3dSCy Schubert #include "ntp_malloc.h"
182b15cb3dSCy Schubert #include "ntp_string.h"
192b15cb3dSCy Schubert #include "ntp_syslog.h"
2068ba7e87SXin LI #include "ntp_keyacc.h"
212b15cb3dSCy Schubert 
22e6bfd18dSCy Schubert #ifndef PATH_MAX
23e6bfd18dSCy Schubert # define PATH_MAX MAX_PATH
24e6bfd18dSCy Schubert #endif
25e6bfd18dSCy Schubert 
262b15cb3dSCy Schubert #ifdef __GNUC__
272b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
282b15cb3dSCy Schubert #else
292b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args)
302b15cb3dSCy Schubert #endif
312b15cb3dSCy Schubert 
322b15cb3dSCy Schubert extern	int	mprintf(const char *, ...) NTP_PRINTF(1, 2);
332b15cb3dSCy Schubert extern	int	mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3);
342b15cb3dSCy Schubert extern	int	mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0);
352b15cb3dSCy Schubert extern	int	mvsnprintf(char *, size_t, const char *, va_list)
362b15cb3dSCy Schubert 			NTP_PRINTF(3, 0);
372b15cb3dSCy Schubert extern	int	msnprintf(char *, size_t, const char *, ...)
382b15cb3dSCy Schubert 			NTP_PRINTF(3, 4);
392b15cb3dSCy Schubert extern	void	msyslog(int, const char *, ...) NTP_PRINTF(2, 3);
409034852cSGleb Smirnoff extern	void	mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0);
412b15cb3dSCy Schubert extern	void	init_logging	(const char *, u_int32, int);
422b15cb3dSCy Schubert extern	int	change_logfile	(const char *, int);
432b15cb3dSCy Schubert extern	void	setup_logfile	(const char *);
44e6bfd18dSCy Schubert #ifndef errno_to_str		/* Windows port defines this */
452b15cb3dSCy Schubert extern	void	errno_to_str(int, char *, size_t);
462b15cb3dSCy Schubert #endif
47c0b746e5SOllivier Robert 
48a466cc55SCy Schubert extern	char *	ntp_realpath(const char *fsname);
49a466cc55SCy Schubert 
50e6bfd18dSCy Schubert extern	int	xvsbprintf(char **, char * const, char const *, va_list)
51e6bfd18dSCy Schubert 				NTP_PRINTF(3, 0);
52e6bfd18dSCy Schubert extern	int	xsbprintf(char **, char * const, char const *, ...)
53e6bfd18dSCy Schubert 				NTP_PRINTF(3, 4);
54c0b746e5SOllivier Robert 
552b15cb3dSCy Schubert #define SAVE_ERRNO(stmt)				\
562b15cb3dSCy Schubert 	{						\
572b15cb3dSCy Schubert 		int preserved_errno;			\
582b15cb3dSCy Schubert 							\
592b15cb3dSCy Schubert 		preserved_errno = socket_errno();	\
602b15cb3dSCy Schubert 		{					\
612b15cb3dSCy Schubert 			stmt				\
622b15cb3dSCy Schubert 		}					\
632b15cb3dSCy Schubert 		errno = preserved_errno;		\
642b15cb3dSCy Schubert 	}
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert typedef void (*ctrl_c_fn)(void);
672b15cb3dSCy Schubert 
682b15cb3dSCy Schubert /* authkeys.c */
69a466cc55SCy Schubert #define AUTHPWD_MAXSECLEN	64	/* max. length of secret blob */
70a466cc55SCy Schubert 
71a466cc55SCy Schubert enum AuthPwdEnc {
72a466cc55SCy Schubert 	AUTHPWD_UNSPEC,	/* format unspecified, length used for discrimination */
73a466cc55SCy Schubert 	AUTHPWD_PLAIN,	/* plain text, used as is */
74a466cc55SCy Schubert 	AUTHPWD_HEX	/* hex-encoded string */
75a466cc55SCy Schubert };
76a466cc55SCy Schubert 
772b15cb3dSCy Schubert extern	void	auth_delkeys	(void);
782b15cb3dSCy Schubert extern	int	auth_havekey	(keyid_t);
79a466cc55SCy Schubert extern	size_t	authdecodepw	(u_char *dst, size_t dstlen, const char *src, enum AuthPwdEnc);
803311ff84SXin LI extern	int	authdecrypt	(keyid_t, u_int32 *, size_t, size_t);
813311ff84SXin LI extern	size_t	authencrypt	(keyid_t, u_int32 *, size_t);
822b15cb3dSCy Schubert extern	int	authhavekey	(keyid_t);
832b15cb3dSCy Schubert extern	int	authistrusted	(keyid_t);
8468ba7e87SXin LI extern	int	authistrustedip	(keyid_t, sockaddr_u *);
852b15cb3dSCy Schubert extern	int	authreadkeys	(const char *);
862b15cb3dSCy Schubert extern	void	authtrust	(keyid_t, u_long);
872b15cb3dSCy Schubert extern	int	authusekey	(keyid_t, int, const u_char *);
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert /*
902b15cb3dSCy Schubert  * Based on the NTP timestamp, calculate the NTP timestamp of
912b15cb3dSCy Schubert  * the corresponding calendar unit. Use the pivot time to unfold
922b15cb3dSCy Schubert  * the NTP timestamp properly, or the current system time if the
932b15cb3dSCy Schubert  * pivot pointer is NULL.
942b15cb3dSCy Schubert  */
952b15cb3dSCy Schubert extern	u_int32	calyearstart	(u_int32 ntptime, const time_t *pivot);
962b15cb3dSCy Schubert extern	u_int32	calmonthstart	(u_int32 ntptime, const time_t *pivot);
972b15cb3dSCy Schubert extern	u_int32	calweekstart	(u_int32 ntptime, const time_t *pivot);
982b15cb3dSCy Schubert extern	u_int32	caldaystart	(u_int32 ntptime, const time_t *pivot);
992b15cb3dSCy Schubert 
1002b15cb3dSCy Schubert extern	const char *clockname	(int);
1012b15cb3dSCy Schubert extern	int	clocktime	(int, int, int, int, int, u_int32, u_long *, u_int32 *);
1022b15cb3dSCy Schubert extern	int	ntp_getopt	(int, char **, const char *);
1032b15cb3dSCy Schubert extern	void	init_auth	(void);
1042b15cb3dSCy Schubert extern	void	init_lib	(void);
1052b15cb3dSCy Schubert extern	struct savekey *auth_findkey (keyid_t);
1062b15cb3dSCy Schubert extern	void	auth_moremem	(int);
1072b15cb3dSCy Schubert extern	void	auth_prealloc_symkeys(int);
1082b15cb3dSCy Schubert extern	int	ymd2yd		(int, int, int);
1092b15cb3dSCy Schubert 
1102b15cb3dSCy Schubert /* a_md5encrypt.c */
111e6bfd18dSCy Schubert extern	size_t	MD5authencrypt	(int type, const u_char *key, size_t klen,
112e6bfd18dSCy Schubert 				 u_int32 *pkt, size_t length);
113e6bfd18dSCy Schubert extern	int	MD5authdecrypt	(int type, const u_char *key, size_t klen,
114e6bfd18dSCy Schubert 				 u_int32 *pkt, size_t length, size_t size,
115e6bfd18dSCy Schubert 				 keyid_t keyno);
1162b15cb3dSCy Schubert extern	u_int32	addr2refid(sockaddr_u *);
1172b15cb3dSCy Schubert 
118e6bfd18dSCy Schubert /* authkeys.c */
119e6bfd18dSCy Schubert extern	void	MD5auth_setkey	(keyid_t, int, const u_char *, size_t,
120e6bfd18dSCy Schubert 				 KeyAccT *c);
121e6bfd18dSCy Schubert 
1222b15cb3dSCy Schubert /* emalloc.c */
1232b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE	/* ntp_malloc.h defines */
1242b15cb3dSCy Schubert extern	void *	ereallocz	(void *, size_t, size_t, int);
125f0574f5cSXin LI extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size, size_t extra);
126276da39aSCy Schubert #define	erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z))
127276da39aSCy Schubert #define	emalloc(n)		ereallocz(NULL, (n), 0, FALSE)
1282b15cb3dSCy Schubert #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE)
129276da39aSCy Schubert #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE)
130276da39aSCy Schubert #define erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE)
131f0574f5cSXin LI #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0)
132f0574f5cSXin LI #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0)
133f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x))
134f0574f5cSXin LI #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x))
1352b15cb3dSCy Schubert extern	char *	estrdup_impl(const char *);
1362b15cb3dSCy Schubert #define	estrdup(s)		estrdup_impl(s)
137c0b746e5SOllivier Robert #else
1382b15cb3dSCy Schubert extern	void *	ereallocz	(void *, size_t, size_t, int,
1392b15cb3dSCy Schubert 				 const char *, int);
140f0574f5cSXin LI extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size,
141f0574f5cSXin LI 				 size_t extra, const char *, int);
1422b15cb3dSCy Schubert #define erealloczsite		ereallocz
1432b15cb3dSCy Schubert #define	emalloc(c)		ereallocz(NULL, (c), 0, FALSE, \
1442b15cb3dSCy Schubert 					  __FILE__, __LINE__)
1452b15cb3dSCy Schubert #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE, \
1462b15cb3dSCy Schubert 					  __FILE__, __LINE__)
147276da39aSCy Schubert #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE, \
1482b15cb3dSCy Schubert 					  __FILE__, __LINE__)
149276da39aSCy Schubert #define	erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE, \
150276da39aSCy Schubert 					  __FILE__, __LINE__)
151f0574f5cSXin LI #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0, \
152276da39aSCy Schubert 					  __FILE__, __LINE__)
153f0574f5cSXin LI #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0, \
154f0574f5cSXin LI 					  __FILE__, __LINE__)
155f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x), \
156f0574f5cSXin LI 					  __FILE__, __LINE__)
157f0574f5cSXin LI #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x), \
1582b15cb3dSCy Schubert 					  __FILE__, __LINE__)
1592b15cb3dSCy Schubert extern	char *	estrdup_impl(const char *, const char *, int);
1602b15cb3dSCy Schubert #define	estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
161c0b746e5SOllivier Robert #endif
162c0b746e5SOllivier Robert 
163c0b746e5SOllivier Robert 
1642b15cb3dSCy Schubert extern	int	atoint		(const char *, long *);
1652b15cb3dSCy Schubert extern	int	atouint		(const char *, u_long *);
1662b15cb3dSCy Schubert extern	int	hextoint	(const char *, u_long *);
1672b15cb3dSCy Schubert extern	const char *	humanlogtime	(void);
1682b15cb3dSCy Schubert extern	const char *	humantime	(time_t);
16968ba7e87SXin LI extern int	is_ip_address	(const char *, u_short, sockaddr_u *);
1702b15cb3dSCy Schubert extern	char *	mfptoa		(u_int32, u_int32, short);
1712b15cb3dSCy Schubert extern	char *	mfptoms		(u_int32, u_int32, short);
1722b15cb3dSCy Schubert extern	const char * modetoa	(size_t);
1732b15cb3dSCy Schubert extern	const char * eventstr	(int);
1742b15cb3dSCy Schubert extern	const char * ceventstr	(int);
1752b15cb3dSCy Schubert extern	const char * res_match_flags(u_short);
1762d4e511cSCy Schubert extern	const char * res_access_flags(u_int32);
1772b15cb3dSCy Schubert #ifdef KERNEL_PLL
1782b15cb3dSCy Schubert extern	const char * k_st_flags	(u_int32);
179c0b746e5SOllivier Robert #endif
1802b15cb3dSCy Schubert extern	char *	statustoa	(int, int);
1812b15cb3dSCy Schubert extern	sockaddr_u * netof	(sockaddr_u *);
1822b15cb3dSCy Schubert extern	char *	numtoa		(u_int32);
1832b15cb3dSCy Schubert extern	char *	numtohost	(u_int32);
1842b15cb3dSCy Schubert extern	const char * socktoa	(const sockaddr_u *);
1852b15cb3dSCy Schubert extern	const char * sockporttoa(const sockaddr_u *);
1862b15cb3dSCy Schubert extern	u_short	sock_hash	(const sockaddr_u *);
1872b15cb3dSCy Schubert extern	int	sockaddr_masktoprefixlen(const sockaddr_u *);
1882b15cb3dSCy Schubert extern	const char * socktohost	(const sockaddr_u *);
1892b15cb3dSCy Schubert extern	int	octtoint	(const char *, u_long *);
1902b15cb3dSCy Schubert extern	u_long	ranp2		(int);
191a466cc55SCy Schubert extern	const char *refnumtoa	(const sockaddr_u *);
1922b15cb3dSCy Schubert extern	const char *refid_str	(u_int32, int);
193c0b746e5SOllivier Robert 
1942b15cb3dSCy Schubert extern	int	decodenetnum	(const char *, sockaddr_u *);
195c0b746e5SOllivier Robert 
1962b15cb3dSCy Schubert extern	const char * FindConfig	(const char *);
197c0b746e5SOllivier Robert 
1982b15cb3dSCy Schubert extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
1992b15cb3dSCy Schubert extern	void	set_ctrl_c_hook (ctrl_c_fn);
200c0b746e5SOllivier Robert 
2012b15cb3dSCy Schubert extern	void	getauthkeys 	(const char *);
2022b15cb3dSCy Schubert extern	void	auth_agekeys	(void);
2032b15cb3dSCy Schubert extern	void	rereadkeys	(void);
204c0b746e5SOllivier Robert 
205c0b746e5SOllivier Robert /*
206c0b746e5SOllivier Robert  * Variable declarations for libntp.
207c0b746e5SOllivier Robert  */
208c0b746e5SOllivier Robert 
209c0b746e5SOllivier Robert /* authkeys.c */
210c0b746e5SOllivier Robert extern u_long	authkeynotfound;	/* keys not found */
211c0b746e5SOllivier Robert extern u_long	authkeylookups;		/* calls to lookup keys */
212c0b746e5SOllivier Robert extern u_long	authnumkeys;		/* number of active keys */
213c0b746e5SOllivier Robert extern u_long	authkeyexpired;		/* key lifetime expirations */
214c0b746e5SOllivier Robert extern u_long	authkeyuncached;	/* cache misses */
215c0b746e5SOllivier Robert extern u_long	authencryptions;	/* calls to encrypt */
216c0b746e5SOllivier Robert extern u_long	authdecryptions;	/* calls to decrypt */
217c0b746e5SOllivier Robert 
218c0b746e5SOllivier Robert extern int	authnumfreekeys;
219c0b746e5SOllivier Robert 
220c0b746e5SOllivier Robert /*
221c0b746e5SOllivier Robert  * The key cache. We cache the last key we looked at here.
222c0b746e5SOllivier Robert  */
223224ba2bdSOllivier Robert extern keyid_t	cache_keyid;		/* key identifier */
2242b15cb3dSCy Schubert extern int	cache_type;		/* key type */
2252b15cb3dSCy Schubert extern u_char *	cache_secret;		/* secret */
2264990d495SXin LI extern size_t	cache_secretsize;	/* secret octets */
2272b15cb3dSCy Schubert extern u_short	cache_flags;		/* KEY_ bit flags */
228c0b746e5SOllivier Robert 
229c0b746e5SOllivier Robert /* getopt.c */
230c0b746e5SOllivier Robert extern char *	ntp_optarg;		/* global argument pointer */
231c0b746e5SOllivier Robert extern int	ntp_optind;		/* global argv index */
232c0b746e5SOllivier Robert 
2332b15cb3dSCy Schubert /* lib_strbuf.c */
2342b15cb3dSCy Schubert extern int	ipv4_works;
2352b15cb3dSCy Schubert extern int	ipv6_works;
236c0b746e5SOllivier Robert 
2372b15cb3dSCy Schubert /* machines.c */
2382b15cb3dSCy Schubert typedef void (*pset_tod_using)(const char *);
2392b15cb3dSCy Schubert extern pset_tod_using	set_tod_using;
2402b15cb3dSCy Schubert 
2412b15cb3dSCy Schubert /* ssl_init.c */
2422b15cb3dSCy Schubert #ifdef OPENSSL
2432b15cb3dSCy Schubert extern	void	ssl_init		(void);
2442b15cb3dSCy Schubert extern	void	ssl_check_version	(void);
2452b15cb3dSCy Schubert extern	int	ssl_init_done;
2462b15cb3dSCy Schubert #define	INIT_SSL()				\
2472b15cb3dSCy Schubert 	do {					\
2482b15cb3dSCy Schubert 		if (!ssl_init_done)		\
2492b15cb3dSCy Schubert 			ssl_init();		\
2502b15cb3dSCy Schubert 	} while (0)
2512b15cb3dSCy Schubert #else	/* !OPENSSL follows */
2522b15cb3dSCy Schubert #define	INIT_SSL()		do {} while (0)
2532b15cb3dSCy Schubert #endif
2542b15cb3dSCy Schubert extern	int	keytype_from_text	(const char *,	size_t *);
2552b15cb3dSCy Schubert extern	const char *keytype_name	(int);
2562b15cb3dSCy Schubert extern	char *	getpass_keytype		(int);
2572b15cb3dSCy Schubert 
2582b15cb3dSCy Schubert /* strl-obsd.c */
2592b15cb3dSCy Schubert #ifndef HAVE_STRLCPY		/* + */
2602b15cb3dSCy Schubert /*
2612b15cb3dSCy Schubert  * Copy src to string dst of size siz.  At most siz-1 characters
2622b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz == 0).
2632b15cb3dSCy Schubert  * Returns strlen(src); if retval >= siz, truncation occurred.
2642b15cb3dSCy Schubert  */
2652b15cb3dSCy Schubert extern	size_t	strlcpy(char *dst, const char *src, size_t siz);
2662b15cb3dSCy Schubert #endif
2672b15cb3dSCy Schubert #ifndef HAVE_STRLCAT		/* + */
2682b15cb3dSCy Schubert /*
2692b15cb3dSCy Schubert  * Appends src to string dst of size siz (unlike strncat, siz is the
2702b15cb3dSCy Schubert  * full size of dst, not space left).  At most siz-1 characters
2712b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
2722b15cb3dSCy Schubert  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
2732b15cb3dSCy Schubert  * If retval >= siz, truncation occurred.
2742b15cb3dSCy Schubert  */
2752b15cb3dSCy Schubert extern	size_t	strlcat(char *dst, const char *src, size_t siz);
2762b15cb3dSCy Schubert #endif
2772b15cb3dSCy Schubert 
2782b15cb3dSCy Schubert 
2792b15cb3dSCy Schubert 
2802b15cb3dSCy Schubert /* lib/isc/win32/strerror.c
2812b15cb3dSCy Schubert  *
2822b15cb3dSCy Schubert  * To minimize Windows-specific changes to the rest of the NTP code,
2832b15cb3dSCy Schubert  * particularly reference clocks, we hijack calls to strerror() to deal
2842b15cb3dSCy Schubert  * with our mixture of error codes from the  C runtime (open, write)
2852b15cb3dSCy Schubert  * and Windows (sockets, serial ports).  This is an ugly hack because
2862b15cb3dSCy Schubert  * both use the lowest values differently, but particularly for ntpd,
2872b15cb3dSCy Schubert  * it's not a problem.
2882b15cb3dSCy Schubert  */
2892b15cb3dSCy Schubert #ifdef NTP_REDEFINE_STRERROR
2902b15cb3dSCy Schubert #define	strerror(e)	ntp_strerror(e)
2912b15cb3dSCy Schubert extern char *	ntp_strerror	(int e);
292c0b746e5SOllivier Robert #endif
293c0b746e5SOllivier Robert 
294c0b746e5SOllivier Robert /* systime.c */
2952b15cb3dSCy Schubert extern double	sys_tick;		/* tick size or time to read */
2962b15cb3dSCy Schubert extern double	measured_tick;		/* non-overridable sys_tick */
2972b15cb3dSCy Schubert extern double	sys_fuzz;		/* min clock read latency */
2982b15cb3dSCy Schubert extern int	trunc_os_clock;		/* sys_tick > measured_tick */
299c0b746e5SOllivier Robert 
300c0b746e5SOllivier Robert /* version.c */
301c0b746e5SOllivier Robert extern const char *Version;		/* version declaration */
3022b15cb3dSCy Schubert 
3032b15cb3dSCy Schubert #endif	/* NTP_STDLIB_H */
304