2 * xlat.c Translate strings.
6 * This is the first version of xlat incorporated to RADIUS
9 static const char rcsid[] =
14 #include <sys/types.h>
24 Convert the value on a VALUE_PAIR to string
26 static int valuepair2str(char * out,int outlen,VALUE_PAIR * pair,int type)
29 return vp_prints_value(out,outlen,pair,0);
33 strncpy(out,"_",outlen-1);
35 case PW_TYPE_INTEGER :
36 strncpy(out,"0",outlen-1);
39 strncpy(out,"?.?.?.?",outlen-1);
42 strncpy(out,"0",outlen-1);
45 strncpy(out,"unknown_type",outlen-1);
53 Returns a string with value of Attribute
55 static int valuebyname(char * out,int outlen,VALUE_PAIR * request, char * attrname)
59 da = dict_attrbyname(attrname);
61 return (valuepair2str(out,outlen,pairfind(request,da->attr),da->type));
70 * Based on radius_xlat from exec.c
71 * After testing will replace the radius_xlat
73 * Replace %<whatever> in a string.
75 * %a Protocol (SLIP/PPP)
78 * %f Framed IP address
79 * %i Calling Station ID
80 * %l request timestamp
81 * %m request month (MM)
84 * %s Speed (PW_CONNECT_INFO)
87 * %y request year (YY)
90 * %D request date (YYYYMMDD)
91 * %I request in ctime format
94 * %T request timestamp in database format
95 * %U Stripped User name
96 * %V Request-Authenticator (Verified/None)
97 * %Y request year (YYYY)
98 * %Z All request attributes except password (must have big buffer)
99 * ${AttributeName} Corresponding value for AttributeName in request
100 * ${request:AttributeName} Corresponding value for AttributeName in request
101 * ${reply:AttributeName} Corresponding value for AttributeName in reply
104 int radius_xlat2(char * out,int outlen, char *str, REQUEST * request, VALUE_PAIR *reply)
113 char tmpdt[40]; /* For temporary storing of dates */
116 for (p = str; *p ; p++) {
117 /* Calculate freespace in output */
118 freespace = outlen - ((int)q-(int)out);
122 if ((c != '%') && (c != '$') && (c != '\\')) {
126 if (*++p == 0) break;
127 if (c == '\\') switch(*p) {
141 } else if (c == '$') switch(*p) {
142 case '{': /* Attribute by Name */
145 while (*p && (*p != '}')) {
149 if (strncasecmp(attrname,"reply:",6) == 0) {
150 q += valuebyname(q,freespace,reply,&attrname[6]);
151 } else if (strncasecmp(attrname,"request:",8) == 0) {
152 q += valuebyname(q,freespace,request->packet->vps,&attrname[8]);
154 q += valuebyname(q,freespace,request->packet->vps,attrname);
161 } else if (c == '%') switch(*p) {
165 case 'a': /* Protocol: */
166 q += valuepair2str(q,freespace,pairfind(reply,PW_FRAMED_PROTOCOL),PW_TYPE_INTEGER);
168 case 'c': /* Callback-Number */
169 q += valuepair2str(q,freespace,pairfind(reply,PW_CALLBACK_NUMBER),PW_TYPE_STRING);
171 case 'd': /* request year */
172 TM = localtime(&request->timestamp);
173 strftime(tmpdt,sizeof(tmpdt),"%d",TM);
174 strncpy(q,tmpdt,freespace);
175 i = strlen(q); q[i] = '\0'; q += i;
177 case 'f': /* Framed IP address */
178 q += valuepair2str(q,freespace,pairfind(reply,PW_FRAMED_IP_ADDRESS),PW_TYPE_IPADDR);
180 case 'i': /* Calling station ID */
181 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CALLING_STATION_ID),PW_TYPE_STRING);
183 case 'l': /* request timestamp */
184 sprintf(tmpdt,"%ld",request->timestamp);
185 strncpy(q,tmpdt,freespace);
186 i = strlen(q); q[i] = '\0'; q += i;
188 case 'm': /* request month */
189 TM = localtime(&request->timestamp);
190 strftime(tmpdt,sizeof(tmpdt),"%m",TM);
191 strncpy(q,tmpdt,freespace);
192 i = strlen(q); q[i] = '\0'; q += i;
194 case 'n': /* NAS IP address */
195 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_IP_ADDRESS),PW_TYPE_IPADDR);
197 case 'p': /* Port number */
198 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_NAS_PORT_ID),PW_TYPE_INTEGER);
200 case 's': /* Speed */
201 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_CONNECT_INFO),PW_TYPE_STRING);
204 q += valuepair2str(q,freespace,pairfind(reply,PW_FRAMED_MTU),PW_TYPE_INTEGER);
206 case 'u': /* User name */
207 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_USER_NAME),PW_TYPE_STRING);
209 case 'y': /* request year */
210 TM = localtime(&request->timestamp);
211 strftime(tmpdt,sizeof(tmpdt),"%y",TM);
212 strncpy(q,tmpdt,freespace);
213 i = strlen(q); q[i] = '\0'; q += i;
215 case 'A': /* radacct_dir */
216 strncpy(q,radacct_dir,freespace-1);
217 i = strlen(q); q[i] = '\0'; q += i;
219 case 'C': /* ClientName */
220 strncpy(q,client_name(request->packet->src_ipaddr),freespace-1);
221 i = strlen(q); q[i] = '\0'; q += i;
223 case 'D': /* request date */
224 TM = localtime(&request->timestamp);
225 strftime(tmpdt,sizeof(tmpdt),"%Y%m%d",TM);
226 strncpy(q,tmpdt,freespace);
227 i = strlen(q); q[i] = '\0'; q += i;
229 case 'I': /* request timestamp */
230 strncpy(q,ctime(&request->timestamp),freespace);
231 i = strlen(q); q[i] = '\0'; q += i;
233 case 'L': /* radlog_dir */
234 strncpy(q,radlog_dir,freespace-1);
235 i = strlen(q); q[i] = '\0'; q += i;
237 case 'R': /* radius_dir */
238 strncpy(q,radius_dir,freespace-1);
239 i = strlen(q); q[i] = '\0'; q += i;
241 case 'T': /* request timestamp */
242 TM = localtime(&request->timestamp);
243 strftime(tmpdt,sizeof(tmpdt),"%Y-%m-%d-%H.%M.%S.000000",TM);
244 strncpy(q,tmpdt,freespace);
245 i = strlen(q); q[i] = '\0'; q += i;
247 case 'U': /* Stripped User name */
248 q += valuepair2str(q,freespace,pairfind(request->packet->vps,PW_STRIPPED_USER_NAME),PW_TYPE_STRING);
250 case 'V': /* Request-Authenticator */
251 if (request->packet->verified)
252 strncpy(q,"Verified",freespace-1);
254 strncpy(q,"None",freespace-1);
255 i = strlen(q); q[i] = '\0'; q += i;
257 case 'Y': /* request year */
258 TM = localtime(&request->timestamp);
259 strftime(tmpdt,sizeof(tmpdt),"%Y",TM);
260 strncpy(q,tmpdt,freespace);
261 i = strlen(q); q[i] = '\0'; q += i;
263 case 'Z': /* Full request pairs except password */
264 tmp = request->packet->vps;
265 while (tmp && (freespace > 3)) {
266 if (tmp->attribute != PW_PASSWORD) {
268 i = vp_prints(q,freespace-2,tmp);
288 * print a string passing by the radius_xlat2
291 static void printf_xlat(char *str, REQUEST * request, VALUE_PAIR *reply)
295 radius_xlat2(p,4096,str,request,reply);