source: trunk/src/sh_utmp.c@ 322

Last change on this file since 322 was 295, checked in by katerina, 14 years ago

Support for IPv6 (ticket #222).

File size: 27.9 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#endif
542 return sh_utmp_init_internal();
543}
544
545/*************
546 *
547 * module cleanup
548 *
549 *************/
550#ifdef HAVE_UTTYPE
551static int sh_utmp_login_clean(void);
552#endif
553
554#if defined(HAVE_PTHREAD)
555static sh_watches inotify_watch;
556#endif
557
558int sh_utmp_end ()
559{
560 struct log_user * user = userlist;
561 struct log_user * userold;
562
563 SL_ENTER(_("sh_utmp_end"));
564 while (user)
565 {
566 userold = user;
567 user = user->next;
568 SH_FREE(userold);
569 }
570 userlist = NULL;
571#ifdef HAVE_UTTYPE
572 (void) sh_utmp_login_clean();
573#endif
574 /* Reset the flag, such that the module
575 * can be re-enabled.
576 */
577 set_defaults();
578 init_done = 0;
579
580#if defined(HAVE_PTHREAD)
581 sh_inotify_remove(&inotify_watch);
582#endif
583
584 SL_RETURN( (0), _("sh_utmp_end"));
585}
586
587
588int sh_utmp_reconf()
589{
590 set_defaults();
591#if defined(HAVE_PTHREAD)
592 sh_inotify_remove(&inotify_watch);
593#endif
594 return 0;
595}
596
597
598/*************
599 *
600 * module timer
601 *
602 *************/
603int sh_utmp_timer (time_t tcurrent)
604{
605#if !defined(HAVE_PTHREAD)
606 retry_msleep(1, 0);
607
608 if ((time_t) (tcurrent - lastcheck) >= ShUtmpInterval)
609 {
610 lastcheck = tcurrent;
611 return (-1);
612 }
613 return 0;
614#else
615 int errnum = 0;
616
617 if ( (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE) &&
618 sh.flag.checkSum != SH_CHECK_INIT )
619 {
620 sh_inotify_wait_for_change(mode_path[1], &inotify_watch,
621 &errnum, ShUtmpInterval);
622 }
623
624 lastcheck = tcurrent;
625
626 if (SH_INOTIFY_ERROR(errnum))
627 {
628 char ebuf[SH_ERRBUF_SIZE];
629
630 SH_MUTEX_LOCK(mutex_thread_nolog);
631 sh_error_message(errnum, ebuf, sizeof(ebuf));
632 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
633 ebuf,
634 _("sh_utmp_timer") );
635 SH_MUTEX_UNLOCK(mutex_thread_nolog);
636 }
637 return -1;
638#endif
639}
640
641/*************
642 *
643 * module check
644 *
645 *************/
646int sh_utmp_check ()
647{
648 SL_ENTER(_("sh_utmp_check"));
649 if (ShUtmpActive == BAD)
650 {
651#if defined(HAVE_PTHREAD)
652 sh_inotify_remove(&inotify_watch);
653#endif
654 SL_RETURN( (-1), _("sh_utmp_check"));
655 }
656 SH_MUTEX_LOCK(mutex_thread_nolog);
657 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_UT_CHECK);
658 SH_MUTEX_UNLOCK(mutex_thread_nolog);
659 sh_utmp_check_internal (1);
660
661 SL_RETURN(0, _("sh_utmp_check"));
662}
663
664/*************
665 *
666 * module setup
667 *
668 *************/
669
670int sh_utmp_set_login_solo (const char * c)
671{
672 int retval;
673 char tmp[32];
674
675 SL_ENTER(_("sh_utmp_set_login_solo"));
676 tmp[0] = '='; tmp[1] = '\0';
677 (void) sl_strlcat (tmp, c, 32);
678 SH_MUTEX_LOCK(mutex_thread_nolog);
679 retval = sh_error_set_level (tmp, &ShUtmpLoginSolo);
680 SH_MUTEX_UNLOCK(mutex_thread_nolog);
681 SL_RETURN(retval, _("sh_utmp_set_login_solo"));
682}
683
684int sh_utmp_set_login_multi (const char * c)
685{
686 int retval;
687 char tmp[32];
688
689 SL_ENTER(_("sh_utmp_set_login_multi"));
690 tmp[0] = '='; tmp[1] = '\0';
691 (void) sl_strlcat (tmp, c, 32);
692 SH_MUTEX_LOCK(mutex_thread_nolog);
693 retval = sh_error_set_level (tmp, &ShUtmpLoginMulti);
694 SH_MUTEX_UNLOCK(mutex_thread_nolog);
695 SL_RETURN(retval, _("sh_utmp_set_login_multi"));
696}
697
698int sh_utmp_set_logout_good (const char * c)
699{
700 int retval;
701 char tmp[32];
702
703 SL_ENTER(_("sh_utmp_set_logout_good"));
704 tmp[0] = '='; tmp[1] = '\0';
705 (void) sl_strlcat (tmp, c, 32);
706 SH_MUTEX_LOCK(mutex_thread_nolog);
707 retval = sh_error_set_level (tmp, &ShUtmpLogout);
708 SH_MUTEX_UNLOCK(mutex_thread_nolog);
709 SL_RETURN(retval, _("sh_utmp_set_logout_good"));
710}
711
712int sh_utmp_set_login_timer (const char * c)
713{
714 long val;
715
716 SL_ENTER(_("sh_utmp_set_login_timer"));
717 val = strtol (c, (char **)NULL, 10);
718 if (val <= 0)
719 {
720 SH_MUTEX_LOCK(mutex_thread_nolog);
721 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
722 _("utmp timer"), c);
723 SH_MUTEX_UNLOCK(mutex_thread_nolog);
724 SL_RETURN((-1), _("sh_utmp_set_login_timer"));
725 }
726
727 ShUtmpInterval = (time_t) val;
728 SL_RETURN(0, _("sh_utmp_set_login_timer"));
729}
730
731int sh_utmp_set_login_activate (const char * c)
732{
733 int i;
734 SL_ENTER(_("sh_utmp_set_login_activate"));
735 i = sh_util_flagval(c, &ShUtmpActive);
736 SL_RETURN(i, _("sh_utmp_set_login_activate"));
737}
738
739#ifdef HAVE_UTTYPE
740struct login_ct {
741 char name[UT_NAMESIZE+1];
742 int nlogin;
743 struct login_ct * next;
744};
745
746static struct login_ct * login_ct_list = NULL;
747
748static int sh_utmp_login_clean(void)
749{
750 struct login_ct * list = login_ct_list;
751 struct login_ct * old;
752
753 login_ct_list = NULL;
754
755 while (list)
756 {
757 old = list;
758 list = list->next;
759 SH_FREE(old);
760 }
761 return 0;
762}
763
764/* add a username to the list of logged-in users
765 */
766static int sh_utmp_login_a(char * str)
767{
768 struct login_ct * list = login_ct_list;
769
770 while (list)
771 {
772 if (0 == sl_strcmp(list->name, str))
773 {
774 ++(list->nlogin);
775 return list->nlogin;
776 }
777 list = list->next;
778 }
779 list = SH_ALLOC(sizeof(struct login_ct));
780 (void) sl_strlcpy(list->name, str, UT_NAMESIZE+1);
781 list->nlogin = 1;
782 list->next = login_ct_list;
783 login_ct_list = list;
784 return 1;
785}
786
787static int sh_utmp_login_r(char * str)
788{
789 struct login_ct * list = login_ct_list;
790 struct login_ct * old = login_ct_list;
791
792 while (list)
793 {
794 if (0 == sl_strcmp(list->name, str))
795 {
796 list->nlogin -= 1;
797 if (list->nlogin > 0)
798 {
799 return list->nlogin;
800 }
801 if (login_ct_list == list) /* modified Apr 4, 2004 */
802 {
803 login_ct_list = list->next;
804 SH_FREE(list);
805 }
806 else
807 {
808 old->next = list->next;
809 SH_FREE(list);
810 }
811 return 0;
812 }
813 old = list;
814 list = list->next;
815 }
816 return 0;
817}
818
819#endif
820
821
822/* for each login:
823 * - allocate a log record
824 * - link device.ut_record -> log_record
825 * - link user.ut_record -> log_record
826 */
827
828#ifdef HAVE_UTTYPE
829static int sh_utmp_is_virtual (char * in_utline, char * in_uthost)
830{
831
832 if (in_uthost != NULL &&
833 in_utline != NULL &&
834 in_uthost[0] == ':' &&
835 in_uthost[1] == '0' &&
836 0 == sl_strncmp(in_utline, _("pts/"), 4))
837 {
838 return 1;
839 }
840
841 return 0;
842}
843#endif
844
845/* These variables are not used anywhere. They only exist
846 * to assign &userold, &user to them, which keeps gcc from
847 * putting them into a register, and avoids the 'clobbered
848 * by longjmp' warning. And no, 'volatile' proved insufficient.
849 */
850static void * sh_dummy_userold = NULL;
851static void * sh_dummy_user = NULL;
852
853
854static void sh_utmp_addlogin (struct SH_UTMP_S * ut)
855{
856 struct log_user * user = userlist;
857 struct log_user * userold = userlist;
858#ifdef HAVE_UTTYPE
859 struct log_user * username = userlist;
860#endif
861
862 char ttt[TIM_MAX];
863#ifdef HAVE_UTTYPE
864 volatile int status;
865#endif
866
867 SL_ENTER(_("sh_utmp_addlogin"));
868
869 /* Take the address to keep gcc from putting them into registers.
870 * Avoids the 'clobbered by longjmp' warning.
871 */
872 sh_dummy_userold = (void*) &userold;
873 sh_dummy_user = (void*) &user;
874
875 if (ut->ut_line[0] == '\0')
876 SL_RET0(_("sh_utmp_addlogin"));
877
878 /* for some stupid reason, AIX repeats the wtmp entry for logouts
879 * with ssh
880 */
881 if (memcmp (&save_utmp, ut, sizeof(struct SH_UTMP_S)) == 0)
882 {
883 memset(&save_utmp, (int) '\0', sizeof(struct SH_UTMP_S));
884 SL_RET0(_("sh_utmp_addlogin"));
885 }
886 memcpy (&save_utmp, ut, sizeof(struct SH_UTMP_S));
887
888
889 /* ------- find user --------
890 */
891 while (user != NULL)
892 {
893 if (0 == sl_strncmp((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE) )
894 break;
895 userold = user;
896 user = user->next;
897 }
898
899#ifdef HAVE_UTTYPE
900 while (username != NULL)
901 {
902 if (0 == sl_strncmp(username->name, ut->ut_name, UT_NAMESIZE) )
903 break;
904 username = username->next;
905 }
906#endif
907
908#ifdef HAVE_UTTYPE
909 /* ---------- LOGIN -------------- */
910 if (ut->ut_type == USER_PROCESS)
911 {
912 if (user == NULL)
913 {
914 user = SH_ALLOC(sizeof(struct log_user));
915 user->next = userlist;
916 userlist = (struct log_user *) user;
917 }
918 (void) sl_strlcpy((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE+1);
919 (void) sl_strlcpy((char*)(user->name), ut->ut_name, UT_NAMESIZE+1);
920#ifdef HAVE_UTHOST
921 (void) sl_strlcpy((char*)(user->ut_host), ut->ut_host, UT_HOSTSIZE+1);
922#else
923 user->ut_host[0] = '\0';
924#endif
925#ifdef HAVE_UTADDR
926#ifdef HAVE_UTADDR_V6
927 my_inet_ntoa(ut->ut_addr_v6, user->ut_ship, SH_IP_BUF);
928#else
929 my_inet_ntoa(ut->ut_addr, user->ut_ship, SH_IP_BUF);
930#endif
931#endif
932 user->time = ut->ut_time;
933
934 if (username == NULL /* not yet logged in */
935 || 0 == sl_strncmp(ut->ut_line, _("ttyp"), 4) /* in virt. console */
936 || 0 == sl_strncmp(ut->ut_line, _("ttyq"), 4) /* in virt. console */
937 ) {
938 status = sh_utmp_login_a((char*)user->name);
939 SH_MUTEX_LOCK(mutex_thread_nolog);
940 (void) sh_unix_time (user->time, ttt, TIM_MAX);
941 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
942#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
943 MSG_UT_LG1X,
944#elif defined(HAVE_UTHOST)
945 MSG_UT_LG1A,
946#else
947 MSG_UT_LG1B,
948#endif
949 user->name,
950 user->ut_tty,
951#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
952 user->ut_host,
953 user->ut_ship,
954#elif defined(HAVE_UTHOST)
955 user->ut_host,
956#endif
957 ttt,
958 status
959 );
960 SH_MUTEX_UNLOCK(mutex_thread_nolog);
961 } else
962 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host))
963 {
964 status = sh_utmp_login_a((char*)user->name);
965 SH_MUTEX_LOCK(mutex_thread_nolog);
966 (void) sh_unix_time (user->time, ttt, TIM_MAX);
967 sh_error_handle( ShUtmpLoginMulti, FIL__, __LINE__, 0,
968#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
969 MSG_UT_LG2X,
970#elif defined(HAVE_UTHOST)
971 MSG_UT_LG2A,
972#else
973 MSG_UT_LG2B,
974#endif
975 user->name,
976 user->ut_tty,
977#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
978 user->ut_host,
979 user->ut_ship,
980#elif defined(HAVE_UTHOST)
981 user->ut_host,
982#endif
983 ttt,
984 status
985 );
986 SH_MUTEX_UNLOCK(mutex_thread_nolog);
987 }
988
989 sh_utmp_login_morechecks(ut);
990 SL_RET0(_("sh_utmp_addlogin"));
991 }
992
993
994 /* --------- LOGOUT ---------------- */
995 else if (ut->ut_name[0] == '\0'
996 || ut->ut_type == DEAD_PROCESS /* solaris does not clear ut_name */
997 )
998 {
999 if (user != NULL)
1000 {
1001#if defined(__linux__)
1002 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host)) {
1003#endif
1004 status = sh_utmp_login_r((char*)user->name);
1005 SH_MUTEX_LOCK(mutex_thread_nolog);
1006 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1007 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1008#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1009 MSG_UT_LG3X,
1010#elif defined(HAVE_UTHOST)
1011 MSG_UT_LG3A,
1012#else
1013 MSG_UT_LG3B,
1014#endif
1015 user->name,
1016 user->ut_tty,
1017#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1018 user->ut_host,
1019 user->ut_ship,
1020#elif defined(HAVE_UTHOST)
1021 user->ut_host,
1022#endif
1023 ttt,
1024 status
1025 );
1026 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1027 userold->next = user->next;
1028 if (user == userlist)
1029 userlist = user->next;
1030 sh_utmp_logout_morechecks((struct log_user *)user);
1031 SH_FREE((struct log_user *)user);
1032 user = NULL;
1033#if defined(__linux__)
1034 }
1035#endif
1036 }
1037 else
1038 {
1039 (void) sl_strlcpy(terminated_line, ut->ut_line, UT_HOSTSIZE);
1040 SH_MUTEX_LOCK(mutex_thread_nolog);
1041 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1042 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1043 MSG_UT_LG3C,
1044 terminated_line,
1045 ttt, 0
1046 );
1047 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1048 }
1049 SL_RET0(_("sh_utmp_addlogin"));
1050 }
1051
1052 /* default */
1053 SL_RET0(_("sh_utmp_addlogin"));
1054
1055 /* #ifdef HAVE_UTTYPE */
1056#else
1057
1058 if (user == NULL) /* probably a login */
1059 {
1060 user = SH_ALLOC(sizeof(struct log_user));
1061 sl_strlcpy(user->ut_tty, ut->ut_line, UT_LINESIZE+1);
1062 sl_strlcpy(user->name, ut->ut_name, UT_NAMESIZE+1);
1063#ifdef HAVE_UTHOST
1064 sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
1065#endif
1066#ifdef HAVE_UTADDR
1067#ifdef HAVE_UTADDR_V6
1068 my_inet_ntoa(ut->ut_addr_v6, user->ut_ship, SH_IP_BUF);
1069#else
1070 my_inet_ntoa(ut->ut_addr, user->ut_ship, SH_IP_BUF);
1071#endif
1072#endif
1073 user->time = ut->ut_time;
1074 user->next = userlist;
1075 userlist = user;
1076
1077 SH_MUTEX_LOCK(mutex_thread_nolog);
1078 (void) sh_unix_time (user->time, ttt, TIM_MAX);
1079 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
1080#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1081 MSG_UT_LG1X,
1082#elif defined(HAVE_UTHOST)
1083 MSG_UT_LG1A,
1084#else
1085 MSG_UT_LG1B,
1086#endif
1087 user->name,
1088 user->ut_tty,
1089#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1090 user->ut_host,
1091 user->ut_ship,
1092#elif defined(HAVE_UTHOST)
1093 user->ut_host,
1094#endif
1095 ttt,
1096 1
1097 );
1098 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1099 sh_utmp_login_morechecks(ut);
1100 }
1101 else /* probably a logout */
1102 {
1103 SH_MUTEX_LOCK(mutex_thread_nolog);
1104 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
1105 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1106#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1107 MSG_UT_LG2X,
1108#elif defined(HAVE_UTHOST)
1109 MSG_UT_LG2A,
1110#else
1111 MSG_UT_LG2B,
1112#endif
1113 user->name,
1114 user->ut_tty,
1115#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1116 user->ut_host,
1117 user->ut_ship,
1118#elif defined(HAVE_UTHOST)
1119 user->ut_host,
1120#endif
1121 ttt,
1122 1
1123 );
1124 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1125 sh_utmp_logout_morechecks(user);
1126 userold->next = user->next;
1127 if (user == userlist) /* inserted Apr 4, 2004 */
1128 userlist = user->next;
1129 SH_FREE(user);
1130 user = NULL;
1131 }
1132
1133 SL_RET0(_("sh_utmp_addlogin"));
1134#endif
1135}
1136
1137static time_t lastmod = 0;
1138static off_t lastsize = 0;
1139static unsigned long lastread = 0;
1140
1141static void sh_utmp_check_internal (int mode)
1142{
1143 struct stat buf;
1144 int error;
1145 struct SH_UTMP_S * ut;
1146 unsigned long this_read;
1147 int val_retry;
1148
1149 SL_ENTER(_("sh_utmp_check_internal"));
1150
1151 /* error if no access
1152 */
1153 do {
1154 val_retry = /*@-unrecog@*/lstat ( mode_path[mode], &buf)/*@+unrecog@*/;
1155 } while (val_retry < 0 && errno == EINTR);
1156
1157 if (0 != val_retry)
1158 {
1159 error = errno;
1160 SH_MUTEX_LOCK(mutex_thread_nolog);
1161 sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
1162 (long) sh.real.uid, mode_path[mode]);
1163 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1164 SL_RET0(_("sh_utmp_check_internal"));
1165 }
1166
1167 /* modification time
1168 */
1169 if (mode < 2)
1170 {
1171 if (/*@-usedef@*/buf.st_mtime <= lastmod/*@+usedef@*/)
1172 {
1173 SL_RET0(_("sh_utmp_check_internal"));
1174 }
1175 else
1176 lastmod = buf.st_mtime;
1177 }
1178
1179 /* file size
1180 */
1181 if (/*@-usedef@*/buf.st_size < lastsize/*@+usedef@*/ && mode < 2)
1182 {
1183 SH_MUTEX_LOCK(mutex_thread_nolog);
1184 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_UT_ROT,
1185 mode_path[mode]);
1186 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1187 lastread = 0;
1188#ifndef USE_SETUTENT
1189 sh_utmp_feed_forward = 0L;
1190#endif
1191 }
1192
1193 if (mode < 2)
1194 lastsize = buf.st_size;
1195
1196 if (buf.st_size == 0)
1197 SL_RET0(_("sh_utmp_check_internal"));
1198
1199 sh_utmp_utmpname(mode_path[mode]);
1200 sh_utmp_setutent();
1201
1202 /*
1203 * feed forward if initializing
1204 * we need to do this here
1205 */
1206 this_read = 0;
1207
1208 if (mode < 2)
1209 {
1210 while (this_read < lastread) {
1211 ut = sh_utmp_getutent();
1212 ++this_read;
1213 }
1214 }
1215
1216 /* start reading
1217 */
1218 this_read = 0;
1219 while (1 == 1) {
1220 ut = sh_utmp_getutent();
1221 if (ut == NULL)
1222 break;
1223 /* modified: ut_user --> ut_name */
1224 if (mode == 1 || (mode == 2 && ut->ut_name[0] != '\0'
1225#ifdef HAVE_UTTYPE
1226 && ut->ut_type != DEAD_PROCESS
1227#endif
1228 ))
1229 sh_utmp_addlogin (ut);
1230 ++this_read;
1231 }
1232
1233 sh_utmp_endutent();
1234
1235 if (mode < 2)
1236 {
1237 lastread += this_read;
1238#ifndef USE_SETUTENT
1239 sh_utmp_feed_forward += (long) (this_read * sizeof(struct SH_UTMP_S));
1240 lastread = 0;
1241#endif
1242 }
1243
1244 SL_RET0(_("sh_utmp_check_internal"));
1245}
1246
1247extern void sh_ltrack_check(struct SH_UTMP_S * ut);
1248
1249static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut)
1250{
1251 sh_ltrack_check(ut);
1252 return;
1253}
1254
1255static void sh_utmp_logout_morechecks(struct log_user * user)
1256{
1257 (void) user;
1258 return;
1259}
1260
1261#endif
1262
1263
1264/* #ifdef SH_USE_UTMP */
1265#endif
1266
1267
1268
Note: See TracBrowser for help on using the repository browser.