source: trunk/src/sh_utmp.c@ 143

Last change on this file since 143 was 143, checked in by rainer, 18 years ago

Bugfixes and threaded process check.

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