source: trunk/src/sh_utmp.c@ 381

Last change on this file since 381 was 363, checked in by katerina, 13 years ago

Change zAVL implementation to allow integer keys.

File size: 28.1 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <errno.h>
29
30#ifdef HAVE_UTADDR
31#include <sys/socket.h>
32#include <netinet/in.h>
33#ifndef S_SPLINT_S
34#include <arpa/inet.h>
35#else
36#define AF_INET 2
37#endif
38#endif
39
40#ifdef SH_USE_UTMP
41
42#ifdef HAVE_UTMPX_H
43
44#ifdef S_SPLINT_S
45typedef pid_t __pid_t;
46#endif
47
48#include <utmpx.h>
49#define SH_UTMP_S utmpx
50#undef ut_name
51#define ut_name ut_user
52#ifdef HAVE_UTXTIME
53#undef ut_time
54#define ut_time ut_xtime
55#else
56#undef ut_time
57#define ut_time ut_tv.tv_sec
58#endif
59
60#else
61#include <utmp.h>
62#define SH_UTMP_S utmp
63#endif
64
65
66#ifdef HAVE_PATHS_H
67#include <paths.h>
68#endif
69
70#undef FIL__
71#define FIL__ _("sh_utmp.c")
72
73#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
74
75
76#include "samhain.h"
77#include "sh_utils.h"
78#include "sh_error.h"
79#include "sh_modules.h"
80#include "sh_utmp.h"
81#include "sh_pthread.h"
82#include "sh_inotify.h"
83
84SH_MUTEX_EXTERN(mutex_thread_nolog);
85
86#ifdef TM_IN_SYS_TIME
87#include <sys/time.h>
88#else
89#include <time.h>
90#endif
91
92#ifdef HAVE_UNISTD_H
93#include <unistd.h>
94#endif
95
96#ifdef HAVE_DIRENT_H
97#include <dirent.h>
98#define NAMLEN(dirent) sl_strlen((dirent)->d_name)
99#else
100#define dirent direct
101#define NAMLEN(dirent) (dirent)->d_namlen
102#ifdef HAVE_SYS_NDIR_H
103#include <sys/ndir.h>
104#endif
105#ifdef HAVE_SYS_DIR_H
106#include <sys/dir.h>
107#endif
108#ifdef HAVE_NDIR_H
109#include <ndir.h>
110#endif
111#endif
112
113#ifndef HAVE_LSTAT
114#define lstat stat
115#endif
116
117#ifndef UT_LINESIZE
118#ifndef __UT_LINESIZE
119#define UT_LINESIZE 12
120#else
121#define UT_LINESIZE __UT_LINESIZE
122#endif
123#endif
124
125#ifndef UT_NAMESIZE
126#ifndef __UT_NAMESIZE
127#define UT_NAMESIZE 8
128#else
129#define UT_NAMESIZE __UT_NAMESIZE
130#endif
131#endif
132
133#ifndef UT_HOSTSIZE
134#ifndef __UT_HOSTSIZE
135#define UT_HOSTSIZE 16
136#else
137#define UT_HOSTSIZE __UT_HOSTSIZE
138#endif
139#endif
140
141#ifdef HAVE_UTMPX_H
142
143#ifndef _PATH_UTMP
144#ifdef UTMPX_FILE
145#define _PATH_UTMP UTMPX_FILE
146#else
147#error You must define UTMPX_FILE in the file config.h
148#endif
149#endif
150#ifndef _PATH_WTMP
151#ifdef WTMPX_FILE
152#define _PATH_WTMP WTMPX_FILE
153#else
154#error You must define WTMPX_FILE in the file config.h
155#endif
156#endif
157
158#else
159
160#ifndef _PATH_UTMP
161#ifdef UTMP_FILE
162#define _PATH_UTMP UTMP_FILE
163#else
164#error You must define UTMP_FILE in the file config.h
165#endif
166#endif
167#ifndef _PATH_WTMP
168#ifdef WTMP_FILE
169#define _PATH_WTMP WTMP_FILE
170#else
171#error You must define WTMP_FILE in the file config.h
172#endif
173#endif
174
175#endif
176
177typedef struct log_user {
178 char ut_tty[UT_LINESIZE+1];
179 char name[UT_NAMESIZE+1];
180 char ut_host[UT_HOSTSIZE+1];
181 char ut_ship[SH_IP_BUF]; /* IP address */
182 time_t time;
183 struct log_user * next;
184} blah_utmp;
185
186#ifdef HAVE_UTTYPE
187static char terminated_line[UT_HOSTSIZE];
188#endif
189
190static char * mode_path[] = { _PATH_WTMP, _PATH_WTMP, _PATH_UTMP };
191
192static struct SH_UTMP_S save_utmp;
193
194static void sh_utmp_logout_morechecks(struct log_user * user);
195static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut);
196static void sh_utmp_addlogin (struct SH_UTMP_S * ut);
197static void sh_utmp_check_internal(int mode);
198
199static int ShUtmpLoginSolo = SH_ERR_INFO;
200static int ShUtmpLoginMulti = SH_ERR_WARN;
201static int ShUtmpLogout = SH_ERR_INFO;
202static int ShUtmpActive = S_TRUE;
203static time_t ShUtmpInterval = 300;
204
205sh_rconf sh_utmp_table[] = {
206 {
207 N_("severityloginmulti"),
208 sh_utmp_set_login_multi
209 },
210 {
211 N_("severitylogin"),
212 sh_utmp_set_login_solo
213 },
214 {
215 N_("severitylogout"),
216 sh_utmp_set_logout_good
217 },
218 {
219 N_("logincheckactive"),
220 sh_utmp_set_login_activate
221 },
222 {
223 N_("logincheckinterval"),
224 sh_utmp_set_login_timer
225 },
226 {
227 N_("logincheckfirst"),
228 sh_login_set_checklevel
229 },
230 {
231 N_("logincheckoutlier"),
232 sh_login_set_siglevel
233 },
234 {
235 N_("logincheckdate"),
236 sh_login_set_def_allow
237 },
238 {
239 N_("logincheckuserdate"),
240 sh_login_set_user_allow
241 },
242 {
243 NULL,
244 NULL
245 },
246};
247
248static void set_defaults(void)
249{
250 ShUtmpLoginSolo = SH_ERR_INFO;
251 ShUtmpLoginMulti = SH_ERR_WARN;
252 ShUtmpLogout = SH_ERR_INFO;
253 ShUtmpActive = S_TRUE;
254 ShUtmpInterval = 300;
255
256 sh_login_reset();
257 return;
258}
259
260
261#if defined (HAVE_SETUTENT) && defined (USE_SETUTENT)
262
263#ifdef HAVE_UTMPX_H
264
265#define sh_utmp_utmpname utmpxname
266#define sh_utmp_setutent setutxent
267#define sh_utmp_endutent endutxent
268#define sh_utmp_getutent getutxent
269#define sh_utmp_getutid getutxid
270#define sh_utmp_getutline getutxline
271
272#else
273
274#define sh_utmp_utmpname utmpname
275#define sh_utmp_setutent setutent
276#define sh_utmp_endutent endutent
277#define sh_utmp_getutent getutent
278#define sh_utmp_getutid getutid
279#define sh_utmp_getutline getutline
280
281#endif
282
283#else
284
285/* BSD lacks getutent() etc.
286 * utmpname(), setutent(), and endutent() return void,
287 * so we do not perform much error handling.
288 * Errors must be recognized by getutent() returning NULL.
289 * Apparently, the application cannot check whether wtmp is empty,
290 * or whether there was an fopen() error.
291 */
292
293static FILE * sh_utmpfile = NULL;
294static char sh_utmppath[80] = _PATH_UTMP;
295
296/* sh_utmp_feed_forward is for optimizing
297 * (fseek instead of getutent loop)
298 */
299static long sh_utmp_feed_forward = 0;
300
301static void sh_utmp_utmpname(const char * str)
302{
303 SL_ENTER(_("sh_utmp_utmpname"));
304 if (sh_utmpfile != NULL)
305 {
306 (void) sl_fclose (FIL__, __LINE__, sh_utmpfile);
307 sh_utmpfile = NULL;
308 }
309
310 (void) sl_strlcpy (sh_utmppath, str, 80);
311 SL_RET0(_("sh_utmp_utmpname"));
312}
313
314static void sh_utmp_setutent(void)
315{
316 int error;
317 int fd;
318
319 SL_ENTER(_("sh_utmp_setutent"));
320
321 ASSERT((sh_utmppath != NULL), _("sh_utmppath != NULL"));
322
323 if (sh_utmppath == NULL)
324 SL_RET0(_("sh_utmp_setutent"));
325
326 if (sh_utmpfile == NULL)
327 {
328 SH_MUTEX_LOCK(mutex_thread_nolog);
329 fd = (int) aud_open (FIL__, __LINE__, SL_NOPRIV,
330 sh_utmppath, O_RDONLY, 0);
331 SH_MUTEX_UNLOCK(mutex_thread_nolog);
332 if (fd >= 0)
333 {
334 sh_utmpfile = fdopen(fd, "r");
335 }
336
337 /* -- If (sh_utmpfile == NULL) then either the open() or the fdopen()
338 * has failed.
339 */
340 if (sh_utmpfile == NULL)
341 {
342 error = errno;
343 SH_MUTEX_LOCK(mutex_thread_nolog);
344 sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
345 (long) sh.real.uid, sh_utmppath);
346 SH_MUTEX_UNLOCK(mutex_thread_nolog);
347 SL_RET0(_("sh_utmp_setutent"));
348 }
349 }
350 (void) fseek (sh_utmpfile, 0L, SEEK_SET);
351 if (-1 == fseek (sh_utmpfile, sh_utmp_feed_forward, SEEK_CUR))
352 {
353 sh_utmp_feed_forward = 0; /* modified Apr 4, 2004 */
354 (void) fseek (sh_utmpfile, 0L, SEEK_SET);
355 }
356 clearerr (sh_utmpfile);
357 SL_RET0(_("sh_utmp_setutent"));
358}
359
360static void sh_utmp_endutent(void)
361{
362 SL_ENTER(_("sh_utmp_endutent"));
363 if (NULL != sh_utmpfile)
364 (void) sl_fclose(FIL__, __LINE__, sh_utmpfile);
365 sh_utmpfile = NULL;
366 SL_RET0(_("sh_utmp_endutent"));
367}
368
369static struct SH_UTMP_S * sh_utmp_getutent(void)
370{
371 size_t in;
372 static struct SH_UTMP_S out;
373
374 SL_ENTER(_("sh_utmp_getutent"));
375
376 ASSERT_RET((sh_utmpfile != NULL), _("sh_utmpfile != NULL"), (NULL))
377
378 in = fread (&out, sizeof(struct SH_UTMP_S), 1, sh_utmpfile);
379
380 if (in != 1)
381 {
382 if (ferror (sh_utmpfile) != 0)
383 {
384 clearerr (sh_utmpfile);
385 SL_RETURN(NULL, _("sh_utmp_getutent"));
386 }
387 else
388 {
389 SL_RETURN(NULL, _("sh_utmp_getutent"));
390 }
391 }
392 SL_RETURN(&out, _("sh_utmp_getutent"));
393}
394
395#ifdef USE_UNUSED
396
397static struct SH_UTMP_S * sh_utmp_getutline(struct SH_UTMP_S * ut)
398{
399 struct SH_UTMP_S * out;
400
401 while (1) {
402 if ((out = sh_utmp_getutent()) == NULL) {
403 return NULL;
404 }
405#ifdef HAVE_UTTYPE
406 if (out->ut_type == USER_PROCESS || out->ut_type == LOGIN_PROCESS)
407 if (sl_strcmp(ut->ut_line, out->ut_line) == 0)
408 return out;
409#else
410 if ( 0 != sl_strncmp (out->ut_name, "reboot", 6) &&
411 0 != sl_strncmp (out->ut_name, "shutdown", 8) &&
412 0 != sl_strncmp (out->ut_name, "date", 4) )
413 return out;
414#endif
415 }
416 return NULL;
417}
418
419static struct SH_UTMP_S * sh_utmp_getutid(struct SH_UTMP_S * ut)
420{
421#ifdef HAVE_UTTYPE
422 struct SH_UTMP_S * out;
423
424 if (ut->ut_type == RUN_LVL || ut->ut_type == BOOT_TIME ||
425 ut->ut_type == NEW_TIME || ut->ut_type == OLD_TIME)
426 {
427 while (1) {
428 if ((out = sh_utmp_getutent()) == NULL) {
429 return NULL;
430 }
431 if (out->ut_type == ut->ut_type)
432 return out;
433 }
434 }
435 else if (ut->ut_type == INIT_PROCESS || ut->ut_type == LOGIN_PROCESS ||
436 ut->ut_type == USER_PROCESS || ut->ut_type == DEAD_PROCESS )
437 {
438 while (1) {
439 if ((out = sh_utmp_getutent()) == NULL) {
440 return NULL;
441 }
442 if (sl_strcmp(ut->ut_id, out->ut_id) == 0)
443 return out;
444 }
445 }
446#endif
447 return NULL;
448}
449/* #ifdef USE_UNUSED */
450#endif
451
452/* #ifdef HAVE_SETUTENT */
453#endif
454
455#ifdef HAVE_UTADDR
456#ifdef HAVE_UTADDR_V6
457static char * my_inet_ntoa(SINT32 * ut_addr_v6, char * buf, size_t buflen)
458{
459 struct in_addr in;
460
461 buf[0] = '\0';
462
463 if (0 == (ut_addr_v6[1] + ut_addr_v6[2] + ut_addr_v6[3]))
464 {
465 memcpy(&in, ut_addr_v6, sizeof(struct in_addr));
466 sl_strlcpy(buf, inet_ntoa(in), buflen);
467 }
468 else
469 {
470 inet_ntop(AF_INET6, ut_addr_v6, buf, buflen);
471 }
472 return buf;
473}
474#else
475static char * my_inet_ntoa(SINT32 ut_addr, char * buf, size_t buflen)
476{
477 struct in_addr in;
478
479 buf[0] = '\0';
480
481 memcpy(&in, ut_addr, sizeof(struct in_addr));
482 sl_strlcpy(buf, inet_ntoa(in), buflen);
483 return buf;
484}
485#endif
486/* #ifdef HAVE_UTADDR */
487#endif
488
489#if defined(__linux__) && !defined(ut_addr)
490#define ut_addr ut_addr_v6[0]
491#endif
492
493
494static struct log_user * userlist = NULL;
495static time_t lastcheck;
496static int init_done = 0;
497
498/*************
499 *
500 * module init
501 *
502 *************/
503
504static int sh_utmp_init_internal (void)
505{
506
507 SL_ENTER(_("sh_utmp_init"));
508 if (ShUtmpActive == BAD)
509 SL_RETURN( (-1), _("sh_utmp_init"));
510
511 /* do not re-initialize after a re-configuration
512 */
513 if (init_done == 1) {
514 SL_RETURN( (0), _("sh_utmp_init"));
515 }
516 lastcheck = time (NULL);
517 userlist = NULL;
518 memset (&save_utmp, 0, sizeof(struct SH_UTMP_S));
519 sh_utmp_check_internal (2); /* current logins */
520 sh_utmp_check_internal (0);
521 init_done = 1;
522 SL_RETURN( (0), _("sh_utmp_init"));
523}
524
525int sh_utmp_init (struct mod_type * arg)
526{
527#if !defined(HAVE_PTHREAD)
528 (void) arg;
529#endif
530 if (ShUtmpActive == BAD)
531 return SH_MOD_FAILED;
532#ifdef HAVE_PTHREAD
533 if (arg != NULL && arg->initval < 0 &&
534 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
535 {
536 if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
537 return SH_MOD_THREAD;
538 else
539 return SH_MOD_FAILED;
540 }
541 else if (arg != NULL && arg->initval == SH_MOD_THREAD &&
542 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
543 {
544 return SH_MOD_THREAD;
545 }
546#endif
547 return sh_utmp_init_internal();
548}
549
550/*************
551 *
552 * module cleanup
553 *
554 *************/
555#ifdef HAVE_UTTYPE
556static int sh_utmp_login_clean(void);
557#endif
558
559#if defined(HAVE_PTHREAD)
560static sh_watches inotify_watch = SH_INOTIFY_INITIALIZER;
561#endif
562
563int sh_utmp_end ()
564{
565 struct log_user * user = userlist;
566 struct log_user * userold;
567
568 SL_ENTER(_("sh_utmp_end"));
569 while (user)
570 {
571 userold = user;
572 user = user->next;
573 SH_FREE(userold);
574 }
575 userlist = NULL;
576#ifdef HAVE_UTTYPE
577 (void) sh_utmp_login_clean();
578#endif
579 /* Reset the flag, such that the module
580 * can be re-enabled.
581 */
582 set_defaults();
583 init_done = 0;
584
585#if defined(HAVE_PTHREAD)
586 sh_inotify_remove(&inotify_watch);
587#endif
588
589 SL_RETURN( (0), _("sh_utmp_end"));
590}
591
592
593int sh_utmp_reconf()
594{
595 set_defaults();
596#if defined(HAVE_PTHREAD)
597 sh_inotify_remove(&inotify_watch);
598#endif
599 return 0;
600}
601
602
603/*************
604 *
605 * module timer
606 *
607 *************/
608int sh_utmp_timer (time_t tcurrent)
609{
610#if !defined(HAVE_PTHREAD)
611 retry_msleep(1, 0);
612
613 if ((time_t) (tcurrent - lastcheck) >= ShUtmpInterval)
614 {
615 lastcheck = tcurrent;
616 return (-1);
617 }
618 return 0;
619#else
620 int errnum = 0;
621
622 if ( (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE) &&
623 sh.flag.checkSum != SH_CHECK_INIT )
624 {
625 sh_inotify_wait_for_change(mode_path[1], &inotify_watch,
626 &errnum, ShUtmpInterval);
627 }
628
629 lastcheck = tcurrent;
630
631 if (SH_INOTIFY_ERROR(errnum))
632 {
633 char ebuf[SH_ERRBUF_SIZE];
634
635 SH_MUTEX_LOCK(mutex_thread_nolog);
636 sh_error_message(errnum, ebuf, sizeof(ebuf));
637 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
638 ebuf,
639 _("sh_utmp_timer") );
640 SH_MUTEX_UNLOCK(mutex_thread_nolog);
641 }
642 return -1;
643#endif
644}
645
646/*************
647 *
648 * module check
649 *
650 *************/
651int sh_utmp_check ()
652{
653 SL_ENTER(_("sh_utmp_check"));
654 if (ShUtmpActive == BAD)
655 {
656#if defined(HAVE_PTHREAD)
657 sh_inotify_remove(&inotify_watch);
658#endif
659 SL_RETURN( (-1), _("sh_utmp_check"));
660 }
661 SH_MUTEX_LOCK(mutex_thread_nolog);
662 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_UT_CHECK);
663 SH_MUTEX_UNLOCK(mutex_thread_nolog);
664 sh_utmp_check_internal (1);
665
666 SL_RETURN(0, _("sh_utmp_check"));
667}
668
669/*************
670 *
671 * module setup
672 *
673 *************/
674
675int sh_utmp_set_login_solo (const char * c)
676{
677 int retval;
678 char tmp[32];
679
680 SL_ENTER(_("sh_utmp_set_login_solo"));
681 tmp[0] = '='; tmp[1] = '\0';
682 (void) sl_strlcat (tmp, c, 32);
683 SH_MUTEX_LOCK(mutex_thread_nolog);
684 retval = sh_error_set_level (tmp, &ShUtmpLoginSolo);
685 SH_MUTEX_UNLOCK(mutex_thread_nolog);
686 SL_RETURN(retval, _("sh_utmp_set_login_solo"));
687}
688
689int sh_utmp_set_login_multi (const char * c)
690{
691 int retval;
692 char tmp[32];
693
694 SL_ENTER(_("sh_utmp_set_login_multi"));
695 tmp[0] = '='; tmp[1] = '\0';
696 (void) sl_strlcat (tmp, c, 32);
697 SH_MUTEX_LOCK(mutex_thread_nolog);
698 retval = sh_error_set_level (tmp, &ShUtmpLoginMulti);
699 SH_MUTEX_UNLOCK(mutex_thread_nolog);
700 SL_RETURN(retval, _("sh_utmp_set_login_multi"));
701}
702
703int sh_utmp_set_logout_good (const char * c)
704{
705 int retval;
706 char tmp[32];
707
708 SL_ENTER(_("sh_utmp_set_logout_good"));
709 tmp[0] = '='; tmp[1] = '\0';
710 (void) sl_strlcat (tmp, c, 32);
711 SH_MUTEX_LOCK(mutex_thread_nolog);
712 retval = sh_error_set_level (tmp, &ShUtmpLogout);
713 SH_MUTEX_UNLOCK(mutex_thread_nolog);
714 SL_RETURN(retval, _("sh_utmp_set_logout_good"));
715}
716
717int sh_utmp_set_login_timer (const char * c)
718{
719 long val;
720
721 SL_ENTER(_("sh_utmp_set_login_timer"));
722 val = strtol (c, (char **)NULL, 10);
723 if (val <= 0)
724 {
725 SH_MUTEX_LOCK(mutex_thread_nolog);
726 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
727 _("utmp timer"), c);
728 SH_MUTEX_UNLOCK(mutex_thread_nolog);
729 SL_RETURN((-1), _("sh_utmp_set_login_timer"));
730 }
731
732 ShUtmpInterval = (time_t) val;
733 SL_RETURN(0, _("sh_utmp_set_login_timer"));
734}
735
736int sh_utmp_set_login_activate (const char * c)
737{
738 int i;
739 SL_ENTER(_("sh_utmp_set_login_activate"));
740 i = sh_util_flagval(c, &ShUtmpActive);
741 SL_RETURN(i, _("sh_utmp_set_login_activate"));
742}
743
744#ifdef HAVE_UTTYPE
745struct login_ct {
746 char name[UT_NAMESIZE+1];
747 int nlogin;
748 struct login_ct * next;
749};
750
751static struct login_ct * login_ct_list = NULL;
752
753static int sh_utmp_login_clean(void)
754{
755 struct login_ct * list = login_ct_list;
756 struct login_ct * old;
757
758 login_ct_list = NULL;
759
760 while (list)
761 {
762 old = list;
763 list = list->next;
764 SH_FREE(old);
765 }
766 return 0;
767}
768
769/* add a username to the list of logged-in users
770 */
771static int sh_utmp_login_a(char * str)
772{
773 struct login_ct * list = login_ct_list;
774
775 while (list)
776 {
777 if (0 == sl_strcmp(list->name, str))
778 {
779 ++(list->nlogin);
780 return list->nlogin;
781 }
782 list = list->next;
783 }
784 list = SH_ALLOC(sizeof(struct login_ct));
785 (void) sl_strlcpy(list->name, str, UT_NAMESIZE+1);
786 list->nlogin = 1;
787 list->next = login_ct_list;
788 login_ct_list = list;
789 return 1;
790}
791
792static int sh_utmp_login_r(char * str)
793{
794 struct login_ct * list = login_ct_list;
795 struct login_ct * old = login_ct_list;
796
797 while (list)
798 {
799 if (0 == sl_strcmp(list->name, str))
800 {
801 list->nlogin -= 1;
802 if (list->nlogin > 0)
803 {
804 return list->nlogin;
805 }
806 if (login_ct_list == list) /* modified Apr 4, 2004 */
807 {
808 login_ct_list = list->next;
809 SH_FREE(list);
810 }
811 else
812 {
813 old->next = list->next;
814 SH_FREE(list);
815 }
816 return 0;
817 }
818 old = list;
819 list = list->next;
820 }
821 return 0;
822}
823
824#endif
825
826
827/* for each login:
828 * - allocate a log record
829 * - link device.ut_record -> log_record
830 * - link user.ut_record -> log_record
831 */
832
833#ifdef HAVE_UTTYPE
834static int sh_utmp_is_virtual (char * in_utline, char * in_uthost)
835{
836
837 if (in_uthost != NULL &&
838 in_utline != NULL &&
839 in_uthost[0] == ':' &&
840 in_uthost[1] == '0' &&
841 0 == sl_strncmp(in_utline, _("pts/"), 4))
842 {
843 return 1;
844 }
845
846 return 0;
847}
848#endif
849
850/* These variables are not used anywhere. They only exist
851 * to assign &userold, &user to them, which keeps gcc from
852 * putting them into a register, and avoids the 'clobbered
853 * by longjmp' warning. And no, 'volatile' proved insufficient.
854 */
855static void * sh_dummy_userold = NULL;
856static void * sh_dummy_user = NULL;
857
858
859static void sh_utmp_addlogin (struct SH_UTMP_S * ut)
860{
861 struct log_user * user = userlist;
862 struct log_user * userold = userlist;
863#ifdef HAVE_UTTYPE
864 struct log_user * username = userlist;
865#endif
866
867 char ttt[TIM_MAX];
868#ifdef HAVE_UTTYPE
869 volatile int status;
870#endif
871
872 SL_ENTER(_("sh_utmp_addlogin"));
873
874 /* Take the address to keep gcc from putting them into registers.
875 * Avoids the 'clobbered by longjmp' warning.
876 */
877 sh_dummy_userold = (void*) &userold;
878 sh_dummy_user = (void*) &user;
879
880 if (ut->ut_line[0] == '\0')
881 SL_RET0(_("sh_utmp_addlogin"));
882
883 /* for some stupid reason, AIX repeats the wtmp entry for logouts
884 * with ssh
885 */
886 if (memcmp (&save_utmp, ut, sizeof(struct SH_UTMP_S)) == 0)
887 {
888 memset(&save_utmp, (int) '\0', sizeof(struct SH_UTMP_S));
889 SL_RET0(_("sh_utmp_addlogin"));
890 }
891 memcpy (&save_utmp, ut, sizeof(struct SH_UTMP_S));
892
893
894 /* ------- find user --------
895 */
896 while (user != NULL)
897 {
898 if (0 == sl_strncmp((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE) )
899 break;
900 userold = user;
901 user = user->next;
902 }
903
904#ifdef HAVE_UTTYPE
905 while (username != NULL)
906 {
907 if (0 == sl_strncmp(username->name, ut->ut_name, UT_NAMESIZE) )
908 break;
909 username = username->next;
910 }
911#endif
912
913#ifdef HAVE_UTTYPE
914 /* ---------- LOGIN -------------- */
915 if (ut->ut_type == USER_PROCESS)
916 {
917 if (user == NULL)
918 {
919 user = SH_ALLOC(sizeof(struct log_user));
920 user->next = userlist;
921 userlist = (struct log_user *) user;
922 }
923 (void) sl_strlcpy((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE+1);
924 (void) sl_strlcpy((char*)(user->name), ut->ut_name, UT_NAMESIZE+1);
925#ifdef HAVE_UTHOST
926 (void) sl_strlcpy((char*)(user->ut_host), ut->ut_host, UT_HOSTSIZE+1);
927#else
928 user->ut_host[0] = '\0';
929#endif
930#ifdef HAVE_UTADDR
931#ifdef HAVE_UTADDR_V6
932 my_inet_ntoa(ut->ut_addr_v6, user->ut_ship, SH_IP_BUF);
933#else
934 my_inet_ntoa(ut->ut_addr, user->ut_ship, SH_IP_BUF);
935#endif
936#endif
937 user->time = ut->ut_time;
938
939 if (username == NULL /* not yet logged in */
940 || 0 == sl_strncmp(ut->ut_line, _("ttyp"), 4) /* in virt. console */
941 || 0 == sl_strncmp(ut->ut_line, _("ttyq"), 4) /* in virt. console */
942 ) {
943 status = sh_utmp_login_a((char*)user->name);
944 SH_MUTEX_LOCK(mutex_thread_nolog);
945 (void) sh_unix_time (user->time, ttt, TIM_MAX);
946 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
947#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
948 MSG_UT_LG1X,
949#elif defined(HAVE_UTHOST)
950 MSG_UT_LG1A,
951#else
952 MSG_UT_LG1B,
953#endif
954 user->name,
955 user->ut_tty,
956#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
957 user->ut_host,
958 user->ut_ship,
959#elif defined(HAVE_UTHOST)
960 user->ut_host,
961#endif
962 ttt,
963 status
964 );
965 SH_MUTEX_UNLOCK(mutex_thread_nolog);
966 } else
967 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host))
968 {
969 status = sh_utmp_login_a((char*)user->name);
970 SH_MUTEX_LOCK(mutex_thread_nolog);
971 (void) sh_unix_time (user->time, ttt, TIM_MAX);
972 sh_error_handle( ShUtmpLoginMulti, FIL__, __LINE__, 0,
973#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
974 MSG_UT_LG2X,
975#elif defined(HAVE_UTHOST)
976 MSG_UT_LG2A,
977#else
978 MSG_UT_LG2B,
979#endif
980 user->name,
981 user->ut_tty,
982#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
983 user->ut_host,
984 user->ut_ship,
985#elif defined(HAVE_UTHOST)
986 user->ut_host,
987#endif
988 ttt,
989 status
990 );
991 SH_MUTEX_UNLOCK(mutex_thread_nolog);
992 }
993
994 sh_utmp_login_morechecks(ut);
995 SL_RET0(_("sh_utmp_addlogin"));
996 }
997
998
999 /* --------- LOGOUT ---------------- */
1000 else if (ut->ut_name[0] == '\0'
1001 || ut->ut_type == DEAD_PROCESS /* solaris does not clear ut_name */
1002 )
1003 {
1004 if (user != NULL)
1005 {
1006#if defined(__linux__)
1007 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host)) {
1008#endif
1009 status = sh_utmp_login_r((char*)user->name);
1010 SH_MUTEX_LOCK(mutex_thread_nolog);
1011 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1012 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1013#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1014 MSG_UT_LG3X,
1015#elif defined(HAVE_UTHOST)
1016 MSG_UT_LG3A,
1017#else
1018 MSG_UT_LG3B,
1019#endif
1020 user->name,
1021 user->ut_tty,
1022#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1023 user->ut_host,
1024 user->ut_ship,
1025#elif defined(HAVE_UTHOST)
1026 user->ut_host,
1027#endif
1028 ttt,
1029 status
1030 );
1031 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1032 userold->next = user->next;
1033 if (user == userlist)
1034 userlist = user->next;
1035 sh_utmp_logout_morechecks((struct log_user *)user);
1036 SH_FREE((struct log_user *)user);
1037 user = NULL;
1038#if defined(__linux__)
1039 }
1040#endif
1041 }
1042 else
1043 {
1044 (void) sl_strlcpy(terminated_line, ut->ut_line, UT_HOSTSIZE);
1045 SH_MUTEX_LOCK(mutex_thread_nolog);
1046 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1047 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1048 MSG_UT_LG3C,
1049 terminated_line,
1050 ttt, 0
1051 );
1052 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1053 }
1054 SL_RET0(_("sh_utmp_addlogin"));
1055 }
1056
1057 /* default */
1058 SL_RET0(_("sh_utmp_addlogin"));
1059
1060 /* #ifdef HAVE_UTTYPE */
1061#else
1062
1063 if (user == NULL) /* probably a login */
1064 {
1065 user = SH_ALLOC(sizeof(struct log_user));
1066 sl_strlcpy(user->ut_tty, ut->ut_line, UT_LINESIZE+1);
1067 sl_strlcpy(user->name, ut->ut_name, UT_NAMESIZE+1);
1068#ifdef HAVE_UTHOST
1069 sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
1070#endif
1071#ifdef HAVE_UTADDR
1072#ifdef HAVE_UTADDR_V6
1073 my_inet_ntoa(ut->ut_addr_v6, user->ut_ship, SH_IP_BUF);
1074#else
1075 my_inet_ntoa(ut->ut_addr, user->ut_ship, SH_IP_BUF);
1076#endif
1077#endif
1078 user->time = ut->ut_time;
1079 user->next = userlist;
1080 userlist = user;
1081
1082 SH_MUTEX_LOCK(mutex_thread_nolog);
1083 (void) sh_unix_time (user->time, ttt, TIM_MAX);
1084 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
1085#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1086 MSG_UT_LG1X,
1087#elif defined(HAVE_UTHOST)
1088 MSG_UT_LG1A,
1089#else
1090 MSG_UT_LG1B,
1091#endif
1092 user->name,
1093 user->ut_tty,
1094#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1095 user->ut_host,
1096 user->ut_ship,
1097#elif defined(HAVE_UTHOST)
1098 user->ut_host,
1099#endif
1100 ttt,
1101 1
1102 );
1103 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1104 sh_utmp_login_morechecks(ut);
1105 }
1106 else /* probably a logout */
1107 {
1108 SH_MUTEX_LOCK(mutex_thread_nolog);
1109 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1110 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1111#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1112 MSG_UT_LG2X,
1113#elif defined(HAVE_UTHOST)
1114 MSG_UT_LG2A,
1115#else
1116 MSG_UT_LG2B,
1117#endif
1118 user->name,
1119 user->ut_tty,
1120#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1121 user->ut_host,
1122 user->ut_ship,
1123#elif defined(HAVE_UTHOST)
1124 user->ut_host,
1125#endif
1126 ttt,
1127 1
1128 );
1129 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1130 sh_utmp_logout_morechecks(user);
1131 userold->next = user->next;
1132 if (user == userlist) /* inserted Apr 4, 2004 */
1133 userlist = user->next;
1134 SH_FREE(user);
1135 user = NULL;
1136 }
1137
1138 SL_RET0(_("sh_utmp_addlogin"));
1139#endif
1140}
1141
1142static time_t lastmod = 0;
1143static off_t lastsize = 0;
1144static unsigned long lastread = 0;
1145
1146static void sh_utmp_check_internal (int mode)
1147{
1148 struct stat buf;
1149 int error;
1150 struct SH_UTMP_S * ut;
1151 unsigned long this_read;
1152 int val_retry;
1153
1154 SL_ENTER(_("sh_utmp_check_internal"));
1155
1156 /* error if no access
1157 */
1158 do {
1159 val_retry = /*@-unrecog@*/lstat ( mode_path[mode], &buf)/*@+unrecog@*/;
1160 } while (val_retry < 0 && errno == EINTR);
1161
1162 if (0 != val_retry)
1163 {
1164 error = errno;
1165 SH_MUTEX_LOCK(mutex_thread_nolog);
1166 sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
1167 (long) sh.real.uid, mode_path[mode]);
1168 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1169 SL_RET0(_("sh_utmp_check_internal"));
1170 }
1171
1172 /* modification time
1173 */
1174 if (mode < 2)
1175 {
1176 if (/*@-usedef@*/buf.st_mtime <= lastmod/*@+usedef@*/)
1177 {
1178 SL_RET0(_("sh_utmp_check_internal"));
1179 }
1180 else
1181 lastmod = buf.st_mtime;
1182 }
1183
1184 /* file size
1185 */
1186 if (/*@-usedef@*/buf.st_size < lastsize/*@+usedef@*/ && mode < 2)
1187 {
1188 SH_MUTEX_LOCK(mutex_thread_nolog);
1189 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_UT_ROT,
1190 mode_path[mode]);
1191 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1192 lastread = 0;
1193#ifndef USE_SETUTENT
1194 sh_utmp_feed_forward = 0L;
1195#endif
1196 }
1197
1198 if (mode < 2)
1199 lastsize = buf.st_size;
1200
1201 if (buf.st_size == 0)
1202 SL_RET0(_("sh_utmp_check_internal"));
1203
1204 sh_utmp_utmpname(mode_path[mode]);
1205 sh_utmp_setutent();
1206
1207 /*
1208 * feed forward if initializing
1209 * we need to do this here
1210 */
1211 this_read = 0;
1212
1213 if (mode < 2)
1214 {
1215 while (this_read < lastread) {
1216 ut = sh_utmp_getutent();
1217 ++this_read;
1218 }
1219 }
1220
1221 /* start reading
1222 */
1223 this_read = 0;
1224 while (1 == 1) {
1225 ut = sh_utmp_getutent();
1226 if (ut == NULL)
1227 break;
1228 /* modified: ut_user --> ut_name */
1229 if (mode == 1 || (mode == 2 && ut->ut_name[0] != '\0'
1230#ifdef HAVE_UTTYPE
1231 && ut->ut_type != DEAD_PROCESS
1232#endif
1233 ))
1234 sh_utmp_addlogin (ut);
1235 ++this_read;
1236 }
1237
1238 sh_utmp_endutent();
1239
1240 if (mode < 2)
1241 {
1242 lastread += this_read;
1243#ifndef USE_SETUTENT
1244 sh_utmp_feed_forward += (long) (this_read * sizeof(struct SH_UTMP_S));
1245 lastread = 0;
1246#endif
1247 }
1248
1249 SL_RET0(_("sh_utmp_check_internal"));
1250}
1251
1252extern void sh_ltrack_check(struct SH_UTMP_S * ut);
1253
1254static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut)
1255{
1256 sh_ltrack_check(ut);
1257 return;
1258}
1259
1260static void sh_utmp_logout_morechecks(struct log_user * user)
1261{
1262 (void) user;
1263 return;
1264}
1265
1266#endif
1267
1268
1269/* #ifdef SH_USE_UTMP */
1270#endif
1271
1272
1273
Note: See TracBrowser for help on using the repository browser.