[go: up one dir, main page]

File: autolog.c

package info (click to toggle)
autolog 0.40-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 280 kB
  • ctags: 101
  • sloc: ansic: 833; sh: 86; makefile: 58
file content (1087 lines) | stat: -rw-r--r-- 38,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
/*****************************************************************************/
/*									     */
/*   Programm: autolog.c 	C-Programm to log out sleeping users	     */
/*             version 0.40						     */
/*									     */
/*   Autor:  Carsten Juerges    Erster Versuch:  06.04.2000		     */
/*	     Kurze Wanne 1      Letztes Update:  28.04.2000		     */
/*	     30926 Seelze						     */
/*	     Germany            send me a postcard, if you like. :)	     */
/*	     juerges@cip-bau.uni-hannover.de 				     */
/*									     */
/*   Rechner:  AMD K6-2		SuSE Linux 6.3				     */
/*									     */
/*   Meldung:  This version seems to worke quite good.			     */
/*									     */
/*   Auftauchende Probleme, known problems / features:			     */
/*	- a program without any login (e.g. start Netscape and log off )     */
/*	  will be kicked out as lost process.				     */
/*	- to become not idle he has to type something into a terminal	     */
/*	  when in beeps somewhere.                                           */
/*	- some lines "//##" have to be adapted to your specific linux.       */
/*	  maybe your ps has different options or a different output.         */
/*	- Looks quite dirty, should be beautified, sometimes :)		     */
/*									     */
/*   Warning:								     */
/*      - Keep in mind: Someone who is kicked off might loose some data.     */
/*      - This program is distributed in the hope that it will be useful,    */
/*        but WITHOUT ANY WARRANTY; without even the implied warranty of     */
/*        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	     */
/*        GNU General Public License for more details.			     */
/*									     */
/*****************************************************************************/
/*  "@(#) autolog.c      0.34
    Originally ported     by David Dickson"
    Modified		  by Michael C. Mitchell, 15Oct94
    Rewritten		  by Kyle Bateman,	  Nov94, Aug95
    Adapted to my System, by Carsten Juerges,	  Apr 2000

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include    <stdio.h>
#include    <signal.h>
#include    <string.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include    <utmp.h>
#include    <time.h>
#include    <pwd.h>
#include    <grp.h>
#include    <regex.h>
#include    <malloc.h>

#define     D_IDLE      30  /* maximum idle time (minutes)		*/
#define     D_GRACE    120  /* grace time (sec) for user reply		*/
#define     D_HARD       0  /* consider connect time only		*/
#define     D_MAIL       1  /* notify user by mail			*/
#define     D_CLEAR      0  /* clear user terminal before warning	*/
#define     D_WARN       1  /* warn user before killing			*/
#define     D_LOG        1  /* log activity in logfile			*/

#define     WARNING      1  /* a warning message			*/
#define     LOGOFF       2  /* a log-off message			*/
#define     NOLOGOFF     3  /* a log-off failure message		*/
#define     ANGRY        4  /* a log-off user returned message		*/
#define     IDLEPUNISH   5  /* punish idling-users message		*/

#define     HUP_WAIT	20  /* time to wait after SIGHUP  (in sec.)	*/
#define     KILLWAIT	 3  /* time to wait after SIGKILL (in sec.)	*/
#define     Max_Sleep 1800  /* max. sleeping time for this daemon ;-)	*/
#define     ChckSleep   90  /* check whether user logged in again	*/
#define     Idle_Ban   120  /* punishment-time for idle users		*/
//#define     HUP_WAIT	 2  /* (demo / debug)				*/
//#define     KILLWAIT	 1  /* (demo / debug)				*/
//#define     Max_Sleep	10  /* (demo / debug)				*/
//#define     ChckSleep	10  /* (demo / debug)				*/


#define     STRLEN      64
#define     LINELEN     256
#define     MAXCONF     512 /* maximum lines in config file */

typedef struct{
	char   Name[20];    /* Name of user.				*/
	char   Device[20];  /* Outputdevice with the minimum idle-time	*/
			    /* "." serves as flag that user logged out, */
			    /*     when chasing lost processes.         */
	uid_t  UserID;	    /* to check whether user has changed	*/
	int    IdleTime;    /* Minimum-time the user is idle		*/
	int    SessStrt;    /* Starttime of the users session	 	*/
	int    Ban_Ends;    /* Endtime of banning user this user	*/
	int    WarnEnds;    /* End of grace-time (after warning).	*/
} userdata;		    /*   e.g. 30 Mins after hardkick.           */

struct utmp *utmpp;         /* pointer to utmp file entry */
char delims[] = "\n\t ";    /* valid delimiters in config file */
char anystrg[] = ".+";      /* matches anything */

int debug	= 0;
int resist	= 1;	    /* 0=> ordin. program 1=> behave as daemon. */
int do_bite	= 1;
int listall	= 0;
char *confname	= "/etc/autolog.conf";
char *logfname	= "/var/log/autolog.log";
char *datfname	= "/var/lib/autolog/autolog.data";
int g_idle	= D_IDLE;
int g_grace	= D_GRACE;
int g_hard	= D_HARD;
int g_mail	= D_MAIL;
int g_clear	= D_CLEAR;
int g_warn	= D_WARN;
int g_log	= D_LOG;

