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" 14*f5f40dd6SCy Schubert #include "lib_strbuf.h" 15e6bfd18dSCy Schubert #include "ntp_md5.h" 162b15cb3dSCy Schubert #include "ntp_net.h" 172b15cb3dSCy Schubert #include "ntp_debug.h" 182b15cb3dSCy Schubert #include "ntp_malloc.h" 19*f5f40dd6SCy Schubert #include "lib_strbuf.h" 202b15cb3dSCy Schubert #include "ntp_string.h" 212b15cb3dSCy Schubert #include "ntp_syslog.h" 2268ba7e87SXin LI #include "ntp_keyacc.h" 232b15cb3dSCy Schubert 24e6bfd18dSCy Schubert #ifndef PATH_MAX 25e6bfd18dSCy Schubert # define PATH_MAX MAX_PATH 26e6bfd18dSCy Schubert #endif 27e6bfd18dSCy Schubert 282b15cb3dSCy Schubert #ifdef __GNUC__ 292b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args))) 302b15cb3dSCy Schubert #else 312b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) 322b15cb3dSCy Schubert #endif 332b15cb3dSCy Schubert 342b15cb3dSCy Schubert extern int mprintf(const char *, ...) NTP_PRINTF(1, 2); 352b15cb3dSCy Schubert extern int mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3); 362b15cb3dSCy Schubert extern int mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0); 372b15cb3dSCy Schubert extern int mvsnprintf(char *, size_t, const char *, va_list) 382b15cb3dSCy Schubert NTP_PRINTF(3, 0); 392b15cb3dSCy Schubert extern int msnprintf(char *, size_t, const char *, ...) 402b15cb3dSCy Schubert NTP_PRINTF(3, 4); 412b15cb3dSCy Schubert extern void msyslog(int, const char *, ...) NTP_PRINTF(2, 3); 429034852cSGleb Smirnoff extern void mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0); 432b15cb3dSCy Schubert extern void init_logging (const char *, u_int32, int); 442b15cb3dSCy Schubert extern int change_logfile (const char *, int); 452b15cb3dSCy Schubert extern void setup_logfile (const char *); 46e6bfd18dSCy Schubert #ifndef errno_to_str /* Windows port defines this */ 472b15cb3dSCy Schubert extern void errno_to_str(int, char *, size_t); 482b15cb3dSCy Schubert #endif 49c0b746e5SOllivier Robert 50a466cc55SCy Schubert extern char * ntp_realpath(const char *fsname); 51a466cc55SCy Schubert 52e6bfd18dSCy Schubert extern int xvsbprintf(char **, char * const, char const *, va_list) 53e6bfd18dSCy Schubert NTP_PRINTF(3, 0); 54e6bfd18dSCy Schubert extern int xsbprintf(char **, char * const, char const *, ...) 55e6bfd18dSCy Schubert NTP_PRINTF(3, 4); 56c0b746e5SOllivier Robert 572b15cb3dSCy Schubert #define SAVE_ERRNO(stmt) \ 582b15cb3dSCy Schubert { \ 592b15cb3dSCy Schubert int preserved_errno; \ 602b15cb3dSCy Schubert \ 612b15cb3dSCy Schubert preserved_errno = socket_errno(); \ 622b15cb3dSCy Schubert { \ 632b15cb3dSCy Schubert stmt \ 642b15cb3dSCy Schubert } \ 652b15cb3dSCy Schubert errno = preserved_errno; \ 662b15cb3dSCy Schubert } 672b15cb3dSCy Schubert 682b15cb3dSCy Schubert typedef void (*ctrl_c_fn)(void); 692b15cb3dSCy Schubert 702b15cb3dSCy Schubert /* authkeys.c */ 71a466cc55SCy Schubert #define AUTHPWD_MAXSECLEN 64 /* max. length of secret blob */ 72a466cc55SCy Schubert 73a466cc55SCy Schubert enum AuthPwdEnc { 74a466cc55SCy Schubert AUTHPWD_UNSPEC, /* format unspecified, length used for discrimination */ 75a466cc55SCy Schubert AUTHPWD_PLAIN, /* plain text, used as is */ 76a466cc55SCy Schubert AUTHPWD_HEX /* hex-encoded string */ 77a466cc55SCy Schubert }; 78a466cc55SCy Schubert 792b15cb3dSCy Schubert extern void auth_delkeys (void); 802b15cb3dSCy Schubert extern int auth_havekey (keyid_t); 81a466cc55SCy Schubert extern size_t authdecodepw (u_char *dst, size_t dstlen, const char *src, enum AuthPwdEnc); 823311ff84SXin LI extern int authdecrypt (keyid_t, u_int32 *, size_t, size_t); 833311ff84SXin LI extern size_t authencrypt (keyid_t, u_int32 *, size_t); 842b15cb3dSCy Schubert extern int authhavekey (keyid_t); 852b15cb3dSCy Schubert extern int authistrusted (keyid_t); 8668ba7e87SXin LI extern int authistrustedip (keyid_t, sockaddr_u *); 872b15cb3dSCy Schubert extern int authreadkeys (const char *); 882b15cb3dSCy Schubert extern void authtrust (keyid_t, u_long); 892b15cb3dSCy Schubert extern int authusekey (keyid_t, int, const u_char *); 902b15cb3dSCy Schubert 912b15cb3dSCy Schubert /* 922b15cb3dSCy Schubert * Based on the NTP timestamp, calculate the NTP timestamp of 932b15cb3dSCy Schubert * the corresponding calendar unit. Use the pivot time to unfold 942b15cb3dSCy Schubert * the NTP timestamp properly, or the current system time if the 952b15cb3dSCy Schubert * pivot pointer is NULL. 962b15cb3dSCy Schubert */ 972b15cb3dSCy Schubert extern u_int32 calyearstart (u_int32 ntptime, const time_t *pivot); 982b15cb3dSCy Schubert extern u_int32 calmonthstart (u_int32 ntptime, const time_t *pivot); 992b15cb3dSCy Schubert extern u_int32 calweekstart (u_int32 ntptime, const time_t *pivot); 1002b15cb3dSCy Schubert extern u_int32 caldaystart (u_int32 ntptime, const time_t *pivot); 1012b15cb3dSCy Schubert 1022b15cb3dSCy Schubert extern const char *clockname (int); 1032b15cb3dSCy Schubert extern int clocktime (int, int, int, int, int, u_int32, u_long *, u_int32 *); 1042b15cb3dSCy Schubert extern int ntp_getopt (int, char **, const char *); 1052b15cb3dSCy Schubert extern void init_auth (void); 1062b15cb3dSCy Schubert extern void init_lib (void); 1072b15cb3dSCy Schubert extern struct savekey *auth_findkey (keyid_t); 1082b15cb3dSCy Schubert extern void auth_moremem (int); 1092b15cb3dSCy Schubert extern void auth_prealloc_symkeys(int); 1102b15cb3dSCy Schubert extern int ymd2yd (int, int, int); 1112b15cb3dSCy Schubert 1122b15cb3dSCy Schubert /* a_md5encrypt.c */ 113e6bfd18dSCy Schubert extern size_t MD5authencrypt (int type, const u_char *key, size_t klen, 114e6bfd18dSCy Schubert u_int32 *pkt, size_t length); 115e6bfd18dSCy Schubert extern int MD5authdecrypt (int type, const u_char *key, size_t klen, 116e6bfd18dSCy Schubert u_int32 *pkt, size_t length, size_t size, 117e6bfd18dSCy Schubert keyid_t keyno); 1182b15cb3dSCy Schubert extern u_int32 addr2refid (sockaddr_u *); 1192b15cb3dSCy Schubert 120e6bfd18dSCy Schubert /* authkeys.c */ 121e6bfd18dSCy Schubert extern void MD5auth_setkey (keyid_t, int, const u_char *, size_t, 122e6bfd18dSCy Schubert KeyAccT *c); 123e6bfd18dSCy Schubert 1242b15cb3dSCy Schubert /* emalloc.c */ 1252b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ 1262b15cb3dSCy Schubert extern void * ereallocz (void *, size_t, size_t, int); 127f0574f5cSXin LI extern void * oreallocarrayxz (void *optr, size_t nmemb, size_t size, size_t extra); 128276da39aSCy Schubert #define erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z)) 129276da39aSCy Schubert #define emalloc(n) ereallocz(NULL, (n), 0, FALSE) 1302b15cb3dSCy Schubert #define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) 131276da39aSCy Schubert #define erealloc(p, c) ereallocz((p), (c), 0, FALSE) 132276da39aSCy Schubert #define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE) 133f0574f5cSXin LI #define ereallocarray(p, n, s) oreallocarrayxz((p), (n), (s), 0) 134f0574f5cSXin LI #define eallocarray(n, s) oreallocarrayxz(NULL, (n), (s), 0) 135f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x) oreallocarrayxz((p), (n), (s), (x)) 136f0574f5cSXin LI #define eallocarrayxz(n, s, x) oreallocarrayxz(NULL, (n), (s), (x)) 1372b15cb3dSCy Schubert extern char * estrdup_impl(const char *); 1382b15cb3dSCy Schubert #define estrdup(s) estrdup_impl(s) 139c0b746e5SOllivier Robert #else 1402b15cb3dSCy Schubert extern void * ereallocz (void *, size_t, size_t, int, 1412b15cb3dSCy Schubert const char *, int); 142f0574f5cSXin LI extern void * oreallocarrayxz (void *optr, size_t nmemb, size_t size, 143f0574f5cSXin LI size_t extra, const char *, int); 1442b15cb3dSCy Schubert #define erealloczsite ereallocz 1452b15cb3dSCy Schubert #define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ 1462b15cb3dSCy Schubert __FILE__, __LINE__) 1472b15cb3dSCy Schubert #define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \ 1482b15cb3dSCy Schubert __FILE__, __LINE__) 149276da39aSCy Schubert #define erealloc(p, c) ereallocz((p), (c), 0, FALSE, \ 1502b15cb3dSCy Schubert __FILE__, __LINE__) 151276da39aSCy Schubert #define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE, \ 152276da39aSCy Schubert __FILE__, __LINE__) 153f0574f5cSXin LI #define ereallocarray(p, n, s) oreallocarrayxz((p), (n), (s), 0, \ 154276da39aSCy Schubert __FILE__, __LINE__) 155f0574f5cSXin LI #define eallocarray(n, s) oreallocarrayxz(NULL, (n), (s), 0, \ 156f0574f5cSXin LI __FILE__, __LINE__) 157f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x) oreallocarrayxz((p), (n), (s), (x), \ 158f0574f5cSXin LI __FILE__, __LINE__) 159f0574f5cSXin LI #define eallocarrayxz(n, s, x) oreallocarrayxz(NULL, (n), (s), (x), \ 1602b15cb3dSCy Schubert __FILE__, __LINE__) 1612b15cb3dSCy Schubert extern char * estrdup_impl(const char *, const char *, int); 1622b15cb3dSCy Schubert #define estrdup(s) estrdup_impl((s), __FILE__, __LINE__) 163c0b746e5SOllivier Robert #endif 164c0b746e5SOllivier Robert 165c0b746e5SOllivier Robert 1662b15cb3dSCy Schubert extern int atoint (const char *, long *); 1672b15cb3dSCy Schubert extern int atouint (const char *, u_long *); 1682b15cb3dSCy Schubert extern int hextoint (const char *, u_long *); 1692b15cb3dSCy Schubert extern const char * humanlogtime (void); 1702b15cb3dSCy Schubert extern const char * humantime (time_t); 17168ba7e87SXin LI extern int is_ip_address (const char *, u_short, sockaddr_u *); 1722b15cb3dSCy Schubert extern char * mfptoa (u_int32, u_int32, short); 1732b15cb3dSCy Schubert extern char * mfptoms (u_int32, u_int32, short); 1742b15cb3dSCy Schubert extern const char * modetoa (size_t); 1752b15cb3dSCy Schubert extern const char * eventstr (int); 1762b15cb3dSCy Schubert extern const char * ceventstr (int); 1772b15cb3dSCy Schubert extern const char * res_match_flags(u_short); 1782d4e511cSCy Schubert extern const char * res_access_flags(u_int32); 1792b15cb3dSCy Schubert #ifdef KERNEL_PLL 1802b15cb3dSCy Schubert extern const char * k_st_flags (u_int32); 181c0b746e5SOllivier Robert #endif 1822b15cb3dSCy Schubert extern char * statustoa (int, int); 1832b15cb3dSCy Schubert extern sockaddr_u * netof (sockaddr_u *); 1842b15cb3dSCy Schubert extern char * numtoa (u_int32); 1852b15cb3dSCy Schubert extern const char * socktoa (const sockaddr_u *); 1862b15cb3dSCy Schubert extern const char * sockporttoa(const sockaddr_u *); 1872b15cb3dSCy Schubert extern u_short sock_hash (const sockaddr_u *); 1882b15cb3dSCy Schubert extern int sockaddr_masktoprefixlen(const sockaddr_u *); 1892b15cb3dSCy Schubert extern const char * socktohost (const sockaddr_u *); 1902b15cb3dSCy Schubert extern int octtoint (const char *, u_long *); 1912b15cb3dSCy Schubert extern u_long ranp2 (int); 192a466cc55SCy Schubert extern const char *refnumtoa (const sockaddr_u *); 1932b15cb3dSCy Schubert extern const char *refid_str (u_int32, int); 194c0b746e5SOllivier Robert 1952b15cb3dSCy Schubert extern int decodenetnum (const char *, sockaddr_u *); 196c0b746e5SOllivier Robert 1972b15cb3dSCy Schubert extern const char * FindConfig (const char *); 198c0b746e5SOllivier Robert 1992b15cb3dSCy Schubert extern void signal_no_reset (int, RETSIGTYPE (*func)(int)); 2002b15cb3dSCy Schubert extern void set_ctrl_c_hook (ctrl_c_fn); 201c0b746e5SOllivier Robert 2022b15cb3dSCy Schubert extern void getauthkeys (const char *); 2032b15cb3dSCy Schubert extern void auth_agekeys (void); 2042b15cb3dSCy Schubert extern void rereadkeys (void); 205c0b746e5SOllivier Robert 206c0b746e5SOllivier Robert /* 207c0b746e5SOllivier Robert * Variable declarations for libntp. 208c0b746e5SOllivier Robert */ 209c0b746e5SOllivier Robert 210c0b746e5SOllivier Robert /* authkeys.c */ 211c0b746e5SOllivier Robert extern u_long authkeynotfound; /* keys not found */ 212c0b746e5SOllivier Robert extern u_long authkeylookups; /* calls to lookup keys */ 213c0b746e5SOllivier Robert extern u_long authnumkeys; /* number of active keys */ 214c0b746e5SOllivier Robert extern u_long authkeyexpired; /* key lifetime expirations */ 215c0b746e5SOllivier Robert extern u_long authkeyuncached; /* cache misses */ 216c0b746e5SOllivier Robert extern u_long authencryptions; /* calls to encrypt */ 217c0b746e5SOllivier Robert extern u_long authdecryptions; /* calls to decrypt */ 218c0b746e5SOllivier Robert 219c0b746e5SOllivier Robert extern int authnumfreekeys; 220c0b746e5SOllivier Robert 221c0b746e5SOllivier Robert /* 222c0b746e5SOllivier Robert * The key cache. We cache the last key we looked at here. 223c0b746e5SOllivier Robert */ 224224ba2bdSOllivier Robert extern keyid_t cache_keyid; /* key identifier */ 2252b15cb3dSCy Schubert extern int cache_type; /* key type */ 2262b15cb3dSCy Schubert extern u_char * cache_secret; /* secret */ 2274990d495SXin LI extern size_t cache_secretsize; /* secret octets */ 2282b15cb3dSCy Schubert extern u_short cache_flags; /* KEY_ bit flags */ 229c0b746e5SOllivier Robert 230c0b746e5SOllivier Robert /* getopt.c */ 231c0b746e5SOllivier Robert extern char * ntp_optarg; /* global argument pointer */ 232c0b746e5SOllivier Robert extern int ntp_optind; /* global argv index */ 233c0b746e5SOllivier Robert 2342b15cb3dSCy Schubert /* lib_strbuf.c */ 2352b15cb3dSCy Schubert extern int ipv4_works; 2362b15cb3dSCy Schubert extern int ipv6_works; 237c0b746e5SOllivier Robert 2382b15cb3dSCy Schubert /* machines.c */ 2392b15cb3dSCy Schubert typedef void (*pset_tod_using)(const char *); 2402b15cb3dSCy Schubert extern pset_tod_using set_tod_using; 2412b15cb3dSCy Schubert 2422b15cb3dSCy Schubert /* ssl_init.c */ 2432b15cb3dSCy Schubert #ifdef OPENSSL 2442b15cb3dSCy Schubert extern void ssl_init (void); 2452b15cb3dSCy Schubert extern void ssl_check_version (void); 246*f5f40dd6SCy Schubert extern EVP_MD_CTX* digest_ctx; /* also ssl_init_done */ 2472b15cb3dSCy Schubert #define INIT_SSL() \ 2482b15cb3dSCy Schubert do { \ 249*f5f40dd6SCy Schubert if (NULL == digest_ctx) { \ 2502b15cb3dSCy Schubert ssl_init(); \ 251*f5f40dd6SCy Schubert } \ 252*f5f40dd6SCy Schubert } while (FALSE) 2532b15cb3dSCy Schubert #else /* !OPENSSL follows */ 254*f5f40dd6SCy Schubert #define ssl_check_version() do {} while (0) 2552b15cb3dSCy Schubert #define INIT_SSL() do {} while (0) 2562b15cb3dSCy Schubert #endif 257*f5f40dd6SCy Schubert extern int keytype_from_text (const char *text, 258*f5f40dd6SCy Schubert size_t *pdigest_len); 259*f5f40dd6SCy Schubert extern const char *keytype_name (int type); 260*f5f40dd6SCy Schubert extern char * getpass_keytype (int type); 2612b15cb3dSCy Schubert 2622b15cb3dSCy Schubert /* strl-obsd.c */ 2632b15cb3dSCy Schubert #ifndef HAVE_STRLCPY /* + */ 2642b15cb3dSCy Schubert /* 2652b15cb3dSCy Schubert * Copy src to string dst of size siz. At most siz-1 characters 2662b15cb3dSCy Schubert * will be copied. Always NUL terminates (unless siz == 0). 2672b15cb3dSCy Schubert * Returns strlen(src); if retval >= siz, truncation occurred. 2682b15cb3dSCy Schubert */ 2692b15cb3dSCy Schubert extern size_t strlcpy(char *dst, const char *src, size_t siz); 2702b15cb3dSCy Schubert #endif 2712b15cb3dSCy Schubert #ifndef HAVE_STRLCAT /* + */ 2722b15cb3dSCy Schubert /* 2732b15cb3dSCy Schubert * Appends src to string dst of size siz (unlike strncat, siz is the 2742b15cb3dSCy Schubert * full size of dst, not space left). At most siz-1 characters 2752b15cb3dSCy Schubert * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 2762b15cb3dSCy Schubert * Returns strlen(src) + MIN(siz, strlen(initial dst)). 2772b15cb3dSCy Schubert * If retval >= siz, truncation occurred. 2782b15cb3dSCy Schubert */ 2792b15cb3dSCy Schubert extern size_t strlcat(char *dst, const char *src, size_t siz); 2802b15cb3dSCy Schubert #endif 2812b15cb3dSCy Schubert 2822b15cb3dSCy Schubert 2832b15cb3dSCy Schubert 2842b15cb3dSCy Schubert /* lib/isc/win32/strerror.c 2852b15cb3dSCy Schubert * 2862b15cb3dSCy Schubert * To minimize Windows-specific changes to the rest of the NTP code, 2872b15cb3dSCy Schubert * particularly reference clocks, we hijack calls to strerror() to deal 2882b15cb3dSCy Schubert * with our mixture of error codes from the C runtime (open, write) 2892b15cb3dSCy Schubert * and Windows (sockets, serial ports). This is an ugly hack because 2902b15cb3dSCy Schubert * both use the lowest values differently, but particularly for ntpd, 2912b15cb3dSCy Schubert * it's not a problem. 2922b15cb3dSCy Schubert */ 2932b15cb3dSCy Schubert #ifdef NTP_REDEFINE_STRERROR 2942b15cb3dSCy Schubert #define strerror(e) ntp_strerror(e) 2952b15cb3dSCy Schubert extern char * ntp_strerror (int e); 296c0b746e5SOllivier Robert #endif 297c0b746e5SOllivier Robert 298c0b746e5SOllivier Robert /* systime.c */ 2992b15cb3dSCy Schubert extern double sys_tick; /* tick size or time to read */ 3002b15cb3dSCy Schubert extern double measured_tick; /* non-overridable sys_tick */ 3012b15cb3dSCy Schubert extern double sys_fuzz; /* min clock read latency */ 3022b15cb3dSCy Schubert extern int trunc_os_clock; /* sys_tick > measured_tick */ 303c0b746e5SOllivier Robert 304c0b746e5SOllivier Robert /* version.c */ 305c0b746e5SOllivier Robert extern const char *Version; /* version declaration */ 3062b15cb3dSCy Schubert 3072b15cb3dSCy Schubert #endif /* NTP_STDLIB_H */ 308