Initial revision
[freeradius.git] / src / main / radzap.c
1 /*
2  * radzap       Zap a user from the radutmp and radwtmp file.
3  *
4  * Version:     @(#)radzap  2.13  08-Aug-1999  miquels@cistron.nl
5  *
6  */
7
8 #include        "autoconf.h"
9
10 #include        <sys/types.h>
11 #include        <sys/time.h>
12 #include        <sys/file.h>
13
14 #include        <stdio.h>
15 #include        <stdlib.h>
16 #include        <fcntl.h>
17 #include        <time.h>
18 #include        <unistd.h>
19 #include        <errno.h>
20 #include        <stdarg.h>
21 #include        <netinet/in.h>
22
23 #if HAVE_MALLOC_H
24 #  include      <malloc.h>
25 #endif
26
27 #include        "radiusd.h"
28
29 int debug_flag = 0;
30 char *progname = "radzap";
31 char *radlog_dir = NULL;
32
33 /*
34  *      Zap a user from the radutmp and radwtmp file.
35  */
36 int main(int argc, char **argv)
37 {
38         NAS     *nas;
39         UINT4   ip = 0;
40         int     nas_port = -1;
41         char    *user = NULL;
42         char    *s;
43         time_t  t;
44         char    buf[256];
45
46         if (argc < 2 || argc > 4 || (argc > 1 && argv[1][0] == '-')) {
47                 fprintf(stderr, "Usage: radzap termserver [port] [user]\n");
48                 fprintf(stderr, "       radzap is only an admin tool to clean the radutmp file!\n");
49                 exit(1);
50         }
51         if (argc > 2) {
52                 s = argv[2];
53                 if (*s == 's' || *s == 'S') s++;
54                 nas_port = atoi(s);
55         }
56         if (argc > 3) user     = argv[3];
57
58         /*
59          *      Read the "naslist" file.
60          */
61         sprintf(buf, "%s/%s", RADIUS_DIR, RADIUS_NASLIST);
62         if (read_naslist_file(buf) < 0)
63                 exit(1);
64
65         /*
66          *      Find the IP address of the terminal server.
67          */
68         if ((nas = nas_findbyname(argv[1])) == NULL && argv[1][0] != 0) {
69                 if ((ip = ip_getaddr(argv[1])) == 0) {
70                         fprintf(stderr, "%s: host not found.\n", argv[1]);
71                         exit(1);
72                 }
73         }
74         if (nas) ip = nas->ipaddr;
75
76         printf("radzap: zapping termserver %s, port %d",
77                 ip_hostname(ip), nas_port);
78         if (user) printf(", user %s", user);
79         printf("\n");
80
81         t = time(NULL);
82         radutmp_zap(ip, nas_port, user, t);
83
84         return 0;
85 }