Change indentation style to modified K&R

The entire source code of timeoutd.c has been passed through GNU indent,
in order to amend the inconsistencies, it uses the K&R C style, but
modified, so that tabs are replaced with 4 spaces, close to what a lot
of the code was already using. (Blasphemy, i know) Also the maximum line
length is 100 chars, apart from long strings, which don't get cut as a
result.
This commit is contained in:
kappa 2020-10-31 10:10:54 +01:00
parent 848c784e7f
commit 1ed5ce8edf

View file

@ -77,25 +77,22 @@ FILE *utfile = NULL;
#ifdef NEED_UTMP_UTILS
void setutent()
{
if (utfile == NULL)
{
if ((utfile = fopen("/etc/utmp", "r")) == NULL)
{
if (utfile == NULL) {
if ((utfile = fopen("/etc/utmp", "r")) == NULL) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Could not open /etc/utmp");
closelog();
exit(1);
}
}
else fseek(utfile, 0L, 0);
} else
fseek(utfile, 0L, 0);
}
struct utmp *getutent() /* returns next utmp file entry */
{
struct utmp *getutent()
{ /* returns next utmp file entry */
static struct utmp uent;
while (fread(&uent, sizeof(struct utmp), 1, utfile) == 1)
{
while (fread(&uent, sizeof(struct utmp), 1, utfile) == 1) {
if (uent.ut_line[0] != 0 && uent.ut_name[0] != 0)
return &uent;
}
@ -207,8 +204,7 @@ char comm[16]; /*seppy; to save the command of a pid*/
#ifdef NEED_STRCASECMP
int strcasecmp(char *s1, char *s2)
{
while (*s1 && *s2)
{
while (*s1 && *s2) {
if (tolower(*s1) < tolower(*s2))
return -1;
else if (tolower(*s1) > tolower(*s2))
@ -232,15 +228,13 @@ char *delim;
char *retp = *stringp;
char *p;
if (!**stringp) return NULL;
if (!**stringp)
return NULL;
while (**stringp)
{
while (**stringp) {
p = delim;
while (*p)
{
if (*p == **stringp)
{
while (*p) {
if (*p == **stringp) {
**stringp = '\0';
(*stringp)++;
return retp;
@ -267,8 +261,7 @@ char *argv[];
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
/* The only valid invocations are "timeoutd" or "timeoutd user tty" */
if (argc != 1 && argc != 3)
{
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();
@ -282,8 +275,7 @@ char *argv[];
* filesystem from which we were started is unmounted. /dev is convenient as
* ut_line fields are relative to it.
*/
if (chdir("/dev"))
{
if (chdir("/dev")) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Could not change working directory to /dev!");
closelog();
@ -292,11 +284,11 @@ char *argv[];
/* Handle the "timeoutd user tty" invocation */
/* This is a bit of a shameless hack, but, well, it works. */
if (argc == 3)
{
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]);
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);
@ -306,8 +298,7 @@ char *argv[];
now_hhmm = now.tm_hour * 100 + now.tm_min;
allow_reread = 0;
read_wtmp(); /* Read in today's wtmp entries */
switch(chk_timeout(argv[1], dev, "", 0, 0))
{
switch (chk_timeout(argv[1], dev, "", 0, 0)) {
case DAYMAX:
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_NOTICE,
@ -323,8 +314,8 @@ char *argv[];
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]);
"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");
@ -341,7 +332,8 @@ char *argv[];
exit(0);
default:
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Internal error checking user %s on %s - unexpected return from chk_timeout",
syslog(LOG_ERR,
"Internal error checking user %s on %s - unexpected return from chk_timeout",
argv[1], argv[2]);
closelog();
exit(30);
@ -362,8 +354,7 @@ char *argv[];
closelog();
/* the child processes all utmp file entries: */
while (1)
{
while (1) {
/* Record time at which we woke up & started checking */
time_now = time((time_t *) 0); /* get current time */
now = *(localtime(&time_now)); /* Break it into bits */
@ -416,8 +407,7 @@ void read_wtmp()
/* Go to end of file minus one structure */
fseek(fp, -1L * sizeof(struct utmp), SEEK_END);
while (fread(&ut, sizeof(struct utmp), 1, fp) == 1)
{
while (fread(&ut, sizeof(struct utmp), 1, fp) == 1) {
/* ut.ut_tv.tv_sec is not guaranteed to be time_t and localtime requires a time_t
argument, and will break otherwise */
time_t tmp_time = ut.ut_tv.tv_sec;
@ -427,14 +417,12 @@ void read_wtmp()
break;
#ifndef SUNOS
if (ut.ut_type == USER_PROCESS ||
ut.ut_type == DEAD_PROCESS ||
ut.ut_type == UT_UNKNOWN || /* SA 19940703 */
ut.ut_type == LOGIN_PROCESS ||
ut.ut_type == BOOT_TIME)
if (ut.ut_type == USER_PROCESS || ut.ut_type == DEAD_PROCESS || ut.ut_type == UT_UNKNOWN || /* SA 19940703 */
ut.ut_type == LOGIN_PROCESS || ut.ut_type == BOOT_TIME)
#endif
{
if ((ut_list_p = (struct ut_list *) malloc(sizeof(struct ut_list))) == NULL)
if ((ut_list_p = (struct ut_list *)
malloc(sizeof(struct ut_list))) == NULL)
bailout("Out of memory in read_wtmp.", 1);
ut_list_p->elem = ut;
ut_list_p->next = wtmplist;
@ -442,7 +430,8 @@ void read_wtmp()
}
/* Position the file pointer 2 structures back */
if (fseek(fp, -2 * sizeof(struct utmp), SEEK_CUR) < 0) break;
if (fseek(fp, -2 * sizeof(struct utmp), SEEK_CUR) < 0)
break;
}
fclose(fp);
#ifdef DEBUG
@ -462,16 +451,15 @@ void free_wtmp()
closelog();
#endif
while (wtmplist)
{
while (wtmplist) {
#ifdef DEBUG_WTMP
struct tm *tm;
tm = localtime(&(wtmplist->elem.ut_time));
printf("%d:%d %s %s %s\n",
tm->tm_hour,tm->tm_min, wtmplist->elem.ut_line,
printf("%d:%d %s %s %s\n", tm->tm_hour, tm->tm_min, wtmplist->elem.ut_line,
wtmplist->elem.ut_user,
#ifndef SUNOS
wtmplist->elem.ut_type == LOGIN_PROCESS?"login":wtmplist->elem.ut_type == BOOT_TIME?"reboot":"logoff"
wtmplist->elem.ut_type ==
LOGIN_PROCESS ? "login" : wtmplist->elem.ut_type == BOOT_TIME ? "reboot" : "logoff"
#else
""
#endif
@ -507,16 +495,15 @@ char *time_str;
p = strtok(time_str, ",");
/* For each day/timerange set, */
while (p)
{
while (p) {
/* Store valid days */
te->days = 0;
while (isalpha(*p))
{
if (!p[1] || !isalpha(p[1]))
{
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);
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;
@ -525,19 +512,18 @@ char *time_str;
p[1] = toupper(p[1]);
i = 0;
while (daynames[i])
{
if (!strncmp(daynames[i], p, 2))
{
while (daynames[i]) {
if (!strncmp(daynames[i], p, 2)) {
te->days |= daynums[i];
break;
}
i++;
}
if (!daynames[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);
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;
@ -546,29 +532,29 @@ char *time_str;
}
/* Store start and end times */
if (*p)
{
if (strlen(p) != 9 || p[4] != '-')
{
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);
syslog(LOG_ERR,
"Malformed time (%s) in time field of config file (%s). Entry ignored.", p,
CONFIG);
closelog();
(*t)->days = 0;
return;
}
te->starttime = atoi(p);
te->endtime = atoi(p + 5);
if ((te->starttime == 0 && strncmp(p, "0000-", 5)) || (te->endtime == 0 && strcmp(p+5, "0000")))
{
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);
syslog(LOG_ERR,
"Invalid range (%s) in time field of config file (%s). Entry ignored.", p,
CONFIG);
closelog();
(*t)->days = 0;
return;
}
}
else
{
} else {
te->starttime = 0;
te->endtime = 2359;
}
@ -584,7 +570,8 @@ char *b;
{
if ((*a = (char *) malloc(strlen(b) + 1)) == NULL)
bailout("Out of memory", 1);
else strcpy(*a, b);
else
strcpy(*a, b);
}
void read_config()
@ -603,8 +590,7 @@ void read_config()
if ((config_file = fopen(CONFIG, "r")) == NULL)
bailout("Cannot open config file", 1);
while (fgets(line, 256, config_file) != NULL)
{
while (fgets(line, 256, config_file) != NULL) {
linenum++;
p = line;
while (*p && (*p == ' ' || *p == '\t'))
@ -613,11 +599,12 @@ void read_config()
while (*p && *p != '#' && *p != '\n')
p++;
*p = '\0';
if (*lstart)
{
if (*lstart) {
if (i == MAXLINES)
bailout("Too many lines in timeouts config file.", 1);
if ((config[i] = (struct config_ent *) malloc(sizeof(struct config_ent))) == NULL)
if ((config[i] = (struct config_ent *)
malloc(sizeof(struct config_ent)))
== NULL)
bailout("Out of memory", 1);
config[i]->times = NULL;
config[i]->ttys = NULL;
@ -632,51 +619,50 @@ void read_config()
config[i]->messages[SESSMSG] = NULL;
config[i]->messages[DAYMSG] = NULL;
config[i]->messages[NOLOGINMSG] = NULL;
if ((tok = strsep(&lstart, ":")) != NULL) store_times(&config[i]->times, tok);
if ((tok = strsep(&lstart, ":")) != NULL) alloc_cp(&config[i]->ttys, tok);
if ((tok = strsep(&lstart, ":")) != NULL) alloc_cp(&config[i]->users, tok);
if ((tok = strsep(&lstart, ":")) != NULL) alloc_cp(&config[i]->groups, tok);
if ((tok = strsep(&lstart, ":")) != NULL)
store_times(&config[i]->times, tok);
if ((tok = strsep(&lstart, ":")) != NULL)
alloc_cp(&config[i]->ttys, tok);
if ((tok = strsep(&lstart, ":")) != NULL)
alloc_cp(&config[i]->users, tok);
if ((tok = strsep(&lstart, ":")) != NULL)
alloc_cp(&config[i]->groups, tok);
tok = strsep(&lstart, ":");
if (tok != NULL && !strncasecmp(tok, "NOLOGIN", 7))
{
if (tok != NULL && !strncasecmp(tok, "NOLOGIN", 7)) {
config[i]->login_allowed = 0;
if (tok[7] == ';') alloc_cp(&config[i]->messages[NOLOGINMSG], tok+8);
else if ((tok = strsep(&lstart, ":")) != NULL) alloc_cp(&config[i]->messages[NOLOGINMSG], tok);
}
else
if (tok != NULL && !strcasecmp(tok, "LOGIN")) config[i]->login_allowed=1;
else
{
if (tok != NULL)
{
if (tok[7] == ';')
alloc_cp(&config[i]->messages[NOLOGINMSG], tok + 8);
else if ((tok = strsep(&lstart, ":")) != NULL)
alloc_cp(&config[i]->messages[NOLOGINMSG], tok);
} else if (tok != NULL && !strcasecmp(tok, "LOGIN"))
config[i]->login_allowed = 1;
else {
if (tok != NULL) {
config[i]->idlemax = atoi(tok);
if ((p = strchr(tok, ';')) != NULL) alloc_cp(&config[i]->messages[IDLEMSG], p+1);
if ((p = strchr(tok, ';')) != NULL)
alloc_cp(&config[i]->messages[IDLEMSG], p + 1);
}
if ((tok = strsep(&lstart, ":")) != NULL)
{
if ((tok = strsep(&lstart, ":")) != NULL) {
config[i]->sessmax = atoi(tok);
if ((p = strchr(tok, ';')) != NULL) alloc_cp(&config[i]->messages[SESSMSG], p+1);
if ((p = strchr(tok, ';')) != NULL)
alloc_cp(&config[i]->messages[SESSMSG], p + 1);
}
if ((tok = strsep(&lstart, ":")) != NULL)
{
if ((tok = strsep(&lstart, ":")) != NULL) {
config[i]->daymax = atoi(tok);
if ((p = strchr(tok, ';')) != NULL) alloc_cp(&config[i]->messages[DAYMSG], p+1);
if ((p = strchr(tok, ';')) != NULL)
alloc_cp(&config[i]->messages[DAYMSG], p + 1);
}
if ((tok = strsep(&lstart, ":")) != NULL)
{
if ((tok = strsep(&lstart, ":")) != NULL) {
config[i]->warntime = atoi(tok);
}
}
if (!config[i]->times || !config[i]->ttys ||
!config[i]->users || !config[i]->groups)
{
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);
syslog(LOG_ERR, "Error on line %d of config file (%s). Line ignored.", linenum,
CONFIG);
closelog();
}
else i++;
} else
i++;
}
}
config[i] = NULL;
@ -686,27 +672,26 @@ void read_config()
#ifdef DEBUG
i = 0;
while (config[i])
{
while (config[i]) {
printf("line %d: ", i);
j = 0;
while (config[i]->times[j].days)
printf("%d(%d-%d):", config[i]->times[j].days,
config[i]->times[j].starttime,
config[i]->times[j].endtime),j++;
config[i]->times[j].starttime, config[i]->times[j].endtime), j++;
printf("%s:%s:%s:%s:%d;%s:%d;%s:%d;%s:%d\n",
config[i]->ttys,
config[i]->users,
config[i]->groups,
config[i]->login_allowed ? "LOGIN" : "NOLOGIN",
config[i]->idlemax,
config[i]->messages[IDLEMSG] == NULL?"builtin":config[i]->messages[IDLEMSG],
config[i]->messages[IDLEMSG] ==
NULL ? "builtin" : config[i]->messages[IDLEMSG],
config[i]->sessmax,
config[i]->messages[SESSMSG] == NULL?"builtin":config[i]->messages[SESSMSG],
config[i]->messages[SESSMSG] ==
NULL ? "builtin" : config[i]->messages[SESSMSG],
config[i]->daymax,
config[i]->messages[DAYMSG] == NULL ? "builtin" : config[i]->messages[DAYMSG],
config[i]->warntime
),i++;
config[i]->warntime), i++;
}
printf("End debug output.\n");
#endif /* DEBUG */
@ -715,18 +700,12 @@ printf("End debug output.\n");
char chktimes(te)
struct time_ent *te;
{
while (te->days)
{
while (te->days) {
if (daynums[now.tm_wday] & te->days && /* Date within range */
((te->starttime <= te->endtime && /* Time within range */
now_hhmm >= te->starttime &&
now_hhmm <= te->endtime)
||
(te->starttime > te->endtime &&
(now_hhmm >= te->starttime ||
now_hhmm <= te->endtime))
)
)
now_hhmm >= te->starttime && now_hhmm <= te->endtime)
|| (te->starttime > te->endtime
&& (now_hhmm >= te->starttime || now_hhmm <= te->endtime))))
return 1;
te++;
}
@ -740,22 +719,19 @@ char *in_set;
char *t;
char *set = (char *) malloc(strlen(in_set) + 1);
if (set == NULL) bailout("Out of memory", 1);
else strcpy(set, in_set);
if (set == NULL)
bailout("Out of memory", 1);
else
strcpy(set, in_set);
t = strtok(set, " ,");
while (t)
{
if (t[strlen(t)-1] == '*')
{
if (!strncmp(t, element, strlen(t) - 1))
{
while (t) {
if (t[strlen(t) - 1] == '*') {
if (!strncmp(t, element, strlen(t) - 1)) {
free(set);
return 1;
}
}
else if (!strcmp(t, element))
{
} else if (!strcmp(t, element)) {
free(set);
return 1;
}
@ -785,20 +761,16 @@ char *user;
#ifndef SUNOS
login_p->elem.ut_type == USER_PROCESS &&
#endif
!strncmp(login_p->elem.ut_user, user, 8) &&
chkmatch(login_p->elem.ut_line, config[configline]->ttys))
{
!strncmp(login_p->elem.ut_user, user, 8)
&& chkmatch(login_p->elem.ut_line, config[configline]->ttys)) {
#ifdef DEBUG_WTMP
struct tm *tm;
tm = localtime(&(login_p->elem.ut_time));
fprintf(stderr, "%d:%d %s %s %s\n",
tm->tm_hour,tm->tm_min, login_p->elem.ut_line,
login_p->elem.ut_user,
"login");
tm->tm_hour, tm->tm_min, login_p->elem.ut_line, login_p->elem.ut_user, "login");
#endif
prev_p = logout_p = login_p->next;
while (logout_p)
{
while (logout_p) {
/*
* SA19931128
* If there has been a crash, then be reasonably fair and use the
@ -808,8 +780,7 @@ char *user;
* was down.
*/
#ifndef SUNOS
if (logout_p->elem.ut_type == BOOT_TIME)
{
if (logout_p->elem.ut_type == BOOT_TIME) {
logout_p = prev_p;
break;
}
@ -822,13 +793,14 @@ char *user;
}
#ifdef DEBUG_WTMP
if (logout_p)
{
if (logout_p) {
tm = localtime(&(logout_p->elem.ut_time));
fprintf(stderr, "%d:%d %s %s %s\n",
tm->tm_hour,tm->tm_min, logout_p->elem.ut_line,
logout_p->elem.ut_user, "logout");
fprintf(stderr, "%s %d minutes\n", user, ((logout_p?logout_p->elem.ut_time:time_now) - login_p->elem.ut_time)/60);
tm->tm_hour, tm->tm_min, logout_p->elem.ut_line, logout_p->elem.ut_user,
"logout");
fprintf(stderr, "%s %d minutes\n", user,
((logout_p ? logout_p->elem.ut_time : time_now) -
login_p->elem.ut_time) / 60);
}
#endif
daytime += (logout_p ? logout_p->elem.ut_time : time_now) - login_p->elem.ut_time;
@ -854,19 +826,24 @@ char *host;
#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);
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");
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: */
sprintf(cmdbuf, "su %s -c \"xmessage -display %s -center 'WARNING: You will be logged out in %d minute%s when your %s limit expires.'&\"", user, host, time_remaining, time_remaining==1?"":"s", limit_names[limit_type]);
sprintf(cmdbuf,
"su %s -c \"xmessage -display %s -center 'WARNING: You will be logged out in %d minute%s when your %s limit expires.'&\"",
user, host, time_remaining, time_remaining == 1 ? "" : "s",
limit_names[limit_type]);
system(cmdbuf);
/*#ifdef DEBUG */
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
@ -877,15 +854,14 @@ char *host;
return;
}
if ((fd = open(tty, O_WRONLY|O_NOCTTY|O_NONBLOCK)) < 0 ||
(ttyf = fdopen(fd, "w")) == NULL)
{
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, "\r\nWARNING:\r\nYou will be logged out in %d minute%s when your %s limit expires.\r\n",
fprintf(ttyf,
"\r\nWARNING:\r\nYou will be logged out in %d minute%s when your %s limit expires.\r\n",
time_remaining, time_remaining == 1 ? "" : "s", limit_names[limit_type]);
fclose(ttyf);
}
@ -911,15 +887,13 @@ int session;
configline = 0;
/* Find primary group for specified user */
if ((pw = getpwnam(user)) == NULL)
{
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)
{
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();
@ -933,19 +907,16 @@ int session;
#endif
/* Check to see if current user matches any entry based on tty/user/group */
while (config[configline])
{
while (config[configline]) {
timematch = chktimes(config[configline]->times);
ttymatch = chkmatch(tty, config[configline]->ttys);
usermatch = chkmatch(user, config[configline]->users);
groupmatch = chkmatch(gr->gr_name, config[configline]->groups);
/* If the primary group doesn't match this entry, check secondaries */
setgrent();
while (!groupmatch && (secgr = getgrent()) != NULL)
{
while (!groupmatch && (secgr = getgrent()) != NULL) {
p = secgr->gr_mem;
while (*p && !groupmatch)
{
while (*p && !groupmatch) {
/*
printf("Group %s member %s\n", secgr->gr_name, *p);
*/
@ -953,21 +924,18 @@ printf("Group %s member %s\n", secgr->gr_name, *p);
groupmatch = chkmatch(secgr->gr_name, config[configline]->groups);
p++;
}
/*
free(gr);
*/
}
/*
endgrent();
*/
/* If so, then check their idle, daily and session times in turn */
if (timematch && ttymatch && usermatch && groupmatch)
{
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);
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);
@ -977,7 +945,8 @@ printf("Group %s member %s\n", secgr->gr_name, *p);
return NOLOGIN;
limit_type = IDLEMSG;
if (disc == N_TTY && config[configline]->idlemax > 0 && idle >= config[configline]->idlemax)
if (disc == N_TTY && config[configline]->idlemax > 0
&& idle >= config[configline]->idlemax)
return IDLEMAX;
limit_type = SESSMSG;
@ -990,12 +959,13 @@ printf("Group %s member %s\n", secgr->gr_name, *p);
/* If none of those have been exceeded, then warn users of upcoming logouts */
limit_type = DAYMSG;
if (config[configline]->daymax > 0 && daytime >= config[configline]->daymax - config[configline]->warntime)
if (config[configline]->daymax > 0
&& daytime >= config[configline]->daymax - config[configline]->warntime)
warnpending(dev, config[configline]->daymax - daytime, user, host);
else
{
else {
limit_type = SESSMSG;
if (config[configline]->sessmax > 0 && session >= config[configline]->sessmax - config[configline]->warntime)
if (config[configline]->sessmax > 0
&& session >= config[configline]->sessmax - config[configline]->warntime)
warnpending(dev, config[configline]->sessmax - session, user, host);
}
@ -1009,8 +979,8 @@ printf("Group %s member %s\n", secgr->gr_name, *p);
return ACTIVE;
}
void check_idle() /* Check for exceeded time limits & logoff exceeders */
{
void check_idle()
{ /* Check for exceeded time limits & logoff exceeders */
char user[sizeof(utmpp->ut_user)];
char host[sizeof(utmpp->ut_host)];
struct stat status, *pstat;
@ -1033,7 +1003,8 @@ void check_idle() /* Check for exceeded time limits & logoff exceeders */
if (!config[0])
return; /* no entries in config */
while (config[++aktconfigline] && aktconfigline >= 0)
if(strcmp(config[aktconfigline]->users, user) == 0 || config[aktconfigline]->users[0] == '*') {
if (strcmp(config[aktconfigline]->users, user) == 0
|| config[aktconfigline]->users[0] == '*') {
aktconfigline = -2; /* we found user or * in config, so he/they has/have restrictions */
break;
}
@ -1041,7 +1012,8 @@ void check_idle() /* Check for exceeded time limits & logoff exceeders */
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);
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 */
@ -1053,11 +1025,9 @@ void check_idle() /* Check for exceeded time limits & logoff exceeders */
strncpy(dev, utmpp->ut_line, sizeof(dev) - 1); /* get device name */
dev[sizeof(dev) - 1] = '\0';
sprintf(path, "/dev/%s", dev);
if (stat(path, pstat) && chk_xsession(dev, host) != TIMEOUTD_XSESSION_LOCAL) /* if can't get status for
if (stat(path, pstat) && chk_xsession(dev, host) != TIMEOUTD_XSESSION_LOCAL) { /* if can't get status for
port && if it's not a local Xsession */
{
sprintf(errmsg, "Can't get status of user %s's terminal (%s)\n",
user, dev);
sprintf(errmsg, "Can't get status of user %s's terminal (%s)\n", user, dev);
/* bailout(errmsg, 1); MOH: is there a reason to exit here? */
return;
}
@ -1069,17 +1039,17 @@ void check_idle() /* Check for exceeded time limits & logoff exceeders */
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);
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 if (chk_xterm(dev, host))
return;
else
#endif
idle = (time_now - max(pstat->st_atime, pstat->st_mtime)) / 60;
sesstime = (time_now - utmpp->ut_time) / 60;
switch(chk_timeout(user, dev, host, idle, sesstime))
{
switch (chk_timeout(user, dev, host, idle, sesstime)) {
case ACTIVE:
#ifdef DEBUG
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
@ -1114,7 +1084,8 @@ void check_idle() /* Check for exceeded time limits & logoff exceeders */
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);
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
@ -1166,18 +1137,18 @@ int tty;
if (config[configline]->messages[limit_type])
msgfile = fopen(config[configline]->messages[limit_type], "r");
if (msgfile)
{
if (msgfile) {
while ((cnt = read(tty, msgbuf, 1024)) > 0)
write(tty, msgbuf, cnt);
fclose(msgfile);
}
else
{
} else {
if (limit_type == NOLOGINMSG)
sprintf(msgbuf, "\r\n\r\nLogins not allowed at this time. Please try again later.\r\n");
sprintf(msgbuf,
"\r\n\r\nLogins not allowed at this time. Please try again later.\r\n");
else
sprintf(msgbuf, "\r\n\r\nYou have exceeded your %s time limit. Logging you off now.\r\n\r\n", limit_names[limit_type]);
sprintf(msgbuf,
"\r\n\r\nYou have exceeded your %s time limit. Logging you off now.\r\n\r\n",
limit_names[limit_type]);
write(tty, msgbuf, strlen(msgbuf));
}
}
@ -1200,8 +1171,7 @@ char *host;
return;
}
/* 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)
{
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();
@ -1215,14 +1185,16 @@ char *host;
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__);
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);
syslog(LOG_NOTICE, "User %s (pid:%d, cpid:%d) logged in via ssh from %s.", user, pid, cpid,
host);
closelog();
#endif
pid = cpid;
@ -1245,16 +1217,15 @@ char *host;
/* Wait a little while in case the above message gets lost during logout */
#ifdef SURE_KILL
signal(SIGHUP, SIG_IGN);
if ((pw = getpwnam(user)) == NULL)
{
if ((pw = getpwnam(user)) == NULL) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Could not log user %s off line %s - unable to determine uid.", user, dev);
closelog();
}
if (setuid(pw->pw_uid))
{
if (setuid(pw->pw_uid)) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Could not log user %s off line %s - unable to setuid(%d).", user, dev, pw->pw_uid);
syslog(LOG_ERR, "Could not log user %s off line %s - unable to setuid(%d).", user, dev,
pw->pw_uid);
closelog();
}
kill(-1, SIGHUP);
@ -1266,8 +1237,7 @@ char *host;
if (!kill(pid, 0)) { /* SIGHUP might be ignored */
kill(pid, SIGKILL); /* then send sure "kill" signal */
sleep(KWAIT);
if (!kill(pid, 0))
{
if (!kill(pid, 0)) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_ERR, "Could not log user %s off line %s.", user, dev);
closelog();
@ -1284,22 +1254,24 @@ int signum;
if (!allow_reread)
pending_reread = 1;
else
{
else {
pending_reread = 0;
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_NOTICE, "Re-reading configuration file.");
closelog();
while (config[i])
{
while (config[i]) {
free(config[i]->times);
free(config[i]->ttys);
free(config[i]->users);
free(config[i]->groups);
if (config[i]->messages[IDLEMSG]) free(config[i]->messages[IDLEMSG]);
if (config[i]->messages[DAYMSG]) free(config[i]->messages[DAYMSG]);
if (config[i]->messages[SESSMSG]) free(config[i]->messages[SESSMSG]);
if (config[i]->messages[NOLOGINMSG]) free(config[i]->messages[NOLOGINMSG]);
if (config[i]->messages[IDLEMSG])
free(config[i]->messages[IDLEMSG]);
if (config[i]->messages[DAYMSG])
free(config[i]->messages[DAYMSG]);
if (config[i]->messages[SESSMSG])
free(config[i]->messages[SESSMSG]);
if (config[i]->messages[NOLOGINMSG])
free(config[i]->messages[NOLOGINMSG]);
free(config[i]);
i++;
}
@ -1328,19 +1300,19 @@ char *host;
if (chk_xsession(d, host) || chk_xterm(d, host))
return N_TTY;
if ((fd = open(d, O_RDONLY|O_NONBLOCK|O_NOCTTY)) < 0)
{
if ((fd = open(d, O_RDONLY | O_NONBLOCK | O_NOCTTY)) < 0) {
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_WARNING, "Could not open %s for checking line discipline - idle limits will be enforced.", d);
syslog(LOG_WARNING,
"Could not open %s for checking line discipline - idle limits will be enforced.", d);
closelog();
return N_TTY;
}
if (ioctl(fd, TIOCGETD, &disc) < 0)
{
if (ioctl(fd, TIOCGETD, &disc) < 0) {
close(fd);
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_WARNING, "Could not get line discipline for %s - idle limits will be enforced.", d);
syslog(LOG_WARNING, "Could not get line discipline for %s - idle limits will be enforced.",
d);
closelog();
return N_TTY;
}
@ -1349,7 +1321,9 @@ char *host;
#ifdef DEBUG
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(SYSLOG_DEBUG, "TTY %s: Discipline=%s.",d,disc==N_SLIP?"SLIP":disc==N_TTY?"TTY":disc==N_PPP?"PPP":disc==N_MOUSE?"MOUSE":"UNKNOWN");
syslog(SYSLOG_DEBUG, "TTY %s: Discipline=%s.", d,
disc == N_SLIP ? "SLIP" : disc == N_TTY ? "TTY" : disc ==
N_PPP ? "PPP" : disc == N_MOUSE ? "MOUSE" : "UNKNOWN");
closelog();
#endif
@ -1382,8 +1356,7 @@ char *dev,*host;
closelog();
#endif
return TIMEOUTD_XSESSION_LOCAL;
}
else if (strstr(dev, ":") && strlen(host) > 1 && gethostbyname(host)) {
} else if (strstr(dev, ":") && strlen(host) > 1 && gethostbyname(host)) {
/* What about remote XDMCP sessions?
* USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
* mark pts/3 mercury Sat11 0.00s 10.99s 0.04s w
@ -1396,8 +1369,7 @@ char *dev,*host;
closelog();
#endif
return TIMEOUTD_XSESSION_REMOTE;
}
else {
} else {
#ifdef DEBUG
openlog("timeoutd", OPENLOG_FLAGS, LOG_DAEMON);
syslog(LOG_DEBUG, "NO xsession detected. device=%s host=%s", dev, host);
@ -1423,8 +1395,7 @@ char *dev,*host;
closelog();
#endif
return 1;
}
else
} else
return 0;
} /* chk_xterm(dev,host) */
@ -1438,7 +1409,8 @@ char *host, *user;
if (limit_type == NOLOGINMSG) {
sprintf(msgbuf, "Logins not allowed at this time. Please try again later.");
} else {
sprintf(msgbuf, "You have exceeded your %s time limit. Logging you off now.", limit_names[limit_type]);
sprintf(msgbuf, "You have exceeded your %s time limit. Logging you off now.",
limit_names[limit_type]);
}
/* then send the message using xmessage */
@ -1460,8 +1432,7 @@ char *host, *user;
if (!kill(pid, 0)) { /* SIGHUP might be ignored */
kill(pid, SIGKILL); /* then send sure "kill" signal */
sleep(KWAIT);
if (!kill(pid, 0))
{
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();
@ -1483,7 +1454,8 @@ pid_t 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);
syslog(LOG_WARNING, "chk_ssh(): PID %d does not exist. Something went wrong. Ignoring.",
pid);
closelog();
return 0;
}
@ -1561,7 +1533,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);
syslog(LOG_NOTICE, "Could not connect to %s to query idle-time for %s. Ignoring.", display,
user);
closelog();
} else {
if (!mitInfo)