6 #include "kod_management.h"
9 #include "ntp_stdlib.h"
10 #include "ntp_worker.h"
11 #include "ntp_debug.h"
13 int kod_init = 0, kod_db_cnt = 0;
14 const char *kod_db_file;
15 struct kod_entry **kod_db; /* array of pointers to kod_entry */
19 * Search for a KOD entry
24 struct kod_entry **dst
27 register int a, b, resc = 0;
29 for (a = 0; a < kod_db_cnt; a++)
30 if (!strcmp(kod_db[a]->hostname, hostname))
38 *dst = eallocarray(resc, sizeof(**dst));
41 for (a = 0; a < kod_db_cnt; a++)
42 if (!strcmp(kod_db[a]->hostname, hostname)) {
43 (*dst)[b] = *kod_db[a];
53 const char * hostname,
54 const char * type /* 4 bytes not \0 terminated */
58 struct kod_entry *pke;
60 pke = emalloc_zero(sizeof(*pke));
61 pke->timestamp = time(NULL);
62 memcpy(pke->type, type, 4);
63 pke->type[sizeof(pke->type) - 1] = '\0';
64 strlcpy(pke->hostname, hostname, sizeof(pke->hostname));
67 * insert in address ("hostname") order to find duplicates
69 for (n = 0; n < kod_db_cnt; n++)
70 if (strcmp(kod_db[n]->hostname, pke->hostname) >= 0)
74 0 == strcmp(kod_db[n]->hostname, pke->hostname)) {
75 kod_db[n]->timestamp = pke->timestamp;
81 kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0]));
82 if (n != kod_db_cnt - 1)
83 memmove(&kod_db[n + 1], &kod_db[n],
84 sizeof(kod_db[0]) * ((kod_db_cnt - 1) - n));
91 const char * hostname,
97 for (a = 0; a < kod_db_cnt; a++)
98 if (!strcmp(kod_db[a]->hostname, hostname)
99 && !strcmp(kod_db[a]->type, type))
109 memmove(&kod_db[a], &kod_db[a + 1],
110 (kod_db_cnt - a) * sizeof(kod_db[0]));
115 atexit_write_kod_db(void)
133 db_s = fopen(kod_db_file, "w");
136 * If opening fails, blindly attempt to create each directory
137 * in the path first, then retry the open.
139 if (NULL == db_s && strlen(kod_db_file)) {
140 dirmode = S_IRUSR | S_IWUSR | S_IXUSR
143 pch = strchr(kod_db_file + 1, DIR_SEP);
144 while (NULL != pch) {
146 if (-1 == mkdir(kod_db_file, dirmode)
147 && errno != EEXIST) {
148 msyslog(LOG_ERR, "mkdir(%s) failed: %m",
153 pch = strchr(pch + 1, DIR_SEP);
155 db_s = fopen(kod_db_file, "w");
159 msyslog(LOG_WARNING, "Can't open KOD db file %s for writing: %m",
165 for (a = 0; a < kod_db_cnt; a++) {
166 fprintf(db_s, "%16.16llx %s %s\n", (unsigned long long)
167 kod_db[a]->timestamp, kod_db[a]->type,
168 kod_db[a]->hostname);
180 const char * db_file,
185 * Max. of 254 characters for hostname, 10 for timestamp, 4 for
186 * kisscode, 2 for spaces, 1 for \n, and 1 for \0
188 char fbuf[254+10+4+2+1+1];
191 unsigned long long ull;
195 TRACE(2, ("Initializing KOD DB...\n"));
197 kod_db_file = estrdup(db_file);
199 db_s = fopen(db_file, "r");
202 msyslog(LOG_WARNING, "kod_init_kod_db(): Cannot open KoD db file %s: %m",
209 printf("Starting to read KoD file %s...\n", db_file);
210 /* First let's see how many entries there are and check for right syntax */
212 while (!feof(db_s) && NULL != fgets(fbuf, sizeof(fbuf), db_s)) {
214 /* ignore blank lines */
220 for (a = 0; a < len; a++) {
224 if ('\n' == fbuf[a]) {
226 if (strcmp(db_file, "/dev/null"))
228 "Syntax error in KoD db file %s in line %i (missing space)",
240 if (0 == kod_db_cnt) {
241 TRACE(2, ("KoD DB %s empty.\n", db_file));
245 TRACE(2, ("KoD DB %s contains %d entries, reading...\n", db_file, kod_db_cnt));
249 /* Allocate the array of pointers to the struct kod_entry items */
250 kod_db = eallocarray(kod_db_cnt, sizeof(kod_db[0]));
252 /* Read contents of file */
254 !feof(db_s) && !ferror(db_s) && b < kod_db_cnt;
257 str_ptr = fgets(fbuf, sizeof(fbuf), db_s);
258 if (NULL == str_ptr) {
263 /* ignore blank lines */
264 if ('\n' == fbuf[0]) {
269 /* Allocate this struct kod_entry item */
270 kod_db[b] = emalloc(sizeof(*kod_db[0]));
272 if (3 != sscanf(fbuf, "%llx %4s %254s", &ull,
273 kod_db[b]->type, kod_db[b]->hostname)) {
281 kod_db[b]->timestamp = (time_t)ull;
284 if (ferror(db_s) || error) {
286 msyslog(LOG_WARNING, "An error occured while parsing the KoD db file %s",
295 for (a = 0; a < kod_db_cnt; a++)
296 TRACE(2, ("KoD entry %d: %s at %llx type %s\n", a,
298 (unsigned long long)kod_db[a]->timestamp,
301 if (!readonly && write_kod_db())
302 atexit(&atexit_write_kod_db);