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
@@ -430,13 +430,13 @@ change_user () { struct passwd *pwd; if (user == NULL) return; - if (getuid () == 0) + if (getuid ()) { logmsg (L_NOTICE, _("not a superuser: ignoring the `user' statement")); return; } pwd = getpwnam (user); @@ -491,21 +491,23 @@ decode_buffer () signal (SIGHUP, SIG_IGN); signal (SIGCHLD, SIG_IGN); child = 1; } } - open_db (TAGR_DB_WR); - for (i = 0; i < reply->n_addr; i++, sp++) + if (open_db (TAGR_DB_WR) == 0) { - sp->in = ntohl (sp->in); - sp->out = ntohl (sp->out); - verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out); - report (sp, reply->timestamp); + for (i = 0; i < reply->n_addr; i++, sp++) + { + sp->in = ntohl (sp->in); + sp->out = ntohl (sp->out); + verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out); + report (sp, reply->timestamp); + } + close_db (); } - close_db (); if (child) exit (0); } @@ -528,15 +530,15 @@ read_input (const char *name) fp = fopen (name, "r"); if (!fp) die (EX_OSERR, _("cannot open file `%s': %s"), name, strerror (errno)); } - verbose (2, _("Reading `%s'"), name); - - open_db (TAGR_DB_WR); + if (open_db (TAGR_DB_WR)) + exit (EX_UNAVAILABLE); + while (getline (&buf, &bufsize, fp) > 0) { char *p; int i; Stat st; @@ -768,12 +770,14 @@ main (int argc, char **argv) if (argp_parse (&argp, argc, argv, 0, &index, NULL)) exit (EX_USAGE); if (html_template_option) html_template = html_template_option; + if (user_option) + user = user_option; argc -= index; argv += index; if (argc != 0 && !(list_option || import_option)) die (EX_USAGE, _("Too many arguments")); @@ -787,26 +791,33 @@ main (int argc, char **argv) CHECK_USAGE (rebuild_option, "--rebuild", "--lint"); CHECK_USAGE (list_option, "--list", "--lint"); CHECK_USAGE (read_option, "--read", "--lint"); exit (0); } - if (log_to_stderr == -1) - log_to_stderr = foreground && isatty (0); - grecs_log_to_stderr = log_to_stderr; - init_syslog (program_invocation_short_name); - if (test_template_option) { CHECK_USAGE (import_option, "--import", "--test-template"); CHECK_USAGE (rebuild_option, "--rebuild", "--test-template"); CHECK_USAGE (list_option, "--list", "--test-template"); CHECK_USAGE (read_option, "--read", "--test-template"); exit (check_template ()); } + if (user) + change_user (); + + if (log_to_stderr == -1) + log_to_stderr = (import_option + || read_option + || rebuild_option + || list_option + || foreground) && isatty (0); + grecs_log_to_stderr = log_to_stderr; + init_syslog (program_invocation_short_name); + if (import_option) { CHECK_USAGE (read_option, "--read", "--import"); if (argc) while (argc--) import (*argv++); @@ -835,15 +846,12 @@ main (int argc, char **argv) if (listen_sockaddr.sa == NULL) { logmsg (L_CRIT, _("listener address is not configured")); exit (EX_CONFIG); } - if (user_option) - change_user (); - sockfd = socket (listen_sockaddr.sa->sa_family, SOCK_DGRAM, 0); if (sockfd < 0) die (EX_OSERR, "socket: %s", strerror (errno)); if ((rc = fcntl (sockfd, F_GETFD, 0)) == -1 diff --git a/src/report.c b/src/report.c index ed3a81b..398881b 100644 --- a/src/report.c +++ b/src/report.c @@ -38,13 +38,13 @@ static GDBM_FILE dbf; static void tagr_db_report (char *str) { logmsg (L_CRIT, "%s: %s", dbname, str); } -void +int open_db (int flag) { dbname = xmalloc (strlen (basedir) + 1 + sizeof (TAGR_DBNAME)); strcpy (dbname, basedir); strcat (dbname, "/"); strcat (dbname, TAGR_DBNAME); @@ -52,14 +52,15 @@ open_db (int flag) flag == TAGR_DB_WR ? GDBM_WRCREAT : GDBM_READER, TAGR_DBMODE, tagr_db_report); if (dbf == NULL) { logmsg (L_ERR, _("cannot open database %s: %s"), dbname, gdbm_strerror (gdbm_errno)); - exit (1); + return 1; } + return 0; } void close_db () { gdbm_close (dbf); @@ -199,13 +200,14 @@ void list_db () { datum key; datum content; struct monitor *mon; - open_db (TAGR_DB_RD); + if (open_db (TAGR_DB_RD)) + exit (EX_UNAVAILABLE); key = gdbm_firstkey (dbf); while (key.dptr) { struct traffic_record *tr; datum nextkey = gdbm_nextkey (dbf, key); _read_db (key, &tr); @@ -275,18 +277,20 @@ rebuild (int force) { datum key; datum content; time_t now = time (NULL); verbose (1, _("rebuild initiated")); - open_db (TAGR_DB_WR); - key = gdbm_firstkey (dbf); - while (key.dptr) + if (open_db (TAGR_DB_RD) == 0) { - datum nextkey = gdbm_nextkey ( dbf, key ); - update_monitor (key, now, force); - free (key.dptr); - key = nextkey; + key = gdbm_firstkey (dbf); + while (key.dptr) + { + datum nextkey = gdbm_nextkey ( dbf, key ); + update_monitor (key, now, force); + free (key.dptr); + key = nextkey; + } + close_db (); } - close_db (); verbose (1, _("rebuild finished")); } @@ -423,13 +423,14 @@ import (const char *dirname) if (stat (dirname, &st)) die (EX_OSERR, _("cannot stat file `%s': %s"), dirname, strerror (errno)); else if (S_ISREG (st.st_mode)) { - open_db (TAGR_DB_WR); + if (open_db (TAGR_DB_WR)) + exit (EX_UNAVAILABLE); if (import_log (dirname) == 0) count++; close_db (); } else if (S_ISDIR (st.st_mode)) { @@ -442,13 +443,14 @@ import (const char *dirname) rc = glob (pattern, 0, NULL, &gl); free (pattern); switch (rc) { case 0: - open_db (TAGR_DB_WR); + if (open_db (TAGR_DB_WR)) + exit (EX_UNAVAILABLE); for (i = 0; i < gl.gl_pathc; i++) if (import_log (gl.gl_pathv[i]) == 0) count++; globfree (&gl); close_db (); break; @@ -213,13 +213,13 @@ struct traffic_record }; /* report.c */ #define TAGR_DB_RD 0 #define TAGR_DB_WR 1 -void open_db (int); +int open_db (int); void close_db (); void read_db (struct monitor *mon, struct traffic_record **tr); void write_db (struct monitor *mon, struct traffic_record *tr); void list_db (void); void rebuild (int force); void tr_init (struct traffic_record *tr); |