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