docsis.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * DOCSIS configuration file encoder.
  3. * Copyright (c) 2001 Cornel Ciocirlan, ctrl@users.sourceforge.net.
  4. * Copyright (c) 2002,2003,2004,2005 Evvolve Media SRL,office@evvolve.com
  5. * Copyright (c) 2014 - 2015 Adrian Simionov, daniel.simionov@gmail.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * DOCSIS is a registered trademark of Cablelabs, http://www.cablelabs.com
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif /* HAVE_CONFIG_H */
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <net-snmp/net-snmp-config.h>
  33. #include <net-snmp/config_api.h>
  34. #include <net-snmp/output_api.h>
  35. #include <net-snmp/mib_api.h>
  36. #include "docsis.h"
  37. #include "docsis_globals.h"
  38. #include "docsis_symtable.h"
  39. #include "ethermac.h"
  40. #include "md5.h"
  41. #include "sha1.h"
  42. struct tlv *global_tlvtree_head;
  43. symbol_type *global_symtable;
  44. unsigned int nohash = 0;
  45. unsigned int dialplan = 0;
  46. static void setup_mib_flags(int resolve_oids, char *custom_mibs);
  47. static unsigned int
  48. add_cm_mic (unsigned char *tlvbuf, unsigned int tlvbuflen)
  49. {
  50. unsigned char digest[16];
  51. MD5_CTX mdContext;
  52. if (tlvbuf == NULL || tlvbuflen == 0)
  53. return 0;
  54. MD5_Init (&mdContext);
  55. MD5_Update (&mdContext, tlvbuf, tlvbuflen);
  56. MD5_Final (digest, &mdContext);
  57. tlvbuf[tlvbuflen] = 6;
  58. tlvbuf[tlvbuflen + 1] = 16;
  59. memcpy (tlvbuf + tlvbuflen + 2, digest, 16);
  60. return (tlvbuflen + 18); /* we added the CM Message Integrity Check */
  61. }
  62. static unsigned int
  63. add_eod_and_pad (unsigned char *tlvbuf, unsigned int tlvbuflen)
  64. {
  65. int nr_pads;
  66. if (tlvbuf == NULL || tlvbuflen == 0)
  67. return 0;
  68. tlvbuf[tlvbuflen] = 255;
  69. tlvbuflen = tlvbuflen + 1;
  70. nr_pads = (4 - (tlvbuflen % 4)) % 4;
  71. memset (&tlvbuf[tlvbuflen], 0, nr_pads);
  72. return (tlvbuflen + nr_pads);
  73. }
  74. static unsigned int
  75. add_cmts_mic (unsigned char *tlvbuf, unsigned int tlvbuflen,
  76. unsigned char *key, int keylen)
  77. {
  78. int i;
  79. register unsigned char *cp, *dp;
  80. unsigned char *cmts_tlvs;
  81. unsigned char digest[17];
  82. /* Only these configuration TLVs must be used to calculate the CMTS MIC */
  83. #define NR_CMTS_MIC_TLVS 21
  84. unsigned char digest_order[NR_CMTS_MIC_TLVS] =
  85. { 1, 2, 3, 4, 17, 43, 6, 18, 19, 20, 22, 23, 24, 25, 28, 29, 26, 35, 36, 37, 40 };
  86. if (tlvbuf == NULL || tlvbuflen == 0 )
  87. return 0;
  88. cmts_tlvs = (unsigned char *) malloc (tlvbuflen + 1); /* Plenty of space */
  89. dp = cmts_tlvs;
  90. for (i = 0; i < NR_CMTS_MIC_TLVS; i++)
  91. {
  92. cp = tlvbuf;
  93. while ((unsigned int) (cp - tlvbuf) < tlvbuflen)
  94. {
  95. if (cp[0] == digest_order[i])
  96. {
  97. memcpy (dp, cp, cp[1] + 2);
  98. dp = dp + cp[1] + 2;
  99. cp = cp + cp[1] + 2;
  100. }
  101. else
  102. {
  103. if ( cp[0] == 64 ) {
  104. cp = cp + (size_t) ntohs(*((unsigned short *)(cp+1))) + 3;
  105. } else {
  106. cp = cp + cp[1] + 2;
  107. }
  108. }
  109. }
  110. }
  111. fprintf (stdout, "##### Calculating CMTS MIC using TLVs:\n");
  112. decode_main_aggregate (cmts_tlvs, dp - cmts_tlvs);
  113. fprintf (stdout, "##### End of CMTS MIC TLVs\n");
  114. hmac_md5 (cmts_tlvs, dp - cmts_tlvs, key, keylen, digest);
  115. md5_print_digest (digest);
  116. tlvbuf[tlvbuflen] = 7; /* CMTS MIC */
  117. tlvbuf[tlvbuflen + 1] = 16; /* length of MD5 digest */
  118. memcpy (&tlvbuf[tlvbuflen + 2], digest, 16);
  119. free (cmts_tlvs);
  120. return (tlvbuflen + 18);
  121. }
  122. static unsigned int
  123. add_mta_hash (unsigned char *tlvbuf, unsigned int tlvbuflen, unsigned int hash) {
  124. SHA_CTX shactx;
  125. unsigned char hash_value[SHA_DIGEST_LENGTH];
  126. SHA1_Init(&shactx);
  127. SHA1_Update(&shactx, tlvbuf, tlvbuflen);
  128. SHA1_Final(hash_value, &shactx);
  129. if (hash == 1) {
  130. memcpy (tlvbuf + tlvbuflen - 3, "\x0b\x28\x30\x26\x06\x0e\x2b\x06\x01\x04\x01\xa3\x0b\x02\x02\x01\x01\x02\x07\x00\x04\x14", 22);
  131. tlvbuflen += 19;
  132. }
  133. if (hash == 2) {
  134. memcpy (tlvbuf + tlvbuflen - 3, "\x0b\x26\x30\x24\x06\x0c\x2b\x06\x01\x04\x01\xba\x08\x01\x01\x02\x09\x00\x04\x14", 20);
  135. tlvbuflen += 17;
  136. }
  137. memcpy (tlvbuf + tlvbuflen, hash_value, SHA_DIGEST_LENGTH);
  138. tlvbuflen += SHA_DIGEST_LENGTH;
  139. memcpy (tlvbuf + tlvbuflen, "\xfe\x01\xff", 3);
  140. tlvbuflen += 3;
  141. return (tlvbuflen);
  142. }
  143. static unsigned int
  144. add_dialplan (unsigned char *tlvbuf, unsigned int tlvbuflen) {
  145. FILE *dialplan_file;
  146. char *dialplan_buffer;
  147. unsigned int fileSize;
  148. unsigned int readSize;
  149. unsigned short local_v_len;
  150. unsigned short *p_local_v_len = &local_v_len;
  151. unsigned short local_char;
  152. unsigned short *p_local_char = &local_char;
  153. dialplan_file = fopen("dialplan.txt", "rb");
  154. if (!dialplan_file) {
  155. fprintf(stderr, "Cannot open dialplan.txt file, fatal error, closing.\n");
  156. exit(-1);
  157. }
  158. fseek(dialplan_file, 0, SEEK_END);
  159. fileSize = ftell (dialplan_file);
  160. fseek(dialplan_file, 0, SEEK_SET);
  161. dialplan_buffer = malloc(fileSize);
  162. if (!dialplan_buffer) {
  163. fprintf(stderr, "Fatal error allocating memory for dialplan buffer, closing.\n");
  164. exit(-1);
  165. }
  166. readSize = fread(dialplan_buffer, fileSize, 1, dialplan_file);
  167. if (!readSize) {
  168. fprintf(stderr, "Something went wrong reading the dialplan file, closing.\n");
  169. exit(-1);
  170. }
  171. fclose(dialplan_file);
  172. tlvbuflen -= 3;
  173. memcpy(tlvbuf + tlvbuflen, "\x40", 1);
  174. tlvbuflen += 1;
  175. if (fileSize > 0x7f) {
  176. local_v_len = htons(2 + 2 + 20 + 2 + 2 + fileSize);
  177. } else if (fileSize > 0x69) {
  178. local_v_len = htons(2 + 2 + 20 + 1 + 1 + fileSize);
  179. } else {
  180. local_v_len = htons(1 + 1 + 20 + 1 + 1 + fileSize);
  181. }
  182. memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
  183. tlvbuflen += sizeof(local_v_len);
  184. memcpy(tlvbuf + tlvbuflen, "\x30", 1);
  185. tlvbuflen += 1;
  186. local_char = 0x16 + fileSize;
  187. if (local_char < 0x80) {
  188. memcpy(tlvbuf + tlvbuflen, p_local_char, sizeof(short));
  189. tlvbuflen += sizeof(char);
  190. } else {
  191. memcpy(tlvbuf + tlvbuflen, "\x82", 1);
  192. tlvbuflen += 1;
  193. if (fileSize > 0x7f) {
  194. local_v_len = htons(20 + 2 + 2 + fileSize);
  195. } else {
  196. local_v_len = htons(20 + 1 + 1 + fileSize);
  197. }
  198. memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
  199. tlvbuflen += sizeof(local_v_len);
  200. }
  201. memcpy(tlvbuf + tlvbuflen, "\x06\x12\x2b\x06\x01\x04\x01\xa3\x0b\x02\x02\x08\x02\x01\x01\x03\x01\x01\x02\x00", 20);
  202. tlvbuflen += 20;
  203. memcpy(tlvbuf + tlvbuflen, "\x04", 1);
  204. tlvbuflen += 1;
  205. if (fileSize > 0x7f) {
  206. memcpy(tlvbuf + tlvbuflen, "\x82", 1);
  207. tlvbuflen += 1;
  208. local_v_len = (unsigned short) htons(fileSize);
  209. memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
  210. tlvbuflen += sizeof(local_v_len);
  211. } else {
  212. local_char = (unsigned short) fileSize;
  213. memcpy(tlvbuf + tlvbuflen, p_local_char, sizeof(short));
  214. tlvbuflen += sizeof(char);
  215. }
  216. memcpy(tlvbuf + tlvbuflen, dialplan_buffer, fileSize);
  217. tlvbuflen = tlvbuflen + fileSize;
  218. free(dialplan_buffer);
  219. memcpy (tlvbuf + tlvbuflen, "\xfe\x01\xff", 3);
  220. tlvbuflen += 3;
  221. return (tlvbuflen);
  222. }
  223. #ifdef __GNUC__
  224. static void usage () __attribute__((__noreturn__));
  225. #endif
  226. static void
  227. usage ()
  228. {
  229. fprintf(stderr, "DOCSIS Configuration File creator, version %s\n", VERSION);
  230. fprintf(stderr, "Copyright (c) 1999,2000,2001 Cornel Ciocirlan, ctrl@users.sourceforge.net\n");
  231. fprintf(stderr, "Copyright (c) 2002,2003,2004,2005 Evvolve Media SRL, docsis@evvolve.com\n");
  232. fprintf(stderr, "Copyright (c) 2014 - 2015 Adrian Simionov, daniel.simionov@gmail.com\n\n");
  233. fprintf(stderr, "To encode a cable modem configuration file: \n\tdocsis [modifiers] -e <modem_cfg_file> <key_file> <output_file>\n");
  234. fprintf(stderr, "To encode multiple cable modem configuration files: \n\tdocsis [modifiers] -m <modem_cfg_file1> ... <key_file> <new_extension>\n");
  235. fprintf(stderr, "To encode a MTA configuration file: \n\tdocsis [modifiers] -p <mta_cfg_file> <output_file>\n");
  236. fprintf(stderr, "To encode multiple MTA configuration files: \n\tdocsis [modifiers] -m -p <mta_file1> ... <new_extension>\n");
  237. fprintf(stderr, "To decode a CM or MTA config file: \n\tdocsis [modifiers] -d <binary_file>\n\n");
  238. fprintf(stderr, "Where:\n<cfg_file>\t\t= name of text (human readable) cable modem or MTA \n"
  239. "\t\t\t configuration file;\n"
  240. "<key_file>\t\t= text file containing the authentication key\n"
  241. "\t\t\t (shared secret) to be used for the CMTS MIC;\n"
  242. "<output_file> \t\t= name of output file where"
  243. " the binary data will\n\t\t\t be written to (if it does not exist it is created);\n"
  244. "<binary_file>\t\t= name of binary file to be decoded;\n"
  245. "<new_extension>\t\t= new extension to be used when encoding multiple files.\n\n");
  246. fprintf(stderr, "The following command-line modifiers are available:\n"
  247. " -o\n"
  248. " Decode OIDs numerically.\n\n"
  249. " -M \"PATH1:PATH2\"\n"
  250. " Specify the SNMP MIB directory when encoding or decoding configuration\n"
  251. " files.\n\n"
  252. " -na | -eu\n"
  253. " Adds CableLabs PacketCable or Excentis EuroPacketCable SHA1 hash\n"
  254. " when encoding an MTA config file.\n\n"
  255. " -dialplan\n"
  256. " Adds a PC20 dialplan from an external file called \"dialplan.txt\" in\n"
  257. " the current directory.\n\n"
  258. " -nohash\n"
  259. " Removes the PacketCable SHA1 hash from the MTA config file when\n"
  260. " decoding.\n"
  261. );
  262. fprintf(stderr, "\nSee examples/*.cfg for sample configuration files.\n");
  263. fprintf(stderr, "\nPlease report bugs or feature requests on GitHub.");
  264. fprintf(stderr, "\nProject repository is https://github.com/rlaager/docsis\n\n");
  265. exit (-10);
  266. }
  267. int
  268. main (int argc, char *argv[])
  269. {
  270. unsigned char key[65];
  271. FILE *kf;
  272. char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL, *custom_mibs=NULL;
  273. unsigned int keylen = 0;
  274. unsigned int encode_docsis = FALSE, decode_bin = FALSE, hash = 0;
  275. int i;
  276. int resolve_oids = 1;
  277. while (argc > 0) {
  278. argc--; argv++;
  279. if (!argc) {
  280. usage();
  281. }
  282. /* the initial command-line parameters are flags / modifiers */
  283. if (!strcmp (argv[0], "-nohash")) {
  284. nohash = 1;
  285. continue;
  286. }
  287. if (!strcmp (argv[0], "-o")) {
  288. resolve_oids = 0;
  289. continue;
  290. }
  291. if (!strcmp (argv[0], "-M")) {
  292. if (argc < 2 ) {
  293. usage();
  294. }
  295. custom_mibs=argv[1];
  296. argc--; argv++;
  297. continue;
  298. }
  299. if (!strcmp (argv[0], "-na")) {
  300. if (hash) {
  301. usage();
  302. }
  303. hash = 1;
  304. continue;
  305. }
  306. if (!strcmp (argv[0], "-eu")) {
  307. if (hash) {
  308. usage();
  309. }
  310. hash = 2;
  311. continue;
  312. }
  313. if (!strcmp (argv[0], "-dialplan")) {
  314. dialplan = 1;
  315. continue;
  316. }
  317. /* the following command-line parameters are actions */
  318. if (!strcmp (argv[0], "-d")) {
  319. if (argc < 2 ) {
  320. usage();
  321. }
  322. decode_bin = TRUE;
  323. config_file = argv[1];
  324. break;
  325. }
  326. if (!strcmp (argv[0], "-e")) {
  327. if (argc < 4 ) {
  328. usage();
  329. }
  330. encode_docsis = TRUE;
  331. config_file = argv[1];
  332. key_file = argv[2];
  333. output_file = argv[3];
  334. break;
  335. }
  336. if (!strcmp (argv[0], "-m")) {
  337. extension_string = argv[argc-1];
  338. key_file = argv[argc-2];
  339. encode_docsis = TRUE;
  340. continue;
  341. }
  342. if (!strcmp (argv[0], "-p")) {
  343. /* encode_docsis may already have been set via the "-m" option */
  344. encode_docsis = 0;
  345. argc--; argv++;
  346. if (argc < 2 ) {
  347. usage();
  348. }
  349. /* -p might be followed by -dialplan. This is allowed for backwards
  350. * compatibility */
  351. if (!strcmp (argv[0], "-dialplan")) {
  352. dialplan = 1;
  353. argc--; argv++;
  354. }
  355. if (argc < 2 ) {
  356. usage();
  357. }
  358. /* if -m has not already been specified, then we expect "<mta_cfg_file> <output_file>" */
  359. if (extension_string == NULL) {
  360. config_file = argv[0];
  361. output_file = argv[1];
  362. }
  363. break;
  364. }
  365. /* no more recognisable options means that we've either finished parsing
  366. * all arguments or else that the remaining arguments refer to a list of
  367. * config files */
  368. if ((argc && encode_docsis) || (argc && decode_bin)) {
  369. break;
  370. }
  371. }
  372. if (encode_docsis)
  373. {
  374. if ((kf = fopen (key_file, "r")) == NULL)
  375. {
  376. fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file);
  377. exit (-5);
  378. }
  379. keylen = fread (key, sizeof (unsigned char), 64, kf);
  380. while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13))
  381. {
  382. keylen--; /* eliminate trailing \n or \r */
  383. }
  384. fclose(kf);
  385. }
  386. init_global_symtable ();
  387. setup_mib_flags(resolve_oids,custom_mibs);
  388. if (decode_bin)
  389. {
  390. decode_file (config_file);
  391. exit(0); // TODO: clean shutdown
  392. }
  393. if (extension_string) { /* encoding multiple files */
  394. if (encode_docsis) {
  395. /* encode argv[argc-3] to argv[0] */
  396. for (i=0; i<argc-2; i++) {
  397. if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
  398. fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
  399. continue;
  400. }
  401. fprintf(stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
  402. if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
  403. exit(2);
  404. }
  405. free (output_file);
  406. output_file = NULL;
  407. }
  408. } else {
  409. /* encode argv[argc-2] to argv[0] */
  410. for (i=0; i<argc-1; i++) {
  411. if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
  412. fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
  413. continue;
  414. }
  415. fprintf (stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
  416. if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
  417. exit(2);
  418. }
  419. free (output_file);
  420. output_file = NULL;
  421. }
  422. }
  423. } else {
  424. if (encode_one_file (config_file, output_file, key, keylen, encode_docsis, hash)) {
  425. exit(2);
  426. }
  427. /* encode argv[1] */
  428. }
  429. free(global_symtable);
  430. shutdown_mib();
  431. return 0;
  432. }
  433. int encode_one_file ( char *input_file, char *output_file,
  434. unsigned char *key, unsigned int keylen, int encode_docsis, unsigned int hash)
  435. {
  436. int parse_result=0;
  437. unsigned int buflen;
  438. unsigned char *buffer;
  439. FILE *of;
  440. /* It's not an error to specify the input and output as "-". */
  441. if (!strcmp (input_file, output_file) && strcmp (input_file, "-"))
  442. {
  443. fprintf(stderr, "docsis: Error: source file is the same as destination file\n");
  444. return -1;
  445. }
  446. parse_result = parse_config_file (input_file, &global_tlvtree_head );
  447. if (parse_result || global_tlvtree_head == NULL)
  448. {
  449. fprintf(stderr, "Error parsing config file %s\n", input_file);
  450. return -1;
  451. }
  452. /* Check whether we're encoding PacketCable */
  453. if (global_tlvtree_head->docs_code == 254) {
  454. fprintf(stderr, "First TLV is MtaConfigDelimiter, forcing PacketCable MTA file.\n");
  455. encode_docsis=0;
  456. }
  457. /* walk the tree to find out how much memory we need */
  458. /* leave some room for CM MIC, CMTS MIC, pad, and a HUGE PC20 dialplan */
  459. buflen = tlvtreelen (global_tlvtree_head);
  460. buffer = (unsigned char *) malloc ( buflen + 255 + 8192 );
  461. buflen = flatten_tlvsubtree(buffer, 0, global_tlvtree_head);
  462. #ifdef DEBUG
  463. fprintf(stderr, "TLVs found in parsed config file:\n");
  464. decode_main_aggregate (buffer, buflen);
  465. #endif
  466. if (encode_docsis)
  467. {
  468. /* CM config file => add CM MIC, CMTS MIC, End-of-Data and pad */
  469. buflen = add_cm_mic (buffer, buflen);
  470. buflen = add_cmts_mic (buffer, buflen, key, keylen);
  471. buflen = add_eod_and_pad (buffer, buflen);
  472. }
  473. if (dialplan == 1) {
  474. printf("Adding PC20 dialplan from external file.\n");
  475. buflen = add_dialplan (buffer, buflen);
  476. }
  477. if (hash == 1) {
  478. printf("Adding NA ConfigHash to MTA file.\n");
  479. buflen = add_mta_hash (buffer, buflen, hash);
  480. }
  481. if (hash == 2) {
  482. printf("Adding EU ConfigHash to MTA file.\n");
  483. buflen = add_mta_hash (buffer, buflen, hash);
  484. }
  485. fprintf (stdout, "Final content of config file:\n");
  486. decode_main_aggregate (buffer, buflen);
  487. if (!strcmp (output_file, "-"))
  488. {
  489. of = stdout;
  490. }
  491. else if ((of = fopen (output_file, "wb")) == NULL)
  492. {
  493. fprintf (stderr, "docsis: error: can't open output file %s\n", output_file);
  494. return -2;
  495. }
  496. if (fwrite (buffer, 1, buflen, of) != buflen)
  497. {
  498. fprintf (stderr, "docsis: error: can't write to output file %s\n", output_file);
  499. return -2;
  500. }
  501. fclose (of);
  502. free(buffer);
  503. return 0;
  504. /*free(global_tlvlist->tlvlist); free(global_tlvlist); */ /* TODO free tree */
  505. }
  506. int
  507. init_global_symtable (void)
  508. {
  509. global_symtable =
  510. (symbol_type *) malloc (sizeof (symbol_type) * NUM_IDENTIFIERS);
  511. if (global_symtable == NULL)
  512. {
  513. fprintf(stderr, "Error allocating memory\n");
  514. exit (255);
  515. }
  516. memcpy (global_symtable, symtable, sizeof (symbol_type) * NUM_IDENTIFIERS);
  517. return 1;
  518. }
  519. void
  520. decode_file (char *file)
  521. {
  522. int ifd;
  523. unsigned char *buffer;
  524. unsigned int buflen = 0;
  525. int rv = 0;
  526. struct stat st;
  527. if ((ifd = open (file, O_RDONLY)) == -1)
  528. {
  529. fprintf(stderr, "Error opening binary file %s: %s\n", file, strerror (errno));
  530. exit (-1);
  531. }
  532. if ((rv = fstat (ifd, &st)))
  533. {
  534. fprintf(stderr, "Can't stat file %s: %s\n", file, strerror (errno));
  535. exit (-1);
  536. }
  537. buffer = (unsigned char *) malloc (st.st_size * sizeof (unsigned char) + 1);
  538. buflen = read (ifd, buffer, st.st_size);
  539. decode_main_aggregate (buffer, buflen);
  540. free(buffer);
  541. }
  542. static void
  543. setup_mib_flags(int resolve_oids, char *custom_mibs) {
  544. #ifdef DEBUG
  545. /* snmp_set_mib_warnings (2); */
  546. #endif /* DEBUG */
  547. /* We do not want warning for normal users. Should be set with an argument on the CLI maybe?
  548. * snmp_set_mib_warnings (1); */
  549. if (custom_mibs)
  550. {
  551. setenv ("MIBDIRS", custom_mibs, 1);
  552. }
  553. if (resolve_oids)
  554. {
  555. setenv ("MIBS", "ALL", 1);
  556. }
  557. if (!resolve_oids) {
  558. if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC)) {
  559. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC);
  560. }
  561. }
  562. /* DOCSIS vendors are notorious for supplying MIBs with invalid syntax */
  563. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL, 1);
  564. #ifdef HAVE_NETSNMP_INIT_MIB
  565. netsnmp_init_mib ();
  566. #else
  567. init_mib ();
  568. #endif
  569. if (!netsnmp_ds_get_boolean
  570. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS))
  571. {
  572. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  573. NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
  574. } /* we want OIDs to appear in numeric form */
  575. if (!netsnmp_ds_get_boolean
  576. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM))
  577. {
  578. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  579. NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
  580. } /* we want enums to appear in numeric form as integers */
  581. if (!netsnmp_ds_get_boolean
  582. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID))
  583. {
  584. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  585. NETSNMP_DS_LIB_PRINT_FULL_OID);
  586. } /* we want to full numeric OID to be printed, including prefix */
  587. if (!netsnmp_ds_get_boolean
  588. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS))
  589. {
  590. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  591. NETSNMP_DS_LIB_DONT_PRINT_UNITS);
  592. }
  593. if (!netsnmp_ds_get_boolean
  594. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS))
  595. {
  596. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  597. NETSNMP_DS_LIB_RANDOM_ACCESS);
  598. } /* so we can use sysContact.0 instead of system.sysContact.0 */
  599. if (!netsnmp_ds_get_boolean
  600. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS))
  601. {
  602. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  603. NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
  604. } /* so we can use sysContact.0 instead of system.sysContact.0 */
  605. }
  606. /*
  607. * Given a string representing a filename path and a new extension_string,
  608. * returns the path with the extension part replaced by the new extension.
  609. * The old filename must have an extension and the new extension cannot be
  610. * longer than the old one.
  611. */
  612. char *get_output_name ( char *input_path, char *extension_string )
  613. {
  614. size_t pathlen=0, i=0, old_ext_len=0;
  615. char *new_path;
  616. if (input_path == NULL || extension_string == NULL)
  617. return NULL;
  618. if ( (new_path = strdup(input_path) ) == NULL )
  619. return NULL; /* out of memory */
  620. pathlen = strlen(input_path);
  621. /* Identify the length of the old extension */
  622. for (i=pathlen; i > 0; i--) {
  623. if ( input_path[i] == '/' || input_path[i] == '\\' )
  624. break;
  625. if ( input_path[i] == '.' ) {
  626. old_ext_len = pathlen - i;
  627. break;
  628. }
  629. }
  630. if (old_ext_len < strlen (extension_string) )
  631. return NULL;
  632. memset (&new_path[pathlen - old_ext_len], 0, old_ext_len);
  633. strncpy (&new_path[pathlen - old_ext_len], extension_string, strlen(extension_string) );
  634. return new_path;
  635. /* !!! caller has to free the new string after using it !!! */
  636. }