net-snmp 5.7
ucdDemoPublic.c
00001 /*
00002  * ucdDemoPublic.c 
00003  */
00004 
00005 #include <net-snmp/net-snmp-config.h>
00006 #if HAVE_STDLIB_H
00007 #include <stdlib.h>
00008 #endif
00009 #if HAVE_STRING_H
00010 #include <string.h>
00011 #else
00012 #include <strings.h>
00013 #endif
00014 
00015 #if TIME_WITH_SYS_TIME
00016 # include <sys/time.h>
00017 # include <time.h>
00018 #else
00019 # if HAVE_SYS_TIME_H
00020 #  include <sys/time.h>
00021 # else
00022 #  include <time.h>
00023 # endif
00024 #endif
00025 
00026 #include <net-snmp/net-snmp-includes.h>
00027 #include <net-snmp/agent/net-snmp-agent-includes.h>
00028 
00029 #include "util_funcs/header_generic.h"
00030 #include "ucdDemoPublic.h"
00031 
00032 #define MYMAX 1024
00033 #define MAXUSERS 10
00034 
00035 int             num = 0;
00036 static char     demoUsers[MAXUSERS][MYMAX + 1];
00037 static char     demopass[MYMAX + 1];
00038 
00039 void
00040 ucdDemo_parse_user(const char *word, char *line)
00041 {
00042     if (num == MAXUSERS)
00043         return;
00044 
00045     if (strlen(line) > MYMAX)
00046         return;
00047 
00048     strcpy(demoUsers[num++], line);
00049 }
00050 
00051 
00052 void
00053 ucdDemo_parse_userpass(const char *word, char *line)
00054 {
00055     if (strlen(line) > MYMAX)
00056         return;
00057 
00058     strcpy(demopass, line);
00059 }
00060 
00061 /*
00062  * this variable defines function callbacks and type return information 
00063  * for the ucdDemoPublic mib 
00064  */
00065 
00066 struct variable2 ucdDemoPublic_variables[] = {
00067     {UCDDEMORESETKEYS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
00068      var_ucdDemoPublic, 1, {1}},
00069     {UCDDEMOPUBLICSTRING, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
00070      var_ucdDemoPublic, 1, {2}},
00071     {UCDDEMOUSERLIST, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
00072      var_ucdDemoPublic, 1, {3}},
00073     {UCDDEMOPASSPHRASE, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
00074      var_ucdDemoPublic, 1, {4}},
00075 
00076 };
00077 
00078 /*
00079  * Define the OID pointer to the top of the mib tree that we're
00080  * registering underneath 
00081  */
00082 oid             ucdDemoPublic_variables_oid[] =
00083     { 1, 3, 6, 1, 4, 1, 2021, 14, 1, 1 };
00084 
00085 void
00086 init_ucdDemoPublic(void)
00087 {
00088     REGISTER_MIB("examples/ucdDemoPublic", ucdDemoPublic_variables,
00089                  variable2, ucdDemoPublic_variables_oid);
00090     snmpd_register_config_handler("demoUser",
00091                                   ucdDemo_parse_user, NULL, "USER");
00092     snmpd_register_config_handler("demoPass",
00093                                   ucdDemo_parse_userpass, NULL,
00094                                   "PASSPHASE");
00095 }
00096 
00097 unsigned char   publicString[MYMAX + 1];
00098 
00099 unsigned char  *
00100 var_ucdDemoPublic(struct variable *vp,
00101                   oid * name,
00102                   size_t * length,
00103                   int exact, size_t * var_len, WriteMethod ** write_method)
00104 {
00105     static long     long_ret;
00106     static char     string[MYMAX + 1], *cp;
00107     int             i;
00108 
00109     *write_method = 0;          /* assume it isnt writable for the time being */
00110     *var_len = sizeof(long_ret);        /* assume an integer and change later if not */
00111 
00112     if (header_generic(vp, name, length, exact, var_len, write_method))
00113         return 0;
00114 
00115     /*
00116      * this is where we do the value assignments for the mib results. 
00117      */
00118     switch (vp->magic) {
00119 
00120     case UCDDEMORESETKEYS:
00121         *write_method = write_ucdDemoResetKeys;
00122         long_ret = 0;
00123         return (unsigned char *) &long_ret;
00124 
00125     case UCDDEMOPUBLICSTRING:
00126         *write_method = write_ucdDemoPublicString;
00127         *var_len = strlen((const char*)publicString);
00128         return (unsigned char *) publicString;
00129 
00130     case UCDDEMOUSERLIST:
00131         cp = string;
00132         for (i = 0; i < num; i++) {
00133             snprintf(cp, sizeof(string)-strlen(string), " %s", demoUsers[i]);
00134             string[MYMAX] = 0;
00135             cp = cp + strlen(cp);
00136         }
00137         *var_len = strlen(string);
00138         return (unsigned char *) string;
00139 
00140     case UCDDEMOPASSPHRASE:
00141         *var_len = strlen(demopass);
00142         return (unsigned char *) demopass;
00143 
00144     default:
00145         DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ucdDemoPublic\n",
00146                     vp->magic));
00147     }
00148     return 0;
00149 }
00150 
00151 int
00152 write_ucdDemoResetKeys(int action,
00153                        u_char * var_val,
00154                        u_char var_val_type,
00155                        size_t var_val_len,
00156                        u_char * statP, oid * name, size_t name_len)
00157 {
00158     /*
00159      * variables we may use later 
00160      */
00161     static long     long_ret;
00162     unsigned char  *engineID;
00163     size_t          engineIDLen;
00164     int             i;
00165     struct usmUser *user;
00166 
00167     if (var_val_type != ASN_INTEGER) {
00168         DEBUGMSGTL(("ucdDemoPublic",
00169                     "write to ucdDemoResetKeys not ASN_INTEGER\n"));
00170         return SNMP_ERR_WRONGTYPE;
00171     }
00172     if (var_val_len > sizeof(long_ret)) {
00173         DEBUGMSGTL(("ucdDemoPublic",
00174                     "write to ucdDemoResetKeys: bad length\n"));
00175         return SNMP_ERR_WRONGLENGTH;
00176     }
00177     if (action == COMMIT) {
00178         long_ret = *((long *) var_val);
00179         if (long_ret == 1) {
00180             engineID = snmpv3_generate_engineID(&engineIDLen);
00181             for (i = 0; i < num; i++) {
00182                 user = usm_get_user(engineID, engineIDLen, demoUsers[i]);
00183                 if (user) {
00184                     usm_set_user_password(user, "userSetAuthPass",
00185                                           demopass);
00186                     usm_set_user_password(user, "userSetPrivPass",
00187                                           demopass);
00188                 }
00189             }
00190             /*
00191              * reset the keys 
00192              */
00193         }
00194     }
00195     return SNMP_ERR_NOERROR;
00196 }
00197 
00198 int
00199 write_ucdDemoPublicString(int action,
00200                           u_char * var_val,
00201                           u_char var_val_type,
00202                           size_t var_val_len,
00203                           u_char * statP, oid * name, size_t name_len)
00204 {
00205     if (var_val_type != ASN_OCTET_STR) {
00206         DEBUGMSGTL(("ucdDemoPublic",
00207                     "write to ucdDemoPublicString not ASN_OCTET_STR\n"));
00208         return SNMP_ERR_WRONGTYPE;
00209     }
00210     if (var_val_len > MYMAX) {
00211         DEBUGMSGTL(("ucdDemoPublic",
00212                     "write to ucdDemoPublicString: bad length\n"));
00213         return SNMP_ERR_WRONGLENGTH;
00214     }
00215     if (action == COMMIT) {
00216         if (var_val_len != 0) {
00217             strncpy((char*)publicString, (const char*)var_val, sizeof(publicString)-1);
00218             /* some sanity checks */
00219             if (strlen((const char*)var_val) > sizeof(publicString)-1 ||
00220                 strlen((const char*)var_val) != var_val_len)
00221                 publicString[sizeof(publicString)-1] = '\0';
00222             else
00223                 publicString[var_val_len] = '\0';
00224         } else
00225             publicString[0] = '\0';
00226     }
00227     return SNMP_ERR_NOERROR;
00228 }