1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
#include <config.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "kod_management.h"
#include "log.h"
#include "sntp-opts.h"
#include "ntp_stdlib.h"
#include "ntp_worker.h"
#include "ntp_debug.h"
int kod_init = 0, kod_db_cnt = 0;
const char *kod_db_file;
struct kod_entry **kod_db; /* array of pointers to kod_entry */
/*
* Search for a KOD entry
*/
int
search_entry(
const char *hostname,
struct kod_entry **dst
)
{
register int a, b, resc = 0;
for (a = 0; a < kod_db_cnt; a++)
if (!strcmp(kod_db[a]->hostname, hostname))
resc++;
if (!resc) {
*dst = NULL;
return 0;
}
*dst = eallocarray(resc, sizeof(**dst));
b = 0;
for (a = 0; a < kod_db_cnt; a++)
if (!strcmp(kod_db[a]->hostname, hostname)) {
(*dst)[b] = *kod_db[a];
b++;
}
return resc;
}
void
add_entry(
const char * hostname,
const char * type /* 4 bytes not \0 terminated */
)
{
int n;
struct kod_entry *pke;
pke = emalloc_zero(sizeof(*pke));
pke->timestamp = time(NULL);
memcpy(pke->type, type, 4);
pke->type[sizeof(pke->type) - 1] = '\0';
strlcpy(pke->hostname, hostname, sizeof(pke->hostname));
/*
* insert in address ("hostname") order to find duplicates
*/
for (n = 0; n < kod_db_cnt; n++)
if (strcmp(kod_db[n]->hostname, pke->hostname) >= 0)
break;
if (n < kod_db_cnt &&
0 == strcmp(kod_db[n]->hostname, pke->hostname)) {
kod_db[n]->timestamp = pke->timestamp;
free(pke);
return;
}
kod_db_cnt++;
kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0]));
if (n != kod_db_cnt - 1)
memmove(&kod_db[n + 1], &kod_db[n],
sizeof(kod_db[0]) * ((kod_db_cnt - 1) - n));
kod_db[n] = pke;
}
void
delete_entry(
const char * hostname,
const char * type
)
{
int a;
for (a = 0; a < kod_db_cnt; a++)
if (!strcmp(kod_db[a]->hostname, hostname)
&& !strcmp(kod_db[a]->type, type))
break;
if (a == kod_db_cnt)
return;
free(kod_db[a]);
kod_db_cnt--;
if (a < kod_db_cnt)
memmove(&kod_db[a], &kod_db[a + 1],
(kod_db_cnt - a) * sizeof(kod_db[0]));
}
void
atexit_write_kod_db(void)
{
#ifdef WORK_FORK
if (worker_process)
return;
#endif
write_kod_db();
}
int
write_kod_db(void)
{
FILE *db_s;
char *pch;
int dirmode;
register int a;
db_s = fopen(kod_db_file, "w");
/*
* If opening fails, blindly attempt to create each directory
* in the path first, then retry the open.
*/
if (NULL == db_s && strlen(kod_db_file)) {
dirmode = S_IRUSR | S_IWUSR | S_IXUSR
| S_IRGRP | S_IXGRP
| S_IROTH | S_IXOTH;
pch = strchr(kod_db_file + 1, DIR_SEP);
while (NULL != pch) {
*pch = '\0';
if (-1 == mkdir(kod_db_file, dirmode)
&& errno != EEXIST) {
msyslog(LOG_ERR, "mkdir(%s) failed: %m",
kod_db_file);
return FALSE;
}
*pch = DIR_SEP;
pch = strchr(pch + 1, DIR_SEP);
}
db_s = fopen(kod_db_file, "w");
}
if (NULL == db_s) {
msyslog(LOG_WARNING, "Can't open KOD db file %s for writing: %m",
kod_db_file);
return FALSE;
}
for (a = 0; a < kod_db_cnt; a++) {
fprintf(db_s, "%16.16llx %s %s\n", (unsigned long long)
kod_db[a]->timestamp, kod_db[a]->type,
kod_db[a]->hostname);
}
fflush(db_s);
fclose(db_s);
return TRUE;
}
void
kod_init_kod_db(
const char * db_file,
int readonly
)
{
/*
* Max. of 254 characters for hostname, 10 for timestamp, 4 for
* kisscode, 2 for spaces, 1 for \n, and 1 for \0
*/
char fbuf[254+10+4+2+1+1];
FILE *db_s;
int a, b, sepc, len;
unsigned long long ull;
char *str_ptr;
char error = 0;
TRACE(2, ("Initializing KOD DB...\n"));
kod_db_file = estrdup(db_file);
db_s = fopen(db_file, "r");
if (NULL == db_s) {
msyslog(LOG_WARNING, "kod_init_kod_db(): Cannot open KoD db file %s: %m",
db_file);
return;
}
if (debug)
printf("Starting to read KoD file %s...\n", db_file);
/* First let's see how many entries there are and check for right syntax */
while (!feof(db_s) && NULL != fgets(fbuf, sizeof(fbuf), db_s)) {
/* ignore blank lines */
if ('\n' == fbuf[0])
continue;
sepc = 0;
len = strlen(fbuf);
for (a = 0; a < len; a++) {
if (' ' == fbuf[a])
sepc++;
if ('\n' == fbuf[a]) {
if (sepc != 2) {
if (strcmp(db_file, "/dev/null"))
msyslog(LOG_DEBUG,
"Syntax error in KoD db file %s in line %i (missing space)",
db_file,
kod_db_cnt + 1);
fclose(db_s);
return;
}
sepc = 0;
kod_db_cnt++;
}
}
}
if (0 == kod_db_cnt) {
TRACE(2, ("KoD DB %s empty.\n", db_file));
goto wrapup;
}
TRACE(2, ("KoD DB %s contains %d entries, reading...\n", db_file, kod_db_cnt));
rewind(db_s);
/* Allocate the array of pointers to the struct kod_entry items */
kod_db = eallocarray(kod_db_cnt, sizeof(kod_db[0]));
/* Read contents of file */
for (b = 0;
!feof(db_s) && !ferror(db_s) && b < kod_db_cnt;
b++) {
str_ptr = fgets(fbuf, sizeof(fbuf), db_s);
if (NULL == str_ptr) {
error = 1;
break;
}
/* ignore blank lines */
if ('\n' == fbuf[0]) {
b--;
continue;
}
/* Allocate this struct kod_entry item */
kod_db[b] = emalloc(sizeof(*kod_db[0]));
if (3 != sscanf(fbuf, "%llx %4s %254s", &ull,
kod_db[b]->type, kod_db[b]->hostname)) {
free(kod_db[b]);
kod_db[b] = NULL;
error = 1;
break;
}
kod_db[b]->timestamp = (time_t)ull;
}
if (ferror(db_s) || error) {
kod_db_cnt = b;
msyslog(LOG_WARNING, "An error occured while parsing the KoD db file %s",
db_file);
fclose(db_s);
return;
}
wrapup:
fclose(db_s);
for (a = 0; a < kod_db_cnt; a++)
TRACE(2, ("KoD entry %d: %s at %llx type %s\n", a,
kod_db[a]->hostname,
(unsigned long long)kod_db[a]->timestamp,
kod_db[a]->type));
if (!readonly && write_kod_db())
atexit(&atexit_write_kod_db);
}
|