docsis.c 16 KB

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