typedef struct conf
    {
    char    *name;	 /* user name			*/
    char    *group;	 /* group name			*/
    char    *line;	 /* tty line			*/
    int     idle;	 /* minutes of idle time	*/
    int     grace;	 /* minutes of grace time	*/
    int     ban;	 /* ban user for ... minutes	*/
    int     hard;	 /* consider connect time only	*/
    int     mail;	 /* notify by mail		*/
    int     clear;	 /* clear screen before warn	*/
    int     warn;	 /* warn before kill		*/
    int     log;	 /* log actions to logfile	*/
    } conf_el;
conf_el c_arr[MAXCONF];
									
int sleeptime	= Max_Sleep; /* maximum amount of time	*/
int sleep_max	= Max_Sleep; /*  to sleep for daemon.	*/
int c_idx	= 0;

int	  userfill=0;	 /* different users found.	*/
int	  usermax =0;	 /* current size of List.	*/
userdata* userlst;	 /* Store user-Data here.	*/

int  	  ids_fill;
int	  ids_max;  	
int	  *ids_lst;

time_t    time();	 /* they are used so often      */
time_t    pres_time;     /* current time.		*/
time_t    chck_pid=0;	 /* don't check pids before ... */
int       lostkill=1;    /* 1 => kill lost processes.   */

char      *ps_cmd;

main(int argc, char *argv[])
    {
    int i;
    for (i = 1; i < argc; i++){
	if (argv[i][0] == '-'){
	    switch(tolower(argv[i][1])) {
		case 'r': resist  = 1;			   break; /* debug */
		case 'o': resist  = 0;			   break;
		case 'a': listall = 1;			   break;
		case 'b': do_bite = 0;/* I got used to */  break;
		case 'd': debug   = 1;			   break;
		case 'h': g_hard  = 1;			   break;
		case 'n': do_bite = 0;			   break;
		case 'l': logfname = argv[++i];		   break;
		case 'f': confname = argv[++i];		   break;
		case 't': g_idle = atoi(argv[++i]);	   break;
		case 'g': g_grace = atoi(argv[++i]);	   break;
		case 'm': g_mail  = (argv[++i][0] == 'y'); break;
		case 'c': g_clear = (argv[++i][0] == 'y'); break;
		case 'w': g_warn  = (argv[++i][0] == 'y'); break;
		case 'L': g_log   = (argv[++i][0] == 'y'); break;
		default:
		fprintf(stderr,"autologout: illegal switch: %s\n", argv[i]);
	    }
	}	
	else    fprintf(stderr,"autologout: illegal parameter: %s\n", argv[i]);
    } /* for */
    ps_cmd="ps aux";

    time(&pres_time);		  /* get current time for log-file */
    log_msg("Starting Service");
    eat_confile();		  /* read config file		    */
    if (!debug)			  /* if not in debug mode,	    */
        if (fork()) exit(0);      /* the parent process exits here. */
//    if (!debug && resist)	  /* if not in debug mode,	    */
//        if (fork()) exit(0);      /* the parent process exits here. */

    userfill = 0;
    usermax  = 8;
    userlst  = (userdata*)calloc(sizeof(userdata),1+usermax);

    load_users();
    if (debug){
        printf("\n");
	printf("From the datafile:\n");
	printf("==================\n");
	show_results();
	printf("Done reading datafile\n");
    }

/* now sit in an infinite loop and work */
    do
    {   time(&pres_time);			   /* get current time       */
	sleeptime = sleep_max;			   /* assume max. sleeptime. */
	setutent();
	if (debug)
	    printf("\n");
	while ((utmpp = getutent()) != (struct utmp *) NULL)
	    check_utmp();
	if (debug)
	    show_results();
	for (i=1; i<=userfill; i++){
    	    if (check_idle(&userlst[i])==2){
		if (debug) printf("knock out.\n");
		userlst[i] = userlst[userfill];
		userfill--;
    	    }
	}

/*.. check for lost processes, from time to time. ...........................*/
	if (lostkill && pres_time>chck_pid){
	  if (debug)
	    printf("check for dead processes.\n");
	  kill_lost_PIDs();
	  chck_pid = pres_time+10*60;

	  if (debug)
	    printf("next check for processes after %10d.\n",chck_pid);
	
	}
	if (debug)
	{ //show_results();
	  printf("daemons maximum sleeptime %7d seconds, today.\n",sleep_max);
	  printf("daemon is falling asleep for%5d seconds, now.\n",sleeptime);
	}
	if  (resist || (sleeptime < sleep_max) ) sleep(sleeptime);
    } while (resist ||                 /* Stay when in daemon-mode           */
            (sleeptime < sleep_max) ); /*      or users are to be kicked off */
    save_users();
    log_msg("Stopped service");
}

/***************************************************************
* This function makes sure we will find in our table:          *
*    - shortest idle-time and device of this.                  *
*    - earliest start-time of all logins.                      *
***************************************************************/

