forked from cschuber/doinkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doinkd.c
1448 lines (1236 loc) · 43.2 KB
/
doinkd.c
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
/*
* Main doinkd routine contols everything.
*/
#include <sys/types.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <sys/time.h> /* for time structs and time() */
#include <sys/resource.h> /* For getrlimit() and RLIMIT_NOFILE */
#include <string.h> /* For strlen(), strcpy(), etc */
#ifdef SYSV
# include <termios.h>
# include <fcntl.h>
#endif /* SYSV */
#include "doinkd.h"
#include "version.h"
/* SystemV type systems use utmp entry types. If the utmp.h has
* not defined this for us, do so now, assuming that those systems
* consistently use 7 for an active user process.
*/
#if defined(SYSV) && !defined(USER_PROCESS)
# define USER_PROCESS 7
#endif
/* If this system does not have utmpx, then the time should just
* be in ut_time.
*/
#ifndef HAVE_UTMPX
#define ut_xtime ut_time
#endif
#ifndef DEBUG
# define DEBUG 0
#endif
#if (DEBUG > 0)
# define debug(level,string) if (DEBUG > level) logfile string
#else
# define debug(level,string) /* Erase debug code, since DEBUG is off */
#endif
struct usertime
{
char uid[NAMELEN + 1]; /* Who is being refused */
time_t start; /* The start of the refuse period */
struct usertime *next; /* Pointer to the next entry */
};
struct user users[MAXUSERS];
struct user *pusers[MAXUSERS];
struct usertime *sess_users = NULL;
struct linetime *errorlines = NULL;
struct qelem *exmpt; /* lists for exemptions */
struct qelem *refuse; /* lists for refusals */
struct qelem *rules; /* lists for timeouts */
struct qelem *session; /* lists for session limits */
/* LEC */
struct qelem *maxuser; /* lists for max user */
/* LEC */
char console_user[NAMELEN]; /* name of the person on console */
int ioidle = FALSE; /* False to use input from user only as idle time */
/* True to use input & output as idle time */
#ifdef XDM_HACK
int zapconsole = FALSE; /* If true, kill all tty's owned by console_user */
#endif
int m_threshold; /* number of users before multiple limits */
int s_threshold; /* number of users before session limits */
int session_default;/* The default session limit time */
int sess_refuse_len;/* The refuse time after a session logout */
int sleeptime; /* max time to sleep between checks */
int warntime; /* time to allow for idle warning time */
int conswins_idle; /* time to allow for idle time for terminals opened
* by the user on console on this machine. */
int conswins_sess; /* max idle time for wins of console user */
int conswins_mult; /* max idle time for wins of console user */
int mult_per_user; /* number of multiple logins allowed per user */
void chk_idle(),
chk_multiple(),
chk_maxuser(),
chk_refuse(),
add_session_refuse(),
chk_session_refuse(),
console_check(),
console_zap (),
finish(),
core_time(),
getgroups_func(),
logfile();
int comp(),
chk_session(),
num_uids();
extern void wakeup();
extern void freelist(),
setlimits(),
warn();
extern char *ctime();
extern time_t time();
extern int xlock_check(); /* From xlock_check.c */
/* Configuration Stuffs */
char *config_file;
int update_configuration();
time_t conf_oldstamp;
void daemonize();
int main(argc,argv)
int argc;
char *argv[];
{
int userptr;
int utmptr;
struct user *user;
int fl_idle = 1;
int fl_multiple = 1;
/* LEC */
int fl_maxuser = 1;
/* LEC */
int fl_refuse = 1;
int fl_session = 1;
int new;
int res;
int utmpfd;
int isConsole;
char pathbuf[20];
char tmpname[NAMELEN + 1];
time_t tempus;
int nextcheck; /* Number of seconds to next check status */
FILE *logfd;
#ifdef HAVE_UTMPX
struct utmpx utmpbuf;
#else
struct utmp utmpbuf;
#endif
struct stat statbuf;
struct passwd *pswd;
int i;
strcpy(console_user,""); /* default to user "" on console */
conswins_idle = -2; /* default the actions to 'normal' logouts */
conswins_sess = -2; /* default the actions to 'normal' sessions */
conswins_mult = -2; /* default the actions to 'normal' multiple logins */
session_default = -1; /* default to no session limit */
sess_refuse_len = 0; /* No refuse time after a session logout */
config_file = CONFIG;
for (i = 1; i < argc; i++)
{
if (argv[i] == NULL)
continue;
if (argv[i][0] == '-' && argv[i][2] == '\0')
{
switch (argv[i][1])
{
case 'm': /* no multiple login checks */
fl_multiple = 0;
break;
case 'i': /* no idle timeouts */
fl_idle = 0;
break;
case 'r': /* no refusals */
fl_refuse = 0;
break;
case 's': /* no session limits */
fl_session = 0;
break;
case 'f': /* Set the configuration file name */
if (argv[i+1] != NULL)
{
config_file = (char *) malloc(strlen(argv[i+1])+1);
strcpy(config_file,argv[i+1]);
fprintf(stdout,"Using configuration file '%s'.\n",config_file);
i++;
}
else
fprintf(stderr,"doinkd: flag -f specified without naming config file\n");
break;
case 'V': /* Print your version info! */
fprintf(stdout,"%s\n",IDLED_VERSION);
exit(0);
default:
(void) fprintf (stderr, "doinkd: bad flag -%c\n", argv[i][1]);
break;
}
}
else
(void) fprintf (stderr, "doinkd: bad flag -%s\n", &argv[i][1]);
}
/* do nothing!! */
if (!fl_idle && !fl_multiple && !fl_refuse && !fl_session)
{
fprintf(stderr,"doinkd: nothing to do. exiting.\n");
exit (0);
}
/* Trap signals! */
(void) signal (SIGHUP, SIG_IGN);
(void) signal (SIGINT, SIG_IGN);
(void) signal (SIGQUIT, SIG_IGN);
/* Trap some error ones */
(void) signal (SIGILL, core_time);
(void) signal (SIGBUS, core_time);
(void) signal (SIGSEGV, core_time);
#if defined(SIGTTOU) && defined(SIGTSTP)
(void) signal (SIGTTOU, SIG_IGN);
(void) signal (SIGTSTP, SIG_IGN);
#endif /* SIGS defined */
(void) signal (SIGTERM, finish);
(void) signal (SIGALRM, finish);
/* a very old stamp for the configuration file */
conf_oldstamp = 1;
/* set up new header nodes for each of the lists */
exmpt = (struct qelem *) malloc (sizeof (struct qelem));
refuse = (struct qelem *) malloc (sizeof (struct qelem));
rules = (struct qelem *) malloc (sizeof (struct qelem));
session = (struct qelem *) malloc (sizeof (struct qelem));
/* LEC */
maxuser = (struct qelem *) malloc (sizeof (struct qelem));
maxuser->q_forw = maxuser->q_back = maxuser;
maxuser->q_item = NULL;
/* LEC */
exmpt->q_forw = exmpt->q_back = exmpt;
refuse->q_forw = refuse->q_back = refuse;
rules->q_forw = rules->q_back = rules;
session->q_forw = session->q_back = session;
exmpt->q_item = refuse->q_item = rules->q_item = session->q_item = NULL;
if ((logfd = fopen (LOGFILE, "a")) != (FILE *) NULL)
{
tempus = time ((time_t *) NULL);
(void) fprintf (logfd, "%24.24s %s started.\n", ctime (&tempus),IDLED_VERSION);
(void) fclose (logfd);
}
else
{
(void) fprintf (stderr, "doinkd: Cannot open log file: %s\n", LOGFILE);
exit (1);
}
/* lose our controlling terminal */
/* No more printing to stdout, or stderr allowed after this point! */
daemonize();
/* now sit in an infinite loop and work */
for (;;)
{
new = update_configuration();
(void) time (&tempus);
if ((utmpfd = open (UTMP_FILE, O_RDONLY, 0)) == SYSERROR)
{
logfile ("%19.19s: Cannot open %s.",ctime(&tempus),UTMP_FILE);
exit (1);
}
/* Set our nextcheck time to the max (sleeptime), though it may
* be lowered in the coming for loop so that an idle tty gets
* only the alloted time to be idle before being warned.
*/
nextcheck = tempus + sleeptime;
/* If we aren't doing the hack to get XDM to work, then set this
* to false so that we won't have to worry about it anywhere else.
*/
#ifndef XDM_HACK
isConsole = FALSE;
#endif
/* Set zapconsole to false, so if it is set to true in this
* check, then the console user will be killed below.
*/
#ifdef XDM_HACK
zapconsole = FALSE;
#endif
/*
* look through the utmp file, compare each entry to the users
* array to see if an entry has changed. If it has, build a new
* record for that person, if it hasn't, see if it is time to
* examine him again.
*/
#ifdef HAVE_UTMPX
for (utmptr = 0, userptr = 0; (res = read (utmpfd, (char *) &utmpbuf, sizeof (struct utmpx))) > 0;)
#else
for (utmptr = 0, userptr = 0; (res = read (utmpfd, (char *) &utmpbuf, sizeof (struct utmp))) > 0;)
#endif
{
if (utmptr >= MAXUSERS)
{
logfile ("Error: too many users. Recompile with higher 'MAXUSERS' setting.");
break;
}
#ifdef HAVE_UTMPX
if (res != sizeof (struct utmpx))
#else
if (res != sizeof (struct utmp))
#endif
{
logfile ("Error reading utmp file, continuing.");
break;
}
(void) time (&tempus);
#ifdef XDM_HACK
isConsole = FALSE;
if (strcmp(utmpbuf.ut_line,XDM_DEV) == 0)
{
/* This is the console. Is there a real name attached? */
if (strlen(utmpbuf.ut_name) > 0)
isConsole = TRUE; /* Yes, use it */
else
strcpy(console_user,""); /* No, clear the console user */
}
#endif
#ifdef SYSV
if (utmpbuf.ut_type == USER_PROCESS || isConsole)
#else /* SYSV */
if (utmpbuf.ut_name[0] != NULL || isConsole)
#endif /* SYSV */
{
user = &users[utmptr];
(void) strncpy (tmpname, utmpbuf.ut_name, NAMELEN);
tmpname[NAMELEN] = 0;
if (!strcmp (user->uid, tmpname) && user->time_on == utmpbuf.ut_xtime)
{
if (new)
setlimits (utmptr);
/* If we are handling idle stuff, check to see if it is time
* to see if this tty is idle, giving ourself five seconds of
* leeway time.
*/
if (fl_idle && tempus > user->next-5)
chk_idle (utmptr);
if (fl_idle && (user->next < nextcheck) && (user->next > tempus))
{
debug(2,("Shortening this pause to %d seconds due to %s.",user->next-tempus,user->line));
nextcheck = user->next;
}
}
else
{
/* Don't include entries if the name is blank */
if ((int)strlen(tmpname) <= 0)
continue;
#ifdef UTMPPID
user->pid = utmpbuf.ut_pid;
#endif /* UTMPPID */
#ifdef UTMPHOST
debug(4,("utmp host is %s",utmpbuf.ut_host));
if ((int)strlen(utmpbuf.ut_host) <= 0)
(void) strncpy (user->host,"localhost",HOSTLEN);
else
(void) strncpy (user->host,utmpbuf.ut_host,HOSTLEN);
user->host[HOSTLEN] = '\0'; /* Make sure that it is null-terminated */
#endif /* UTMPHOST */
(void) strcpy (pathbuf, DEV);
(void) strcat (pathbuf, utmpbuf.ut_line);
(void) strcpy (user->line, pathbuf);
#ifdef XDM_HACK
if (isConsole)
(void) strcpy(user->line,CONSOLE_NAME);
#endif
(void) stat (pathbuf, &statbuf);
(void) strcpy (user->uid, tmpname);
pswd = getpwnam (user->uid);
if (pswd == NULL)
logfile ("Error: could not get info on supposed user %s.",user->uid);
else
getgroups_func (pswd->pw_name, user->groups, pswd->pw_gid);
user->time_on = utmpbuf.ut_xtime;
setlimits (utmptr);
user->next = tempus;
chk_session_refuse(user);
/* This is a new user, so remove this line from the errorlines
* list if it is in there.
*/
checklinetimeremove(user,&errorlines);
/* If we are handling idle stuff, check to see if it has been
* idle too long.
*/
if (fl_idle)
chk_idle (utmptr);
/* If this is the line for CONSOLE_NAME (ie. "/dev/console")
* then set console_user to this person.
*/
if (strcmp(CONSOLE_NAME,user->line) == 0)
{
strcpy(console_user,user->uid);
}
/* Ensure that the sleep time is not greater than the smallest
* allowed idle time
*/
if (user->idle < sleeptime && user->idle > 0)
{
sleeptime = user->idle;
if ((tempus + sleeptime) < nextcheck)
nextcheck = tempus+sleeptime;
debug(2,("Shortening sleeptime to %d seconds due to %s.",sleeptime,user->line));
}
}
debug(1,
("debug: %s %s %s %d %d %s %x %x",user->uid,user->line,user->host,
user->session,user->idle,
user->refuse == true ? "true" : "false",
user->exempt,user->warned));
pusers[userptr++] = user;
}
else
{
/* This is not a current user tty! */
/* If this line is the console, then make our console_user
* string empty, since no one is on it!
*/
if (strcmp(CONSOLE_NAME,utmpbuf.ut_line) == 0)
{
strcpy(console_user,"");
}
}
utmptr++;
}
debug(5,("Checked The entire UTMP file!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
(void) close (utmpfd);
#ifdef XDM_HACK
/* If the console user has been marked for killing, then do it. */
if (zapconsole)
console_zap (userptr);
#endif
if (fl_refuse)
chk_refuse (userptr);
if (fl_session)
{
tempus = chk_session (userptr);
if ((tempus > 0) && (tempus < nextcheck))
nextcheck = tempus;
}
if (fl_multiple)
chk_multiple (userptr);
/* LEC */
if (fl_maxuser)
chk_maxuser (user, userptr);
/* LEC */
(void) time (&tempus);
(void) sleep ((unsigned) (nextcheck - tempus));
}
}
/**************************************************************************
* If the configuration file has changed, then read in the new settings. *
* If it is gone, then log that fact, and keep going. *
* Returns: 0 == No changes. *
* 1 == Changes. Set "new" in calling procedure to true. *
**************************************************************************/
int update_configuration()
{
FILE *conffd;
struct stat statbuf;
time_t tempus;
(void) time(&tempus);
if (stat (config_file, &statbuf) == SYSERROR)
{
logfile ("%19.19s: Cannot stat conf file.",ctime(&tempus));
if (conf_oldstamp == 1)
{
logfile ("Have never read in conf settings. Must die.");
exit (1);
}
else
{
logfile ("Ignoring, and maintaining old settings.");
return 0;
}
}
if (statbuf.st_mtime > conf_oldstamp)
{
logfile ("%19.19s Reading in configuration file.", ctime(&tempus));
if ((conffd = freopen (config_file, "r", stdin)) == (FILE *) NULL)
{
logfile ("%19.19s: Cannot open configuration file.", ctime(&tempus));
if (conf_oldstamp == 1)
{
logfile ("Have never read in conf settings. Must die.");
exit (1);
}
else
{
logfile ("Ignoring, and maintaining old settings.");
return 0;
}
}
/* get rid of the old rules and exempt lists */
freelist (exmpt);
freelist (refuse);
freelist (rules);
freelist (session);
/* LEC */
freelist (maxuser);
/* LEC */
m_threshold = 0;
s_threshold = 0;
/* Default to proportional multiple logins allowed */
mult_per_user = -1;
/* default the actions to 'normal' logouts */
conswins_idle = -2;
conswins_sess = -2;
conswins_mult = -2;
/* default to no session limit, and no refuse time after
* a session warn/logout.
*/
session_default = -1;
sess_refuse_len = 0;
/* Default to input required from user to not be idle */
ioidle = FALSE;
/* now read the conf file and set up the rules */
(void) yyparse ();
(void) yyrestart (conffd); /* jn */
(void) fclose (conffd);
/* Update the stamp time */
conf_oldstamp = statbuf.st_mtime;
return 1;
}
else
return 0;
}
/**************************************************************************
* Add this user to the list of session refusals. *
**************************************************************************/
void add_session_refuse(user)
struct user *user;
{
struct usertime *tmpsu;
time_t tempus;
/* Don't bother to insert into the list if it will have no effect */
if (sess_refuse_len <= 0)
return;
(void) time (&tempus);
/* Check to see if the user is already in the list. If the user
* is in the list, then update the time (so it will be longer before
* login is allowed again).
*/
tmpsu = sess_users;
while (tmpsu != NULL)
{
if (strcmp(tmpsu->uid,user->uid) == 0)
{
debug(3,("updated %s to session refuse, start time %d",user->uid,tempus));
tmpsu->start = tempus;
return; /* Done it, so leave */
}
tmpsu = tmpsu->next;
}
/* The user was not in the list, so add a new entry to the start
* of the list.
*/
tmpsu = (struct usertime *) malloc(sizeof(struct usertime));
strcpy(tmpsu->uid,user->uid);
tmpsu->start = tempus;
tmpsu->next = sess_users;
sess_users = tmpsu;
debug(3,("added %s to session refuse, start time %d",user->uid,tempus));
}
/* LEC */
void chk_maxuser(user, n_users)
struct user *user;
int n_users;
{
register int who;
int i,i2;
int grpcnt = 0;
int tgrpcnt = 0;
if (user->mgroup != 0) {
for (i = 0; i < n_users; i++) {
if ( (*pusers[i]).uid == user->uid)
who = i;
tgrpcnt = 0;
for (i2 = 0; i2 < 1000; i2++) {
if ( (*pusers[i]).groups[i2] == user->mgroup) {
tgrpcnt = 1;
}
}
grpcnt += tgrpcnt;
}
if (!((*pusers[who]).exempt & IS_MAXU) && (grpcnt > user->maxuser)) {
debug(3,("expiring %s for %d in maxusers",user->uid,grpcnt));
user->maxed = true;
warn (who, IS_MAXU, DO_MSG);
}
}
}
/* LEC */
/**************************************************************************
* check the state of refusals caused from a user overlapping session *
* limits. Remove any expired refusals first, then see if a refusal *
* applies to this user. *
**************************************************************************/
void chk_session_refuse(user)
struct user *user;
{
struct usertime **tmpsu, *delsu, *tsu;
time_t tempus;
(void) time (&tempus);
/* Check for expired session limits */
tmpsu = &sess_users;
while ((*tmpsu) != NULL)
{
if ((*tmpsu)->start + sess_refuse_len < tempus)
{
debug(3,("expiring for %s in session refuse",user->uid));
delsu = *tmpsu; /* store the address to free */
*tmpsu = (*tmpsu)->next;
free(delsu);
}
else
tmpsu = &((*tmpsu)->next);
}
/* Check to see if this user is in the list */
tsu = sess_users;
while (tsu != NULL)
{
if (strcmp(tsu->uid,user->uid) == 0)
{
debug(3,("refusing for %s in session refuse",user->uid));
user->refuse = true;
return;
}
tsu = tsu->next;
}
}
/**************************************************************************
* check for refusal, tell user, then zap him *
**************************************************************************/
void chk_refuse(n_users)
int n_users;
{
register int who;
for (who = 0; who < n_users; who++)
if (!(pusers[who]->exempt & IS_REFU) && pusers[who]->refuse == true)
warn (who, IS_REFU, DO_MSG);
}
/**************************************************************************
* check session limits, warn users who have exceeded session limits *
**************************************************************************/
int chk_session(n_users)
int n_users;
{
int who;
int timeon, mintime, nexttime, allowed_time, save;
time_t tempus;
mintime = 0; /* Default is to not change the nextcheck time */
if (s_threshold == 0 || n_users < s_threshold)
return 0;
(void) time (&tempus);
for (who = 0; who < n_users; who++)
{
/* Set default allowed time. Might be different for the console user */
allowed_time = pusers[who]->session;
/* If a session time has not been set for this user, set it to the
* default session limit, if any.
*/
if (allowed_time <= 0)
allowed_time = session_default * 60;
/* If the user is not exempt from sessions and has a limit set */
/* AND the user is over the session limit */
if (!(pusers[who]->exempt & IS_LIMIT) && (allowed_time > 0))
{
debug(3,("Session: user=%s. time_on=%d. tty=%s. sesstime=%d",(*pusers[who]).uid,(*pusers[who]).time_on,
(*pusers[who]).line,((tempus - pusers[who]->time_on))));
/* If console windows are immune from session (-1), then skip them.
* If they are not normal (-2) then they have a set time, so use it.
*/
if ((strcmp(console_user,pusers[who]->uid) == 0) && (conswins_sess == -1))
{
debug(3,(" not checking console user %s for session",console_user));
continue;
}
else if ((strcmp(console_user,pusers[who]->uid) == 0) && (conswins_sess != -2))
{
allowed_time = conswins_sess;
}
/* If the user has not been on for their session time yet, skip it */
if ((tempus - pusers[who]->time_on) < allowed_time)
{
pusers[who]->warned &= ~IS_LIMIT;
continue;
}
/* * Then if the user has NOT been warned, warn now.
* * If the user HAS been warned, see that a warning period of
* warntime seconds has been given before doin' the nuken
* Give 5 seconds of leeway for determining if it is nukin' time
*/
if (!(pusers[who]->warned & IS_LIMIT) ||
((tempus - pusers[who]->time_on) >= (allowed_time + warntime - 5)))
{
/* Add this user to the session refuse list, if it is in use */
add_session_refuse(pusers[who]);
/* If there is no warn time, just kill the user */
if (warntime <= 0)
pusers[who]->warned |= IS_LIMIT;
save = pusers[who]->session; /* Save original session value */
pusers[who]->session = allowed_time; /* Set it to what we are using */
warn (who, IS_LIMIT, DO_MSG); /* Warn or Zap, using right time */
pusers[who]->session = save; /* Restore original value */
}
/* Set the nextcheck time so that it is not longer than what we
* need to kill the user on schedule.
*/
timeon = tempus - pusers[who]->time_on; /* How long this user
* has been on */
nexttime = warntime - (timeon - allowed_time);
/* When to next check
* this tty */
if (nexttime < 10)
nexttime = 10; /* Always allow 10 seconds */
debug(2,("set mintime in chk_session() to be %d.",nexttime));
if ((nexttime < mintime) || (mintime == 0))
mintime = nexttime;
}
}
if (mintime > 0)
return (mintime + tempus);
else
return 0;
}
/**************************************************************************
* given the number of users (tty's in use), warn any of them who have *
* multiple logins *
**************************************************************************/
void chk_multiple(n_users)
int n_users;
{
int i, numper, save, numallowed;
int curuser;
/* If we will not do multiple login checks, then set the IS_MULT
* warned bit for all sessions to false. This prevents problems
* of warning a tty, then users log out so the threshold is not
* met, then someone logging in to meet the threshold and the
* previously warned tty will be killed immediately without
* warning, even though much time may have elapsed.
*/
if (m_threshold == 0 || n_users < m_threshold)
{
for (i = 0; i < n_users; i++)
(*pusers[i]).warned &= ~IS_MULT;
return;
}
qsort ((char *) pusers, n_users, sizeof (struct user *), comp);
/* Get the number of multiple logins allowed per user */
if (mult_per_user < 0)
numper = m_threshold / num_uids(n_users);
else
numper = mult_per_user;
/* Ensure that users will get to keep at least 1 tty */
if (numper <= 0)
numper = 1;
curuser = -1;
for (i = 0; i < n_users; i++)
{
debug(3,("Multiple: user=%s. time_on=%d. tty=%s.",(*pusers[i]).uid,(*pusers[i]).time_on,(*pusers[i]).line));
/* If this user is on console and the console user is immune
* from multiple logins (conswins multiple off), then skip
* the user.
*/
if ((strcmp(console_user,(*pusers[i]).uid) == 0) && (conswins_mult == -1))
{
debug(3,(" not checking console user %s for multiple",console_user));
continue;
}
/* If this user has not been checked yet, which we
* know if curuser < 0 or if the curuser uid is not
* the same as the i uid, then we consider this tty
* to be their first. If it is the same user, then
* we check to see if this is a tty that should be
* warned and killed.
*/
if (curuser >= 0)
{
if (strcmp((*pusers[i]).uid, (*pusers[curuser]).uid) != 0)
curuser = i;
}
else
curuser = i;
/* Set the number allowed to the default numper */
numallowed = numper;
/* If this is the console user and there is a separate multiple
* specification for the console user, then set numallowed
* to that value, so we can compare against it, instead.
*/
if ((strcmp(console_user,(*pusers[i]).uid) == 0) && (conswins_mult >= 1))
{
numallowed = conswins_mult;
}
/* If we have not already skipped 'numper' ttys, then let
* this one go as one they can keep.
*/
if (i-curuser < numallowed)
{
(*pusers[i]).warned &= ~IS_MULT; /* Unset warn bit, if set */
continue; /* Let this tty live */
}
/* Too many logins, but let the user go if exempt */
if ((*pusers[i]).exempt & IS_MULT)
continue;
debug(3,(" Warning/Zapping this tty."));
/* Warn/Zap the offending tty */
save = (*pusers[i]).next; /* Save the next value we are going to use */
(*pusers[i]).next = numallowed; /* Set it to what we want to print in warn */
warn (i, IS_MULT, DO_MSG); /* Warn/Zap, saying correct # of logins */
(*pusers[i]).next = save; /* Restore original next value */
}
}
/**************************************************************************
* Return the number of different uids currently logged on. *
* This should only be called after pusers[] has been sorted by uid. *
**************************************************************************/
int num_uids(n_users)
int n_users;
{
int i, curuser, numusers;
numusers = 0;
curuser = -1;
for (i = 0; i < n_users; i++)
{
if (curuser >= 0)
{
if (strcmp((*pusers[i]).uid, (*pusers[curuser]).uid) != 0)
{
curuser = i;
numusers++;
}
}
else
{
curuser = i;
numusers++;
}
}
debug(4,("num_uids is %d",numusers));
return numusers;
}
/**************************************************************************
* Given a user, see if we want to warn him about idle time. *
* First check the exempt vector to see if he is exempt. *
* As a side effect this routine also calculates the next time *
* this user structure should be examined. *
**************************************************************************/
void chk_idle(i)
int i;
{
struct stat statbuf;
int touch_time, allowed_time, save;
time_t tempus;
/* If there is no idle time set for the user, than the user
* can't be idle.
*/
if (users[i].idle < 0)
return;
if (strcmp(users[i].line,CONSOLE_NAME) == 0)
{
console_check(i);
return;
}
(void) time (&tempus);
(void) stat (users[i].line, &statbuf);
/* If we are considering output, only do so BEFORE warning. If
* we have warned, then we have messed up the modified time, so
* we shouldn't look at it.
*/
if (ioidle && (users[i].warned & IS_IDLE) == 0)
touch_time = max (statbuf.st_atime, statbuf.st_mtime);
else /* input_idle only */
touch_time = statbuf.st_atime;
/* Default allowed time for the user is whatever it is. :-) */
allowed_time = users[i].idle;