s/T_INVALID/T_OP_INVALID/
[freeradius.git] / src / lib / print.c
1 /*
2  * print.c      Routines to print stuff.
3  *
4  * Version:     $Id$
5  *
6  *   This library is free software; you can redistribute it and/or
7  *   modify it under the terms of the GNU Lesser General Public
8  *   License as published by the Free Software Foundation; either
9  *   version 2.1 of the License, or (at your option) any later version.
10  *
11  *   This library 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 GNU
14  *   Lesser General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Lesser General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19  *
20  * Copyright 2000  The FreeRADIUS server project
21  */
22
23 static const char rcsid[] = "$Id$";
24
25 #include        "autoconf.h"
26
27 #include        <stdio.h>
28 #include        <stdlib.h>
29 #include        <sys/types.h>
30 #include        <ctype.h>
31 #include        <string.h>
32
33 #include        "libradius.h"
34
35 /*
36  *      Convert a string to something printable.
37  *      The output string has to be _at least_ 4x the size
38  *      of the input string!
39  */
40 void librad_safeprint(char *in, int inlen, char *out, int outlen)
41 {
42         unsigned char   *str = (unsigned char *)in;
43         int             done = 0;
44         int             sp = 0;
45
46         if (inlen < 0) inlen = strlen(in);
47
48         while (inlen-- > 0 && (done + 3) < outlen) {
49                 /*
50                  *      Hack: never print trailing zero.
51                  *      Some clients send strings with an off-by-one
52                  *      length (confused with strings in C).
53                  */
54                 if (inlen == 0 && *str == 0)
55                         break;
56
57                 sp = 0;
58
59                 switch (*str) {
60                         case '\\':
61                                 sp = '\\';
62                                 break;
63                         case '\r':
64                                 sp = 'r';
65                                 break;
66                         case '\n':
67                                 sp = 'n';
68                                 break;
69                         case '\t':
70                                 sp = 't';
71                                 break;
72                         default:
73                                 if (*str < 32 || (*str >= 128)){
74                                         snprintf(out, outlen, "\\%03o", *str);
75                                         done += 4;
76                                         out  += 4;
77                                         outlen -= 4;
78                                 } else {
79                                         *out++ = *str;
80                                         outlen--;
81                                         done++;
82                                 }
83                 }
84                 if (sp) {
85                         *out++ = '\\';
86                         *out++ = sp;
87                         outlen -= 2;
88                         done += 2;
89                 }
90                 str++;
91         }
92         *out = 0;
93 }
94
95
96 /*
97  *  Print one value into a string.
98  *  delimitst will define if strings and dates will be delimited by '"'
99  */
100 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
101 {
102         DICT_VALUE  *v;
103         char        buf[1024];
104         char        *a;
105         time_t      t;
106         struct tm   s_tm;
107
108         out[0] = 0;
109         if (!vp) return 0;
110
111         switch (vp->type) {
112                 case PW_TYPE_STRING:
113                         if ((delimitst == 1) && vp->flags.has_tag) {
114                                 /* Tagged attribute: print delimter and ignore tag */
115                                 buf[0] = '"';
116                                 librad_safeprint((char *)(vp->strvalue),
117                                                  vp->length, buf + 1, sizeof(buf) - 2);
118                                 strcat(buf, "\"");
119                         } else if (delimitst == 1) {
120                                 /* Non-tagged attribute: print delimter */
121                                 buf[0] = '"';
122                                 librad_safeprint((char *)vp->strvalue,
123                                                  vp->length, buf + 1, sizeof(buf) - 2);
124                                 strcat(buf, "\"");
125
126                         } else if (delimitst < 0) {
127                                 strNcpy(out, vp->strvalue, outlen);
128                                 return strlen(out);
129
130                         } else {
131                                 /* Non-tagged attribute: no delimiter */
132                                 librad_safeprint((char *)vp->strvalue,
133                                                  vp->length, buf, sizeof(buf));
134                         }
135                         a = buf;
136                         break;
137                 case PW_TYPE_INTEGER:
138                         if ( vp->flags.has_tag ) {
139                                 /* Attribute value has a tag, need to ignore it */
140                                 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue & 0xffffff)))
141                                     != NULL)
142                                         a = v->name;
143                                 else {
144                                         snprintf(buf, sizeof(buf), "%u", (vp->lvalue & 0xffffff));
145                                         a = buf;
146                                 }
147                         } else {
148                                 /* Normal, non-tagged attribute */
149                                 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
150                                     != NULL)
151                                         a = v->name;
152                                 else {
153                                         snprintf(buf, sizeof(buf), "%u", vp->lvalue);
154                                         a = buf;
155                                 }
156                         }
157                         break;
158                 case PW_TYPE_DATE:
159                         t = vp->lvalue;
160                         if (delimitst) {
161                           strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
162                                    localtime_r(&t, &s_tm));
163                         } else {
164                           strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
165                                    localtime_r(&t, &s_tm));
166                         }
167                         a = buf;
168                         break;
169                 case PW_TYPE_IPADDR:
170                         if (vp->strvalue[0])
171                                 a = (char *)vp->strvalue;
172                         else
173                                 a = ip_hostname((char *)vp->strvalue,
174                                                 sizeof(vp->strvalue),
175                                                 vp->lvalue);
176                         break;
177                 case PW_TYPE_ABINARY:
178 #ifdef ASCEND_BINARY
179                   a = buf;
180                   print_abinary(vp, (unsigned char *)buf, sizeof(buf));
181                   break;
182 #else
183                   /* FALL THROUGH */
184 #endif
185                 case PW_TYPE_OCTETS:
186                   strcpy(buf, "0x");
187                   a = buf + 2;
188                   for (t = 0; t < vp->length; t++) {
189                         sprintf(a, "%02x", vp->strvalue[t]);
190                         a += 2;
191                   }
192                   a = buf;
193                   break;
194
195                 case PW_TYPE_IFID:
196                         a = ifid_ntoa(buf, sizeof(buf), vp->strvalue);
197                         break;
198
199                 case PW_TYPE_IPV6ADDR:
200                         a = ipv6_ntoa(buf, sizeof(buf), vp->strvalue);
201                         break;
202
203                 default:
204                         a = 0;
205                         break;
206         }
207         strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
208
209         return strlen(out);
210 }
211
212 /*
213  *  This is a hack, and has to be kept in sync with tokens.h
214  */
215 static const char *vp_tokens[] = {
216   "?",                          /* T_OP_INVALID */
217   "EOL",                        /* T_EOL */
218   "{",
219   "}",
220   "(",
221   ")",
222   ",",
223   ";",
224   "+=",
225   "-=",
226   ":=",
227   "=",
228   "!=",
229   ">=",
230   ">",
231   "<=",
232   "<",
233   "=~",
234   "!~",
235   "=*",
236   "~*",
237   "==",
238   "#",
239   "<BARE-WORD>",
240   "<\"STRING\">",
241   "<'STRING'>",
242   "<`STRING`>"
243 };
244
245
246 /*
247  *      Print one attribute and value into a string.
248  */
249 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
250 {
251         int             len;
252         const char      *token = NULL;
253
254         out[0] = 0;
255         if (!vp) return 0;
256
257         if (strlen(vp->name) + 3 > (size_t)outlen) {
258                 return 0;
259         }
260
261         if ((vp->operator > T_OP_INVALID) &&
262             (vp->operator < T_TOKEN_LAST)) {
263                 token = vp_tokens[vp->operator];
264         } else {
265                 token = "<INVALID-TOKEN>";
266         }
267
268         if( vp->flags.has_tag ) {
269
270                 snprintf(out, outlen, "%s:%d %s ", vp->name, vp->flags.tag,
271                          token);
272
273                 len = strlen(out);
274                 vp_prints_value(out + len, outlen - len, vp, 1);
275
276         } else {
277
278                 snprintf(out, outlen, "%s %s ", vp->name, token);
279                 len = strlen(out);
280                 vp_prints_value(out + len, outlen - len, vp, 1);
281
282         }
283
284         return strlen(out);
285 }
286
287
288 /*
289  *      Print one attribute and value.
290  */
291 void vp_print(FILE *fp, VALUE_PAIR *vp)
292 {
293         char    buf[1024];
294
295         vp_prints(buf, sizeof(buf), vp);
296         fputs(buf, fp);
297 }
298
299
300 /*
301  *      Print a whole list of attributes, indented by a TAB
302  *      and with a newline at the end.
303  */
304 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
305 {
306         for (; vp; vp = vp->next) {
307                 fprintf(fp, "\t");
308                 vp_print(fp, vp);
309                 fprintf(fp, "\n");
310         }
311 }
312