check_utmp(){		/* select utmp entries needing killing */
    char dev[STRLEN], name[STRLEN], prname[STRLEN];
    char *msg;
    char tmp_str[80];

    struct stat status;
    time_t idle, atime;

    int	    userpos=0;	/* position of user found, 0 => not found */ 	

    sprintf(prname,"/proc/%d",utmpp->ut_pid);   /* append /proc/ to proclist */
    sprintf(dev,   "/dev/%s" ,utmpp->ut_line);  /* append /dev/ to base name */
    msg="";

/*.. Get information about the current process. .............................*/
    if (stat(prname, &status))          msg="Dead process:    ";
    if (utmpp->ut_type != USER_PROCESS) msg="Non-user process:";
    if (strlen(msg)){
	if (listall)
	    printf("%s N:%-8s P:%5d Login:%s",
		    msg,utmpp->ut_user,utmpp->ut_pid,ctime(&utmpp->ut_time));
	return(0);
    }
    if (dev[5] != ':' && stat(dev, &status)){		     /* if can't get status for port */
	snprintf(tmp_str, 79, "Can't get status of user's terminal: %s", dev);
	bailout(tmp_str, 1);
	return(0);
    }

/*.. Find out everything about current time, sleep time and session time. ...*/
    if (utmpp->ut_time > status.st_atime)	   /* get last access time   */
	 atime = utmpp->ut_time;		   /* proc - starttime 	     */
    else atime = status.st_atime;		   /* proc - accesstime      */
    idle  = (pres_time - atime         ) / 60;     /* total idle minutes     */

/*.. Find out username and groupname of this login. .........................*/
    strncpy(name, utmpp->ut_user, UT_NAMESIZE); /* get user name         */
    name[UT_NAMESIZE] = '\0';         /* null terminate user name string */

    if (listall)
	printf("Checking: %-11s on %-12s I:%4d  T:%d Login: %s",
		name,dev,idle,utmpp->ut_type,ctime(&utmpp->ut_time));

    strcpy(userlst[0].Name,  name);
    strcpy(userlst[0].Device,utmpp->ut_line);
    userlst[0].IdleTime =idle;

/*.. Get Position of user in userlst. .......................................*/
    userpos=userfill;
    while ( strcmp(userlst[userpos].Name,name) ) userpos--; 
	
/*.. if not found -> add user to userlst. ...................................*/
    if ( userpos ==0 ) {
	userfill++;
	if (userfill>usermax){
	    usermax = 2*usermax;
	    userlst = (userdata*)
	               realloc(userlst,sizeof(userdata) * (1+usermax));
	}
	strcpy(userlst[userfill].Name,  name);
	strcpy(userlst[userfill].Device,utmpp->ut_line);
	userlst[userfill].IdleTime = idle;
	userlst[userfill].UserID   = status.st_uid;
	userlst[userfill].SessStrt = utmpp->ut_time;
	userlst[userfill].WarnEnds = 0;
    }
    else{
    	if (userlst[userpos].IdleTime > idle){  /* less idle-time found. ....*/
	    userlst[userpos].IdleTime = idle;
	    strcpy(userlst[userpos].Device,utmpp->ut_line);
	}
    	if (userlst[userpos].SessStrt > utmpp->ut_time){  /* earlier start. */
	    userlst[userpos].SessStrt = utmpp->ut_time;
	}
    }
}

show_results(){
    int	    userpos=0;	/* position of user found, 0 => not found */ 	

    printf("\nfound: %2d\n\n",userfill);
    printf("     Username  UserID Terminal     idle Starttime  BanningEnd WarningEnd\n");

    for (userpos=1; userpos<=userfill; userpos++){
	printf(" -->  %-10s %4d  %-10s %5d %10d %10d %10d\n",
		  userlst[userpos].Name,     userlst[userpos].UserID,
		  userlst[userpos].Device,
		  userlst[userpos].IdleTime,userlst[userpos].SessStrt,
		  userlst[userpos].Ban_Ends,userlst[userpos].WarnEnds);
    }
}

save_users(){
    int	    userpos=0;	/* position of user found, 0 => not found */	

    FILE* f;

    if (!(f=fopen(datfname,"w+"))) {
	bailout("Can't create data-file.", 6);
	return;
    }

    fprintf(f,"%d\n",userfill);

    for (userpos=1; userpos<=userfill; userpos++){
	fprintf(f," %-10s %4d %-10s %5d %10d %10d %10d\n",
		  userlst[userpos].Name,     userlst[userpos].UserID,
		  userlst[userpos].Device,
		  userlst[userpos].IdleTime,userlst[userpos].SessStrt,
		  userlst[userpos].Ban_Ends,userlst[userpos].WarnEnds);
    }
    fclose(f);
}

load_users(){
    int	    userpos=0;	/* position of user found, 0 => not found */	

    FILE* f;

/*.. if no table exists, no problem, just return. ...........................*/
    if (!(f=fopen(datfname,"r"))) { return; }

/*.. check, how many lines with data will follow. ...........................*/
    fscanf(f,"%d",&userfill);

/*.. if necessary extend list. ..............................................*/
    if (userfill>usermax){
	usermax = 2*userfill;
	userlst = (userdata*)
			realloc(userlst,sizeof(userdata) * (1+usermax));
    }

/*.. read all the data-lines. ...............................................*/
    for (userpos=1; userpos<=userfill; userpos++){
	fscanf(f," %s %d %s %d %d %d %d",
		  &userlst[userpos].Name,     &userlst[userpos].UserID,
		  &userlst[userpos].Device,
		  &userlst[userpos].IdleTime, &userlst[userpos].SessStrt,
		  &userlst[userpos].Ban_Ends, &userlst[userpos].WarnEnds);
    }
    fclose(f);
}

/*---------------------------------------------------------------------------*/
/*===    The following functions cope with the config-file.               ===*/
/*---------------------------------------------------------------------------*/

