source: trunk/src/sh_utmp.c@ 259

Last change on this file since 259 was 259, checked in by katerina, 15 years ago

Use inotify in utmp module (ticket #177).

File size: 27.2 KB
RevLine 
[1]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"
[143]81#include "sh_pthread.h"
[259]82#include "sh_inotify.h"
[1]83
[143]84SH_MUTEX_EXTERN(mutex_thread_nolog);
[1]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[16]; /* 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
[259]190static char * mode_path[] = { _PATH_WTMP, _PATH_WTMP, _PATH_UTMP };
191
[1]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 NULL,
228 NULL
229 },
230};
231
[149]232static void set_defaults(void)
[1]233{
[149]234 ShUtmpLoginSolo = SH_ERR_INFO;
235 ShUtmpLoginMulti = SH_ERR_WARN;
236 ShUtmpLogout = SH_ERR_INFO;
237 ShUtmpActive = S_TRUE;
238 ShUtmpInterval = 300;
239 return;
[1]240}
241
242
243#if defined (HAVE_SETUTENT) && defined (USE_SETUTENT)
244
245#ifdef HAVE_UTMPX_H
246
247#define sh_utmp_utmpname utmpxname
248#define sh_utmp_setutent setutxent
249#define sh_utmp_endutent endutxent
250#define sh_utmp_getutent getutxent
251#define sh_utmp_getutid getutxid
252#define sh_utmp_getutline getutxline
253
254#else
255
256#define sh_utmp_utmpname utmpname
257#define sh_utmp_setutent setutent
258#define sh_utmp_endutent endutent
259#define sh_utmp_getutent getutent
260#define sh_utmp_getutid getutid
261#define sh_utmp_getutline getutline
262
263#endif
264
265#else
266
267/* BSD lacks getutent() etc.
268 * utmpname(), setutent(), and endutent() return void,
269 * so we do not perform much error handling.
270 * Errors must be recognized by getutent() returning NULL.
271 * Apparently, the application cannot check whether wtmp is empty,
272 * or whether there was an fopen() error.
273 */
274
275static FILE * sh_utmpfile = NULL;
276static char sh_utmppath[80] = _PATH_UTMP;
277
278/* sh_utmp_feed_forward is for optimizing
279 * (fseek instead of getutent loop)
280 */
281static long sh_utmp_feed_forward = 0;
282
283static void sh_utmp_utmpname(const char * str)
284{
285 SL_ENTER(_("sh_utmp_utmpname"));
286 if (sh_utmpfile != NULL)
287 {
[252]288 (void) sl_fclose (FIL__, __LINE__, sh_utmpfile);
[1]289 sh_utmpfile = NULL;
290 }
291
292 (void) sl_strlcpy (sh_utmppath, str, 80);
293 SL_RET0(_("sh_utmp_utmpname"));
294}
295
296static void sh_utmp_setutent(void)
297{
298 int error;
299 int fd;
300
301 SL_ENTER(_("sh_utmp_setutent"));
302
303 ASSERT((sh_utmppath != NULL), _("sh_utmppath != NULL"));
304
305 if (sh_utmppath == NULL)
306 SL_RET0(_("sh_utmp_setutent"));
307
308 if (sh_utmpfile == NULL)
309 {
[143]310 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]311 fd = (int) aud_open (FIL__, __LINE__, SL_NOPRIV,
312 sh_utmppath, O_RDONLY, 0);
[143]313 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]314 if (fd >= 0)
315 {
316 sh_utmpfile = fdopen(fd, "r");
317 }
318
319 /* -- If (sh_utmpfile == NULL) then either the open() or the fdopen()
320 * has failed.
321 */
322 if (sh_utmpfile == NULL)
323 {
324 error = errno;
[143]325 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]326 sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
327 (long) sh.real.uid, sh_utmppath);
[143]328 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]329 SL_RET0(_("sh_utmp_setutent"));
330 }
331 }
332 (void) fseek (sh_utmpfile, 0L, SEEK_SET);
333 if (-1 == fseek (sh_utmpfile, sh_utmp_feed_forward, SEEK_CUR))
334 {
335 sh_utmp_feed_forward = 0; /* modified Apr 4, 2004 */
336 (void) fseek (sh_utmpfile, 0L, SEEK_SET);
337 }
338 clearerr (sh_utmpfile);
339 SL_RET0(_("sh_utmp_setutent"));
340}
341
342static void sh_utmp_endutent(void)
343{
344 SL_ENTER(_("sh_utmp_endutent"));
[30]345 if (NULL != sh_utmpfile)
[252]346 (void) sl_fclose(FIL__, __LINE__, sh_utmpfile);
[1]347 sh_utmpfile = NULL;
348 SL_RET0(_("sh_utmp_endutent"));
349}
350
351static struct SH_UTMP_S * sh_utmp_getutent(void)
352{
353 size_t in;
354 static struct SH_UTMP_S out;
355
356 SL_ENTER(_("sh_utmp_getutent"));
357
358 ASSERT_RET((sh_utmpfile != NULL), _("sh_utmpfile != NULL"), (NULL))
359
360 in = fread (&out, sizeof(struct SH_UTMP_S), 1, sh_utmpfile);
361
362 if (in != 1)
363 {
364 if (ferror (sh_utmpfile) != 0)
365 {
366 clearerr (sh_utmpfile);
367 SL_RETURN(NULL, _("sh_utmp_getutent"));
368 }
369 else
370 {
371 SL_RETURN(NULL, _("sh_utmp_getutent"));
372 }
373 }
374 SL_RETURN(&out, _("sh_utmp_getutent"));
375}
376
377#ifdef USE_UNUSED
378
379static struct SH_UTMP_S * sh_utmp_getutline(struct SH_UTMP_S * ut)
380{
381 struct SH_UTMP_S * out;
382
383 while (1) {
384 if ((out = sh_utmp_getutent()) == NULL) {
385 return NULL;
386 }
387#ifdef HAVE_UTTYPE
388 if (out->ut_type == USER_PROCESS || out->ut_type == LOGIN_PROCESS)
389 if (sl_strcmp(ut->ut_line, out->ut_line) == 0)
390 return out;
391#else
392 if ( 0 != sl_strncmp (out->ut_name, "reboot", 6) &&
393 0 != sl_strncmp (out->ut_name, "shutdown", 8) &&
394 0 != sl_strncmp (out->ut_name, "date", 4) )
395 return out;
396#endif
397 }
398 return NULL;
399}
400
401static struct SH_UTMP_S * sh_utmp_getutid(struct SH_UTMP_S * ut)
402{
403#ifdef HAVE_UTTYPE
404 struct SH_UTMP_S * out;
405
406 if (ut->ut_type == RUN_LVL || ut->ut_type == BOOT_TIME ||
407 ut->ut_type == NEW_TIME || ut->ut_type == OLD_TIME)
408 {
409 while (1) {
410 if ((out = sh_utmp_getutent()) == NULL) {
411 return NULL;
412 }
413 if (out->ut_type == ut->ut_type)
414 return out;
415 }
416 }
417 else if (ut->ut_type == INIT_PROCESS || ut->ut_type == LOGIN_PROCESS ||
418 ut->ut_type == USER_PROCESS || ut->ut_type == DEAD_PROCESS )
419 {
420 while (1) {
421 if ((out = sh_utmp_getutent()) == NULL) {
422 return NULL;
423 }
424 if (sl_strcmp(ut->ut_id, out->ut_id) == 0)
425 return out;
426 }
427 }
428#endif
429 return NULL;
430}
431/* #ifdef USE_UNUSED */
432#endif
433
434/* #ifdef HAVE_SETUTENT */
435#endif
436
437#ifdef HAVE_UTADDR
438#ifdef HAVE_INET_ATON
439static char * my_inet_ntoa(struct in_addr in)
440{
441 return /*@-unrecog@*/inet_ntoa(in)/*@+unrecog@*/;
442}
443#else
444static char * my_inet_ntoa(struct in_addr in)
445{
446 unsigned char a, b, c, d;
447 static char foo[16];
448 char bar[4];
449 memcpy (bar, &(in.s_addr), 4); /* memory alignment (?) */
450 memcpy (&a, &bar[0], 1);
451 memcpy (&b, &bar[1], 1);
452 memcpy (&c, &bar[2], 1);
453 memcpy (&d, &bar[3], 1);
[34]454 sprintf(foo, _("%d.%d.%d.%d"), /* known to fit */
[1]455 (int) a, (int) b, (int) c, (int) d);
456 return foo;
457}
458#endif
459/* #ifdef HAVE_UTADDR */
460#endif
461
462#if defined(__linux__) && !defined(ut_addr)
463#define ut_addr ut_addr_v6[0]
464#endif
465
466
467static struct log_user * userlist = NULL;
468static time_t lastcheck;
[142]469static int init_done = 0;
[1]470
471/*************
472 *
473 * module init
474 *
475 *************/
[142]476
[170]477static int sh_utmp_init_internal (void)
[1]478{
479
480 SL_ENTER(_("sh_utmp_init"));
481 if (ShUtmpActive == BAD)
482 SL_RETURN( (-1), _("sh_utmp_init"));
483
484 /* do not re-initialize after a re-configuration
485 */
[142]486 if (init_done == 1) {
[1]487 SL_RETURN( (0), _("sh_utmp_init"));
488 }
489 lastcheck = time (NULL);
490 userlist = NULL;
491 memset (&save_utmp, 0, sizeof(struct SH_UTMP_S));
492 sh_utmp_check_internal (2); /* current logins */
493 sh_utmp_check_internal (0);
[142]494 init_done = 1;
[1]495 SL_RETURN( (0), _("sh_utmp_init"));
496}
497
[142]498int sh_utmp_init (struct mod_type * arg)
499{
500 if (ShUtmpActive == BAD)
501 return SH_MOD_FAILED;
502#ifdef HAVE_PTHREAD
[144]503 if (arg != NULL && arg->initval < 0 &&
504 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE))
[142]505 {
506 if (0 == sh_pthread_create(sh_threaded_module_run, (void *)arg))
507 return SH_MOD_THREAD;
508 else
509 return SH_MOD_FAILED;
510 }
511#endif
512 return sh_utmp_init_internal();
513}
514
[1]515/*************
516 *
517 * module cleanup
518 *
519 *************/
520#ifdef HAVE_UTTYPE
[170]521static int sh_utmp_login_clean(void);
[1]522#endif
523
[259]524#if defined(HAVE_PTHREAD)
525static sh_watches inotify_watch;
526#endif
527
[1]528int sh_utmp_end ()
529{
530 struct log_user * user = userlist;
531 struct log_user * userold;
532
533 SL_ENTER(_("sh_utmp_end"));
534 while (user)
535 {
536 userold = user;
537 user = user->next;
538 SH_FREE(userold);
539 }
540 userlist = NULL;
541#ifdef HAVE_UTTYPE
542 (void) sh_utmp_login_clean();
543#endif
[142]544 /* Reset the flag, such that the module
545 * can be re-enabled.
546 */
[149]547 set_defaults();
[142]548 init_done = 0;
[259]549
550 sh_inotify_remove(&inotify_watch);
551
[1]552 SL_RETURN( (0), _("sh_utmp_end"));
553}
554
[259]555
[149]556int sh_utmp_reconf()
557{
558 set_defaults();
[259]559 sh_inotify_remove(&inotify_watch);
[149]560 return 0;
561}
[1]562
[149]563
[1]564/*************
565 *
566 * module timer
567 *
568 *************/
569int sh_utmp_timer (time_t tcurrent)
570{
[259]571#if !defined(HAVE_PTHREAD)
572 retry_msleep(1, 0);
573
[1]574 if ((time_t) (tcurrent - lastcheck) >= ShUtmpInterval)
575 {
576 lastcheck = tcurrent;
577 return (-1);
578 }
579 return 0;
[259]580#else
581 int errnum = 0;
582
583 if (sh.flag.checkSum != SH_CHECK_INIT)
584 sh_inotify_wait_for_change(mode_path[1], &inotify_watch,
585 &errnum, ShUtmpInterval);
586
587 lastcheck = tcurrent;
588 if (SH_INOTIFY_ERROR(errnum))
589 {
590 char ebuf[SH_ERRBUF_SIZE];
591
592 SH_MUTEX_LOCK(mutex_thread_nolog);
593 sh_error_message(errnum, ebuf, sizeof(ebuf));
594 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
595 ebuf,
596 _("sh_utmp_timer") );
597 SH_MUTEX_UNLOCK(mutex_thread_nolog);
598 }
599 return -1;
600#endif
[1]601}
602
603/*************
604 *
605 * module check
606 *
607 *************/
608int sh_utmp_check ()
609{
610 SL_ENTER(_("sh_utmp_check"));
[142]611 if (ShUtmpActive == BAD)
[259]612 {
613#if defined(HAVE_PTHREAD)
614 sh_inotify_remove(&inotify_watch);
615#endif
616 SL_RETURN( (-1), _("sh_utmp_check"));
617 }
[143]618 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]619 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_UT_CHECK);
[143]620 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]621 sh_utmp_check_internal (1);
622
623 SL_RETURN(0, _("sh_utmp_check"));
624}
625
626/*************
627 *
628 * module setup
629 *
630 *************/
631
[68]632int sh_utmp_set_login_solo (const char * c)
[1]633{
634 int retval;
635 char tmp[32];
636
637 SL_ENTER(_("sh_utmp_set_login_solo"));
638 tmp[0] = '='; tmp[1] = '\0';
639 (void) sl_strlcat (tmp, c, 32);
[143]640 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]641 retval = sh_error_set_level (tmp, &ShUtmpLoginSolo);
[143]642 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]643 SL_RETURN(retval, _("sh_utmp_set_login_solo"));
644}
645
[68]646int sh_utmp_set_login_multi (const char * c)
[1]647{
648 int retval;
649 char tmp[32];
650
651 SL_ENTER(_("sh_utmp_set_login_multi"));
652 tmp[0] = '='; tmp[1] = '\0';
653 (void) sl_strlcat (tmp, c, 32);
[143]654 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]655 retval = sh_error_set_level (tmp, &ShUtmpLoginMulti);
[143]656 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]657 SL_RETURN(retval, _("sh_utmp_set_login_multi"));
658}
659
[68]660int sh_utmp_set_logout_good (const char * c)
[1]661{
662 int retval;
663 char tmp[32];
664
665 SL_ENTER(_("sh_utmp_set_logout_good"));
666 tmp[0] = '='; tmp[1] = '\0';
667 (void) sl_strlcat (tmp, c, 32);
[143]668 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]669 retval = sh_error_set_level (tmp, &ShUtmpLogout);
[143]670 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]671 SL_RETURN(retval, _("sh_utmp_set_logout_good"));
672}
673
[68]674int sh_utmp_set_login_timer (const char * c)
[1]675{
676 long val;
677
678 SL_ENTER(_("sh_utmp_set_login_timer"));
679 val = strtol (c, (char **)NULL, 10);
680 if (val <= 0)
681 {
[143]682 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]683 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
684 _("utmp timer"), c);
[143]685 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[170]686 SL_RETURN((-1), _("sh_utmp_set_login_timer"));
[1]687 }
688
689 ShUtmpInterval = (time_t) val;
690 SL_RETURN(0, _("sh_utmp_set_login_timer"));
691}
692
[68]693int sh_utmp_set_login_activate (const char * c)
[1]694{
695 int i;
696 SL_ENTER(_("sh_utmp_set_login_activate"));
697 i = sh_util_flagval(c, &ShUtmpActive);
698 SL_RETURN(i, _("sh_utmp_set_login_activate"));
699}
700
701#ifdef HAVE_UTTYPE
702struct login_ct {
703 char name[UT_NAMESIZE+1];
704 int nlogin;
705 struct login_ct * next;
706};
707
708static struct login_ct * login_ct_list = NULL;
709
[170]710static int sh_utmp_login_clean(void)
[1]711{
712 struct login_ct * list = login_ct_list;
713 struct login_ct * old;
714
715 login_ct_list = NULL;
716
717 while (list)
718 {
719 old = list;
720 list = list->next;
721 SH_FREE(old);
722 }
723 return 0;
724}
725
726/* add a username to the list of logged-in users
727 */
728static int sh_utmp_login_a(char * str)
729{
730 struct login_ct * list = login_ct_list;
731
732 while (list)
733 {
734 if (0 == sl_strcmp(list->name, str))
735 {
736 ++(list->nlogin);
737 return list->nlogin;
738 }
739 list = list->next;
740 }
741 list = SH_ALLOC(sizeof(struct login_ct));
742 (void) sl_strlcpy(list->name, str, UT_NAMESIZE+1);
743 list->nlogin = 1;
744 list->next = login_ct_list;
745 login_ct_list = list;
746 return 1;
747}
748
749static int sh_utmp_login_r(char * str)
750{
751 struct login_ct * list = login_ct_list;
752 struct login_ct * old = login_ct_list;
753
754 while (list)
755 {
756 if (0 == sl_strcmp(list->name, str))
757 {
758 list->nlogin -= 1;
759 if (list->nlogin > 0)
760 {
761 return list->nlogin;
762 }
763 if (login_ct_list == list) /* modified Apr 4, 2004 */
764 {
765 login_ct_list = list->next;
766 SH_FREE(list);
767 }
768 else
769 {
770 old->next = list->next;
771 SH_FREE(list);
772 }
773 return 0;
774 }
775 old = list;
776 list = list->next;
777 }
778 return 0;
779}
780
781#endif
782
783
784/* for each login:
785 * - allocate a log record
786 * - link device.ut_record -> log_record
787 * - link user.ut_record -> log_record
788 */
[259]789
[1]790#ifdef HAVE_UTTYPE
791static int sh_utmp_is_virtual (char * in_utline, char * in_uthost)
792{
[259]793
794 if (in_uthost != NULL &&
795 in_utline != NULL &&
[1]796 in_uthost[0] == ':' &&
797 in_uthost[1] == '0' &&
[259]798 0 == sl_strncmp(in_utline, _("pts/"), 4))
[1]799 {
[259]800 return 1;
[1]801 }
[259]802
803 return 0;
[1]804}
805#endif
806
[154]807/* These variables are not used anywhere. They only exist
808 * to assign &userold, &user to them, which keeps gcc from
809 * putting them into a register, and avoids the 'clobbered
810 * by longjmp' warning. And no, 'volatile' proved insufficient.
811 */
[170]812static void * sh_dummy_userold = NULL;
813static void * sh_dummy_user = NULL;
[154]814
815
[1]816static void sh_utmp_addlogin (struct SH_UTMP_S * ut)
817{
[154]818 struct log_user * user = userlist;
819 struct log_user * userold = userlist;
[1]820#ifdef HAVE_UTTYPE
821 struct log_user * username = userlist;
822#endif
823
824 char ttt[TIM_MAX];
825#ifdef HAVE_UTTYPE
[153]826 volatile int status;
[1]827#endif
828
829 SL_ENTER(_("sh_utmp_addlogin"));
830
[154]831 /* Take the address to keep gcc from putting them into registers.
832 * Avoids the 'clobbered by longjmp' warning.
833 */
834 sh_dummy_userold = (void*) &userold;
835 sh_dummy_user = (void*) &user;
836
[1]837 if (ut->ut_line[0] == '\0')
838 SL_RET0(_("sh_utmp_addlogin"));
839
840 /* for some stupid reason, AIX repeats the wtmp entry for logouts
841 * with ssh
842 */
843 if (memcmp (&save_utmp, ut, sizeof(struct SH_UTMP_S)) == 0)
844 {
845 memset(&save_utmp, (int) '\0', sizeof(struct SH_UTMP_S));
846 SL_RET0(_("sh_utmp_addlogin"));
847 }
848 memcpy (&save_utmp, ut, sizeof(struct SH_UTMP_S));
849
850
851 /* ------- find user --------
852 */
853 while (user != NULL)
854 {
[154]855 if (0 == sl_strncmp((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE) )
[1]856 break;
857 userold = user;
858 user = user->next;
859 }
860
861#ifdef HAVE_UTTYPE
862 while (username != NULL)
863 {
864 if (0 == sl_strncmp(username->name, ut->ut_name, UT_NAMESIZE) )
865 break;
866 username = username->next;
867 }
868#endif
869
870#ifdef HAVE_UTTYPE
871 /* ---------- LOGIN -------------- */
872 if (ut->ut_type == USER_PROCESS)
873 {
874 if (user == NULL)
875 {
876 user = SH_ALLOC(sizeof(struct log_user));
877 user->next = userlist;
[154]878 userlist = (struct log_user *) user;
[1]879 }
[154]880 (void) sl_strlcpy((char*)(user->ut_tty), ut->ut_line, UT_LINESIZE+1);
881 (void) sl_strlcpy((char*)(user->name), ut->ut_name, UT_NAMESIZE+1);
[1]882#ifdef HAVE_UTHOST
[154]883 (void) sl_strlcpy((char*)(user->ut_host), ut->ut_host, UT_HOSTSIZE+1);
[1]884#else
885 user->ut_host[0] = '\0';
886#endif
887#ifdef HAVE_UTADDR
888 /*@-type@*//* ut_addr does exist !!! */
[154]889 (void) sl_strlcpy((char*)(user->ut_ship),
[1]890 my_inet_ntoa(*(struct in_addr*)&(ut->ut_addr)), 16);
891 /*@+type@*/
892#endif
893 user->time = ut->ut_time;
894
895 if (username == NULL /* not yet logged in */
896 || 0 == sl_strncmp(ut->ut_line, _("ttyp"), 4) /* in virt. console */
897 || 0 == sl_strncmp(ut->ut_line, _("ttyq"), 4) /* in virt. console */
898 ) {
[154]899 status = sh_utmp_login_a((char*)user->name);
[143]900 SH_MUTEX_LOCK(mutex_thread_nolog);
[132]901 (void) sh_unix_time (user->time, ttt, TIM_MAX);
[1]902 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
903#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
904 MSG_UT_LG1X,
905#elif defined(HAVE_UTHOST)
906 MSG_UT_LG1A,
907#else
908 MSG_UT_LG1B,
909#endif
910 user->name,
911 user->ut_tty,
912#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
913 user->ut_host,
914 user->ut_ship,
915#elif defined(HAVE_UTHOST)
916 user->ut_host,
917#endif
918 ttt,
919 status
920 );
[143]921 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]922 } else
[259]923 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host))
[1]924 {
[154]925 status = sh_utmp_login_a((char*)user->name);
[143]926 SH_MUTEX_LOCK(mutex_thread_nolog);
[132]927 (void) sh_unix_time (user->time, ttt, TIM_MAX);
[1]928 sh_error_handle( ShUtmpLoginMulti, FIL__, __LINE__, 0,
929#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
930 MSG_UT_LG2X,
931#elif defined(HAVE_UTHOST)
932 MSG_UT_LG2A,
933#else
934 MSG_UT_LG2B,
935#endif
936 user->name,
937 user->ut_tty,
938#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
939 user->ut_host,
940 user->ut_ship,
941#elif defined(HAVE_UTHOST)
942 user->ut_host,
943#endif
944 ttt,
945 status
946 );
[143]947 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]948 }
949
950 sh_utmp_login_morechecks(ut);
951 SL_RET0(_("sh_utmp_addlogin"));
952 }
953
954
955 /* --------- LOGOUT ---------------- */
956 else if (ut->ut_name[0] == '\0'
957 || ut->ut_type == DEAD_PROCESS /* solaris does not clear ut_name */
958 )
959 {
960 if (user != NULL)
961 {
[259]962#if defined(__linux__)
963 if (0 == sh_utmp_is_virtual(ut->ut_line, (char*)user->ut_host)) {
964#endif
965 status = sh_utmp_login_r((char*)user->name);
966 SH_MUTEX_LOCK(mutex_thread_nolog);
967 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
968 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
[1]969#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
[259]970 MSG_UT_LG3X,
[1]971#elif defined(HAVE_UTHOST)
[259]972 MSG_UT_LG3A,
[1]973#else
[259]974 MSG_UT_LG3B,
[1]975#endif
[259]976 user->name,
977 user->ut_tty,
[1]978#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
[259]979 user->ut_host,
980 user->ut_ship,
[1]981#elif defined(HAVE_UTHOST)
[259]982 user->ut_host,
[1]983#endif
[259]984 ttt,
985 status
986 );
987 SH_MUTEX_UNLOCK(mutex_thread_nolog);
988 userold->next = user->next;
989 if (user == userlist)
990 userlist = user->next;
991 sh_utmp_logout_morechecks((struct log_user *)user);
992 SH_FREE((struct log_user *)user);
993 user = NULL;
994#if defined(__linux__)
995 }
996#endif
[1]997 }
998 else
999 {
1000 (void) sl_strlcpy(terminated_line, ut->ut_line, UT_HOSTSIZE);
[143]1001 SH_MUTEX_LOCK(mutex_thread_nolog);
[132]1002 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
[1]1003 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1004 MSG_UT_LG3C,
1005 terminated_line,
1006 ttt, 0
1007 );
[143]1008 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]1009 }
1010 SL_RET0(_("sh_utmp_addlogin"));
1011 }
1012
1013 /* default */
1014 SL_RET0(_("sh_utmp_addlogin"));
1015
1016 /* #ifdef HAVE_UTTYPE */
1017#else
1018
1019 if (user == NULL) /* probably a login */
1020 {
1021 user = SH_ALLOC(sizeof(struct log_user));
1022 sl_strlcpy(user->ut_tty, ut->ut_line, UT_LINESIZE+1);
1023 sl_strlcpy(user->name, ut->ut_name, UT_NAMESIZE+1);
1024#ifdef HAVE_UTHOST
1025 sl_strlcpy(user->ut_host, ut->ut_host, UT_HOSTSIZE+1);
1026#endif
1027#ifdef HAVE_UTADDR
1028 sl_strlcpy(user->ut_ship,my_inet_ntoa((struct in_addr)ut->ut_addr),16);
1029#endif
1030 user->time = ut->ut_time;
1031 user->next = userlist;
1032 userlist = user;
1033
[143]1034 SH_MUTEX_LOCK(mutex_thread_nolog);
[132]1035 (void) sh_unix_time (user->time, ttt, TIM_MAX);
[1]1036 sh_error_handle( ShUtmpLoginSolo, FIL__, __LINE__, 0,
1037#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1038 MSG_UT_LG1X,
1039#elif defined(HAVE_UTHOST)
1040 MSG_UT_LG1A,
1041#else
1042 MSG_UT_LG1B,
1043#endif
1044 user->name,
1045 user->ut_tty,
1046#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1047 user->ut_host,
1048 user->ut_ship,
1049#elif defined(HAVE_UTHOST)
1050 user->ut_host,
1051#endif
1052 ttt,
1053 1
1054 );
[143]1055 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]1056 sh_utmp_login_morechecks(ut);
1057 }
1058 else /* probably a logout */
1059 {
[143]1060 SH_MUTEX_LOCK(mutex_thread_nolog);
[132]1061 (void) sh_unix_time (ut->ut_time, ttt, TIM_MAX);
[1]1062 sh_error_handle( ShUtmpLogout, FIL__, __LINE__, 0,
1063#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1064 MSG_UT_LG2X,
1065#elif defined(HAVE_UTHOST)
1066 MSG_UT_LG2A,
1067#else
1068 MSG_UT_LG2B,
1069#endif
1070 user->name,
1071 user->ut_tty,
1072#if defined(HAVE_UTHOST) && defined(HAVE_UTADDR)
1073 user->ut_host,
1074 user->ut_ship,
1075#elif defined(HAVE_UTHOST)
1076 user->ut_host,
1077#endif
1078 ttt,
1079 1
1080 );
[143]1081 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]1082 sh_utmp_logout_morechecks(user);
1083 userold->next = user->next;
1084 if (user == userlist) /* inserted Apr 4, 2004 */
1085 userlist = user->next;
1086 SH_FREE(user);
1087 user = NULL;
1088 }
1089
1090 SL_RET0(_("sh_utmp_addlogin"));
1091#endif
1092}
1093
1094static time_t lastmod = 0;
1095static off_t lastsize = 0;
1096static unsigned long lastread = 0;
1097
1098static void sh_utmp_check_internal (int mode)
1099{
1100 struct stat buf;
1101 int error;
1102 struct SH_UTMP_S * ut;
[170]1103 unsigned long this_read;
[143]1104 int val_retry;
[1]1105
1106 SL_ENTER(_("sh_utmp_check_internal"));
1107
1108 /* error if no access
1109 */
[143]1110 do {
1111 val_retry = /*@-unrecog@*/lstat ( mode_path[mode], &buf)/*@+unrecog@*/;
1112 } while (val_retry < 0 && errno == EINTR);
1113
1114 if (0 != val_retry)
[1]1115 {
1116 error = errno;
[143]1117 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]1118 sh_error_handle((-1), FIL__, __LINE__, error, MSG_E_ACCESS,
1119 (long) sh.real.uid, mode_path[mode]);
[143]1120 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]1121 SL_RET0(_("sh_utmp_check_internal"));
1122 }
1123
1124 /* modification time
1125 */
1126 if (mode < 2)
1127 {
1128 if (/*@-usedef@*/buf.st_mtime <= lastmod/*@+usedef@*/)
1129 {
1130 SL_RET0(_("sh_utmp_check_internal"));
1131 }
1132 else
1133 lastmod = buf.st_mtime;
1134 }
1135
1136 /* file size
1137 */
1138 if (/*@-usedef@*/buf.st_size < lastsize/*@+usedef@*/ && mode < 2)
1139 {
[143]1140 SH_MUTEX_LOCK(mutex_thread_nolog);
[1]1141 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_UT_ROT,
1142 mode_path[mode]);
[143]1143 SH_MUTEX_UNLOCK(mutex_thread_nolog);
[1]1144 lastread = 0;
1145#ifndef USE_SETUTENT
1146 sh_utmp_feed_forward = 0L;
1147#endif
1148 }
1149
1150 if (mode < 2)
1151 lastsize = buf.st_size;
1152
1153 if (buf.st_size == 0)
1154 SL_RET0(_("sh_utmp_check_internal"));
1155
1156 sh_utmp_utmpname(mode_path[mode]);
1157 sh_utmp_setutent();
1158
1159 /*
1160 * feed forward if initializing
1161 * we need to do this here
1162 */
[170]1163 this_read = 0;
1164
[1]1165 if (mode < 2)
1166 {
1167 while (this_read < lastread) {
1168 ut = sh_utmp_getutent();
1169 ++this_read;
1170 }
1171 }
1172
1173 /* start reading
1174 */
1175 this_read = 0;
1176 while (1 == 1) {
1177 ut = sh_utmp_getutent();
1178 if (ut == NULL)
1179 break;
1180 /* modified: ut_user --> ut_name */
1181 if (mode == 1 || (mode == 2 && ut->ut_name[0] != '\0'
1182#ifdef HAVE_UTTYPE
1183 && ut->ut_type != DEAD_PROCESS
1184#endif
1185 ))
1186 sh_utmp_addlogin (ut);
1187 ++this_read;
1188 }
1189
1190 sh_utmp_endutent();
1191
1192 if (mode < 2)
1193 {
1194 lastread += this_read;
1195#ifndef USE_SETUTENT
1196 sh_utmp_feed_forward += (long) (this_read * sizeof(struct SH_UTMP_S));
1197 lastread = 0;
1198#endif
1199 }
1200
1201 SL_RET0(_("sh_utmp_check_internal"));
1202}
1203
1204
1205static void sh_utmp_login_morechecks(struct SH_UTMP_S * ut)
1206{
1207 if (ut)
1208 return;
1209 return;
1210}
1211
1212static void sh_utmp_logout_morechecks(struct log_user * user)
1213{
1214 if (user)
1215 return;
1216 return;
1217}
1218
1219#endif
1220
1221
1222/* #ifdef SH_USE_UTMP */
1223#endif
1224
1225
1226
Note: See TracBrowser for help on using the repository browser.