root/fwknop/tags/fwknop-1.8.3/knopwatchd.c

Revision 794, 23.7 kB (checked in by mbr, 1 year ago)

replaced references to knopwatchd.conf to fwknop.conf

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2 *****************************************************************************
3 *
4 *  File: knopwatchd.c
5 *
6 *  Purpose: knopwatchd checks on an interval of every five seconds to make
7 *           sure that both knopmd and fwknop are running on the box.  If
8 *           either daemon has died, knopwatchd will restart it and notify
9 *           each email address in EMAIL_ADDRESSES that the daemon has been
10 *           restarted.
11 *
12 *  Author: Michael Rash (mbr@cipherdyne.org)
13 *
14 *  Credits:  (see the CREDITS file)
15 *
16 *  Version: 1.8
17 *
18 *  Copyright (C) 2004-2007 Michael Rash (mbr@cipherdyne.org)
19 *
20 *  License (GNU Public License):
21 *
22 *     This program is distributed in the hope that it will be useful,
23 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
24 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 *     GNU General Public License for more details.
26 *
27 *     You should have received a copy of the GNU General Public License
28 *     along with this program; if not, write to the Free Software
29 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
30 *     USA
31 *
32 *****************************************************************************
33 *
34 *  $Id$
35 */
36
37 /* includes */
38 #include "fwknop.h"
39
40 /* defines */
41 #define FWKNOP_CONF "/etc/fwknop/fwknop.conf"
42
43 /* globals */
44 unsigned short int fwknopd_syscalls_ctr = 0;
45 unsigned short int knopmd_syscalls_ctr = 0;
46 unsigned short int no_email = 0;
47 unsigned short int check_knopmd = 1;
48 unsigned short int check_knoptm = 0;  /* PCAP-based rule timeouts */
49 const char mail_redr[] = " < /dev/null > /dev/null 2>&1";
50 char hostname[MAX_GEN_LEN];
51 char mail_addrs[MAX_GEN_LEN];
52 char shCmd[MAX_GEN_LEN];
53 char mailCmd[MAX_GEN_LEN];
54 char config_file[MAX_PATH_LEN];
55 char fwknop_run_dir[MAX_PATH_LEN];
56 char alerting_methods[MAX_GEN_LEN];
57 char fwknopdCmd[MAX_PATH_LEN];
58 char fwknopd_pid_file[MAX_PATH_LEN];
59 char fwknopd_cmdline_file[MAX_PATH_LEN];
60 char knopmdCmd[MAX_PATH_LEN];
61 char knoptmCmd[MAX_PATH_LEN];
62 char knopmd_pid_file[MAX_PATH_LEN];
63 char knoptm_pid_file[MAX_PATH_LEN];
64 char knopwatchd_pid_file[MAX_PATH_LEN];
65 char char_knopwatchd_check_interval[MAX_NUM_LEN];
66 char char_knopwatchd_max_retries[MAX_NUM_LEN];
67 unsigned int knopwatchd_check_interval = 5;  /* default to 5 seconds */
68 unsigned int knopwatchd_max_retries = 10; /* default to 10 tries */
69
70 static volatile sig_atomic_t received_sighup = 0;
71
72 /* prototypes */
73 static void parse_config(void);
74 static void expand_config_vars(void);
75 static void find_sub_var_value(
76     char *value,
77     char *sub_var,
78     char *pre_str,
79     char *post_str
80 );
81
82 static void check_process(
83     const char *pid_name,
84     const char *pid_file,
85     const char *cmdline_file,
86     const char *binary_path,
87     unsigned int max_retries
88 );
89 static void check_auth_mode(void);
90 static void incr_syscall_ctr(const char *pid_name, unsigned int max_retries);
91 static void reset_syscall_ctr(const char *pid_name);
92 static void give_up(const char *pid_name);
93 static void exec_binary(const char *binary_path, const char *cmdline_file);
94 static void sighup_handler(int sig);
95
96 /* main */
97 int main(int argc, char *argv[]) {
98     int cmdlopt;
99
100 #ifdef DEBUG
101     fprintf(stderr, "[+] Entering DEBUG mode...\n");
102     sleep(1);
103 #endif
104
105     strlcpy(config_file, FWKNOP_CONF, MAX_PATH_LEN);
106
107     /* handle command line arguments */
108     while((cmdlopt = getopt(argc, argv, "c:")) != -1) {
109         switch(cmdlopt) {
110             case 'c':
111                 strlcpy(config_file, optarg, MAX_PATH_LEN);
112                 break;
113             default:
114                 printf("[+] Usage: knopwatchd [-c <config file>] ");
115                 exit(EXIT_FAILURE);
116         }
117     }
118
119 #ifdef DEBUG
120     fprintf(stderr, "[+] parsing config_file: %s\n", config_file);
121 #endif
122
123     /* parse the config file */
124     parse_config();
125
126     /* see if we are supposed to disable all email alerts */
127     if (strncmp("noemail", alerting_methods, MAX_GEN_LEN) == 0)
128         no_email = 1;
129
130     /* first make sure there isn't another knopwatchd already running */
131     check_unique_pid(knopwatchd_pid_file, "knopwatchd");
132
133 #ifndef DEBUG
134     /* become a daemon */
135     daemonize_process(knopwatchd_pid_file);
136 #endif
137
138     /* install signal handler for HUP signals */
139     signal(SIGHUP, sighup_handler);
140
141     /* start doing the real work now that the daemon is running and
142      * the config file has been processed */
143
144     /* MAIN LOOP */
145     for (;;) {
146         /* restart processes as necessary */
147         check_process("fwknopd", fwknopd_pid_file, fwknopd_cmdline_file,
148             fwknopdCmd, knopwatchd_max_retries);
149
150         if (check_knopmd)
151             check_process("knopmd", knopmd_pid_file, NULL,
152                 knopmdCmd, knopwatchd_max_retries);
153
154         if (check_knoptm)
155             check_process("knoptm", knoptm_pid_file, NULL,
156                 knoptmCmd, knopwatchd_max_retries);
157
158         /* sleep and then check to see if we received any signals */
159         sleep(knopwatchd_check_interval);
160
161         /* check for sighup */
162         if (received_sighup) {
163             received_sighup = 0;
164 #ifdef DEBUG
165     fprintf(stderr, "[+] re-parsing config file: %s\n", config_file);
166 #endif
167             /* reparse the config file since we received a
168              * HUP signal */
169             parse_config();
170
171             slogr("fwknopd(knopwatchd)",
172                     "received HUP signal, re-imported fwknop.conf");
173         }
174     }
175
176     /* this statement doesn't get executed, but for completeness... */
177     exit(EXIT_SUCCESS);
178 }
179 /******************** end main ********************/
180
181 static void check_process(
182     const char *pid_name,
183     const char *pid_file,
184     const char *cmdline_file,
185     const char *binary_path,
186     unsigned int max_retries)
187 {
188     FILE *pidfile_ptr;
189     pid_t pid;
190     unsigned short int restart = 0;
191     char mail_str[MAX_MSG_LEN] = "";
192     char pid_line[MAX_PID_SIZE];
193
194     if ((pidfile_ptr = fopen(pid_file, "r")) == NULL) {
195 #ifdef DEBUG
196     fprintf(stderr, "[+] Could not open pid_file: %s\n", pid_file);
197 #endif
198         /* the pid file must not exist (or we can't read it), so
199          * setup to start the appropriate process */
200         restart = 1;
201     }
202
203     /* read the first line of the pid_file, which will contain the
204      * process id of any running pid_name process. */
205     if (! restart) {
206         if (fgets(pid_line, MAX_PID_SIZE, pidfile_ptr) == NULL) {
207 #ifdef DEBUG
208             fprintf(stderr, "[+] Could not read the pid_file: %s\n", pid_file);
209 #endif
210             /* see if we need to give up */
211             incr_syscall_ctr(pid_name, max_retries);
212             fclose(pidfile_ptr);
213             return;
214         }
215
216         /* convert the pid_line into an integer */
217         pid = atoi(pid_line);
218
219         /* close the pid_file now that we have read it */
220         fclose(pidfile_ptr);
221
222         if (kill(pid, 0) != 0) {
223             /* the process is not running so start it */
224             restart = 1;
225         }
226     }
227
228     if (restart) {
229 #ifdef DEBUG
230         fprintf(stderr, "[+] executing exec_binary(%s)\n", binary_path);
231 #endif
232         snprintf(mail_str, MAX_MSG_LEN,
233                 " -s \"[*] knopwatchd: Restarting %s on %s\" %s%s",
234                 pid_name, hostname, mail_addrs, mail_redr);
235         mail_str[MAX_MSG_LEN-1] = '\0';
236
237 #ifdef DEBUG
238         fprintf(stderr, "[+] sending mail: %s\n", mail_str);
239 #endif
240         if (! no_email) {
241             /* send the email */
242             send_alert_email(shCmd, mailCmd, mail_str);
243         }
244
245         /* execute the binary_path fwknopd daemon */
246         exec_binary(binary_path, cmdline_file);
247
248         /* increment the number of times we have tried to restart the binary */
249         incr_syscall_ctr(pid_name, max_retries);
250     } else {
251 #ifdef DEBUG
252         fprintf(stderr, "[+] %s is running.\n", pid_name);
253 #endif
254         /* reset the syscall counter since the process is successfully
255          * running. */
256         reset_syscall_ctr(pid_name);
257     }
258     return;
259 }
260
261 static void incr_syscall_ctr(const char *pid_name, unsigned int max_retries)
262 {
263     if (strcmp("fwknopd", pid_name) == 0) {
264         fwknopd_syscalls_ctr++;
265 #ifdef DEBUG
266         fprintf(stderr,
267             "[+] %s not running.  Trying to restart (%d tries so far).\n",
268             pid_name, fwknopd_syscalls_ctr);
269 #endif
270         if (fwknopd_syscalls_ctr >= max_retries)
271             give_up(pid_name);
272     } else if (strcmp("knopmd", pid_name) == 0) {
273         knopmd_syscalls_ctr++;
274 #ifdef DEBUG
275         fprintf(stderr,
276             "[+] %s not running.  Trying to restart (%d tries so far).\n",
277             pid_name, knopmd_syscalls_ctr);
278 #endif
279         if (knopmd_syscalls_ctr >= max_retries)
280             give_up(pid_name);
281     }
282     return;
283 }
284
285 static void reset_syscall_ctr(const char *pid_name)
286 {
287     if (strcmp("fwknopd", pid_name) == 0) {
288         fwknopd_syscalls_ctr = 0;
289     } else if (strcmp("knopmd", pid_name) == 0) {
290         knopmd_syscalls_ctr = 0;
291     }
292     return;
293 }
294
295 static void give_up(const char *pid_name)
296 {
297     char mail_str[MAX_MSG_LEN] = "";
298 #ifdef DEBUG
299     fprintf(stderr, "[*] Could not restart %s process.  Exiting.\n", pid_name);
300 #endif
301     snprintf(mail_str, MAX_MSG_LEN,
302             " -s \"[*] knopwatchd: Could not restart %s on %s. Exiting.\" %s%s",
303             pid_name, hostname, mail_addrs, mail_redr);
304     mail_str[MAX_MSG_LEN-1] = '\0';
305
306     if (! no_email) {
307         /* Send the email */
308         send_alert_email(shCmd, mailCmd, mail_str);
309     }
310     exit(EXIT_FAILURE);
311 }
312
313 static void exec_binary(const char *binary, const char *cmdlinefile)
314 {
315     FILE *cmdline_ptr;
316     char *prog_argv[MAX_ARG_LEN];
317     char cmdline_buf[MAX_LINE_BUF];
318     char *index;
319     pid_t child_pid;
320     int arg_num=0, non_ws, i;
321
322     prog_argv[arg_num] = (char *) safe_malloc(strlen(binary)+1);
323     if (prog_argv[arg_num] == NULL) {
324         exit(EXIT_FAILURE);
325     }
326     strlcpy(prog_argv[arg_num], binary, strlen(binary)+1);
327     arg_num++;
328
329     if (cmdlinefile != NULL) {
330         /* restart binary with its command line arguments intact */
331         if ((cmdline_ptr = fopen(cmdlinefile, "r")) == NULL) {
332             exit(EXIT_FAILURE);
333         }
334         if ((fgets(cmdline_buf, MAX_LINE_BUF, cmdline_ptr)) == NULL) {
335             exit(EXIT_FAILURE);
336         }
337         fclose(cmdline_ptr);
338
339         /* initialize index to the beginning of the line */
340         index = cmdline_buf;
341
342         /* advance the index pointer through any whitespace
343          * at the beginning of the line */
344         while (*index == ' ' || *index == '\t') index++;
345
346         while (*index != '\n' && *index != '\0') {
347             non_ws = 0;
348             while (*index != ' ' && *index != '\t'
349                     && index != '\0' && *index != '\n') {
350                 index++;
351                 non_ws++;
352             }
353             prog_argv[arg_num] = (char *) safe_malloc(non_ws+1);
354             if (prog_argv[arg_num] == NULL) {
355                 exit(EXIT_FAILURE);
356             }
357             for (i=0; i<non_ws; i++)
358                 prog_argv[arg_num][i] = *(index - (non_ws - i));
359             prog_argv[arg_num][i] = '\0';
360
361             arg_num++;
362
363             /* get past any whitespace */
364             while (*index == ' ' || *index == '\t') index++;
365         }
366     }
367
368     if (arg_num >= MAX_ARG_LEN)
369         exit(EXIT_FAILURE);
370     prog_argv[arg_num] = NULL;
371
372     if ((child_pid = fork()) < 0)
373         /* could not fork */
374         exit(EXIT_FAILURE);
375     else if (child_pid > 0) {
376         wait(NULL);
377         for (i=0; i<=arg_num; i++) {
378             free(prog_argv[i]);
379         }
380     } else {
381 #ifdef DEBUG
382         fprintf(stderr, "[+] restarting %s\n", binary);
383 #endif
384         execve(binary, prog_argv, NULL);  /* don't use environment */
385     }
386     return;
387 }
388
389 static void parse_config(void)
390 {
391     FILE *config_ptr;         /* FILE pointer to the config file */
392     int linectr = 0;
393     char config_buf[MAX_LINE_BUF];
394     char char_knopwatchd_check_interval[MAX_NUM_LEN];
395     char char_knopwatchd_max_retries[MAX_NUM_LEN];
396     char *index;
397
398     /* first check to see if knopmd and knoptm should not be running (i.e.
399      * AUTH_MODE in the fwknop.conf file is set to a pcap-based method).
400      * This will set check_knopmd and check_knoptm appropriately */
401     check_auth_mode();
402
403     if ((config_ptr = fopen(config_file, "r")) == NULL) {
404         perror("[*] Could not open config file");
405         exit(EXIT_FAILURE);
406     }
407
408     /* increment through each line of the config file */
409     while ((fgets(config_buf, MAX_LINE_BUF, config_ptr)) != NULL) {
410         linectr++;
411         index = config_buf;  /* set the index pointer to the
412                                 beginning of the line */
413
414         /* advance the index pointer through any whitespace
415          * at the beginning of the line */
416         while (*index == ' ' || *index == '\t') index++;
417
418         /* skip comments and blank lines, etc. */
419         if ((*index != '#') && (*index != '\n') &&
420                 (*index != ';') && (index != NULL)) {
421
422             find_char_var("fwknopdCmd ", fwknopdCmd, index);
423             find_char_var("HOSTNAME ", hostname, index);
424             find_char_var("FWKNOP_RUN_DIR", fwknop_run_dir, index);
425             find_char_var("FWKNOP_PID_FILE ", fwknopd_pid_file, index);
426             find_char_var("FWKNOP_CMDLINE_FILE ", fwknopd_cmdline_file, index);
427             find_char_var("knopmdCmd ", knopmdCmd, index);
428             find_char_var("knoptmCmd ", knoptmCmd, index);
429             find_char_var("KNOPMD_PID_FILE ", knopmd_pid_file, index);
430             find_char_var("KNOPTM_PID_FILE ", knoptm_pid_file, index);
431             find_char_var("shCmd ", shCmd, index);
432             find_char_var("mailCmd ", mailCmd, index);
433             find_char_var("EMAIL_ADDRESSES ", mail_addrs, index);
434             find_char_var("KNOPWATCHD_CHECK_INTERVAL ",
435                 char_knopwatchd_check_interval, index);
436             find_char_var("KNOPWATCHD_MAX_RETRIES ",
437                 char_knopwatchd_max_retries, index);
438             find_char_var("KNOPWATCHD_PID_FILE ", knopwatchd_pid_file, index);
439             find_char_var("ALERTING_METHODS ", alerting_methods, index);
440         }
441     }
442     fclose(config_ptr);
443
444     if (fwknopdCmd[0] == '\0') {
445         fprintf(stderr, "[*] Could not get fwknopdCmd from %s\n",
446                 config_file);
447         exit(EXIT_FAILURE);
448     }
449     if (hostname[0] == '\0') {
450         fprintf(stderr, "[*] Could not get HOSTNAME from %s\n",
451                 config_file);
452         exit(EXIT_FAILURE);
453     }
454     if (fwknopd_pid_file[0] == '\0') {
455         fprintf(stderr, "[*] Could not get FWKNOP_PID_FILE from %s\n",
456                 config_file);
457         exit(EXIT_FAILURE);
458     }
459     if (fwknopd_cmdline_file[0] == '\0') {
460         fprintf(stderr, "[*] Could not get FWKNOP_CMDLINE_FILE from %s\n",
461                 config_file);
462         exit(EXIT_FAILURE);
463     }
464     if (knopmdCmd[0] == '\0') {
465         fprintf(stderr, "[*] Could not get knopmdCmd from %s\n",
466                 config_file);
467         exit(EXIT_FAILURE);
468     }
469     if (knoptmCmd[0] == '\0') {
470         fprintf(stderr, "[*] Could not get knoptmCmd from %s\n",
471                 config_file);
472         exit(EXIT_FAILURE);
473     }
474     if (knopmd_pid_file[0] == '\0') {
475         fprintf(stderr, "[*] Could not get KNOPMD_PID_FILE from %s\n",
476                 config_file);
477         exit(EXIT_FAILURE);
478     }
479     if (knoptm_pid_file[0] == '\0') {
480         fprintf(stderr, "[*] Could not get KNOPTM_PID_FILE from %s\n",
481                 config_file);
482         exit(EXIT_FAILURE);
483     }
484     if (shCmd[0] == '\0') {
485         fprintf(stderr, "[*] Could not get shCmd from %s\n",
486                 config_file);
487         exit(EXIT_FAILURE);
488     }
489     if (mailCmd[0] == '\0') {
490         fprintf(stderr, "[*] Could not get mailCmd from %s\n",
491                 config_file);
492         exit(EXIT_FAILURE);
493     }
494     if (mail_addrs[0] == '\0') {
495         fprintf(stderr, "[*] Could not get EMAIL_ADDRESSES from %s\n",
496                 config_file);
497         exit(EXIT_FAILURE);
498     }
499     if (char_knopwatchd_check_interval[0] == '\0') {
500         fprintf(stderr, "[*] Could not get KNOPWATCHD_CHECK_INTERVAL from %s\n",
501                 config_file);
502         exit(EXIT_FAILURE);
503     }
504     if (char_knopwatchd_max_retries[0] == '\0') {
505         fprintf(stderr, "[*] Could not get KNOPWATCHD_MAX_RETRIES from %s\n",
506                 config_file);
507         exit(EXIT_FAILURE);
508     }
509     if (knopwatchd_pid_file[0] == '\0') {
510         fprintf(stderr, "[*] Could not get KNOPWATCHD_PID_FILE from %s\n",
511                 config_file);
512         exit(EXIT_FAILURE);
513     }
514
515     /* resolve any embedded variables */
516     expand_config_vars();
517
518     knopwatchd_check_interval = atoi(char_knopwatchd_check_interval);
519     knopwatchd_max_retries    = atoi(char_knopwatchd_max_retries);
520
521     return;
522 }
523
524 static void expand_config_vars(void)
525 {
526     char sub_var[MAX_GEN_LEN]  = "";
527     char pre_str[MAX_GEN_LEN]  = "";
528     char post_str[MAX_GEN_LEN] = "";
529     int found_sub_var = 1, resolve_ctr = 0;
530
531     while (found_sub_var) {
532         resolve_ctr++;
533         if (resolve_ctr >= 20) {
534             fprintf(stderr, "[*] Exceeded maximum variable resolution attempts.\n");
535             exit(EXIT_FAILURE);
536         }
537         found_sub_var = 0;
538         if (has_sub_var("EMAIL_ADDRESSES", mail_addrs, sub_var,
539                 pre_str, post_str)) {
540             find_sub_var_value(mail_addrs, sub_var, pre_str, post_str);
541             found_sub_var = 1;
542         }
543
544         if (has_sub_var("HOSTNAME", hostname, sub_var,
545                 pre_str, post_str)) {
546             find_sub_var_value(hostname, sub_var, pre_str, post_str);
547             found_sub_var = 1;
548         }
549
550         if (has_sub_var("FWKNOP_RUN_DIR", fwknop_run_dir, sub_var,
551                 pre_str, post_str)) {
552             find_sub_var_value(fwknop_run_dir, sub_var, pre_str, post_str);
553             found_sub_var = 1;
554         }
555
556         if (has_sub_var("FWKNOP_PID_FILE", fwknopd_pid_file, sub_var,
557                 pre_str, post_str)) {
558             find_sub_var_value(fwknopd_pid_file, sub_var, pre_str, post_str);
559             found_sub_var = 1;
560         }
561
562         if (has_sub_var("FWKNOP_CMDLINE_FILE", fwknopd_cmdline_file, sub_var,
563                 pre_str, post_str)) {
564             find_sub_var_value(fwknopd_cmdline_file, sub_var, pre_str, post_str);
565             found_sub_var = 1;
566         }
567
568         if (has_sub_var("KNOPMD_PID_FILE", knopmd_pid_file, sub_var,
569                 pre_str, post_str)) {
570             find_sub_var_value(knopmd_pid_file, sub_var, pre_str, post_str);
571             found_sub_var = 1;
572         }
573
574         if (has_sub_var("KNOPTM_PID_FILE", knoptm_pid_file, sub_var,
575                 pre_str, post_str)) {
576             find_sub_var_value(knoptm_pid_file, sub_var, pre_str, post_str);
577             found_sub_var = 1;
578         }
579
580         if (has_sub_var("KNOPWATCHD_PID_FILE", knopwatchd_pid_file, sub_var,
581                 pre_str, post_str)) {
582             find_sub_var_value(knopwatchd_pid_file, sub_var, pre_str, post_str);
583             found_sub_var = 1;
584         }
585
586         if (has_sub_var("KNOPWATCHD_CHECK_INTERVAL",
587                 char_knopwatchd_check_interval, sub_var,
588                 pre_str, post_str)) {
589             find_sub_var_value(char_knopwatchd_check_interval,
590                 sub_var, pre_str, post_str);
591             found_sub_var = 1;
592         }
593
594         if (has_sub_var("KNOPWATCHD_MAX_RETRIES", char_knopwatchd_max_retries,
595                 sub_var, pre_str, post_str)) {
596             find_sub_var_value(char_knopwatchd_max_retries,
597                 sub_var, pre_str, post_str);
598             found_sub_var = 1;
599         }
600
601         if (has_sub_var("mailCmd", mailCmd, sub_var,
602                 pre_str, post_str)) {
603             find_sub_var_value(mailCmd, sub_var, pre_str, post_str);
604             found_sub_var = 1;
605         }
606
607         if (has_sub_var("shCmd", shCmd, sub_var,
608                 pre_str, post_str)) {
609             find_sub_var_value(shCmd, sub_var, pre_str, post_str);
610             found_sub_var = 1;
611         }
612
613         if (has_sub_var("knopmdCmd", knopmdCmd, sub_var,
614                 pre_str, post_str)) {
615             find_sub_var_value(knopmdCmd, sub_var, pre_str, post_str);
616             found_sub_var = 1;
617         }
618
619         if (has_sub_var("fwknopdCmd", fwknopdCmd, sub_var,
620                 pre_str, post_str)) {
621             find_sub_var_value(fwknopdCmd, sub_var, pre_str, post_str);
622             found_sub_var = 1;
623         }
624     }
625
626     return;
627 }
628
629 static void find_sub_var_value(char *value, char *sub_var, char *pre_str,
630     char *post_str)
631 {
632     int found_var = 0;
633     if (strncmp(sub_var, "EMAIL_ADDRESSES", MAX_GEN_LEN) == 0) {
634         strlcpy(sub_var, mail_addrs, MAX_GEN_LEN);
635         found_var = 1;
636     } else if (strncmp(sub_var, "HOSTNAME", MAX_GEN_LEN) == 0) {
637         strlcpy(sub_var, hostname, MAX_GEN_LEN);
638         found_var = 1;
639     } else if (strncmp(sub_var, "FWKNOP_RUN_DIR", MAX_GEN_LEN) == 0) {
640         strlcpy(sub_var, fwknop_run_dir, MAX_GEN_LEN);
641         found_var = 1;
642     } else if (strncmp(sub_var, "FWKNOP_PID_FILE", MAX_GEN_LEN) == 0) {
643         strlcpy(sub_var, fwknopd_pid_file, MAX_GEN_LEN);
644         found_var = 1;
645     } else if (strncmp(sub_var, "FWKNOP_CMDLINE_FILE", MAX_GEN_LEN) == 0) {
646         strlcpy(sub_var, fwknopd_cmdline_file, MAX_GEN_LEN);
647         found_var = 1;
648     } else if (strncmp(sub_var, "KNOPMD_PID_FILE", MAX_GEN_LEN) == 0) {
649         strlcpy(sub_var, knopmd_pid_file, MAX_GEN_LEN);
650         found_var = 1;
651     } else if (strncmp(sub_var, "KNOPTM_PID_FILE", MAX_GEN_LEN) == 0) {
652         strlcpy(sub_var, knoptm_pid_file, MAX_GEN_LEN);
653         found_var = 1;
654     } else if (strncmp(sub_var, "KNOPWATCHD_PID_FILE", MAX_GEN_LEN) == 0) {
655         strlcpy(sub_var, knopwatchd_pid_file, MAX_GEN_LEN);
656         found_var = 1;
657     } else if (strncmp(sub_var, "KNOPWATCDHD_CHECK_INTERVAL", MAX_GEN_LEN) == 0) {
658         strlcpy(sub_var, char_knopwatchd_check_interval, MAX_GEN_LEN);
659         found_var = 1;
660     } else if (strncmp(sub_var, "KNOPWATCDHD_MAX_RETRIES", MAX_GEN_LEN) == 0) {
661         strlcpy(sub_var, char_knopwatchd_max_retries, MAX_GEN_LEN);
662         found_var = 1;
663     } else if (strncmp(sub_var, "mailCmd", MAX_GEN_LEN) == 0) {
664         strlcpy(sub_var, mailCmd, MAX_GEN_LEN);
665         found_var = 1;
666     } else if (strncmp(sub_var, "shCmd", MAX_GEN_LEN) == 0) {
667         strlcpy(sub_var, shCmd, MAX_GEN_LEN);
668         found_var = 1;
669     } else if (strncmp(sub_var, "knopmdCmd", MAX_GEN_LEN) == 0) {
670         strlcpy(sub_var, knopmdCmd, MAX_GEN_LEN);
671         found_var = 1;
672     } else if (strncmp(sub_var, "fwknopdCmd", MAX_GEN_LEN) == 0) {
673         strlcpy(sub_var, fwknopdCmd, MAX_GEN_LEN);
674         found_var = 1;
675     }
676
677     if (found_var)
678
679         /* substitute the variable value */
680         expand_sub_var_value(value, sub_var, pre_str, post_str);
681
682     else {
683         fprintf(stderr, "[*] Could not resolve sub-var: %s to a value.\n",
684             sub_var);
685         exit(EXIT_FAILURE);
686     }
687     return;
688 }
689
690 static void check_auth_mode(void)
691 {
692     FILE *config_ptr;   /* FILE pointer to the config file */
693     char config_buf[MAX_LINE_BUF];
694     char auth_mode[MAX_GEN_LEN];
695     char *index;
696
697     if ((config_ptr = fopen(FWKNOP_CONF, "r")) == NULL) {
698         fprintf(stderr, "[-] Could not open %s for reading.\n",
699             FWKNOP_CONF);
700         exit(EXIT_FAILURE);
701     }
702
703     auth_mode[0] = '\0';
704
705     /* increment through each line of the config file */
706     while ((fgets(config_buf, MAX_LINE_BUF, config_ptr)) != NULL) {
707         /* set the index pointer to the beginning of the line */
708         index = config_buf;
709
710         /* advance the index pointer through any whitespace
711          * at the beginning of the line */
712         while (*index == ' ' || *index == '\t') index++;
713
714         /* skip comments and blank lines, etc. */
715         if ((*index != '#') && (*index != '\n') &&
716                 (*index != ';') && (index != NULL)) {
717
718             find_char_var("AUTH_MODE ", auth_mode, index);
719         }
720     }
721     fclose(config_ptr);
722
723     /* see if we are using the ULOG_PCAP mode */
724     if (strncmp(auth_mode, "ULOG_PCAP", MAX_GEN_LEN) == 0)
725         check_knopmd = 0;
726
727     /* see if we are using the PCAP mode */
728     if (strncmp(auth_mode, "PCAP", MAX_GEN_LEN) == 0) {
729         check_knopmd = 0;
730         check_knoptm = 1;
731     }
732
733     return;
734 }
735
736 static void sighup_handler(int sig)
737 {
738     received_sighup = 1;
739 }
Note: See TracBrowser for help on using the browser.