set_defs(int i){
    c_arr[i].name  = anystrg;
    c_arr[i].group = anystrg;
    c_arr[i].line  = anystrg;
    c_arr[i].idle  = g_idle;
    c_arr[i].ban   = 0;
    c_arr[i].grace = g_grace;
    c_arr[i].hard  = g_hard;
    c_arr[i].mail  = g_mail;
    c_arr[i].clear = g_clear;
    c_arr[i].warn  = g_warn;
    c_arr[i].log   = g_log;
}

eat_confile()
    {
    FILE *f;
    char *s, iline[LINELEN];
    int i, lev;
    int idle;

    if (!(f=fopen(confname, "r")) ){
      if (debug)
	printf("Can't find file: %s\n", confname);
    }
    else {
        while (fgets(iline, LINELEN, f)){
            if (*iline == '#' || strlen(iline) <= 1)
                continue;

/*.. check for  ps=command  in config-file,  extract command. ...............*/
            s=strstr(iline,"ps=");
            if (s){
		ps_cmd = strcpy((char *)malloc(strlen(s)+1),s+3);
		continue;
            }
	    s=strstr(iline,"nolostkill");
	    if (s) {
		lostkill = 0;
		continue;
	    }
            set_defs(c_idx);
            s=strtok(iline,delims);
            do{
		lev = 1;
		if (!strncmp(s,"name=",5) && *(s+=5))
		    c_arr[c_idx].name=strcpy((char *)malloc(strlen(s)+1),s);
		else if (!strncmp(s,"group=",6) && *(s+=6))
		     c_arr[c_idx].group=strcpy((char *)malloc(strlen(s)+1),s);
		else if (!strncmp(s,"line=", 5) && *(s+=5))
		    c_arr[c_idx].line=strcpy((char *)malloc(strlen(s)+1),s);
		else if (!strncmp(s,"idle=", 5) && *(s+=5))
		    c_arr[c_idx].idle=atoi(s);
		else if (!strncmp(s,"grace=",6) && *(s+=6))
		    c_arr[c_idx].grace=atoi(s);
		else if (!strncmp(s,"ban=",  4) && *(s+=4))
		    c_arr[c_idx].ban =atoi(s);
		else {
		     if (!strncmp(s,"no",2)){
			lev=0;
			s+=2;
		     }
		     if (!strcmp(s,"hard"))
			c_arr[c_idx].hard=lev;
		     else if (!strcmp(s,"clear"))
			c_arr[c_idx].clear=lev;
		    else if (!strcmp(s,"mail"))
			c_arr[c_idx].mail=lev;
		    else if (!strcmp(s,"warn"))
			c_arr[c_idx].warn=lev;
		    else if (!strcmp(s,"log"))
			c_arr[c_idx].log=lev;
		    else if (debug)
			printf("Unknown token in file: %s: %s\n",confname,s);
		}
	    }
	    while(s=strtok(0,delims));
	    idle=c_arr[c_idx].idle;
	
/*.. Maybe it is necessary to reduce the max. sleeptime to shortest session. */
	    if (0< c_arr[c_idx].hard && 0<idle)	 /* but not to zero seconds. */
		if (2*60*idle < sleep_max)  sleep_max=2*60*idle;
	    c_idx++;
	}
        fclose(f);
    }
    if (!c_idx)                 /* if no entries made yet */
	set_defs(c_idx++);      /* make one */
    if (debug){
	printf("\nConfig File:\n");
	printf("name     group    line  idle grace mail warn log hard  ban\n");
	printf("----------------------------------------------------------\n");
	for(i=0;i < c_idx; i++)
	    printf("%-8s %-8s %-6s %3d %5d %4d %4d %3d %4d %4d\n",
	        c_arr[i].name, c_arr[i].group,c_arr[i].line,c_arr[i].idle,
	        c_arr[i].grace,c_arr[i].mail, c_arr[i].warn,c_arr[i].log,
	        c_arr[i].hard, c_arr[i].ban);
	printf("\n");
	printf("[%s]\n",ps_cmd);
	if (lostkill)	printf("Lost processes will be killed.\n");
	else		printf("Lost processes stay alive.\n");

    }
}

/* I've apparently found some bug in certain LINUX versions of the */
/* regular expressions library routines.  One of these following two */
/* versions of the routine should work for you. */
/* #define AVOID_REGEX_BUG*/
#ifdef AVOID_REGEX_BUG				/* some strange bug in re_exec */
pat_match(char *patt, char *strg)
    {
    struct re_pattern_buffer rpb;
    int len, retval = 0;
	
/*    if (debug)
        printf("pat_match:%s:%s:\n",patt,strg); */
    len = strlen(patt+256);
    rpb.buffer = malloc(len);
    rpb.allocated = len;
    rpb.fastmap = 0;
    rpb.translate = 0;
    if (!re_compile_pattern(patt,strlen(patt),&rpb))
        {
        if (re_match(&rpb,strg,strlen(strg),0,0) == strlen(strg))
            retval = 1;
        }
    free(rpb.buffer);
    return(retval);
    }

#else
/* return true if strg matches the regex in pattern */
pat_match(char *pattern,char *strg)
    {
    re_comp(pattern);
    return(re_exec(strg));
    }
#endif

/*---------------------------------------------------------------------------*/
/*===    This function checks whether we have to kill a session.          ===*/
/*---------------------------------------------------------------------------*/

