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