summaryrefslogtreecommitdiff
path: root/other/utmpcloak/utcl.c
blob: c2be2324fe49c0fc694c0a36a763abb326a03f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*  HiHo,
   this utility changes the host of a given username (all his logins affected)
   usage is utcl <user> <host> where host is the desired host (only 20 chars)

   this piec of software is mostly ripped from ute.c which i found on my hdd
   and dont know where it came from.. many thanks to the author of this one..
   thanks goes also out to xdr who gave me the idea of writing this (he wrote 
   such thingie, but then lost it @#$! ;)

   have fun...
   -hendy (flames to hendy@winterland.net)

   greetings: (oh, this is lame, i know) to #!teso, #hax, #hack

   // hope it's not too lame

 */

#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
#include <utmp.h>
#include <pwd.h>
#include <lastlog.h>
// #define UTMP_FILE "/var/run/utmp" /* should have been defined in utmp.h */
#define MAX_ENT 100

int 
main (int argc, char **argv)
{
  int item;
  struct utmp Entry[MAX_ENT + 1];
  off_t position[MAX_ENT + 1];
  FILE *fptr;

  if (argc < 3)
    {
      printf ("usage: %s <user> <host>\n", argv[0]);
      exit (1);
    }

  if ((fptr = fopen (UTMP_FILE, "r+")) != NULL)
    {
      int last, num, i = 1;	/* get all utmp entries. */
      while (fread (&Entry[i], sizeof (Entry[i]), 1, fptr) > 0)
	{
	  if (strcmp (Entry[i].ut_line, "") != 0)	/* skip empty entries */
	    {
	      position[i] = ftell (fptr) - (long) (sizeof (Entry[i]));
	      i++;
	    }
	}
      last = i - 1;		/* keep a tab on how many entries there are. */
      position[i] = ftell (fptr);	/* keep track of EOF */

      for (item = 1; item <= last; item++)
	{

	  if (!(strcmp (Entry[item].ut_name, argv[1])))
	    {
	      strcpy (Entry[item].ut_host, argv[2]);	/* insert new host */
	      fseek (fptr, position[item], SEEK_SET);	/* seek position in utmp */
	      fwrite (&Entry[item], sizeof (Entry[num]), 1, fptr);	/* write to file */

	    }
	}


      fclose (fptr);
    }
  else
    {
      printf ("\nERROR: cannot open file %s \n", UTMP_FILE);
    }
  return (0);
}