check_idle(userdata* akt_usr)
    {
    char ddev[STRLEN],*gn = ".*";
    char dev[STRLEN], name[STRLEN], prname[STRLEN];
    int  idle;
    struct stat status;
    time_t start, stime;
    struct passwd *passwd_entry;
    struct group *group_entry;
    conf_el *ce;
    int  i;

    strcpy(name,akt_usr->Name );
    strcpy(dev, akt_usr->Device);
    idle  =     akt_usr->IdleTime;
    stime = pres_time - akt_usr->SessStrt;

    akt_usr->IdleTime = 30000;	     /* 20 days will "clear" the idletime */

    if (debug)
	printf("\nuser: %-10s %-10s %-d\n",name,dev,idle);

    if(dev[0] == ':')   /* ignore xdm */
	    return(0);

    sprintf(ddev,"/dev/%s",dev);	  /* append /dev/ to base name    */

/*.. If user has logged out, check his Session time and so. .................*/

    if (debug)
	printf("now:  %d = %s",pres_time,ctime(&pres_time));

    if (listall)
	printf("\nChecking: %-11s on %-12s I:%-4d\n",name,dev,idle);

    /* now try to find the group of this person */
    /* if usernames in utmp are limited to 8 chars, we will may fail on */
    /* names that are longer than this, so we'll try to find it by uid */
    if (!(passwd_entry = getpwnam(name)))         /* If can't find by name */
	passwd_entry = getpwuid(akt_usr->UserID); /* try by uid */
    if (passwd_entry){
	strcpy(name,passwd_entry->pw_name);
	if(group_entry = getgrgid( passwd_entry->pw_gid ))
	    gn = group_entry->gr_name;
	else if (debug)
		    printf("Can't find group entry for user: %s\n",name);
    }
    else if (debug) printf("Can't find password entry for user: %s\n",name);

    for(i = 0; i < c_idx; i++){
	if (pat_match(c_arr[i].name, name) &&
	    pat_match(c_arr[i].group,gn) &&
	    pat_match(c_arr[i].line, dev)){
//	    if (debug)
//		printf("Match #%2d: U:%-12s Grp:%-8s Line:%-9s Pid:%-6d Sess:%3d:%02d\n",
//		    i+1,name,gn,utmpp->ut_line,utmpp->ut_pid,stime/60,stime%60);
	    if (c_arr[i].idle<0){     /* if user exempt (idle<0) */
		if (debug) printf("User exempt\n");
		return(0);	/* then don't kill him */
	    }
	  ce = &c_arr[i];	/* get address of matched record */
	  break;
	}
    }
    if (i >= c_idx) {
	if (debug) printf("No match for process\n");
	return(0);
    }

/* device empty or new user => check whether banning is over. ...............*/
    if (stat(ddev, &status) || status.st_uid != akt_usr->UserID){
        if (pres_time>akt_usr->Ban_Ends){	/* End of banning-time       */
            akt_usr->SessStrt = pres_time;	/* allow new session. 	     */
            akt_usr->Ban_Ends = 0;		/*  (just for debugging).    */
	    akt_usr->WarnEnds = 0;	        /* user may return, now.     */
            return(2);				/*  0 => user done.	     */
        } else lower_sleep(ChckSleep);		/* user might come back      */

	strcpy(akt_usr->Device, ".");		/* "Flag": user logged out.  */
	
	if (debug){
	    if (stat(ddev, &status))
		printf("user has logged out.\n");
	    else if (status.st_uid != akt_usr->UserID){
		printf("%4d <> %4d\n",status.st_uid,akt_usr->UserID);
		printf("user has changed -> do not disturb.\n");
	    }	
	}
	return(0);
    }

/* Check whether to knock out this user. ....................................*/
    if (!c_arr[i].hard) {	/* if considering idle time */
      if ( akt_usr->Ban_Ends<pres_time ){
	if (idle < ce->idle){		      /* if user was recently active */
	    akt_usr->WarnEnds = 0;	      /* set to not warned.          */
	    return(0);		
	}
	if (debug)
	    printf("Subject to logout   Idle time: %4d (%2d allowed)\n",
			idle,ce->idle);
      }
      else if (debug) printf("User temporary banned.\n");
    }
    else {
        int time_left;
/*.. In case, he leaves now, his ban will end after his banning-time. .......*/
/*.. This will also increase his banning-time in case he returnes to early. .*/
/*.. Set end of banning to  current time + time to ban user. ................*/
	akt_usr->Ban_Ends = pres_time    + 60*ce->ban;	
	if (debug) printf("Banning should end: %10d\n", akt_usr->Ban_Ends );
			
/*.. Make sure, daemon doesn't sleep too long from now. .....................*/
	if (debug) printf("idle-state: %3d / %d:\n",idle,    ce->idle );
	if (debug) printf("sess-state: %3d / %d:\n",stime/60,ce->idle );
	
	time_left=60*ce->idle - stime;
	if (debug)
	    printf("time left: %4d sec.\n", time_left);
	if (0<time_left){			/* there is some time left   */
	     lower_sleep(time_left); return(0);}
	else lower_sleep(ce->grace);		/* process is to be killed   */

	if (debug)
	    printf("Subject to logout   Total time: %4d (%2d allowed)\n",
			stime/60,ce->idle);
    }
    if (stat(ddev, &status)){
	bailout("Can't get status of user's terminal", 2);
	return(0);
    }
	

/*.. action either warning or killing. */
//    if (akt_usr->WarnEndsed && (pres_time > akt_usr->Ban_Ends) ) {
    if (0<akt_usr->WarnEnds && (pres_time > akt_usr->WarnEnds) ) {
	if (debug)
	  printf("Killing user, now.\n");
	
	if (kill_PIDs(name,dev)==1) 	/* try to kill users' processes      */
	     mesg(LOGOFF,  name, ddev, stime/60, idle, ce); /* mail to user  */
	else mesg(NOLOGOFF,name, ddev, stime/60, idle, ce); /* couldn't kill */
	if (ce->hard)
	      akt_usr->WarnEnds =-1;	/* if user returnes at ones -> angry */
	else{ akt_usr->WarnEnds =-2;	
	      akt_usr->Ban_Ends = pres_time /* give other users a chance     */
				+ Idle_Ban; /*  to catch this computer.      */
	     lower_sleep(ChckSleep);
	}
    }
    else if (akt_usr->WarnEnds==-2){    /* punish being idle for too long.   */
	mesg(IDLEPUNISH, name, ddev, stime/60, idle, ce);
	akt_usr->WarnEnds = pres_time+5;   /* give some time to read message */
	lower_sleep(5);
	if (debug)
	  printf("idle-punish\n");
    }
    else if (akt_usr->WarnEnds==-1){
	mesg(ANGRY, name, ddev, stime/60, idle, ce);    /* angry about user. */
	if (debug)
	  printf("I am angry, user returned...\n");
	if (kill_PIDs(name, dev)==1) 		/* try to kill user, at once */
	     mesg(LOGOFF,  name, ddev, stime/60, idle, ce); /* mail to user  */
	else mesg(NOLOGOFF,name, ddev, stime/60, idle, ce); /* couldn't kill */
    }
    else {
      if (akt_usr->WarnEnds==0){
	mesg(WARNING, name, ddev, stime/60, idle, ce);  /* try to warn user. */
	akt_usr->WarnEnds = pres_time+ce->grace;    /* wait until grace ends */
	if (debug)
	  printf("Warning user:%s Line:%s  Sleep %d sec\n",name,dev,ce->grace);
      }
      lower_sleep(ce->grace);
    }
    return(0);
}

