keys: store IDs in uint32_t

This commit is contained in:
Miroslav Lichvar 2014-09-26 14:14:54 +02:00
parent c404786b92
commit 361726b3ae
9 changed files with 54 additions and 52 deletions

View file

@ -2607,8 +2607,8 @@ authenticate_from_config(const char *filename)
CMD_Reply rx_message;
char line[2048], keyfile[2048], *command, *arg, *password;
const char *hashname;
unsigned long key_id = 0, key_id2 = -1;
int ret;
uint32_t key_id = 0, key_id2;
int key_id_valid = 1, ret;
FILE *in;
in = fopen(filename, "r");
@ -2625,13 +2625,12 @@ authenticate_from_config(const char *filename)
if (!strcasecmp(command, "keyfile")) {
snprintf(keyfile, sizeof (keyfile), "%s", arg);
} else if (!strcasecmp(command, "commandkey")) {
if (sscanf(arg, "%lu", &key_id) != 1)
key_id = -1;
key_id_valid = sscanf(arg, "%"SCNu32, &key_id) == 1;
}
}
fclose(in);
if (!*keyfile || key_id == -1) {
if (!*keyfile || !key_id_valid) {
fprintf(stderr, "Could not read keyfile or commandkey in file %s\n", filename);
return 0;
}
@ -2642,6 +2641,7 @@ authenticate_from_config(const char *filename)
return 0;
}
key_id2 = key_id + 1;
while (fgets(line, sizeof (line), in)) {
CPS_NormalizeLine(line);
if (!*line || !CPS_ParseKey(line, &key_id2, &hashname, &password))
@ -2659,7 +2659,7 @@ authenticate_from_config(const char *filename)
ret = 0;
}
} else {
fprintf(stderr, "Could not find key %lu in keyfile %s\n", key_id, keyfile);
fprintf(stderr, "Could not find key %"PRIu32" in keyfile %s\n", key_id, keyfile);
ret = 0;
}

View file

