2 * radzap.c Zap a user from the radutmp and radwtmp file.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include "libradius.h"
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
47 const char *radlog_dir = NULL;
48 const char *radius_dir = NULL;
49 const char *radacct_dir = NULL;
50 const char *radlib_dir = NULL;
51 radlog_dest_t radlog_dest = RADLOG_FILES;
54 int log_stripped_names;
55 struct main_config_t mainconfig;
56 uint32_t radiusip = INADDR_NONE;
57 static void usage(void);
59 struct radutmp_config_t {
63 static CONF_PARSER module_config[] = {
64 { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn, RADUTMP },
65 { NULL, -1, 0, NULL, NULL }
68 #define LOCK_LEN sizeof(struct radutmp)
70 static int radutmp_lookup(struct radutmp *u, uint32_t nasaddr,
71 uint32_t port, const char *user)
75 if ((fd = open(radutmpconfig.radutmp_fn, O_RDONLY|O_CREAT, 0644)) >= 0) {
79 rad_lockfd(fd, LOCK_LEN);
82 * Find the entry for this NAS / portno combination.
84 while (read(fd, u, sizeof(*u)) == sizeof(*u)) {
85 if ((nasaddr != 0 && nasaddr != u->nas_address) ||
86 (port != u->nas_port) ||
88 strncmp(u->login, user, sizeof u->login) != 0) ||
101 static int do_accton_packet(uint32_t nasaddr);
102 static int do_stop_packet(const struct radutmp *u);
105 * Display the syntax for starting this program.
107 static void usage(void)
110 "Usage: %s [-d raddb] [-p acct_port] [-r servername|serverip] termserver [port] [user]\n", progname);
111 fprintf(stderr, "Options:\n\n");
112 fprintf(stderr, " -d raddb Set the raddb directory (default is %s)\n", RADIUS_DIR);
113 fprintf(stderr, " -p acct_port Accounting port on radius server\n");
114 fprintf(stderr, " -r radserver Radius server name or IP address\n");
115 fprintf(stderr, " termserver Terminal Server (NAS) name or IP address to match, can be '' for any\n");
116 fprintf(stderr, " [port] Terminal Server port to match\n");
117 fprintf(stderr, " [user] Login account to match\n");
123 * Zap a user from the radutmp and radwtmp file.
125 int main(int argc, char **argv)
130 uint32_t nas_port = ~0;
139 radius_dir = strdup(RADIUS_DIR);
141 /* Process the options. */
142 while ((argval = getopt(argc, argv, "d:p:r:")) != EOF) {
147 if (radius_dir) free(radius_dir);
148 radius_dir = strdup(optarg);
151 acct_port = atoi(optarg);
155 if ((radiusip = ip_getaddr(optarg)) == INADDR_NONE) {
156 fprintf(stderr, "%s: %s: radius server unknown\n",
169 if (argc == optind) { /* no terminal server specified */
174 if (argc > optind + 1) { /* NAS port given */
176 if (*s == 's' || *s == 'S') s++;
177 nas_port = strtoul(s, NULL, 10);
180 if (argc > optind + 2) { /* username (login) given */
181 user = argv[optind+2];
185 * Find the IP address of the terminal server.
187 if ((nas = nas_findbyname(argv[optind])) == NULL && argv[optind][0] != 0) {
188 if ((ip = ip_getaddr(argv[optind])) == INADDR_NONE) {
189 fprintf(stderr, "%s: host not found.\n", argv[optind]);
197 * Ensure that the configuration is initialized.
199 memset(&mainconfig, 0, sizeof(mainconfig));
201 /* Read radiusd.conf */
202 if (read_mainconfig(0) < 0) {
203 fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
207 /* Read the radutmp section of radiusd.conf */
208 cs = cf_section_sub_find(cf_section_find("modules"), "radutmp");
210 fprintf(stderr, "%s: No configuration information in radutmp section of radiusd.conf!\n",
215 cf_section_parse(cs, NULL, module_config);
217 printf("%s: zapping termserver %s, port %u",
218 progname, ip_hostname(buf, sizeof(buf), ip), nas_port);
220 printf(", user %s", user);
223 if (nas_port == ~0) {
224 return do_accton_packet(ip);
227 if (!radutmp_lookup(&u, ip, nas_port, user)) {
228 fprintf(stderr, "Entry not found\n");
232 return do_stop_packet(&u);
235 static int getport(const char *name)
239 svp = getservbyname(name, "udp");
244 return ntohs(svp->s_port);
247 static const char *getsecret(uint32_t server)
251 cl = client_find(server);
254 radlog(L_ERR|L_CONS, "No clients entry for %s",
255 ip_ntoa(buf,server));
258 return (const char *)cl->secret;
261 /* Packet-fabrication macros. Don't stare directly at them without protective
263 #define PAIR(n,v,t,e) do { \
264 if(!(vp=paircreate(n, t))) { \
265 radlog(L_ERR|L_CONS, "no memory"); \
266 pairfree(&req->vps); \
270 pairadd(&req->vps, vp); \
272 #define INTPAIR(n,v) PAIR(n,v,PW_TYPE_INTEGER,lvalue)
273 #define IPPAIR(n,v) PAIR(n,v,PW_TYPE_IPADDR,lvalue)
274 #define STRINGPAIR(n,v) do { \
275 if(!(vp=paircreate(n, PW_TYPE_STRING))) { \
276 radlog(L_ERR|L_CONS, "no memory"); \
277 pairfree(&req->vps); \
280 strNcpy((char *)vp->strvalue, v, sizeof vp->strvalue); \
281 vp->length=strlen(v); \
282 pairadd(&req->vps, vp); \
285 static int do_packet(int allports, uint32_t nasaddr, const struct radutmp *u)
287 int i, retries=5, timeout=3;
289 RADIUS_PACKET *req, *rep = NULL;
293 if ((req = rad_alloc(1)) == NULL) {
294 librad_perror("radzap");
297 req->id = getpid() & 0xFF;
298 req->code = PW_ACCOUNTING_REQUEST;
299 req->dst_port = acct_port;
300 if(req->dst_port == 0)
301 req->dst_port = getport("radacct");
302 if(req->dst_port == 0)
303 req->dst_port = PW_ACCT_UDP_PORT;
304 if (radiusip == INADDR_NONE) {
305 req->dst_ipaddr = ip_getaddr("localhost");
308 req->dst_ipaddr = radiusip;
311 req->dst_ipaddr = 0x7f000001;
313 secret = getsecret(req->dst_ipaddr);
316 INTPAIR(PW_ACCT_STATUS_TYPE, PW_STATUS_ACCOUNTING_OFF);
317 IPPAIR(PW_NAS_IP_ADDRESS, nasaddr);
318 INTPAIR(PW_ACCT_DELAY_TIME, 0);
320 char login[sizeof u->login+1];
321 char session_id[sizeof u->session_id+1];
322 strNcpy(login, u->login, sizeof login);
323 strNcpy(session_id, u->session_id, sizeof session_id);
324 INTPAIR(PW_ACCT_STATUS_TYPE, PW_STATUS_STOP);
325 IPPAIR(PW_NAS_IP_ADDRESS, u->nas_address);
326 INTPAIR(PW_ACCT_DELAY_TIME, 0);
327 STRINGPAIR(PW_USER_NAME, login);
328 INTPAIR(PW_NAS_PORT, u->nas_port);
329 STRINGPAIR(PW_ACCT_SESSION_ID, session_id);
331 INTPAIR(PW_SERVICE_TYPE, PW_FRAMED_USER);
332 INTPAIR(PW_FRAMED_PROTOCOL, PW_PPP);
333 } else if(u->proto=='S') {
334 INTPAIR(PW_SERVICE_TYPE, PW_FRAMED_USER);
335 INTPAIR(PW_FRAMED_PROTOCOL, PW_SLIP);
337 INTPAIR(PW_SERVICE_TYPE, PW_LOGIN_USER); /* A guess, really */
339 IPPAIR(PW_FRAMED_IP_ADDRESS, u->framed_address);
340 INTPAIR(PW_ACCT_SESSION_TIME, 0);
341 INTPAIR(PW_ACCT_INPUT_OCTETS, 0);
342 INTPAIR(PW_ACCT_OUTPUT_OCTETS, 0);
343 INTPAIR(PW_ACCT_INPUT_PACKETS, 0);
344 INTPAIR(PW_ACCT_OUTPUT_PACKETS, 0);
346 if ((req->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
347 perror("radzap: socket: ");
351 for (i = 0; i < retries; i++) {
354 rad_send(req, NULL, secret);
356 /* And wait for reply, timing out as necessary */
358 FD_SET(req->sockfd, &rdfdesc);
360 tv.tv_sec = (int)timeout;
361 tv.tv_usec = 1000000 * (timeout - (int)timeout);
363 /* Something's wrong if we don't get exactly one fd. */
364 if (select(req->sockfd + 1, &rdfdesc, NULL, NULL, &tv) != 1) {
368 rep = rad_recv(req->sockfd);
371 } else { /* NULL: couldn't receive the packet */
372 librad_perror("radzap:");
377 /* No response or no data read (?) */
379 fprintf(stderr, "%s: no response from server\n", progname);
383 if (rad_decode(rep, req, secret) != 0) {
384 librad_perror("rad_decode");
388 vp_printlist(stdout, rep->vps);
392 static int do_accton_packet(uint32_t nasaddr)
394 return do_packet(1, nasaddr, 0);
397 static int do_stop_packet(const struct radutmp *u)
399 return do_packet(0, 0, u);