/*---------------------------------------------------------------------------*/
/*===    This function finds out the PIDs of the current user.            ===*/
/*---------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
     void get_PIDs                  /* find processes of a given user.        */
/*----------------------------------------------------------------------------*/
     (char *u_name, char *u_dev)                 /* name of the user.                      */
/*----------------------------------------------------------------------------*/
/* this version assumes that ps -au returns one header-line and then	      */
/* lines with usernames, pids and some other stuff			      */
{   char   prname[LINELEN], iline[LINELEN];	
    char   *s;
    char   *ps_name, *ps_pid, *ps_dev;

    struct stat status;

    FILE   *ps;
    int    i, pid, uid;

    if (!(ps = popen(ps_cmd, "r")) ){
	bailout("Can't use ps program", 6);
	return;
    }
    fgets(iline, LINELEN, ps);		 /* get header-line */

    ids_fill =  0;
    while (fgets(iline, LINELEN, ps)){
	ps_name= strtok(iline,delims);   /* manual: Never use this function. */
	ps_pid = strtok(0,delims);
	pid    = atoi(ps_pid);
	for(i=0;i<5;i++) ps_dev = strtok(0,delims);
	sprintf(prname,"/proc/%d",pid);   /* append /proc/ to proclist */
	if (stat(prname, &status))
	   printf("Dead process:    \n");
	uid = status.st_uid;
	if (debug)
	    printf("%-8s/%s -> %5s %5d %5d\n", ps_name, ps_dev,ps_pid, pid,uid);
	if ( strcmp(u_name,ps_name)==0 && strcmp(u_dev,ps_dev)==0 ){
	    if (debug)
		printf("     -> ok, Name = %s, dev = %s  => kill.\n",u_name,u_dev);
	    ids_fill++;
	    if (ids_fill > ids_max) {
		ids_max = 2*(ids_max);
		ids_lst  = (int*) realloc(ids_lst,sizeof(int)*
						  (1+(ids_max)));
	    }
	    ids_lst[ids_fill]=pid;
	}
    }
    fclose(ps);

    if (debug)
    {	printf (" |-> ");
	for (i=1; i<=ids_fill; i++)
	    printf (" %5d",ids_lst[i]);
	printf ("\n");
    }
}

/*---------------------------------------------------------------------------*/
/*===    The following functions have a murder job.                       ===*/
/*---------------------------------------------------------------------------*/

int kill_PIDs(char *u_name, char *u_dev){
    int  i,ok=1;

    ids_max  = 50;
    ids_lst  = (int*)calloc(sizeof(int),1+(int)(ids_max));

    if (debug)
    printf("\nget PIDs for %s\n",u_name);

/*.. Tell processes to hang up. .............................................*/
//    ids_fill =  0;
    get_PIDs(u_name, u_dev);
    if (do_bite)
	for (i=1; i<=ids_fill; i++) kill_HUP(ids_lst[i]);
    sleep(HUP_WAIT);

/*.. Check for processes that still survived and now kill them. .............*/
/*.. This will also kill a user that returns at once. Without warning. :) ...*/
    ids_fill =  0;
    get_PIDs(u_name, u_dev);
    if (do_bite)
	for (i=1; i<=ids_fill; i++) kill_KILL(ids_lst[i]);
    sleep(KILLWAIT);

/*.. Check for processes that survived killing. .............................*/
    if (do_bite)
	for (i=1; i<=ids_fill; i++) { if (kill_Res(ids_lst[i])) ok=0; }

    free (ids_lst);
    return(ok);
}

