how to logout someone after a period of inactivity 2 : SUMMARY 2

From: Bendriss Elmehdi <e.bendriss_at_menara.ma>
Date: Fri Apr 11 2003 - 13:11:42 EDT
hi
some one asked me to summarize :
many told me to put an : export TMOUT=xx (xx in seconds) in /etc/profile :
usefull above all when users use xterm
many others recommend the "autologout" environment variable (csh) that do
the
same thing, but users may avoid it by putting something like : unset
autologout in their own profile file
some one sent me a link : http://access1.sun.com/codesamples/kstat.html
there's a source code to get mouse and keyboard idle time : doesn't suite me
because my users connect using telnet/ssh
Beavers, Reginald sent me this shell code that i haven't try yet : as he
said
: logicaly it should do the job:
###################################################
#!/bin/sh

# egrep those who are immune

who -u | egrep -v 'root|me' |while read line; do

pid=`echo $line | awk '{print $7}'`
idle=`echo $line | awk '{print $6}' | awk -F: '{print $2}'`

if [ $idle >= 10 ];
kill -9 $pid
fi

done
#####################################################"

and finaly, the C source code that John Adams sent me;

/* "@(#) autologout.c by David Dickson" */

/* modifications for exempt list processing by John Adams <jna@retina.net> */

#include <stdio.h>

#include <signal.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <utmp.h>

#define GRACE 60 /* grace time (sec) for user reply */

#define KWAIT 20 /* time to wait after kill (in sec.) */

#define WARNING 1 /* a warning message */

#define LOGOFF 2 /* a log-off message */

#define NOLOGOFF 3 /* a log-off failure message */

#define SEC_M 60 /* seconds per minute */

#define TIMEMAX 20 /* maximum idle time (minutes) */

/* uncomment this to not fork during processing */

/* #define NOFORK */

/* uncomment this to send mail to users when they go idle */

/* #define MAIL_USER */

struct utmp *utmpp; /* pointer to utmp file entry */

char *ctime(); /* returns pointer to time string */

struct utmp *getutent(); /* returns next utmp file entry */

typedef struct listent {

char user[8];

struct listent *next;

} LIST;



/* exempt list */

LIST *head;

LIST *tail;

LIST *list_append(LIST *tail,char *item)

{

LIST *lp = (LIST *)malloc(sizeof(struct listent));

if (tail == NULL)

{

tail = lp;

strcpy(tail->user,item);

tail->next = NULL;

}

else

{

/* append to end */

tail->next = lp;

strcpy(lp->user,item);

lp->next = NULL;

}

/* return a pointer to the end */

return lp;

}

list_free(LIST *h)

{

/* walk a list, disposing of elements */

LIST *p = h;

LIST *next;

while (p != NULL)

{

next = p->next;

free(p);

p = next;

}

}

int list_search(LIST *h,char *uname)

{

LIST *p;

p = h; /* start at beginning */

while (p != NULL)

{

if (strcmp(p->user,uname) == 0)

return 1;

p = p->next;

}


/* not found */

return 0;

}



void init_exempt()

{

FILE *f;

char readbuf[255];

head=NULL;

tail=NULL;

if ((f = fopen("/etc/autologout.exempt","r")) != NULL)

{

while (!feof(f))

{

fgets(readbuf,255,f);

/* hack off newline */

if (readbuf[strlen(readbuf)-1] == '\n')

readbuf[strlen(readbuf)-1] = '\0';


/* strip comments and blanks */

if (readbuf[0] != ' ' && readbuf[0] != '#' && ( *readbuf != '\0' ))

{

tail = list_append(tail,readbuf);

if ( head == NULL )

head = tail;

}

}

}

fclose(f);

}



main()

{

#ifndef NOFORK

if (fork()) /* the parent process */

exit(0); /* exits */

#endif

/* load up the exempt list */

init_exempt();

/* the child processes all utmp file entries: */

while ((utmpp = getutent()) != (struct utmp *) NULL)

check_idle();

/* free the list */

list_free(head);

exit(0); /* done, so bye */

}

check_idle() /* select utmp entries older than TIMEMAX */