@ -127,7 +127,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
line += n;
}
} else if (!strcasecmp(cmd, "key")) {
if (sscanf(line, "%lu%n", &src->params.authkey, &n) != 1 ||
if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
src->params.authkey == INACTIVE_AUTHKEY) {
result = CPS_BadKey;
done = 1;
@ -238,7 +238,7 @@ CPS_SplitWord(char *line)
/* ================================================== */
int
CPS_ParseKey(char *line, unsigned long *id, const char **hash, char **key)
CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
{
char *s1, *s2, *s3, *s4;
@ -251,7 +251,7 @@ CPS_ParseKey(char *line, unsigned long *id, const char **hash, char **key)
if (!*s2 || *s4)
return 0;
if (sscanf(s1, "%lu", id) != 1)
if (sscanf(s1, "%"SCNu32, id) != 1)
return 0;
if (*s3) {

View file

@ -62,6 +62,6 @@ extern void CPS_NormalizeLine(char *line);
extern char *CPS_SplitWord(char *line);
/* Parse a key from keyfile */
extern int CPS_ParseKey(char *line, unsigned long *id, const char **hash, char **key);
extern int CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key);
#endif /* GOT_CMDPARSE_H */

12
conf.c
View file

@ -48,7 +48,7 @@
static int parse_string(char *line, char **result);
static int parse_int(char *line, int *result);
static int parse_unsignedlong(char *, unsigned long *result);
static int parse_uint32(char *, uint32_t *result);
static int parse_double(char *line, double *result);
static int parse_null(char *line);
@ -85,7 +85,7 @@ static int ntp_port = 123;
static char *keys_file = NULL;
static char *drift_file = NULL;
static char *rtc_file = NULL;
static unsigned long command_key_id;
static uint32_t command_key_id;
static double max_update_skew = 1000.0;
static double correction_time_ratio = 3.0;
static double max_clock_error = 1.0; /* in ppm */
@ -393,7 +393,7 @@ CNF_ParseLine(const char *filename, int number, char *line)
} else if (!strcasecmp(command, "combinelimit")) {
parse_double(p, &combine_limit);
} else if (!strcasecmp(command, "commandkey")) {
parse_unsignedlong(p, &command_key_id);
parse_uint32(p, &command_key_id);
} else if (!strcasecmp(command, "corrtimeratio")) {
parse_double(p, &correction_time_ratio);
} else if (!strcasecmp(command, "deny")) {
@ -516,10 +516,10 @@ parse_int(char *line, int *result)
/* ================================================== */
static int
parse_unsignedlong(char *line, unsigned long *result)
parse_uint32(char *line, uint32_t *result)
{
check_number_of_args(line, 1);
if (sscanf(line, "%lu", result) != 1) {
if (sscanf(line, "%"SCNu32, result) != 1) {
command_parse_error();
return 0;
}
@ -1376,7 +1376,7 @@ CNF_GetRtcDevice(void)
/* ================================================== */
unsigned long
uint32_t
CNF_GetCommandKey(void)
{
return command_key_id;

2
conf.h
View file

@ -57,7 +57,7 @@ extern int CNF_GetLogRefclocks(void);
extern int CNF_GetLogTempComp(void);
extern char *CNF_GetKeysFile(void);
extern char *CNF_GetRtcFile(void);
extern unsigned long CNF_GetCommandKey(void);
extern uint32_t CNF_GetCommandKey(void);
extern int CNF_GetGenerateCommandKey(void);
extern int CNF_GetDumpOnExit(void);
extern int CNF_GetManualEnabled(void);

38
keys.c
View file

@ -41,7 +41,7 @@
typedef struct {
unsigned long id;
uint32_t id;
char *val;
int len;
int hash_id;
@ -51,15 +51,15 @@ typedef struct {
static ARR_Instance keys;
static int command_key_valid;
static int command_key_id;
static uint32_t command_key_id;
static int cache_valid;
static unsigned long cache_key_id;
static uint32_t cache_key_id;
static int cache_key_pos;
/* ================================================== */
static int
generate_key(unsigned long key_id)
generate_key(uint32_t key_id)
{
#ifdef FEAT_SECHASH
unsigned char key[20];
@ -100,7 +100,7 @@ generate_key(unsigned long key_id)
return 0;
}
fprintf(f, "\n%lu %s HEX:", key_id, hashname);
fprintf(f, "\n%"PRIu32" %s HEX:", key_id, hashname);
for (i = 0; i < sizeof (key); i++)
fprintf(f, "%02hhX", key[i]);
fprintf(f, "\n");
@ -109,7 +109,7 @@ generate_key(unsigned long key_id)
/* Erase the key from stack */
memset(key, 0, sizeof (key));
LOG(LOGS_INFO, LOGF_Keys, "Generated key %lu", key_id);
LOG(LOGS_INFO, LOGF_Keys, "Generated key %"PRIu32, key_id);
return 1;
}
@ -165,7 +165,7 @@ get_key(unsigned int index)
/* ================================================== */
static int
determine_hash_delay(unsigned long key_id)
determine_hash_delay(uint32_t key_id)
{
NTP_Packet pkt;
struct timeval before, after;
@ -188,7 +188,7 @@ determine_hash_delay(unsigned long key_id)
/* Add on a bit extra to allow for copying, conversions etc */
min_usecs += min_usecs >> 4;
DEBUG_LOG(LOGF_Keys, "authentication delay for key %lu: %ld useconds", key_id, min_usecs);
DEBUG_LOG(LOGF_Keys, "authentication delay for key %"PRIu32": %ld useconds", key_id, min_usecs);
return min_usecs;
}
@ -220,7 +220,7 @@ KEY_Reload(void)
{
unsigned int i, line_number;
FILE *in;
unsigned long key_id;
uint32_t key_id;
char line[2048], *keyval, *key_file;
const char *hashname;
Key key;
@ -253,13 +253,13 @@ KEY_Reload(void)
key.hash_id = HSH_GetHashId(hashname);
if (key.hash_id < 0) {
LOG(LOGS_WARN, LOGF_Keys, "Unknown hash function in key %lu", key_id);
LOG(LOGS_WARN, LOGF_Keys, "Unknown hash function in key %"PRIu32, key_id);
continue;
}
key.len = UTI_DecodePasswordFromText(keyval);
if (!key.len) {
LOG(LOGS_WARN, LOGF_Keys, "Could not decode password in key %lu", key_id);
LOG(LOGS_WARN, LOGF_Keys, "Could not decode password in key %"PRIu32, key_id);
continue;
}
@ -279,7 +279,7 @@ KEY_Reload(void)
/* Check for duplicates */
for (i = 1; i < ARR_GetSize(keys); i++) {
if (get_key(i - 1)->id == get_key(i)->id)
LOG(LOGS_WARN, LOGF_Keys, "Detected duplicate key %lu", get_key(i - 1)->id);
LOG(LOGS_WARN, LOGF_Keys, "Detected duplicate key %"PRIu32, get_key(i - 1)->id);
}
/* Erase any passwords from stack */
@ -292,7 +292,7 @@ KEY_Reload(void)
/* ================================================== */
static int
lookup_key(unsigned long id)
lookup_key(uint32_t id)
{
Key specimen, *where, *keys_ptr;
int pos;
@ -312,7 +312,7 @@ lookup_key(unsigned long id)
/* ================================================== */
static Key *
get_key_by_id(unsigned long key_id)
get_key_by_id(uint32_t key_id)
{
int position;
@ -334,7 +334,7 @@ get_key_by_id(unsigned long key_id)
/* ================================================== */
unsigned long
uint32_t
KEY_GetCommandKey(void)
{
if (!command_key_valid) {
@ -347,7 +347,7 @@ KEY_GetCommandKey(void)
/* ================================================== */
int
KEY_KeyKnown(unsigned long key_id)
KEY_KeyKnown(uint32_t key_id)
{
return get_key_by_id(key_id) != NULL;
}
@ -355,7 +355,7 @@ KEY_KeyKnown(unsigned long key_id)
/* ================================================== */
int
KEY_GetAuthDelay(unsigned long key_id)
KEY_GetAuthDelay(uint32_t key_id)
{
Key *key;
@ -370,7 +370,7 @@ KEY_GetAuthDelay(unsigned long key_id)
/* ================================================== */
int
KEY_GenerateAuth(unsigned long key_id, const unsigned char *data, int data_len,
KEY_GenerateAuth(uint32_t key_id, const unsigned char *data, int data_len,
unsigned char *auth, int auth_len)
{
Key *key;
@ -387,7 +387,7 @@ KEY_GenerateAuth(unsigned long key_id, const unsigned char *data, int data_len,
/* ================================================== */
int
KEY_CheckAuth(unsigned long key_id, const unsigned char *data, int data_len,
KEY_CheckAuth(uint32_t key_id, const unsigned char *data, int data_len,
const unsigned char *auth, int auth_len)
{
Key *key;

14
keys.h
View file

@ -27,20 +27,22 @@
#ifndef GOT_KEYS_H
#define GOT_KEYS_H
#include "sysincl.h"
extern void KEY_Initialise(void);
extern void KEY_Finalise(void);
extern void KEY_Reload(void);
extern unsigned long KEY_GetCommandKey(void);
extern uint32_t KEY_GetCommandKey(void);
extern int KEY_GetKey(unsigned long key_id, char **key, int *len);
extern int KEY_KeyKnown(unsigned long key_id);
extern int KEY_GetAuthDelay(unsigned long key_id);
extern int KEY_GetKey(uint32_t key_id, char **key, int *len);
extern int KEY_KeyKnown(uint32_t key_id);
extern int KEY_GetAuthDelay(uint32_t key_id);
extern int KEY_GenerateAuth(unsigned long key_id, const unsigned char *data,
extern int KEY_GenerateAuth(uint32_t key_id, const unsigned char *data,
int data_len, unsigned char *auth, int auth_len);
extern int KEY_CheckAuth(unsigned long key_id, const unsigned char *data,
extern int KEY_CheckAuth(uint32_t key_id, const unsigned char *data,
int data_len, const unsigned char *auth, int auth_len);
#endif /* GOT_KEYS_H */

View file

@ -123,7 +123,7 @@ struct NCR_Instance_Record {
the association is symmetric). Note
: we don't authenticate if we can't
find the key in our database. */
unsigned long auth_key_id; /* The ID of the authentication key to
uint32_t auth_key_id; /* The ID of the authentication key to
use. */
/* Count of how many packets we have transmitted since last successful
@ -435,12 +435,12 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
if (params->authkey == INACTIVE_AUTHKEY) {
result->do_auth = 0;
result->auth_key_id = 0UL;
result->auth_key_id = 0;
} else {
result->do_auth = 1;
result->auth_key_id = params->authkey;
if (!KEY_KeyKnown(result->auth_key_id)) {
LOG(LOGS_WARN, LOGF_NtpCore, "Source %s added with unknown key %lu",
LOG(LOGS_WARN, LOGF_NtpCore, "Source %s added with unknown key %"PRIu32,
UTI_IPToString(&result->remote_addr.ip_addr), result->auth_key_id);
}
}
@ -524,7 +524,7 @@ NCR_ResetInstance(NCR_Instance instance)
/* ================================================== */
static int
check_packet_auth(NTP_Packet *pkt, unsigned long keyid, int auth_len)
check_packet_auth(NTP_Packet *pkt, uint32_t keyid, int auth_len)
{
return KEY_CheckAuth(keyid, (void *)pkt, offsetof(NTP_Packet, auth_keyid),
(void *)&(pkt->auth_data), auth_len);
@ -686,7 +686,7 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
int my_poll, /* The log2 of the local poll interval */
int version, /* The NTP version to be set in the packet */
int do_auth, /* Boolean indicating whether to authenticate the packet or not */
unsigned long key_id, /* The authentication key ID */
uint32_t key_id, /* The authentication key ID */
NTP_int64 *orig_ts, /* Originate timestamp (from received packet) */
struct timeval *local_rx, /* Local time request packet was received */
struct timeval *local_tx, /* RESULT : Time this reply
@ -791,7 +791,7 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
sizeof (message.auth_keyid) + auth_len);
} else {
DEBUG_LOG(LOGF_NtpCore,
"Could not generate auth data with key %lu to send packet",
"Could not generate auth data with key %"PRIu32" to send packet",
key_id);
return 0;
}
@ -930,7 +930,7 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins
double pkt_root_delay;
double pkt_root_dispersion;
unsigned long auth_key_id;
uint32_t auth_key_id;
/* The local time to which the (offset, delay, dispersion) triple will
be taken to relate. For client/server operation this is practically
@ -1597,7 +1597,7 @@ NCR_ProcessUnknown
NTP_Mode my_mode;
int my_poll, version;
int valid_auth, auth_len;
unsigned long key_id;
uint32_t key_id;
/* Ignore the packet if it wasn't received by server socket */
if (!NIO_IsServerSocket(local_addr->sock_fd)) {

View file

@ -38,7 +38,7 @@ typedef struct {
int iburst;
int min_stratum;
int poll_target;
unsigned long authkey;
uint32_t authkey;
double max_delay;
double max_delay_ratio;
double max_delay_dev_ratio;
@ -54,6 +54,6 @@ typedef struct {
#define SRC_DEFAULT_MAXDELAYDEVRATIO 10.0
#define SRC_DEFAULT_MINSTRATUM 0
#define SRC_DEFAULT_POLLTARGET 6
#define INACTIVE_AUTHKEY 0UL
#define INACTIVE_AUTHKEY 0
#endif /* GOT_SRCPARAMS_H */