kill_HUP(int pid){      /* terminate process using SIGHUP, first. */
	 kill(pid, SIGHUP); 	/* first send "hangup" signal     */
    }

kill_KILL(int pid){     /* terminate process using SIGKILL, later.*/
	kill(pid, SIGKILL);	/* then send sure "kill" signal.  */
    }

int kill_Res(int pid){	/* Check for processes that refused to die.*/
    if (!kill(pid, 0))
    	 return(1);		/* failure--refuses to die!	   */
    else return(0);		/* successful kill with SIGKILL    */
}

/*---------------------------------------------------------------------------*/
/*===    The following functions spread the messages to everywhere.       ===*/
/*---------------------------------------------------------------------------*/

mesg(int flag, char *name, char *dev, int stime, int idle, conf_el *ce)
    {
    char    mbuf[LINELEN];          /* message buffer */
    char    *hint;
    time_t  tvec;
    FILE    *fp, *mprog;

    time(&tvec);				/* tvec = current time. .....*/
    if (flag == IDLEPUNISH && ce->warn) {	/* process warning message.  */
	hint="** IDLE-PUNISH **";
      if (!(fp = fopen(dev, "w")) ){
	bailout("Can't open user's terminal", 5);
	return(0);
      }

      if (ce->clear){
	sprintf (mbuf,"clear >%s",dev);
	system (mbuf);
      }
	fprintf(fp,"\n\n\r"); /* \r is sometimes needed...*/
	fprintf(fp,".--------------------------------------------.\n\r");
        fprintf(fp,"|  %10s, you have been idle too long.  |\n\r",name);
        fprintf(fp,"|  Punishment: Log out for %3d seconds!      |\n\r",Idle_Ban);
	fprintf(fp,"`--------------------------------------------'\n\r");
	fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n\r");
	sleep(10);		      /* just some time to read this message */
      fclose(fp);
    }
    if (flag == ANGRY && ce->warn) {		/* process warning message.  */
	hint=" ** ANGRY **";
      if (!(fp = fopen(dev, "w")) ){
	bailout("Can't open user's terminal", 5);
	return(0);
      }
      if (ce->clear){
	sprintf (mbuf,"clear >%s",dev);
	system (mbuf);
      }
      if (ce->hard){
	fprintf(fp,"\n\n\r"); /* \r is sometimes needed...*/
	fprintf(fp,".--------------------------------------------------.\n\r");
        fprintf(fp,"|  %10s, you have just been kicked off.      |\n\r",
		    name);
        fprintf(fp,"|  Make off, don't try it again, today.            |\n\r");
	fprintf(fp,"`--------------------------------------------------'\n\r");
	fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n\r");
	sleep(10);		      /* just some time to read this message */
      }
      fclose(fp);
    }
    if (flag == WARNING && ce->warn) {		/* process warning message.  */
	hint="** NOTIFIED **";
      if (!(fp = fopen(dev, "w")) ){
	bailout("Can't open user's terminal", 5);
	return(0);
      }
      if (ce->clear){
	sprintf (mbuf,"clear >%s",dev);
	system (mbuf);
      }
      if (ce->hard){
	fprintf(fp,"\n\n\r"); /* \r is sometimes needed...*/
	fprintf(fp,".--------------------------------------------------.\n\r");
        fprintf(fp,"|  %10s: You've been on for %3d min.         |\n\r",
		    name, stime);
        fprintf(fp,"|  You'll be logged off in %3d sec. so finish up.  |\n\r",
        	    ce->grace);
	fprintf(fp,"`--------------------------------------------------'\n\r");
	fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n\r");
      }
      else {
	fprintf(fp,"\n\n\r"); /* \r is sometimes needed...*/
	fprintf(fp,".----------------------------------------------.\n\r");
	fprintf(fp,"| %10s: You've been idle for %3d min.    |\n\r",
		    name, idle);
	fprintf(fp,"|        You'll be logged off in %3d sec       |\n\r",
		    ce->grace);
	fprintf(fp,"|             unless you hit a key.            |\n\r");
	fprintf(fp,"`----------------------------------------------'\n\r");

/*.. Try to wake up sleeping user by beeping 3 times. . .....................*/
	fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n");
	sleep(1); fprintf(fp,"\07\n\r");
      }
      fclose(fp);
      if (ce->mail){
	sprintf(mbuf, "/usr/bin/mail -s \"++WARNING - LOG-OFF ++\" %s", name);
	/* open pipe to mail program for writing */
	if (!(mprog = popen(mbuf, "w")) ){
	    bailout("Can't use /usr/bin/mail program", 6);
	    return(0);
	}
	if (ce->hard){
	  fprintf(mprog, "Your session-limit will be reached soon.\n");
	  fprintf(mprog, "   %s: You've been on for %3d min.\n", name, stime);
	  fprintf(mprog, "   You'll be logged off in %3d sec.\n",ce->grace);
	  fprintf(mprog, "   So finish up, please.\n");
	}
	else{
	  fprintf(mprog, "You have been idle for quite a long time.\n");
	  fprintf(mprog, "   %s: You've been idle for %3d min.\n", name, stime);
	  fprintf(mprog, "   You'll be logged off in %3d sec.\n",ce->grace);
	  fprintf(mprog, "   Please: Type something into a terminal \n");
	  fprintf(mprog, "           or you'll be kicked off.\n");
	}
	fclose(mprog);
      }
    }
    if (flag == LOGOFF){
        hint="** LOGOFF **";
        if (ce->mail){
	    sprintf(mbuf, "/usr/bin/mail -s \"Logged off, you were idle\" %s", name);
            /* open pipe to mail program for writing */
            if (!(mprog = popen(mbuf, "w")) ){
                bailout("Can't use /usr/bin/mail program", 6);
		return(0);
	    }
	    fprintf(mprog, "Subject: Excess Idle Time\n\n");
	    fprintf(mprog, "  Logged off - %s - %s\n",name, ctime(&tvec));
	    fprintf(mprog, "    - excess idle time - \n");
	    fprintf(mprog, "      on tty = %s\n",dev+5);
	    fclose(mprog);
            }
        }
    if (flag == NOLOGOFF){
        hint="** LOGOFF FAILED **";
        if (ce->mail){
	    sprintf(mbuf, "/usr/bin/mail -s \"Couldn't log out [%s] \" root",name);
            if ((mprog = popen(mbuf, "w")) == (FILE *) NULL){
                bailout("Can't use /usr/bin/mail program", 7);
		return(0);
	    }
            fprintf(mprog, "Subject: Can't logoff %s\n", name);
            fprintf(mprog, "Can't Log off - %s %s\n",    name, ctime(&tvec));
            fprintf(mprog, "tty = %s\n", dev+5);
            fclose(mprog);
        }
    }
    if (ce->log){				/* Generate logfile message */
	char msg[100];
	sprintf(msg, "%-20s %-8s %-5s idle:%3d sess:%3d",
		      hint,name, dev+5, idle, stime);
	log_msg(msg);
    }
    return(0);
}

