diff options
author | Sergey Poznyakoff <gray@Pirx.gnu.org.ua> | 2009-04-27 17:23:50 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@Pirx.gnu.org.ua> | 2009-04-27 17:23:50 +0300 |
commit | 4d22b4e8668cc717440d66120b8607be9a273c97 (patch) | |
tree | 93b4addac108a1c8e913f0d5c68cb513f5c82c31 | |
parent | fe8f5226e8bc39bdc75e40e1c1ea464e9fc60ad3 (diff) | |
download | tagr-4d22b4e8668cc717440d66120b8607be9a273c97.tar.gz tagr-4d22b4e8668cc717440d66120b8607be9a273c97.tar.bz2 |
Bugfixes.
* src/main.c (change_user): Bugfix
(main): Reset user to the value of user_option, if set.
Fix (again) log_to_stderr initialization.
Switch to user privileges before actually performing any
actions that modify the database and other output files.
* src/report.c (open_db): Don't exit if unable to open
database. Return 1 instead. All callers updated.
* src/tagr.h (open_db): Update prototype.
-rw-r--r-- | src/main.c | 46 | ||||
-rw-r--r-- | src/report.c | 26 | ||||
-rw-r--r-- | src/stat.c | 6 | ||||
-rw-r--r-- | src/tagr.h | 2 |
4 files changed, 47 insertions, 33 deletions
@@ -433,7 +433,7 @@ change_user () | |||
433 | if (user == NULL) | 433 | if (user == NULL) |
434 | return; | 434 | return; |
435 | 435 | ||
436 | if (getuid () == 0) | 436 | if (getuid ()) |
437 | { | 437 | { |
438 | logmsg (L_NOTICE, _("not a superuser: ignoring the `user' statement")); | 438 | logmsg (L_NOTICE, _("not a superuser: ignoring the `user' statement")); |
439 | return; | 439 | return; |
@@ -494,15 +494,17 @@ decode_buffer () | |||
494 | } | 494 | } |
495 | } | 495 | } |
496 | 496 | ||
497 | open_db (TAGR_DB_WR); | 497 | if (open_db (TAGR_DB_WR) == 0) |
498 | for (i = 0; i < reply->n_addr; i++, sp++) | ||
499 | { | 498 | { |
500 | sp->in = ntohl (sp->in); | 499 | for (i = 0; i < reply->n_addr; i++, sp++) |
501 | sp->out = ntohl (sp->out); | 500 | { |
502 | verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out); | 501 | sp->in = ntohl (sp->in); |
503 | report (sp, reply->timestamp); | 502 | sp->out = ntohl (sp->out); |
503 | verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out); | ||
504 | report (sp, reply->timestamp); | ||
505 | } | ||
506 | close_db (); | ||
504 | } | 507 | } |
505 | close_db (); | ||
506 | 508 | ||
507 | if (child) | 509 | if (child) |
508 | exit (0); | 510 | exit (0); |
@@ -531,9 +533,9 @@ read_input (const char *name) | |||
531 | name, strerror (errno)); | 533 | name, strerror (errno)); |
532 | } | 534 | } |
533 | 535 | ||
534 | verbose (2, _("Reading `%s'"), name); | 536 | if (open_db (TAGR_DB_WR)) |
535 | 537 | exit (EX_UNAVAILABLE); | |
536 | open_db (TAGR_DB_WR); | 538 | |
537 | while (getline (&buf, &bufsize, fp) > 0) | 539 | while (getline (&buf, &bufsize, fp) > 0) |
538 | { | 540 | { |
539 | char *p; | 541 | char *p; |
@@ -771,6 +773,8 @@ main (int argc, char **argv) | |||
771 | 773 | ||
772 | if (html_template_option) | 774 | if (html_template_option) |
773 | html_template = html_template_option; | 775 | html_template = html_template_option; |
776 | if (user_option) | ||
777 | user = user_option; | ||
774 | 778 | ||
775 | argc -= index; | 779 | argc -= index; |
776 | argv += index; | 780 | argv += index; |
@@ -790,11 +794,6 @@ main (int argc, char **argv) | |||
790 | exit (0); | 794 | exit (0); |
791 | } | 795 | } |
792 | 796 | ||
793 | if (log_to_stderr == -1) | ||
794 | log_to_stderr = foreground && isatty (0); | ||
795 | grecs_log_to_stderr = log_to_stderr; | ||
796 | init_syslog (program_invocation_short_name); | ||
797 | |||
798 | if (test_template_option) | 797 | if (test_template_option) |
799 | { | 798 | { |
800 | CHECK_USAGE (import_option, "--import", "--test-template"); | 799 | CHECK_USAGE (import_option, "--import", "--test-template"); |
@@ -804,6 +803,18 @@ main (int argc, char **argv) | |||
804 | exit (check_template ()); | 803 | exit (check_template ()); |
805 | } | 804 | } |
806 | 805 | ||
806 | if (user) | ||
807 | change_user (); | ||
808 | |||
809 | if (log_to_stderr == -1) | ||
810 | log_to_stderr = (import_option | ||
811 | || read_option | ||
812 | || rebuild_option | ||
813 | || list_option | ||
814 | || foreground) && isatty (0); | ||
815 | grecs_log_to_stderr = log_to_stderr; | ||
816 | init_syslog (program_invocation_short_name); | ||
817 | |||
807 | if (import_option) | 818 | if (import_option) |
808 | { | 819 | { |
809 | CHECK_USAGE (read_option, "--read", "--import"); | 820 | CHECK_USAGE (read_option, "--read", "--import"); |
@@ -838,9 +849,6 @@ main (int argc, char **argv) | |||
838 | exit (EX_CONFIG); | 849 | exit (EX_CONFIG); |
839 | } | 850 | } |
840 | 851 | ||
841 | if (user_option) | ||
842 | change_user (); | ||
843 | |||
844 | sockfd = socket (listen_sockaddr.sa->sa_family, | 852 | sockfd = socket (listen_sockaddr.sa->sa_family, |
845 | SOCK_DGRAM, 0); | 853 | SOCK_DGRAM, 0); |
846 | if (sockfd < 0) | 854 | if (sockfd < 0) |
diff --git a/src/report.c b/src/report.c index ed3a81b..398881b 100644 --- a/src/report.c +++ b/src/report.c | |||
@@ -41,7 +41,7 @@ tagr_db_report (char *str) | |||
41 | logmsg (L_CRIT, "%s: %s", dbname, str); | 41 | logmsg (L_CRIT, "%s: %s", dbname, str); |
42 | } | 42 | } |
43 | 43 | ||
44 | void | 44 | int |
45 | open_db (int flag) | 45 | open_db (int flag) |
46 | { | 46 | { |
47 | dbname = xmalloc (strlen (basedir) + 1 + sizeof (TAGR_DBNAME)); | 47 | dbname = xmalloc (strlen (basedir) + 1 + sizeof (TAGR_DBNAME)); |
@@ -55,8 +55,9 @@ open_db (int flag) | |||
55 | { | 55 | { |
56 | logmsg (L_ERR, _("cannot open database %s: %s"), | 56 | logmsg (L_ERR, _("cannot open database %s: %s"), |
57 | dbname, gdbm_strerror (gdbm_errno)); | 57 | dbname, gdbm_strerror (gdbm_errno)); |
58 | exit (1); | 58 | return 1; |
59 | } | 59 | } |
60 | return 0; | ||
60 | } | 61 | } |
61 | 62 | ||
62 | void | 63 | void |
@@ -202,7 +203,8 @@ list_db () | |||
202 | datum content; | 203 | datum content; |
203 | struct monitor *mon; | 204 | struct monitor *mon; |
204 | 205 | ||
205 | open_db (TAGR_DB_RD); | 206 | if (open_db (TAGR_DB_RD)) |
207 | exit (EX_UNAVAILABLE); | ||
206 | key = gdbm_firstkey (dbf); | 208 | key = gdbm_firstkey (dbf); |
207 | while (key.dptr) | 209 | while (key.dptr) |
208 | { | 210 | { |
@@ -278,15 +280,17 @@ rebuild (int force) | |||
278 | time_t now = time (NULL); | 280 | time_t now = time (NULL); |
279 | 281 | ||
280 | verbose (1, _("rebuild initiated")); | 282 | verbose (1, _("rebuild initiated")); |
281 | open_db (TAGR_DB_WR); | 283 | if (open_db (TAGR_DB_RD) == 0) |
282 | key = gdbm_firstkey (dbf); | ||
283 | while (key.dptr) | ||
284 | { | 284 | { |
285 | datum nextkey = gdbm_nextkey ( dbf, key ); | 285 | key = gdbm_firstkey (dbf); |
286 | update_monitor (key, now, force); | 286 | while (key.dptr) |
287 | free (key.dptr); | 287 | { |
288 | key = nextkey; | 288 | datum nextkey = gdbm_nextkey ( dbf, key ); |
289 | update_monitor (key, now, force); | ||
290 | free (key.dptr); | ||
291 | key = nextkey; | ||
292 | } | ||
293 | close_db (); | ||
289 | } | 294 | } |
290 | close_db (); | ||
291 | verbose (1, _("rebuild finished")); | 295 | verbose (1, _("rebuild finished")); |
292 | } | 296 | } |
@@ -426,7 +426,8 @@ import (const char *dirname) | |||
426 | dirname, strerror (errno)); | 426 | dirname, strerror (errno)); |
427 | else if (S_ISREG (st.st_mode)) | 427 | else if (S_ISREG (st.st_mode)) |
428 | { | 428 | { |
429 | open_db (TAGR_DB_WR); | 429 | if (open_db (TAGR_DB_WR)) |
430 | exit (EX_UNAVAILABLE); | ||
430 | if (import_log (dirname) == 0) | 431 | if (import_log (dirname) == 0) |
431 | count++; | 432 | count++; |
432 | close_db (); | 433 | close_db (); |
@@ -445,7 +446,8 @@ import (const char *dirname) | |||
445 | switch (rc) | 446 | switch (rc) |
446 | { | 447 | { |
447 | case 0: | 448 | case 0: |
448 | open_db (TAGR_DB_WR); | 449 | if (open_db (TAGR_DB_WR)) |
450 | exit (EX_UNAVAILABLE); | ||
449 | for (i = 0; i < gl.gl_pathc; i++) | 451 | for (i = 0; i < gl.gl_pathc; i++) |
450 | if (import_log (gl.gl_pathv[i]) == 0) | 452 | if (import_log (gl.gl_pathv[i]) == 0) |
451 | count++; | 453 | count++; |
@@ -216,7 +216,7 @@ struct traffic_record | |||
216 | /* report.c */ | 216 | /* report.c */ |
217 | #define TAGR_DB_RD 0 | 217 | #define TAGR_DB_RD 0 |
218 | #define TAGR_DB_WR 1 | 218 | #define TAGR_DB_WR 1 |
219 | void open_db (int); | 219 | int open_db (int); |
220 | void close_db (); | 220 | void close_db (); |
221 | void read_db (struct monitor *mon, struct traffic_record **tr); | 221 | void read_db (struct monitor *mon, struct traffic_record **tr); |
222 | void write_db (struct monitor *mon, struct traffic_record *tr); | 222 | void write_db (struct monitor *mon, struct traffic_record *tr); |