1/*
2 * $Id: radacct.c,v 1.1.1.1 2008/10/15 03:30:40 james26_jang Exp $
3 *
4 * Copyright (C) 1995,1996 Lars Fenneberg
5 *
6 * See the file COPYRIGHT for the respective terms and conditions.
7 * If the file is missing contact me at lf@elemental.net
8 * and I'll send you a copy.
9 *
10 */
11
12static char	rcsid[] =
13		"$Id: radacct.c,v 1.1.1.1 2008/10/15 03:30:40 james26_jang Exp $";
14
15#include <config.h>
16#include <includes.h>
17#include <radiusclient.h>
18#include <messages.h>
19#include <pathnames.h>
20
21static char *pname;
22
23void usage(void)
24{
25	fprintf(stderr,"Usage: %s [-Vh] [-f <config_file>] [-i <client_port>]\n\n", pname);
26	fprintf(stderr,"  -V            output version information\n");
27	fprintf(stderr,"  -h            output this text\n");
28	fprintf(stderr,"  -f		filename of alternate config file\n");
29	fprintf(stderr,"  -i            ttyname to send to the server\n");
30	exit(ERROR_RC);
31}
32
33void version(void)
34{
35	fprintf(stderr,"%s: %s\n", pname ,rcsid);
36	exit(ERROR_RC);
37}
38
39int
40main (int argc, char **argv)
41{
42	int			result = ERROR_RC;
43	VALUE_PAIR	*send = NULL;
44   	UINT4		client_port;
45   	int			c;
46	VALUE_PAIR	*vp;
47	DICT_VALUE  *dval;
48	char *username, *service, *fproto, *type;
49	char *path_radiusclient_conf = RC_CONFIG_FILE;
50	char *ttyn = NULL;
51
52	extern char *optarg;
53
54	pname = (pname = strrchr(argv[0],'/'))?pname+1:argv[0];
55
56	rc_openlog(pname);
57
58	while ((c = getopt(argc,argv,"f:i:hV")) > 0)
59	{
60		switch(c)
61		{
62			case 'f':
63				path_radiusclient_conf = optarg;
64				break;
65			case 'i':
66				ttyn = optarg;
67				break;
68			case 'V':
69				version();
70				break;
71			case 'h':
72				usage();
73				break;
74			default:
75				exit(ERROR_RC);
76				break;
77		}
78	}
79
80	if (rc_read_config(path_radiusclient_conf) != 0)
81		exit(ERROR_RC);
82
83	if (rc_read_dictionary(rc_conf_str("dictionary")) != 0)
84		exit (ERROR_RC);
85
86	if (rc_read_mapfile(rc_conf_str("mapfile")) != 0)
87		exit (ERROR_RC);
88
89	if (ttyn != NULL)
90	{
91		client_port = rc_map2id(ttyn);
92	}
93	else
94	{
95		/* we take stdout here, because stdin is usually connected
96	 	 *  to our input file
97	 	 */
98	 	if ((ttyn = ttyname(1)) != NULL)
99	 	{
100			client_port = rc_map2id(ttyn);
101		}
102		else
103		{
104			client_port = 0;
105		}
106	}
107
108	if ((send = rc_avpair_readin(stdin))) {
109
110		username = service = type = "(unknown)";
111		fproto = NULL;
112
113		if ((vp = rc_avpair_get(send, PW_ACCT_STATUS_TYPE)) != NULL)
114				if ((dval = rc_dict_getval(vp->lvalue, vp->name)) != NULL) {
115					type = dval->name;
116				}
117
118		if ((vp = rc_avpair_get(send, PW_USER_NAME)) != NULL)
119				username = vp->strvalue;
120
121		if ((vp = rc_avpair_get(send, PW_SERVICE_TYPE)) != NULL)
122				if ((dval = rc_dict_getval(vp->lvalue, vp->name)) != NULL) {
123					service = dval->name;
124				}
125
126		if (vp && (vp->lvalue == PW_FRAMED) &&
127			((vp = rc_avpair_get(send, PW_FRAMED_PROTOCOL)) != NULL))
128				if ((dval = rc_dict_getval(vp->lvalue, vp->name)) != NULL) {
129					fproto = dval->name;
130				}
131
132		result = rc_acct(client_port, send);
133		if (result == OK_RC)
134		{
135			fprintf(stderr, SC_ACCT_OK);
136			rc_log(LOG_NOTICE, "accounting OK, type %s, username %s, service %s%s%s",
137				   type, username, service,(fproto)?"/":"", (fproto)?fproto:"");
138		}
139		else
140		{
141			fprintf(stderr, SC_ACCT_FAILED, result);
142			rc_log(LOG_NOTICE, "accounting FAILED, type %s, username %s, service %s%s%s",
143				   type, username, service,(fproto)?"/":"", (fproto)?fproto:"");
144		}
145		rc_avpair_free(send);
146	}
147
148	exit (result);
149}
150