log_msg(char *message) { /* This function adds something to a logfile. ......*/
    FILE    *log = 0;
    struct stat status;
    char str_time[30];

    if (stat(logfname, &status) >= 0){			/* if logfile exists */
	log = fopen(logfname, "a");		 	/* open to append    */
	if (log==NULL) return(1);			/* fopen failed.     */
	sprintf(str_time,"%s",ctime(&pres_time)+3);
	str_time[strlen(str_time)-1]=0;
	
        fprintf(log, "%s - %s\n",str_time,message);
	fclose(log);
    	return(0);
    }
}

bailout(char *message, int status){		/* Handle error message.     */
	char msg[100];                          /* Try to log the message.   */
	sprintf(msg, "** ERROR ** %s", message);
	log_msg(msg);
}

/*.. reduce time to sleep for the next time the daemon falls asleep. ........*/
lower_sleep(int sleep_left){
        if ( 0<sleep_left &&
	       sleep_left < sleeptime) sleeptime=sleep_left;
}

/*---------------------------------------------------------------------------*/
/*===    By the way have a look for forgotten user-processes              ===*/
/*---------------------------------------------------------------------------*/

kill_lost_PIDs()
{   char   prname[LINELEN], iline[LINELEN];
    char   *s;
    char   *ps_name, *ps_pid;

    struct stat status;

    FILE   *ps;
    int    i, pid, uid;
    char    mbuf[LINELEN];	   /* message buffer */

    int  userpos=0;		   /* position of user found, 0 => not found */

/*.. have ps tell us all current users, uids and pids. ......................*/
    if (!(ps = popen(ps_cmd, "r")) ){
	bailout("Can't use ps program", 6);
	return;
    }
    fgets(iline, LINELEN, ps);		 /* get header-line */

    ids_fill =  0;
    while (fgets(iline, LINELEN, ps)){
	ps_name= strtok(iline,delims);   /* manual: Never use this function. */
	ps_pid = strtok(0,delims);
	pid    = atoi(ps_pid);
	
	sprintf(prname,"/proc/%d",pid);   /* append /proc/ to proclist */
	if (stat(prname, &status)){
	    userpos=-1;			  /* => process will be killed. */
	    if (do_bite) kill(pid, SIGKILL); /* send the "kill" signal */
	    sprintf(mbuf,"Dead , killed: %-10s %5d : %5d",ps_name,uid,pid);
	    log_msg(mbuf);
	}
	else{
	    uid = status.st_uid;
	    if (debug)
		printf("%-8s -> %5s %5d %5d\n", ps_name, ps_pid, pid,uid);

	    if (500 <= uid && uid <= 60000){	  /* neither root nor nobody */
	
/*.. Get Position of user in userlst. .......................................*/
		strcpy(userlst[0].Name,  ps_name);
		userpos=userfill;
		while ( strcmp(userlst[userpos].Name,ps_name) ) userpos--;
		
		if (userpos ==0 ){	     /* user not found => not active */
		  if (do_bite) kill(pid, SIGKILL); /* send the "kill" signal */
		  sprintf(mbuf,"Lost, killed: %-10s %5d : %5d",ps_name,uid,pid);
		  log_msg(mbuf);
		}
		else if(strlen(userlst[userpos].Device)==1){ /* "." */
		  if (do_bite) kill(pid, SIGKILL); /* send the "kill" signal */
		  sprintf(mbuf,"Left, killed: %-10s %5d : %5d",ps_name,uid,pid);
		  log_msg(mbuf);
		}
	    }
	}
    }
    fclose(ps);
}