Back to index

nagios-plugins  1.4.16
attrprint.c
Go to the documentation of this file.
00001 /*
00002  *
00003  *     RADIUS
00004  *     Remote Authentication Dial In User Service
00005  *
00006  *
00007  *     Livingston Enterprises, Inc.
00008  *     6920 Koll Center Parkway
00009  *     Pleasanton, CA   94566
00010  *
00011  *     Copyright 1992 Livingston Enterprises, Inc.
00012  *
00013  *     Permission to use, copy, modify, and distribute this software for any
00014  *     purpose and without fee is hereby granted, provided that this
00015  *     copyright and permission notice appear on all copies and supporting
00016  *     documentation, the name of Livingston Enterprises, Inc. not be used
00017  *     in advertising or publicity pertaining to distribution of the
00018  *     program without specific prior permission, and notice be given
00019  *     in supporting documentation that copying and distribution is by
00020  *     permission of Livingston Enterprises, Inc.   
00021  *
00022  *     Livingston Enterprises, Inc. makes no representations about
00023  *     the suitability of this software for any purpose.  It is
00024  *     provided "as is" without express or implied warranty.
00025  *
00026  */
00027 
00028 char attrprint_sccsid[] =
00029 "@(#)attrprint.c     1.3 Copyright 1992 Livingston Enterprises Inc\n"
00030 "                    2.1 Copyright 1998 Cistron Internet Services B.V.";
00031 
00032 #include      <sys/types.h>
00033 #include      <sys/socket.h>
00034 #include      <sys/time.h>
00035 #include      <netinet/in.h>
00036 
00037 #include      <stdio.h>
00038 #include      <netdb.h>
00039 #include      <pwd.h>
00040 #include      <time.h>
00041 #include      <ctype.h>
00042 
00043 #include      "radiusd.h"
00044 
00045 
00046 /*
00047  *     Print the Attribute-value pair to the desired File.
00048  *     FIXME: work with logging routines in log.c
00049  */
00050 void debug_pair(FILE *fd, VALUE_PAIR *pair)
00051 {
00052        if(debug_flag) {
00053               fputs("    ", fd);
00054               fprint_attr_val(fd, pair);
00055               fputs("\n", fd);
00056        }
00057 }
00058 
00059 
00060 /*
00061  *     Write a whole list of A/V pairs.
00062  */
00063 void fprint_attr_list(FILE *fd, VALUE_PAIR *pair)
00064 {
00065        while(pair) {
00066               fprintf(fd, "    ");
00067               fprint_attr_val(fd, pair);
00068               fprintf(fd, "\n");
00069               pair = pair->next;
00070        }
00071 }
00072 
00073 
00074 /*
00075  *     Write a printable version of the attribute-value
00076  *     pair to the supplied File.
00077  */
00078 void fprint_attr_val(FILE *fd, VALUE_PAIR *pair)
00079 {
00080        DICT_VALUE    *dict_valget();
00081        DICT_VALUE    *dval;
00082        char          buffer[32];
00083        u_char        *ptr;
00084        UINT4         vendor;
00085        int           i, left;
00086 
00087        switch(pair->type) {
00088 
00089        case PW_TYPE_STRING:
00090               fprintf(fd, "%s = \"", pair->name);
00091               ptr = (u_char *)pair->strvalue;
00092               if (pair->attribute != PW_VENDOR_SPECIFIC) {
00093                      left = pair->length;
00094                      while(left-- > 0) {
00095                             /*
00096                              *     Ugh! Ascend gear sends "foo"
00097                              *     as "foo\0", length 4.
00098                              *     Suppress trailing zeros.
00099                              */
00100                             if (left == 0 && *ptr == 0)
00101                                    break;
00102                             if(!(isprint(*ptr)))
00103                                    fprintf(fd, "\\%03o", *ptr);
00104                             else
00105                                    fputc(*ptr, fd);
00106                             ptr++;
00107                      }
00108                      fputc('"', fd);
00109                      break;
00110               }
00111               /*
00112                *     Special format, print out as much
00113                *     info as we can.
00114                */
00115               if (pair->length < 6) {
00116                      fprintf(fd, "(invalid length: %d)\"", pair->length);
00117                      break;
00118               }
00119               memcpy(&vendor, ptr, 4);
00120               ptr += 4;
00121               fprintf(fd, "V%d", (int)ntohl(vendor));
00122               left = pair->length - 4;
00123               while (left >= 2) {
00124                      fprintf(fd, ":T%d:L%d:", ptr[0], ptr[1]);
00125                      left -= 2;
00126                      ptr += 2;
00127                      i = ptr[1] - 2;
00128                      while (i > 0 && left > 0) {
00129                             if(!(isprint(*ptr)))
00130                                    fprintf(fd, "\\%03o", *ptr);
00131                             else
00132                                    fputc(*ptr, fd);
00133                             ptr++;
00134                             i--;
00135                             left--;
00136                      }
00137               }
00138               fputc('"', fd);
00139               break;
00140 
00141        case PW_TYPE_INTEGER:
00142               dval = dict_valget(pair->lvalue, pair->name);
00143               if(dval != (DICT_VALUE *)NULL) {
00144                      fprintf(fd, "%s = %s", pair->name, dval->name);
00145               }
00146               else {
00147                      fprintf(fd, "%s = %ld", pair->name, (long)pair->lvalue);
00148               }
00149               break;
00150 
00151        case PW_TYPE_IPADDR:
00152               ipaddr2str(buffer, pair->lvalue);
00153               fprintf(fd, "%s = %s", pair->name, buffer);
00154               break;
00155 
00156        case PW_TYPE_DATE:
00157               strftime(buffer, sizeof(buffer), "%b %e %Y",
00158                                    localtime((time_t *)&pair->lvalue));
00159               fprintf(fd, "%s = \"%s\"", pair->name, buffer);
00160               break;
00161 
00162        default:
00163               fprintf(fd, "Unknown type %d", pair->type);
00164               break;
00165        }
00166 }