docsis.c 14 KB

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