docsis.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. struct tlv *global_tlvtree_head;
  42. symbol_type *global_symtable;
  43. static void setup_mib_flags(int resolve_oids, char *custom_mibs);
  44. static unsigned int
  45. add_cm_mic (unsigned char *tlvbuf, unsigned int tlvbuflen)
  46. {
  47. unsigned char digest[16];
  48. MD5_CTX mdContext;
  49. if (tlvbuf == NULL || tlvbuflen == 0)
  50. return 0;
  51. MD5_Init (&mdContext);
  52. MD5_Update (&mdContext, tlvbuf, tlvbuflen);
  53. MD5_Final (digest, &mdContext);
  54. tlvbuf[tlvbuflen] = 6;
  55. tlvbuf[tlvbuflen + 1] = 16;
  56. memcpy (tlvbuf + tlvbuflen + 2, digest, 16);
  57. return (tlvbuflen + 18); /* we added the CM Message Integrity Check */
  58. }
  59. static unsigned int
  60. add_eod_and_pad (unsigned char *tlvbuf, unsigned int tlvbuflen)
  61. {
  62. int nr_pads;
  63. if (tlvbuf == NULL || tlvbuflen == 0)
  64. return 0;
  65. tlvbuf[tlvbuflen] = 255;
  66. tlvbuflen = tlvbuflen + 1;
  67. nr_pads = (4 - (tlvbuflen % 4)) % 4;
  68. memset (&tlvbuf[tlvbuflen], 0, nr_pads);
  69. return (tlvbuflen + nr_pads);
  70. }
  71. static unsigned int
  72. add_cmts_mic (unsigned char *tlvbuf, unsigned int tlvbuflen,
  73. unsigned char *key, int keylen)
  74. {
  75. int i;
  76. register unsigned char *cp, *dp;
  77. unsigned char *cmts_tlvs;
  78. unsigned char digest[17];
  79. /* Only these configuration TLVs must be used to calculate the CMTS MIC */
  80. #define NR_CMTS_MIC_TLVS 21
  81. unsigned char digest_order[NR_CMTS_MIC_TLVS] =
  82. { 1, 2, 3, 4, 17, 43, 6, 18, 19, 20, 22, 23, 24, 25, 28, 29, 26, 35, 36, 37, 40 };
  83. if (tlvbuf == NULL || tlvbuflen == 0 )
  84. return 0;
  85. cmts_tlvs = (unsigned char *) malloc (tlvbuflen + 1); /* Plenty of space */
  86. dp = cmts_tlvs;
  87. for (i = 0; i < NR_CMTS_MIC_TLVS; i++)
  88. {
  89. cp = tlvbuf;
  90. while ((unsigned int) (cp - tlvbuf) < tlvbuflen)
  91. {
  92. if (cp[0] == digest_order[i])
  93. {
  94. memcpy (dp, cp, cp[1] + 2);
  95. dp = dp + cp[1] + 2;
  96. cp = cp + cp[1] + 2;
  97. }
  98. else
  99. {
  100. if ( cp[0] == 64 ) {
  101. cp = cp + (size_t) ntohs(*((unsigned short *)(cp+1))) + 3;
  102. } else {
  103. cp = cp + cp[1] + 2;
  104. }
  105. }
  106. }
  107. }
  108. fprintf (stderr, "##### Calculating CMTS MIC using TLVs:\n");
  109. decode_main_aggregate (cmts_tlvs, dp - cmts_tlvs);
  110. fprintf (stderr, "##### End of CMTS MIC TLVs\n");
  111. hmac_md5 (cmts_tlvs, dp - cmts_tlvs, key, keylen, digest);
  112. md5_print_digest (digest);
  113. tlvbuf[tlvbuflen] = 7; /* CMTS MIC */
  114. tlvbuf[tlvbuflen + 1] = 16; /* length of MD5 digest */
  115. memcpy (&tlvbuf[tlvbuflen + 2], digest, 16);
  116. free (cmts_tlvs);
  117. return (tlvbuflen + 18);
  118. }
  119. #ifdef __GNUC__
  120. static void usage () __attribute__((__noreturn__));
  121. #endif
  122. static void
  123. usage ()
  124. {
  125. fprintf(stderr, "DOCSIS Configuration File creator, version %s\n", VERSION);
  126. fprintf(stderr, "Copyright (c) 1999,2000,2001 Cornel Ciocirlan, ctrl@users.sourceforge.net\n");
  127. fprintf(stderr, "Copyright (c) 2002,2003,2004,2005 Evvolve Media SRL, docsis@evvolve.com\n");
  128. fprintf(stderr, "Copyright (c) 2014 - 2015 Adrian Simionov, daniel.simionov@gmail.com\n\n");
  129. fprintf(stderr, "To encode a cable modem configuration file: \n\t docsis -e <modem_cfg_file> <key_file> <output_file>\n");
  130. fprintf(stderr, "To encode multiple cable modem configuration files: \n\t docsis -m <modem_cfg_file1> ... <key_file> <new_extension>\n");
  131. fprintf(stderr, "To encode a MTA configuration file: \n\t docsis -p <mta_cfg_file> <output_file>\n");
  132. fprintf(stderr, "To encode multiple MTA configuration files: \n\t docsis -m -p <mta_file1> ... <new_extension>\n");
  133. fprintf(stderr, "To decode a CM or MTA config file: \n\t docsis -d <binary_file>\n");
  134. fprintf(stderr, "To decode a CM or MTA config file with OIDs: \n\t docsis -o -d <binary_file>\n");
  135. fprintf(stderr, "\nTo specify the MIBPATH encode or decode use: \n"
  136. "\tdocsis -M \"PATH1:PATH2\" -d <binary_file>\n"
  137. "\tdocsis -M \"PATH1:PATH2\" -e <modem_cfg_file> <key_file> <output_file>\n"
  138. "\tdocsis -M \"PATH1:PATH2\" -m <modem_cfg_file1> ... <key_file> <new_extension>\n"
  139. "\tdocsis -M \"PATH1:PATH2\" -p <mta_cfg_file> <output_file>\n"
  140. "\tdocsis -M \"PATH1:PATH2\" -m -p <mta_file1> ... <new_extension>\n");
  141. fprintf(stderr, "\nWhere:\n<cfg_file>\t\t= name of text (human readable) cable modem or MTA \n"
  142. "\t\t\t configuration file;\n"
  143. "<key_file>\t\t= text file containing the authentication key\n"
  144. "\t\t\t (shared secret) to be used for the CMTS MIC;\n"
  145. "<output_file> \t\t= name of output file where"
  146. " the binary data will\n\t\t\t be written to (if it does not exist it is created);\n"
  147. "<binary_file>\t\t= name of binary file to be decoded;\n"
  148. "<new_extension>\t\t= new extension to be used when encoding multiple files.\n");
  149. fprintf(stderr, "\nSee examples/*.cfg for configuration file format.\n");
  150. fprintf(stderr, "\nPlease report bugs or feature requests on GitHub.");
  151. fprintf(stderr, "\nProject repository is https://github.com/rlaager/docsis\n\n");
  152. exit (-10);
  153. }
  154. int
  155. main (int argc, char *argv[])
  156. {
  157. unsigned char key[65];
  158. FILE *kf;
  159. char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL, *custom_mibs=NULL;
  160. unsigned int keylen = 0;
  161. unsigned int encode_docsis = FALSE, decode_bin = FALSE;
  162. int i;
  163. int resolve_oids = 1;
  164. if (argc < 2 ) {
  165. usage();
  166. }
  167. if (!strcmp (argv[1], "-o") ){
  168. resolve_oids = 0;
  169. if (!strcmp (argv[2], "-d")) {
  170. decode_bin = TRUE;
  171. config_file = argv[3];
  172. } else {
  173. usage();
  174. }
  175. }else if (!strcmp (argv[1], "-m") ){ /* variable number of args, encoding multiple files */
  176. if (argc < 5 ) {
  177. usage();
  178. }
  179. extension_string = argv[argc-1];
  180. if (!strcmp ( argv[2], "-p")) {
  181. key_file = NULL;
  182. } else {
  183. key_file = argv[argc-2];
  184. encode_docsis = TRUE;
  185. }
  186. } else if (!strcmp (argv[1], "-M") ){ /* define custom MIBDIRS */
  187. if (argc < 4 ) {
  188. usage();
  189. }
  190. custom_mibs=argv[2];
  191. if (!strcmp (argv[3], "-d")) {
  192. decode_bin = TRUE;
  193. config_file = argv[4];
  194. } else if (!strcmp (argv[3], "-m")) {
  195. if (argc < 5 ) {
  196. usage();
  197. }
  198. extension_string = argv[argc-1];
  199. if (!strcmp ( argv[4], "-p")) {
  200. key_file = NULL;
  201. } else {
  202. key_file = argv[argc-2];
  203. encode_docsis = TRUE;
  204. }
  205. } else if (!strcmp (argv[3], "-p")) {
  206. config_file = argv[4];
  207. output_file = argv[5];
  208. } else if (!strcmp (argv[3], "-e")) {
  209. encode_docsis = TRUE;
  210. config_file = argv[4];
  211. key_file = argv[5];
  212. output_file = argv[6];
  213. } else {
  214. usage();
  215. }
  216. } else {
  217. switch (argc)
  218. {
  219. case 3:
  220. if (strcmp (argv[1], "-d"))
  221. usage ();
  222. decode_bin = TRUE;
  223. config_file = argv[2];
  224. break;
  225. ;;
  226. case 4:
  227. if (strcmp (argv[1], "-p"))
  228. usage ();
  229. config_file = argv[2];
  230. output_file = argv[3];
  231. break;
  232. ;;
  233. case 5:
  234. if (strcmp (argv[1], "-e"))
  235. usage ();
  236. encode_docsis = TRUE;
  237. config_file = argv[2];
  238. key_file = argv[3];
  239. output_file = argv[4];
  240. break;
  241. ;;
  242. default:
  243. usage ();
  244. }
  245. }
  246. if (encode_docsis)
  247. {
  248. if ((kf = fopen (key_file, "r")) == NULL)
  249. {
  250. fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file);
  251. exit (-5);
  252. }
  253. keylen = fread (key, sizeof (unsigned char), 64, kf);
  254. while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13))
  255. {
  256. keylen--; /* eliminate trailing \n or \r */
  257. }
  258. }
  259. init_global_symtable ();
  260. setup_mib_flags(resolve_oids,custom_mibs);
  261. if (decode_bin)
  262. {
  263. decode_file (config_file);
  264. exit(0); // TODO: clean shutdown
  265. }
  266. if (extension_string) { /* encoding multiple files */
  267. if (encode_docsis) {
  268. /* encode argv[argc-3] to argv[2] */
  269. for (i=2; i<argc-2; i++) {
  270. if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
  271. fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
  272. continue;
  273. }
  274. fprintf(stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
  275. if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) {
  276. exit(2);
  277. }
  278. free (output_file);
  279. output_file = NULL;
  280. }
  281. } else {
  282. /* encode argv[argc-2] to argv[3] */
  283. for (i=3; i<argc-1; i++) {
  284. if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
  285. fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
  286. continue;
  287. }
  288. fprintf (stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
  289. if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) {
  290. exit(2);
  291. }
  292. free (output_file);
  293. output_file = NULL;
  294. }
  295. }
  296. } else {
  297. if (encode_one_file (config_file, output_file, key, keylen, encode_docsis)) {
  298. exit(2);
  299. }
  300. /* encode argv[1] */
  301. }
  302. free(global_symtable);
  303. shutdown_mib();
  304. return 0;
  305. }
  306. int encode_one_file ( char *input_file, char *output_file,
  307. unsigned char *key, unsigned int keylen, int encode_docsis )
  308. {
  309. int parse_result=0;
  310. unsigned int buflen;
  311. unsigned char *buffer;
  312. FILE *of;
  313. /* It's not an error to specify the input and output as "-". */
  314. if (!strcmp (input_file, output_file) && strcmp (input_file, "-"))
  315. {
  316. fprintf(stderr, "docsis: Error: source file is the same as destination file\n");
  317. return -1;
  318. }
  319. parse_result = parse_config_file (input_file, &global_tlvtree_head );
  320. if (parse_result || global_tlvtree_head == NULL)
  321. {
  322. fprintf(stderr, "Error parsing config file %s\n", input_file);
  323. return -1;
  324. }
  325. /* Check whether we're encoding PacketCable */
  326. if (global_tlvtree_head->docs_code == 254) {
  327. fprintf(stderr, "First TLV is MtaConfigDelimiter, forcing PacketCable MTA file.\n");
  328. encode_docsis=0;
  329. }
  330. /* walk the tree to find out how much memory we need */
  331. /* leave some room for CM MIC, CMTS MIC, pad */
  332. buflen = tlvtreelen (global_tlvtree_head);
  333. buffer = (unsigned char *) malloc ( buflen + 255 );
  334. buflen = flatten_tlvsubtree(buffer, 0, global_tlvtree_head);
  335. #ifdef DEBUG
  336. fprintf(stderr, "TLVs found in parsed config file:\n");
  337. decode_main_aggregate (buffer, buflen);
  338. #endif
  339. if (encode_docsis)
  340. {
  341. /* CM config file => add CM MIC, CMTS MIC, End-of-Data and pad */
  342. buflen = add_cm_mic (buffer, buflen);
  343. buflen = add_cmts_mic (buffer, buflen, key, keylen);
  344. buflen = add_eod_and_pad (buffer, buflen);
  345. }
  346. fprintf (stderr, "Final content of config file:\n");
  347. decode_main_aggregate (buffer, buflen);
  348. if (!strcmp (output_file, "-"))
  349. {
  350. of = stdout;
  351. }
  352. else if ((of = fopen (output_file, "wb")) == NULL)
  353. {
  354. fprintf (stderr, "docsis: error: can't open output file %s\n", output_file);
  355. return -2;
  356. }
  357. fwrite (buffer, sizeof (unsigned char), buflen, of);
  358. fclose (of);
  359. free(buffer);
  360. return 0;
  361. /*free(global_tlvlist->tlvlist); free(global_tlvlist); */ /* TODO free tree */
  362. }
  363. int
  364. init_global_symtable (void)
  365. {
  366. global_symtable =
  367. (symbol_type *) malloc (sizeof (symbol_type) * NUM_IDENTIFIERS);
  368. if (global_symtable == NULL)
  369. {
  370. fprintf(stderr, "Error allocating memory\n");
  371. exit (255);
  372. }
  373. memcpy (global_symtable, symtable, sizeof (symbol_type) * NUM_IDENTIFIERS);
  374. return 1;
  375. }
  376. void
  377. decode_file (char *file)
  378. {
  379. int ifd;
  380. unsigned char *buffer;
  381. unsigned int buflen = 0;
  382. int rv = 0;
  383. struct stat st;
  384. if ((ifd = open (file, O_RDONLY)) == -1)
  385. {
  386. fprintf(stderr, "Error opening binary file %s: %s\n", file, strerror (errno));
  387. exit (-1);
  388. }
  389. if ((rv = fstat (ifd, &st)))
  390. {
  391. fprintf(stderr, "Can't stat file %s: %s\n", file, strerror (errno));
  392. exit (-1);
  393. }
  394. buffer = (unsigned char *) malloc (st.st_size * sizeof (unsigned char) + 1);
  395. buflen = read (ifd, buffer, st.st_size);
  396. decode_main_aggregate (buffer, buflen);
  397. free(buffer);
  398. }
  399. static void
  400. setup_mib_flags(int resolve_oids, char *custom_mibs) {
  401. #ifdef DEBUG
  402. /* snmp_set_mib_warnings (2); */
  403. #endif /* DEBUG */
  404. /* We do not want warning for normal users. Should be set with an argument on the CLI maybe?
  405. * snmp_set_mib_warnings (1); */
  406. if (custom_mibs)
  407. {
  408. setenv ("MIBDIRS", custom_mibs, 1);
  409. }
  410. if (resolve_oids)
  411. {
  412. setenv ("MIBS", "ALL", 1);
  413. }
  414. #ifdef HAVE_NETSNMP_INIT_MIB
  415. netsnmp_init_mib ();
  416. #else
  417. init_mib ();
  418. #endif
  419. if (!netsnmp_ds_get_boolean
  420. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS))
  421. {
  422. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  423. NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
  424. } /* we want OIDs to appear in numeric form */
  425. if (!netsnmp_ds_get_boolean
  426. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM))
  427. {
  428. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  429. NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
  430. } /* we want enums to appear in numeric form as integers */
  431. if (!netsnmp_ds_get_boolean
  432. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID))
  433. {
  434. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  435. NETSNMP_DS_LIB_PRINT_FULL_OID);
  436. } /* we want to full numeric OID to be printed, including prefix */
  437. if (!netsnmp_ds_get_boolean
  438. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS))
  439. {
  440. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  441. NETSNMP_DS_LIB_DONT_PRINT_UNITS);
  442. }
  443. if (!netsnmp_ds_get_boolean
  444. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS))
  445. {
  446. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  447. NETSNMP_DS_LIB_RANDOM_ACCESS);
  448. } /* so we can use sysContact.0 instead of system.sysContact.0 */
  449. if (!netsnmp_ds_get_boolean
  450. (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS))
  451. {
  452. netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
  453. NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
  454. } /* so we can use sysContact.0 instead of system.sysContact.0 */
  455. }
  456. /*
  457. * Given a string representing a filename path and a new extension_string,
  458. * returns the path with the extension part replaced by the new extension.
  459. * The old filename must have an extension and the new extension cannot be
  460. * longer than the old one.
  461. */
  462. char *get_output_name ( char *input_path, char *extension_string )
  463. {
  464. size_t pathlen=0, i=0, old_ext_len=0;
  465. char *new_path;
  466. if (input_path == NULL || extension_string == NULL)
  467. return NULL;
  468. if ( (new_path = strdup(input_path) ) == NULL )
  469. return NULL; /* out of memory */
  470. pathlen = strlen(input_path);
  471. /* Identify the length of the old extension */
  472. for (i=pathlen; i > 0; i--) {
  473. if ( input_path[i] == '/' || input_path[i] == '\\' )
  474. break;
  475. if ( input_path[i] == '.' ) {
  476. old_ext_len = pathlen - i;
  477. break;
  478. }
  479. }
  480. if (old_ext_len < strlen (extension_string) )
  481. return NULL;
  482. memset (&new_path[pathlen - old_ext_len], 0, old_ext_len);
  483. strncpy (&new_path[pathlen - old_ext_len], extension_string, strlen(extension_string) );
  484. return new_path;
  485. /* !!! caller has to free the new string after using it !!! */
  486. }