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