Cover V02, I03
Article
Figure 1
Figure 2
Figure 3
Figure 4
Listing 1
Listing 2

may93.tar


Listing 1

1  |#include <sys/types.h>
2  |#include <time.h>
3  |#include <utmp.h>
4  |#include <stdio.h>
5  |
6  |#define FAILEDLOGIN_FILENAME "/etc/security/failedlogin"
7  |
8  |char usage[][25] = { "flogin\n",
9  |                     "flogin -h\n",
10  |                     "flogin -u <user_name>\n" };
11  |char FF = '\014';
12  |
13  |/* ********************************************************************* *
14  | *                                                                       *
15  | * Procedure : main                                                      *
16  | * Purpose   : Top loop for utmp print function                          *
17  | *                                                                       *
18  | * ********************************************************************* */
19  |
20  |int main(int argc,char *argv[])
21  |
22  |{
23  |
24  |  int nusers,
25  |      counter;
26  |
27  |  if (!strcmp(argv[1],"-h")) {
28  |   for (counter = 0;counter <= 3; counter++)
29  |    printf("%s",usage[counter]);
30  |  } /* end then */
31  |  else {
32  |   if (!strcmp(argv[1],"-u"))
33  |    nusers = print_users(argv[2]);
34  |   else
35  |    nusers = print_users("NOBODY");
36  |  } /* end else */
37  |  exit(0);
38  |
39  |} /* end main */
40  |
41  |/* ********************************************************************* *
42  | *                                                                       *
43  | * Procedure : print_users                                               *
44  | * Purpose   : The procedure print_users reads the failedlogin file and  *
45  | *             prints the failed login for the specified user or all     *
46  | *             failed logins in chronological order.                     *
47  | * Example   : The following examples runs the failedlogin report for    *
48  | *             each user with headings and totals.                       *
49  | *             Add pipe to printer for hard copy (ie. |lp -dljet         *
50  | *             lsuser -a ALL|cut -f1 -d" "|xargs -i% flogin -u %         *
51  | *                                                                       *
52  | * ********************************************************************* */
53  |
54  |int print_users(char *user_name)
55  |
56  |{
57  |
58  |  FILE *utmpf;
59  |  int nusers,
60  |      failed=0;
61  |  struct utmp utmp;
62  |
63  |
64  |  nusers=0;
65  |  if (!(utmpf = fopen(FAILEDLOGIN_FILENAME, "r"))) {
66  |    perror(FAILEDLOGIN_FILENAME);
67  |    exit(1);
68  |  } /* end then */
69  |  if (strcmp(user_name,"NOBODY"))
70  |   printf("LOGIN REPORT FOR : [ %s ]\n",user_name);
71  |  while (fread((char *)&utmp,sizeof(utmp),1,utmpf) == 1) {
72  |   if (*utmp.ut_name && utmp.ut_type == USER_PROCESS)
73  |    nusers++;
74  |   if (!strcmp(user_name,"NOBODY")) {
75  |    printf("USER [%10s]  TERMINAL [%12s]  TIME : %s",
76  |            utmp.ut_user,utmp.ut_line,asctime(localtime(&utmp.ut_time)));
77  |   } /* end then */
78  |   else
79  |    if (!strcmp(user_name,utmp.ut_user)) {
80  |     printf("USER [%10s]  TERMINAL [%12s]  TIME : %s",
81  |             utmp.ut_user,utmp.ut_line,asctime(localtime(&utmp.ut_time)));
82  |     failed++;
83  |    } /* end then */
84  |  } /* end while */
85  |  printf("NUMBER OF FAILED LOGINS : [ %i ]\n",failed);
86  |  printf("%c",FF);
87  |  return (nusers);
88  |
89  |} /* end print_user */