{

char dev[24], name[12];

struct stat status, *pstat;

time_t idle, pres_time, start, time();

pstat = &status; /* point to status structure */

if (utmpp->ut_type != USER_PROCESS) /* if not user process */

return(0); /* skip the utmp entry */

strcpy(dev, "/dev/"); /* add "/dev/" directory prefix */

strcat(dev, utmpp->ut_line); /* append basename of port */

if (stat(dev, pstat)) /* if can't get status for port */

bailout("can't get status of user's terminal", 1);

pres_time = time((time_t *)0); /* get current time */

strncpy(name, utmpp->ut_user, 8); /* else get user name */

name[8] = '\0'; /* null terminate user name string */

/* should we even bother? */

if (list_search(head,name))

return; /* exempt. forget it. */

/* idle time is current time less last access time: */

idle = (pres_time - pstat->st_atime) / SEC_M;

if (idle < TIMEMAX) /* if user was recently active */

return(0); /* ignore this utmp entry */


mesg(WARNING, name, dev, (int)idle); /* send warning to user */

if (stat(dev, pstat))

bailout("can't get status of user's terminal", 2);

start = pstat->st_atime; /* start time for countdown */

sleep(GRACE);

if (stat(dev, pstat))

bailout("can't get status of user's terminal", 3);

if (start < pstat->st_atime) /* user did something */

return(0);

else { /* user abandoned terminal */

if (!killit(utmpp->ut_pid))

mesg(NOLOGOFF, name, dev, (int)idle); /* couldn't kill */

else

mesg(LOGOFF, name, dev, (int)idle); /* okay */

return(0);

}

}

mesg(flag, name, dev, idle) /* mail to user and log message */

int flag, idle; /* flag indicates message type */

char *name, *dev;

{

char mbuf[256]; /* message buffer */

time_t tvec;

FILE *fopen(), *fp, *log, *mprog;

time(&tvec); /* store time in tvec */

if ((log = fopen("/usr/adm/logoutlog", "a")) == (FILE *) NULL)

bailout("can't open log file", 4);

if (flag == WARNING) { /* process warning message */

if ((fp = fopen(dev, "w")) == (FILE *) NULL)

bailout("can't open user's terminal", 5);

fprintf(fp,

"%s: You've been idle for %d min.\07\n", name, idle);

fprintf(fp,

"you'll be logged off in 60 sec. unless you hit return:");

fclose(fp);

fprintf(log, "** WARNING ** %s %s (%d min idle time) %s",

name, dev+5, idle, ctime(&tvec)+3);

}

if (flag == LOGOFF) { /* process log-off message */

fprintf(log, "** LOGOFF ** %s %s (%d min idle time) %s",

name, dev + 5, idle, ctime(&tvec) + 3);

sprintf(mbuf, "/bin/mail %s", name);

#ifdef MAIL_USER

/* open pipe to mail program for writing */

if ((mprog = popen(mbuf, "w")) == (FILE *) NULL)

bailout("can't use /bin/mail program", 6);

fprintf(mprog, "Subject: Excess Idle Time\nLogged \

off - excess idle time - %s %s\ntty = %s\n",

name, ctime(&tvec), dev + 5);

fclose(mprog);

#endif /* MAIL_USER */

}

if (flag == NOLOGOFF) { /* send mail to root if can't kill */

fprintf(log, "** LOGOFF-FAIL ** %s (pid = %d) \

%s (%d min idle time) %s",

name, utmpp->ut_pid, dev+5, idle, ctime(&tvec)+3);

sprintf(mbuf, "/bin/mail root");

if ((mprog = popen(mbuf, "w")) == (FILE *) NULL)

bailout("can't use /bin/mail program", 7);

fprintf(mprog, "Subject: Can't logoff %s\nCan't Log off \

- %s %s\ntty = %s\n", name, name, ctime(&tvec), dev+5);

fclose(mprog);

}

fclose(log);

return(0);

}

killit(pid) /* terminate process using SIGHUP, then SIGKILL */

int pid;

{

kill(pid, SIGHUP); /* first send "hangup" signal */

sleep(KWAIT);

if (!kill(pid, 0)) { /* SIGHUP might be ignored */

kill(pid, SIGKILL); /* then send sure "kill" signal */

sleep(KWAIT);

if (!kill(pid, 0))

return(0); /* failure--refuses to die! */

else

return(1); /* successful kill with SIGKILL */

} else

return(1); /* successful kill with SIGHUP */

}

bailout(message, status) /* display error message and exit */

int status; /* exit status */

char *message; /* pointer to the error message */

{

fprintf(stderr, "autologout: %s\n", message);

exit(status);

}




that's it.

Cordialement/ Best Regards
BENDRISS Elmehdi
Administrateur Systhme
Tel : +212 37 71 88 73
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Fri Apr 11 13:14:24 2003

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:43:08 EST