From 608f4fb896ad9b4553c12824d4afad4b2ee90f33 Mon Sep 17 00:00:00 2001 From: Petar Kapris Date: Thu, 17 Dec 2020 12:28:25 +0100 Subject: [PATCH] Remove unnecessary openlog() and closelog() calls --- timeoutd.c | 124 ----------------------------------------------------- 1 file changed, 124 deletions(-) diff --git a/timeoutd.c b/timeoutd.c index a670fbc..7c46385 100644 --- a/timeoutd.c +++ b/timeoutd.c @@ -186,9 +186,7 @@ char *argv[]; /* The only valid invocations are "timeoutd" or "timeoutd user tty" */ if (argc != 1 && argc != 3) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Incorrect invocation of timeoutd (argc=%d) by UID %d.", argc, getuid()); - closelog(); exit(5); } @@ -200,9 +198,7 @@ char *argv[]; * ut_line fields are relative to it. */ if (chdir("/dev")) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not change working directory to /dev!"); - closelog(); exit(1); } @@ -210,10 +206,8 @@ char *argv[]; /* This is a bit of a shameless hack, but, well, it works. */ if (argc == 3) { #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Running in user check mode. Checking user %s on %s.", argv[1], argv[2]); - closelog(); #endif strncpy(dev, argv[2], sizeof(dev) - 1); dev[sizeof(dev) - 1] = '\0'; @@ -224,11 +218,9 @@ char *argv[]; read_wtmp(); /* Read in today's wtmp entries */ switch (chk_timeout(argv[1], dev, "", 0, 0)) { case DAYMAX: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s on %s exceeded maximum daily limit (%d minutes). Login check failed.", argv[1], argv[2], config[configline]->daymax); - closelog(); /* printf("\r\nLogin not permitted. You have exceeded your maximum daily limit.\r\n"); printf("Please try again tomorrow.\r\n"); @@ -236,11 +228,9 @@ char *argv[]; logoff_msg(1); exit(10); case NOLOGIN: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s not allowed to login on %s at this time. Login check failed.", argv[1], argv[2]); - closelog(); /* printf("\r\nLogin not permitted at this time. Please try again later.\r\n"); */ @@ -248,18 +238,14 @@ char *argv[]; exit(20); case ACTIVE: #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "User %s on %s passed login check.", argv[1], argv[2]); - closelog(); #endif free_wtmp(); exit(0); default: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Internal error checking user %s on %s - unexpected return from chk_timeout", argv[1], argv[2]); - closelog(); exit(30); } } @@ -268,9 +254,7 @@ char *argv[]; pid_t pid; if ((pid = fork()) < 0) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Failed to execute fork number 1"); - closelog(); exit(1); } if (pid > 0) @@ -278,9 +262,7 @@ char *argv[]; struct rlimit r; if (getrlimit(RLIMIT_NOFILE, &r) == -1) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Coudln't get file resource limit."); - closelog(); exit(1); } for (int i = r.rlim_cur; i >= 0; --i) { @@ -288,16 +270,12 @@ char *argv[]; } if (setsid() < 0) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Failed to set new session ID at startup."); - closelog(); exit(1); } if ((pid = fork()) < 0) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Failed to execute fork number 2"); - closelog(); exit(1); } if (pid > 0) @@ -305,9 +283,7 @@ char *argv[]; umask(0); - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Daemon started."); - closelog(); /* the child processes all utmp file entries: */ while (1) { @@ -319,9 +295,7 @@ char *argv[]; read_wtmp(); /* Read in today's wtmp entries */ setutent(); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Time to check utmp for exceeded limits."); - closelog(); #endif while ((utmpp = getutent()) != (struct utmp *) NULL) check_idle(); @@ -330,9 +304,7 @@ char *argv[]; if (pending_reread) reread_config(SIGHUP); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Finished checking utmp... sleeping for 1 minute."); - closelog(); #endif sleep(60); } @@ -348,18 +320,14 @@ void read_wtmp() struct tm *tm; #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Reading today's wtmp entries."); - closelog(); #endif if ((fp = fopen(WTMP_FILE, "r")) == NULL) bailout("Could not open wtmp file!", 1); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Seek to end of wtmp"); - closelog(); #endif /* Go to end of file minus one structure */ fseek(fp, -1L * sizeof(struct utmp), SEEK_END); @@ -392,9 +360,7 @@ void read_wtmp() } fclose(fp); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Finished reading today's wtmp entries."); - closelog(); #endif } @@ -404,9 +370,7 @@ void free_wtmp() { struct ut_list *ut_list_p; #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Freeing list of today's wtmp entries."); - closelog(); #endif while (wtmplist) { @@ -428,9 +392,7 @@ void free_wtmp() free(ut_list_p); } #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Finished freeing list of today's wtmp entries."); - closelog(); #endif } @@ -458,11 +420,9 @@ char *time_str; te->days = 0; while (isalpha(*p)) { if (!p[1] || !isalpha(p[1])) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Malformed day name (%c%c) in time field of config file (%s). Entry ignored.", p[0], p[1], CONFIG); - closelog(); (*t)->days = 0; return; } @@ -478,11 +438,9 @@ char *time_str; i++; } if (!daynames[i]) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Malformed day name (%c%c) in time field of config file (%s). Entry ignored.", p[0], p[1], CONFIG); - closelog(); (*t)->days = 0; return; } @@ -492,11 +450,9 @@ char *time_str; /* Store start and end times */ if (*p) { if (strlen(p) != 9 || p[4] != '-') { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Malformed time (%s) in time field of config file (%s). Entry ignored.", p, CONFIG); - closelog(); (*t)->days = 0; return; } @@ -504,11 +460,9 @@ char *time_str; te->endtime = atoi(p + 5); if ((te->starttime == 0 && strncmp(p, "0000-", 5)) || (te->endtime == 0 && strcmp(p + 5, "0000"))) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Invalid range (%s) in time field of config file (%s). Entry ignored.", p, CONFIG); - closelog(); (*t)->days = 0; return; } @@ -615,10 +569,8 @@ void read_config() } } if (!config[i]->times || !config[i]->ttys || !config[i]->users || !config[i]->groups) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Error on line %d of config file (%s). Line ignored.", linenum, CONFIG); - closelog(); } else i++; } @@ -783,18 +735,14 @@ char *host; char cmdbuf[1024]; #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Warning %s@%s on %s of pending logoff in %d minutes.", user, host, tty, time_remaining); - closelog(); #endif if (chk_xsession(tty, host)) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Warning %s running X on %s for pending logout! (%d min%s left)", user, tty, time_remaining, time_remaining == 1 ? "" : "s"); - closelog(); /* then send the message using xmessage */ /* well, this is not really clean: */ @@ -804,18 +752,14 @@ char *host; limit_names[limit_type]); system(cmdbuf); /*#ifdef DEBUG */ - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "cmdbuf=%s", cmdbuf); - closelog(); /*#endif */ sleep(KWAIT); /* and give the user some time to read the message ;) */ return; } if ((fd = open(tty, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0 || (ttyf = fdopen(fd, "w")) == NULL) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not open %s to warn of impending logoff.\n", tty); - closelog(); return; } fprintf(ttyf, @@ -846,22 +790,16 @@ int session; /* Find primary group for specified user */ if ((pw = getpwnam(user)) == NULL) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not get password entry for %s.", user); - closelog(); return 0; } if ((gr = getgrgid(pw->pw_gid)) == NULL) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not get group name for %s.", user); - closelog(); return 0; } #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Checking user %s group %s tty %s.", user, gr->gr_name, tty); - closelog(); #endif /* Check to see if current user matches any entry based on tty/user/group */ @@ -887,14 +825,12 @@ printf("Group %s member %s\n", secgr->gr_name, *p); if (timematch && ttymatch && usermatch && groupmatch) { get_day_time(user); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "Matched entry %d", configline); syslog(SYSLOG_DEBUG, "Idle=%d (max=%d) Sess=%d (max=%d) Daily=%d (max=%d) warntime=%d", idle, config[configline]->idlemax, session, config[configline]->sessmax, daytime, config[configline]->daymax, config[configline]->warntime); - closelog(); #endif disc = getdisc(dev, host); @@ -968,10 +904,8 @@ void check_idle() if (aktconfigline > 0) { /* > 0 if user is not in config */ #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "User %s or * not in config -> No restrictions. Not checking %s on %s", user, user, dev); - closelog(); #endif return; /* now, we return because the user beeing checked is not in config, so he has no restrictions */ } @@ -995,10 +929,8 @@ void check_idle() #ifdef TIMEOUTDX11 if (chk_xsession(dev, host) && !chk_xterm(dev, host)) { /* check idle for Xsession, but not for xterm */ idle = get_xidle(user, host) / 1000 / 60; /* get_xidle returns millisecs, we need mins */ - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "get_xidle(%s,%s) returned %d mins idle for %s.", dev, host, (int) idle, user); - closelog(); } else if (chk_xterm(dev, host)) return; else @@ -1009,50 +941,38 @@ void check_idle() switch (chk_timeout(user, dev, host, idle, sesstime)) { case ACTIVE: #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(SYSLOG_DEBUG, "User %s is active.", user); - closelog(); #endif break; case IDLEMAX: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s exceeded idle limit (idle for %ld minutes, max=%d).\n", user, idle, config[configline]->idlemax); - closelog(); killit(utmpp->ut_pid, user, dev, host); break; case SESSMAX: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s exceeded maximum session limit at %s (on for %ld minutes, max=%d).\n", user, dev, sesstime, config[configline]->sessmax); - closelog(); killit(utmpp->ut_pid, user, dev, host); break; case DAYMAX: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s exceeded maximum daily limit (on for %d minutes, max=%d).\n", user, daytime, config[configline]->daymax); - closelog(); killit(utmpp->ut_pid, user, dev, host); break; case NOLOGIN: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); #ifdef DEBUG syslog(LOG_NOTICE, "NOLOGIN period reached for user %s@%s. (pid %d)", user, host, utmpp->ut_pid); #else syslog(LOG_NOTICE, "NOLOGIN period reached for user %s %s", user, host); #endif - closelog(); killit(utmpp->ut_pid, user, dev, host); break; default: - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Internal error - unexpected return from chk_timeout"); - closelog(); } } @@ -1060,27 +980,21 @@ void bailout(message, status) /* display error message and exit */ int status; /* exit status */ char *message; /* pointer to the error message */ { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Exiting - %s", message); - closelog(); exit(status); } void shut_down(signum) int signum; { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Received SIGTERM.. exiting."); - closelog(); exit(0); } void segfault(signum) int signum; { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Received SIGSEGV.. Something went wrong! Exiting!"); - closelog(); exit(0); } @@ -1129,9 +1043,7 @@ char *host; } /* Tell user which limit they have exceeded and that they will be logged off */ if ((tty = open(dev, O_WRONLY | O_NOCTTY | O_NONBLOCK)) < 0) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not write logoff message to %s.", dev); - closelog(); return; } @@ -1141,18 +1053,14 @@ char *host; (because we don't want to slay another user ;) */ cpid = getcpid(pid); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "I am at killit() pid=%d user=%s child=%d line %d", pid, user, cpid, __LINE__); - closelog(); #endif if (chk_ssh(pid) && chk_ssh(cpid) && !strcmp(getusr(cpid), user)) { #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "User %s (pid:%d, cpid:%d) logged in via ssh from %s.", user, pid, cpid, host); - closelog(); #endif pid = cpid; } @@ -1162,9 +1070,7 @@ char *host; close(tty); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Would normally kill pid %d user %s on %s", pid, user, dev); - closelog(); return; #endif @@ -1178,9 +1084,7 @@ char *host; kill(pid, SIGKILL); /* then send sure "kill" signal */ sleep(KWAIT); if (!kill(pid, 0)) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not log user %s off line %s.", user, dev); - closelog(); } } exit(0); @@ -1195,9 +1099,7 @@ int signum; pending_reread = 1; else { pending_reread = 0; - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Re-reading configuration file."); - closelog(); while (config[i]) { free(config[i]->times); free(config[i]->ttys); @@ -1257,9 +1159,7 @@ char *dev, *host; * dennis pts/3 localhost 20:01 0.00s 0.01s 0.00s w */ #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "LOCAL Xsession detected. device=%s host=%s", dev, host); - closelog(); #endif return TIMEOUTD_XSESSION_LOCAL; } else if (strstr(dev, ":") && strlen(host) > 1 && gethostbyname(host)) { @@ -1270,16 +1170,12 @@ char *dev, *host; */ #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "REMOTE Xsession detected. device=%s host=%s", dev, host); - closelog(); #endif return TIMEOUTD_XSESSION_REMOTE; } else { #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "NO xsession detected. device=%s host=%s", dev, host); - closelog(); #endif return TIMEOUTD_XSESSION_NONE; } @@ -1296,9 +1192,7 @@ char *dev, *host; { if (strncmp(dev, "pts/0", 3) == 0 && strncmp(host, ":0", 1) == 0) { #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "XTERM detected. device=%s host=%s Ignoring.", dev, host); - closelog(); #endif return 1; } else @@ -1324,9 +1218,7 @@ char *host, *user; sprintf(cmdbuf, "su %s -c \"xmessage -display %s -center '%s'&\"", user, host, msgbuf); system(cmdbuf); #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "cmdbuf=%s", cmdbuf); - closelog(); #endif sleep(KWAIT); /* and give the user some time to read the message ;) */ @@ -1339,15 +1231,11 @@ char *host, *user; kill(pid, SIGKILL); /* then send sure "kill" signal */ sleep(KWAIT); if (!kill(pid, 0)) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not log user %s off line %s. (running X)", user, host); - closelog(); } } #else - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Would normally logoff user %s running X (kill PID %d)", user, pid); - closelog(); #endif } @@ -1359,10 +1247,8 @@ pid_t pid; sprintf(path, "/proc/%d/stat", pid); proc_file = fopen(path, "r"); if (!proc_file) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_WARNING, "chk_ssh(): PID %d does not exist. Something went wrong. Ignoring.", pid); - closelog(); return 0; } @@ -1382,9 +1268,7 @@ pid_t pid; sprintf(path, "/proc/%d/status", pid); proc_file = fopen(path, "r"); if (!proc_file) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "getusr(): PID %d does not exist. Ignoring.", pid); - closelog(); return "unknown"; } while (!fscanf(proc_file, "Uid: %s", uid)) @@ -1408,15 +1292,11 @@ char *display; pwEntry = getpwnam(user); if (!pwEntry) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not get passwd-entry for user %s", user); - closelog(); } #ifdef DEBUG - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_DEBUG, "su-ing to %s(%d) and connecting to X", user, pwEntry->pw_uid); - closelog(); #endif /*change into the user running x. we need that to connect to X */ @@ -1428,9 +1308,7 @@ char *display; /*become user */ if (seteuid(pwEntry->pw_uid) == -1) { - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_ERR, "Could not seteuid(%d).", pwEntry->pw_uid); - closelog(); } sprintf(homedir, "HOME=%s", pwEntry->pw_dir); @@ -1438,10 +1316,8 @@ char *display; /* First, check if there is a xserver.. */ if ((dpy = XOpenDisplay(display)) == NULL) { /* = intended */ - openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON); syslog(LOG_NOTICE, "Could not connect to %s to query idle-time for %s. Ignoring.", display, user); - closelog(); } else { if (!mitInfo) mitInfo = XScreenSaverAllocInfo();