snmpPasswd.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*_############################################################################
  2. _##
  3. _## snmpPasswd.cpp
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8. _##
  9. _## This software is based on SNMP++2.6 from Hewlett Packard:
  10. _##
  11. _## Copyright (c) 1996
  12. _## Hewlett-Packard Company
  13. _##
  14. _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15. _## Permission to use, copy, modify, distribute and/or sell this software
  16. _## and/or its documentation is hereby granted without fee. User agrees
  17. _## to display the above copyright notice and this license notice in all
  18. _## copies of the software and any documentation of the software. User
  19. _## agrees to assume all liability for the use of the software;
  20. _## Hewlett-Packard and Jochen Katz make no representations about the
  21. _## suitability of this software for any purpose. It is provided
  22. _## "AS-IS" without warranty of any kind, either express or implied. User
  23. _## hereby grants a royalty-free license to any and all derivatives based
  24. _## upon this software code base.
  25. _##
  26. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. char snmppasswd_cpp_version[]="@(#) SNMP++ $Id: snmpPasswd.cpp 264 2006-06-16 20:53:15Z fock $";
  30. #include "snmp_pp/snmp_pp.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #ifdef WIN32
  34. #define strcasecmp stricmp
  35. #endif
  36. #ifdef SNMP_PP_NAMESPACE
  37. using namespace Snmp_pp;
  38. #endif
  39. #if (__GNUC__ > 2)
  40. #include <iostream>
  41. using std::cerr;
  42. using std::cout;
  43. using std::endl;
  44. using std::flush;
  45. #else
  46. #include <iostream.h>
  47. #endif
  48. #ifdef _SNMPv3
  49. USM *usm;
  50. void KeyChange(Snmp* snmp, Pdu& myPdu,
  51. const OctetStr& user, const OctetStr& newpass,
  52. SnmpTarget& target, int type)
  53. {
  54. struct UsmKeyUpdate *uku = NULL;
  55. int stat;
  56. int status;
  57. uku = usm->key_update_prepare(user, target, newpass,
  58. myPdu, type, stat);
  59. if (uku == NULL)
  60. cout << "Key update preparation failed *************" << endl
  61. << "with " << snmp->error_msg(stat) << endl <<endl;
  62. if (( status = snmp->set( myPdu,target)) == SNMP_CLASS_SUCCESS) {
  63. Vb vb3;
  64. Oid oid3;
  65. myPdu.get_vb( vb3,0);
  66. vb3.get_oid(oid3);
  67. Vb vb4;
  68. Oid oid4;
  69. myPdu.get_vb( vb4,1);
  70. vb4.get_oid(oid4);
  71. if (myPdu.get_type() == REPORT_MSG) {
  72. cout << "Received a reportPDU! with Oid "
  73. << oid3.get_printable() << endl
  74. << snmp->error_msg(oid3) << endl;
  75. usm->key_update_abort(uku);
  76. }
  77. else {
  78. cout << flush << endl
  79. << "Oid = " << vb3.get_printable_oid() << endl
  80. << "Value = " << vb3.get_printable_value() << endl;
  81. cout << flush << endl
  82. << "Oid = " << vb4.get_printable_oid() << endl
  83. << "Value = " << vb4.get_printable_value() << endl;
  84. int resul = usm->key_update_commit(uku, USM_PasswordAllKeyUpdate);
  85. cout << endl << "result of local key update: "
  86. << resul << endl;
  87. }
  88. }
  89. else {
  90. cout << "SNMP++ KeyChange Error, " << snmp->error_msg( status)
  91. << " (" << status <<")"<< endl;
  92. usm->key_update_abort(uku);
  93. }
  94. cout << "******************************** END"
  95. << endl << endl << flush;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. //---------[ check the arg count ]----------------------------------------
  100. if ( argc < 4) {
  101. cout << "Usage:\n";
  102. cout << "snmpPasswd IpAddress | DNSName user newpassword [options]\n";
  103. cout << "Oid: sysDescr object is default\n";
  104. cout << "options: -vN , use SNMP version 1, 2 or 3, default is 1\n";
  105. cout << " -PPort , remote port to use\n";
  106. cout << " -CCommunity_name, specify community default is 'public' \n";
  107. cout << " -rN , retries default is N = 1 retry\n";
  108. cout << " -tN , timeout in hundredths of seconds; default is N = 100\n";
  109. cout << " -snSecurityName, " << endl;
  110. cout << " -slN , securityLevel to use, default N = 3 = authPriv" << endl;
  111. cout << " -smN , securityModel to use, only default N = 3 = USM possible\n";
  112. cout << " -cnContextName, default empty string" << endl;
  113. cout << " -ceContextEngineID, as hex e.g. 800007E580, default empty string" << endl;
  114. cout << " -authPROT, use authentication protocol NONE, SHA or MD5\n";
  115. cout << " -privPROT, use privacy protocol NONE, DES, 3DESEDE, IDEA, AES128, AES192 or AES256\n";
  116. cout << " -uaAuthPassword\n";
  117. cout << " -upPrivPassword\n";
  118. cout << " -eEngineID, as hex\n";
  119. return 1;
  120. }
  121. Snmp::socket_startup(); // Initialize socket subsystem
  122. //---------[ make a GenAddress and Oid object to retrieve ]---------------
  123. UdpAddress address( argv[1]); // make a SNMP++ Generic address
  124. if ( !address.valid()) { // check validity of address
  125. cout << "Invalid Address or DNS Name, " << argv[1] << "\n";
  126. return 1;
  127. }
  128. OctetStr newUser, newPassword;
  129. if (((strstr( argv[2],"-")==0) && (strstr( argv[3],"-")==0))) {
  130. newUser = argv[2];
  131. newPassword = argv[3];
  132. }
  133. else
  134. {
  135. cout << "wrong parameters..." << endl;
  136. return 1;
  137. }
  138. //---------[ determine options to use ]-----------------------------------
  139. snmp_version version=version1; // default is v1
  140. int retries=1; // default retries is 1
  141. int timeout=100; // default is 1 second
  142. u_short port=161; // default snmp port is 161
  143. OctetStr community("public"); // community name
  144. OctetStr privPassword("");
  145. OctetStr authPassword("");
  146. OctetStr securityName("");
  147. int securityModel = SecurityModel_USM;
  148. int securityLevel = SecurityLevel_authPriv;
  149. OctetStr contextName("");
  150. OctetStr contextEngineID("");
  151. long authProtocol = SNMPv3_usmNoAuthProtocol;
  152. long privProtocol = SNMPv3_usmNoPrivProtocol;
  153. OctetStr engineID;
  154. v3MP *v3_MP;
  155. char *ptr;
  156. for(int x=1;x<argc;x++) { // parse for version
  157. if ( strstr( argv[x],"-v2")!= 0) {
  158. version = version2c;
  159. continue;
  160. }
  161. if ( strstr( argv[x],"-r")!= 0) { // parse for retries
  162. ptr = argv[x]; ptr++; ptr++;
  163. retries = atoi(ptr);
  164. if (( retries<0)|| (retries>5)) retries=1;
  165. continue;
  166. }
  167. if ( strstr( argv[x], "-t")!=0) { // parse for timeout
  168. ptr = argv[x]; ptr++; ptr++;
  169. timeout = atoi( ptr);
  170. if (( timeout < 100)||( timeout>500)) timeout=100;
  171. continue;
  172. }
  173. if ( strstr( argv[x],"-C")!=0) {
  174. ptr = argv[x]; ptr++; ptr++;
  175. community = ptr;
  176. continue;
  177. }
  178. if ( strstr( argv[x],"-P")!=0) {
  179. ptr = argv[x]; ptr++; ptr++;
  180. sscanf(ptr, "%hu", &port);
  181. continue;
  182. }
  183. if ( strstr( argv[x],"-v3")!= 0) {
  184. version = version3;
  185. continue;
  186. }
  187. if ( strstr( argv[x],"-auth") != 0) {
  188. ptr = argv[x]; ptr+=5;
  189. if (strcasecmp(ptr, "SHA") == 0)
  190. authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
  191. else if (strcasecmp(ptr, "MD5") == 0)
  192. authProtocol = SNMP_AUTHPROTOCOL_HMACMD5;
  193. else
  194. authProtocol = SNMP_AUTHPROTOCOL_NONE;
  195. continue;
  196. }
  197. if ( strstr( argv[x],"-priv") != 0) {
  198. ptr = argv[x]; ptr+=5;
  199. if (strcasecmp(ptr, "DES") == 0)
  200. privProtocol = SNMP_PRIVPROTOCOL_DES;
  201. else if (strcasecmp(ptr, "3DESEDE") == 0)
  202. privProtocol = SNMP_PRIVPROTOCOL_3DESEDE;
  203. else if (strcasecmp(ptr, "IDEA") == 0)
  204. privProtocol = SNMP_PRIVPROTOCOL_IDEA;
  205. else if (strcasecmp(ptr, "AES128") == 0)
  206. privProtocol = SNMP_PRIVPROTOCOL_AES128;
  207. else if (strcasecmp(ptr, "AES192") == 0)
  208. privProtocol = SNMP_PRIVPROTOCOL_AES192;
  209. else if (strcasecmp(ptr, "AES256") == 0)
  210. privProtocol = SNMP_PRIVPROTOCOL_AES256;
  211. else
  212. privProtocol = SNMP_PRIVPROTOCOL_NONE;
  213. printf("\n\nPrivProt : %ld\n", privProtocol);
  214. continue;
  215. }
  216. if ( strstr( argv[x],"-sn")!=0) {
  217. ptr = argv[x]; ptr+=3;
  218. securityName = ptr;
  219. continue;
  220. }
  221. if ( strstr( argv[x], "-sl")!=0) {
  222. ptr = argv[x]; ptr+=3;
  223. securityLevel = atoi( ptr);
  224. if (( securityLevel < SecurityLevel_noAuthNoPriv) ||
  225. ( securityLevel > SecurityLevel_authPriv))
  226. securityLevel = SecurityLevel_authPriv;
  227. continue;
  228. }
  229. if ( strstr( argv[x], "-sm")!=0) {
  230. ptr = argv[x]; ptr+=3;
  231. securityModel = atoi( ptr);
  232. if (( securityModel < SecurityModel_v1) ||
  233. ( securityModel > SecurityModel_USM))
  234. securityModel = SecurityModel_USM;
  235. continue;
  236. }
  237. if ( strstr( argv[x],"-cn")!=0) {
  238. ptr = argv[x]; ptr+=3;
  239. contextName = ptr;
  240. continue;
  241. }
  242. if ( strstr( argv[x],"-ce")!=0) {
  243. ptr = argv[x]; ptr+=3;
  244. contextEngineID = OctetStr::from_hex_string(ptr);
  245. continue;
  246. }
  247. if ( strstr( argv[x],"-ua")!=0) {
  248. ptr = argv[x]; ptr+=3;
  249. authPassword = ptr;
  250. continue;
  251. }
  252. if ( strstr( argv[x],"-up")!=0) {
  253. ptr = argv[x]; ptr+=3;
  254. privPassword = ptr;
  255. continue;
  256. }
  257. if ( strstr( argv[x],"-e")!=0) {
  258. ptr = argv[x]; ptr+=2;
  259. engineID = OctetStr::from_hex_string(ptr);
  260. continue;
  261. }
  262. }
  263. //----------[ create a SNMP++ session ]-----------------------------------
  264. int status;
  265. // bind to any port and use IPv6 if needed
  266. Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
  267. if ( status != SNMP_CLASS_SUCCESS) {
  268. cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
  269. return 1;
  270. }
  271. //---------[ init SnmpV3 ]--------------------------------------------
  272. if (version == version3) {
  273. OctetStr engineId = "snmpPasswd";
  274. char *filename = "snmpv3_boot_counter";
  275. unsigned int snmpEngineBoots = 0;
  276. int status;
  277. status = getBootCounter(filename, engineId, snmpEngineBoots);
  278. if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
  279. {
  280. cout << "Error loading snmpEngineBoots counter: " << status << endl;
  281. return 1;
  282. }
  283. snmpEngineBoots++;
  284. status = saveBootCounter(filename, engineId, snmpEngineBoots);
  285. if (status != SNMPv3_OK)
  286. {
  287. cout << "Error saving snmpEngineBoots counter: " << status << endl;
  288. return 1;
  289. }
  290. int construct_status;
  291. v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
  292. usm = v3_MP->get_usm();
  293. usm->add_usm_user(securityName,
  294. authProtocol, privProtocol,
  295. authPassword, privPassword);
  296. }
  297. else
  298. {
  299. // MUST create a dummy v3MP object if _SNMPv3 is enabled!
  300. int construct_status;
  301. v3_MP = new v3MP("dummy", 0, construct_status);
  302. }
  303. //--------[ build up SNMP++ object needed ]-------------------------------
  304. Pdu pdu; // construct a Pdu object
  305. Vb vb; // construct a Vb object
  306. vb.set_oid(Oid("1.3.6.1.2.1.1.1.0")); // set the Oid portion of the Vb
  307. pdu += vb; // add the vb to the Pdu
  308. address.set_port(port);
  309. CTarget ctarget( address); // make a target using the address
  310. UTarget utarget( address);
  311. if (version == version3) {
  312. utarget.set_version( version); // set the SNMP version SNMPV1 or V2 or V3
  313. utarget.set_retry( retries); // set the number of auto retries
  314. utarget.set_timeout( timeout); // set timeout
  315. utarget.set_security_model( securityModel);
  316. utarget.set_security_name( securityName);
  317. pdu.set_security_level( securityLevel);
  318. pdu.set_context_name (contextName);
  319. pdu.set_context_engine_id(contextEngineID);
  320. }
  321. else {
  322. ctarget.set_version( version); // set the SNMP version SNMPV1 or V2
  323. ctarget.set_retry( retries); // set the number of auto retries
  324. ctarget.set_timeout( timeout); // set timeout
  325. ctarget.set_readcommunity( community); // set the read community name
  326. }
  327. //-------[ issue the request, blocked mode ]-----------------------------
  328. cout << "SNMP++ Get to " << argv[1] << " SNMPV"
  329. << ((version==version3) ? (version) : (version+1))
  330. << " Retries=" << retries
  331. << " Timeout=" << timeout * 10 <<"ms";
  332. if (version == version3)
  333. cout << endl
  334. << "securityName= " << securityName.get_printable()
  335. << ", securityLevel= " << securityLevel
  336. << ", securityModel= " << securityModel << endl
  337. << "contextName= " << contextName.get_printable()
  338. << ", contextEngineID= " << contextEngineID.get_printable()
  339. << endl;
  340. else
  341. cout << " Community=" << community.get_printable() << endl << flush;
  342. SnmpTarget *target;
  343. if (version == version3)
  344. target = &utarget;
  345. else
  346. target = &ctarget;
  347. Pdu pduKeyChange;
  348. if (version == version3) {
  349. pduKeyChange.set_security_level( securityLevel);
  350. pduKeyChange.set_context_name (contextName);
  351. pduKeyChange.set_context_engine_id(contextEngineID);
  352. }
  353. snmp.get( pdu, *target);
  354. KeyChange(&snmp, pduKeyChange, newUser, newPassword, *target, AUTHKEY);
  355. Snmp::socket_cleanup(); // Shut down socket subsystem
  356. }
  357. #else
  358. #include <stdio.h>
  359. int main()
  360. {
  361. printf("This example needs _SNMPv3 defined.\n");
  362. }
  363. #endif