154359Sroberto/* 254359Sroberto * ntp_stdlib.h - Prototypes for NTP lib. 354359Sroberto */ 4285612Sdelphij#ifndef NTP_STDLIB_H 5285612Sdelphij#define NTP_STDLIB_H 6285612Sdelphij 754359Sroberto#include <sys/types.h> 8285612Sdelphij#ifdef HAVE_SYS_SOCKET_H 9132451Sroberto#include <sys/socket.h> 10285612Sdelphij#endif 1154359Sroberto 12285612Sdelphij#include "declcond.h" /* ntpd uses ntpd/declcond.h, others include/ */ 13285612Sdelphij#include "l_stdlib.h" 14285612Sdelphij#include "ntp_net.h" 15285612Sdelphij#include "ntp_debug.h" 16285612Sdelphij#include "ntp_malloc.h" 1754359Sroberto#include "ntp_string.h" 18285612Sdelphij#include "ntp_syslog.h" 19294569Sdelphij#include "ntp_keyacc.h" 2054359Sroberto 21285612Sdelphij#ifdef __GNUC__ 22285612Sdelphij#define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args))) 23285612Sdelphij#else 24285612Sdelphij#define NTP_PRINTF(fmt, args) 25285612Sdelphij#endif 26285612Sdelphij 27285612Sdelphijextern int mprintf(const char *, ...) NTP_PRINTF(1, 2); 28285612Sdelphijextern int mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3); 29285612Sdelphijextern int mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0); 30285612Sdelphijextern int mvsnprintf(char *, size_t, const char *, va_list) 31285612Sdelphij NTP_PRINTF(3, 0); 32285612Sdelphijextern int msnprintf(char *, size_t, const char *, ...) 33285612Sdelphij NTP_PRINTF(3, 4); 34285612Sdelphijextern void msyslog(int, const char *, ...) NTP_PRINTF(2, 3); 35289997Sglebiusextern void mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0); 36285612Sdelphijextern void init_logging (const char *, u_int32, int); 37285612Sdelphijextern int change_logfile (const char *, int); 38285612Sdelphijextern void setup_logfile (const char *); 39285612Sdelphij#ifndef errno_to_str 40285612Sdelphijextern void errno_to_str(int, char *, size_t); 41285612Sdelphij#endif 42285612Sdelphij 4354359Sroberto/* 44285612Sdelphij * When building without OpenSSL, use a few macros of theirs to 45285612Sdelphij * minimize source differences in NTP. 4654359Sroberto */ 47285612Sdelphij#ifndef OPENSSL 48285612Sdelphij#define NID_md5 4 /* from openssl/objects.h */ 49285612Sdelphij/* from openssl/evp.h */ 50285612Sdelphij#define EVP_MAX_MD_SIZE 64 /* longest known is SHA512 */ 5154359Sroberto#endif 5254359Sroberto 53285612Sdelphij#define SAVE_ERRNO(stmt) \ 54285612Sdelphij { \ 55285612Sdelphij int preserved_errno; \ 56285612Sdelphij \ 57285612Sdelphij preserved_errno = socket_errno(); \ 58285612Sdelphij { \ 59285612Sdelphij stmt \ 60285612Sdelphij } \ 61285612Sdelphij errno = preserved_errno; \ 62285612Sdelphij } 6354359Sroberto 64285612Sdelphijtypedef void (*ctrl_c_fn)(void); 6554359Sroberto 66285612Sdelphij/* authkeys.c */ 67285612Sdelphijextern void auth_delkeys (void); 68285612Sdelphijextern int auth_havekey (keyid_t); 69293650Sglebiusextern int authdecrypt (keyid_t, u_int32 *, size_t, size_t); 70293650Sglebiusextern size_t authencrypt (keyid_t, u_int32 *, size_t); 71285612Sdelphijextern int authhavekey (keyid_t); 72285612Sdelphijextern int authistrusted (keyid_t); 73294569Sdelphijextern int authistrustedip (keyid_t, sockaddr_u *); 74285612Sdelphijextern int authreadkeys (const char *); 75285612Sdelphijextern void authtrust (keyid_t, u_long); 76285612Sdelphijextern int authusekey (keyid_t, int, const u_char *); 77285612Sdelphij 78285612Sdelphij/* 79285612Sdelphij * Based on the NTP timestamp, calculate the NTP timestamp of 80285612Sdelphij * the corresponding calendar unit. Use the pivot time to unfold 81285612Sdelphij * the NTP timestamp properly, or the current system time if the 82285612Sdelphij * pivot pointer is NULL. 83285612Sdelphij */ 84285612Sdelphijextern u_int32 calyearstart (u_int32 ntptime, const time_t *pivot); 85285612Sdelphijextern u_int32 calmonthstart (u_int32 ntptime, const time_t *pivot); 86285612Sdelphijextern u_int32 calweekstart (u_int32 ntptime, const time_t *pivot); 87285612Sdelphijextern u_int32 caldaystart (u_int32 ntptime, const time_t *pivot); 88285612Sdelphij 89285612Sdelphijextern const char *clockname (int); 90285612Sdelphijextern int clocktime (int, int, int, int, int, u_int32, u_long *, u_int32 *); 91285612Sdelphijextern int ntp_getopt (int, char **, const char *); 92285612Sdelphijextern void init_auth (void); 93285612Sdelphijextern void init_lib (void); 94285612Sdelphijextern struct savekey *auth_findkey (keyid_t); 95285612Sdelphijextern void auth_moremem (int); 96285612Sdelphijextern void auth_prealloc_symkeys(int); 97285612Sdelphijextern int ymd2yd (int, int, int); 98285612Sdelphij 99285612Sdelphij/* a_md5encrypt.c */ 100330141Sdelphijextern int MD5authdecrypt (int, const u_char *, size_t, u_int32 *, size_t, size_t); 101330141Sdelphijextern size_t MD5authencrypt (int, const u_char *, size_t, u_int32 *, size_t); 102294569Sdelphijextern void MD5auth_setkey (keyid_t, int, const u_char *, size_t, KeyAccT *c); 103285612Sdelphijextern u_int32 addr2refid (sockaddr_u *); 104285612Sdelphij 105285612Sdelphij/* emalloc.c */ 106285612Sdelphij#ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ 107285612Sdelphijextern void * ereallocz (void *, size_t, size_t, int); 108316069Sdelphijextern void * oreallocarrayxz (void *optr, size_t nmemb, size_t size, size_t extra); 109285612Sdelphij#define erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z)) 110285612Sdelphij#define emalloc(n) ereallocz(NULL, (n), 0, FALSE) 111285612Sdelphij#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) 112285612Sdelphij#define erealloc(p, c) ereallocz((p), (c), 0, FALSE) 113285612Sdelphij#define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE) 114316069Sdelphij#define ereallocarray(p, n, s) oreallocarrayxz((p), (n), (s), 0) 115316069Sdelphij#define eallocarray(n, s) oreallocarrayxz(NULL, (n), (s), 0) 116316069Sdelphij#define ereallocarrayxz(p, n, s, x) oreallocarrayxz((p), (n), (s), (x)) 117316069Sdelphij#define eallocarrayxz(n, s, x) oreallocarrayxz(NULL, (n), (s), (x)) 118285612Sdelphijextern char * estrdup_impl(const char *); 119285612Sdelphij#define estrdup(s) estrdup_impl(s) 12054359Sroberto#else 121285612Sdelphijextern void * ereallocz (void *, size_t, size_t, int, 122285612Sdelphij const char *, int); 123316069Sdelphijextern void * oreallocarrayxz (void *optr, size_t nmemb, size_t size, 124316069Sdelphij size_t extra, const char *, int); 125285612Sdelphij#define erealloczsite ereallocz 126285612Sdelphij#define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ 127285612Sdelphij __FILE__, __LINE__) 128285612Sdelphij#define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \ 129285612Sdelphij __FILE__, __LINE__) 130285612Sdelphij#define erealloc(p, c) ereallocz((p), (c), 0, FALSE, \ 131285612Sdelphij __FILE__, __LINE__) 132285612Sdelphij#define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE, \ 133285612Sdelphij __FILE__, __LINE__) 134316069Sdelphij#define ereallocarray(p, n, s) oreallocarrayxz((p), (n), (s), 0, \ 135285612Sdelphij __FILE__, __LINE__) 136316069Sdelphij#define eallocarray(n, s) oreallocarrayxz(NULL, (n), (s), 0, \ 137285612Sdelphij __FILE__, __LINE__) 138316069Sdelphij#define ereallocarrayxz(p, n, s, x) oreallocarrayxz((p), (n), (s), (x), \ 139316069Sdelphij __FILE__, __LINE__) 140316069Sdelphij#define eallocarrayxz(n, s, x) oreallocarrayxz(NULL, (n), (s), (x), \ 141316069Sdelphij __FILE__, __LINE__) 142285612Sdelphijextern char * estrdup_impl(const char *, const char *, int); 143285612Sdelphij#define estrdup(s) estrdup_impl((s), __FILE__, __LINE__) 14454359Sroberto#endif 14554359Sroberto 14654359Sroberto 147285612Sdelphijextern int atoint (const char *, long *); 148285612Sdelphijextern int atouint (const char *, u_long *); 149285612Sdelphijextern int hextoint (const char *, u_long *); 150285612Sdelphijextern const char * humanlogtime (void); 151285612Sdelphijextern const char * humantime (time_t); 152294569Sdelphijextern int is_ip_address (const char *, u_short, sockaddr_u *); 153285612Sdelphijextern char * mfptoa (u_int32, u_int32, short); 154285612Sdelphijextern char * mfptoms (u_int32, u_int32, short); 155285612Sdelphijextern const char * modetoa (size_t); 156285612Sdelphijextern const char * eventstr (int); 157285612Sdelphijextern const char * ceventstr (int); 158285612Sdelphijextern const char * res_match_flags(u_short); 159285612Sdelphijextern const char * res_access_flags(u_short); 160285612Sdelphij#ifdef KERNEL_PLL 161285612Sdelphijextern const char * k_st_flags (u_int32); 162285612Sdelphij#endif 163285612Sdelphijextern char * statustoa (int, int); 164285612Sdelphijextern sockaddr_u * netof (sockaddr_u *); 165285612Sdelphijextern char * numtoa (u_int32); 166285612Sdelphijextern char * numtohost (u_int32); 167285612Sdelphijextern const char * socktoa (const sockaddr_u *); 168285612Sdelphijextern const char * sockporttoa(const sockaddr_u *); 169285612Sdelphijextern u_short sock_hash (const sockaddr_u *); 170285612Sdelphijextern int sockaddr_masktoprefixlen(const sockaddr_u *); 171285612Sdelphijextern const char * socktohost (const sockaddr_u *); 172285612Sdelphijextern int octtoint (const char *, u_long *); 173285612Sdelphijextern u_long ranp2 (int); 174285612Sdelphijextern const char *refnumtoa (sockaddr_u *); 175285612Sdelphijextern const char *refid_str (u_int32, int); 17654359Sroberto 177285612Sdelphijextern int decodenetnum (const char *, sockaddr_u *); 17854359Sroberto 179285612Sdelphijextern const char * FindConfig (const char *); 18054359Sroberto 181285612Sdelphijextern void signal_no_reset (int, RETSIGTYPE (*func)(int)); 182285612Sdelphijextern void set_ctrl_c_hook (ctrl_c_fn); 18354359Sroberto 184285612Sdelphijextern void getauthkeys (const char *); 185285612Sdelphijextern void auth_agekeys (void); 186285612Sdelphijextern void rereadkeys (void); 18754359Sroberto 18854359Sroberto/* 18954359Sroberto * Variable declarations for libntp. 19054359Sroberto */ 19154359Sroberto 19254359Sroberto/* authkeys.c */ 19354359Srobertoextern u_long authkeynotfound; /* keys not found */ 19454359Srobertoextern u_long authkeylookups; /* calls to lookup keys */ 19554359Srobertoextern u_long authnumkeys; /* number of active keys */ 19654359Srobertoextern u_long authkeyexpired; /* key lifetime expirations */ 19754359Srobertoextern u_long authkeyuncached; /* cache misses */ 19854359Srobertoextern u_long authencryptions; /* calls to encrypt */ 19954359Srobertoextern u_long authdecryptions; /* calls to decrypt */ 20054359Sroberto 20154359Srobertoextern int authnumfreekeys; 20254359Sroberto 20354359Sroberto/* 20454359Sroberto * The key cache. We cache the last key we looked at here. 20554359Sroberto */ 20682498Srobertoextern keyid_t cache_keyid; /* key identifier */ 207285612Sdelphijextern int cache_type; /* key type */ 208285612Sdelphijextern u_char * cache_secret; /* secret */ 209298699Sdelphijextern size_t cache_secretsize; /* secret octets */ 210285612Sdelphijextern u_short cache_flags; /* KEY_ bit flags */ 21154359Sroberto 21254359Sroberto/* getopt.c */ 21354359Srobertoextern char * ntp_optarg; /* global argument pointer */ 21454359Srobertoextern int ntp_optind; /* global argv index */ 21554359Sroberto 216285612Sdelphij/* lib_strbuf.c */ 217285612Sdelphijextern int ipv4_works; 218285612Sdelphijextern int ipv6_works; 219285612Sdelphij 22054359Sroberto/* machines.c */ 221285612Sdelphijtypedef void (*pset_tod_using)(const char *); 222285612Sdelphijextern pset_tod_using set_tod_using; 22354359Sroberto 224285612Sdelphij/* ssl_init.c */ 225285612Sdelphij#ifdef OPENSSL 226285612Sdelphijextern void ssl_init (void); 227285612Sdelphijextern void ssl_check_version (void); 228285612Sdelphijextern int ssl_init_done; 229285612Sdelphij#define INIT_SSL() \ 230285612Sdelphij do { \ 231285612Sdelphij if (!ssl_init_done) \ 232285612Sdelphij ssl_init(); \ 233285612Sdelphij } while (0) 234285612Sdelphij#else /* !OPENSSL follows */ 235285612Sdelphij#define INIT_SSL() do {} while (0) 23654359Sroberto#endif 237285612Sdelphijextern int keytype_from_text (const char *, size_t *); 238285612Sdelphijextern const char *keytype_name (int); 239285612Sdelphijextern char * getpass_keytype (int); 24054359Sroberto 241285612Sdelphij/* strl-obsd.c */ 242285612Sdelphij#ifndef HAVE_STRLCPY /* + */ 243285612Sdelphij/* 244285612Sdelphij * Copy src to string dst of size siz. At most siz-1 characters 245285612Sdelphij * will be copied. Always NUL terminates (unless siz == 0). 246285612Sdelphij * Returns strlen(src); if retval >= siz, truncation occurred. 247285612Sdelphij */ 248285612Sdelphijextern size_t strlcpy(char *dst, const char *src, size_t siz); 249285612Sdelphij#endif 250285612Sdelphij#ifndef HAVE_STRLCAT /* + */ 251285612Sdelphij/* 252285612Sdelphij * Appends src to string dst of size siz (unlike strncat, siz is the 253285612Sdelphij * full size of dst, not space left). At most siz-1 characters 254285612Sdelphij * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 255285612Sdelphij * Returns strlen(src) + MIN(siz, strlen(initial dst)). 256285612Sdelphij * If retval >= siz, truncation occurred. 257285612Sdelphij */ 258285612Sdelphijextern size_t strlcat(char *dst, const char *src, size_t siz); 259285612Sdelphij#endif 260285612Sdelphij 261285612Sdelphij 262285612Sdelphij 263285612Sdelphij/* lib/isc/win32/strerror.c 264285612Sdelphij * 265285612Sdelphij * To minimize Windows-specific changes to the rest of the NTP code, 266285612Sdelphij * particularly reference clocks, we hijack calls to strerror() to deal 267285612Sdelphij * with our mixture of error codes from the C runtime (open, write) 268285612Sdelphij * and Windows (sockets, serial ports). This is an ugly hack because 269285612Sdelphij * both use the lowest values differently, but particularly for ntpd, 270285612Sdelphij * it's not a problem. 271285612Sdelphij */ 272285612Sdelphij#ifdef NTP_REDEFINE_STRERROR 273285612Sdelphij#define strerror(e) ntp_strerror(e) 274285612Sdelphijextern char * ntp_strerror (int e); 275285612Sdelphij#endif 276285612Sdelphij 27754359Sroberto/* systime.c */ 278285612Sdelphijextern double sys_tick; /* tick size or time to read */ 279285612Sdelphijextern double measured_tick; /* non-overridable sys_tick */ 280285612Sdelphijextern double sys_fuzz; /* min clock read latency */ 281285612Sdelphijextern int trunc_os_clock; /* sys_tick > measured_tick */ 28254359Sroberto 28354359Sroberto/* version.c */ 28454359Srobertoextern const char *Version; /* version declaration */ 285285612Sdelphij 286285612Sdelphij#endif /* NTP_STDLIB_H */ 287