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
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
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
36 #undef PACKAGE_BUGREPORT
37 #undef PACKAGE_NAME
38 #undef PACKAGE_STRING
39 #undef PACKAGE_TARNAME
40 #undef PACKAGE_VERSION
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif /* HAVE_CONFIG_H */
45
46 #include "docsis.h"
47 #include "docsis_globals.h"
48 #include "docsis_symtable.h"
49 #include "ethermac.h"
50 #include "md5.h"
51 #include <openssl/sha.h>
52
53 struct tlv *global_tlvtree_head;
54 symbol_type *global_symtable;
55 unsigned int nohash = 0;
56 unsigned int dialplan = 0;
57
58 static void setup_mib_flags(int resolve_oids, char *custom_mibs);
59
60 static unsigned int
add_cm_mic(unsigned char * tlvbuf,unsigned int tlvbuflen)61 add_cm_mic (unsigned char *tlvbuf, unsigned int tlvbuflen)
62 {
63 unsigned char digest[16];
64 MD5_CTX mdContext;
65
66 if (tlvbuf == NULL || tlvbuflen == 0)
67 return 0;
68
69 MD5_Init (&mdContext);
70 MD5_Update (&mdContext, tlvbuf, tlvbuflen);
71 MD5_Final (digest, &mdContext);
72 tlvbuf[tlvbuflen] = 6;
73 tlvbuf[tlvbuflen + 1] = 16;
74 memcpy (tlvbuf + tlvbuflen + 2, digest, 16);
75 return (tlvbuflen + 18); /* we added the CM Message Integrity Check */
76 }
77
78 static unsigned int
add_eod_and_pad(unsigned char * tlvbuf,unsigned int tlvbuflen)79 add_eod_and_pad (unsigned char *tlvbuf, unsigned int tlvbuflen)
80 {
81 int nr_pads;
82
83 if (tlvbuf == NULL || tlvbuflen == 0)
84 return 0;
85
86 tlvbuf[tlvbuflen] = 255;
87 tlvbuflen = tlvbuflen + 1;
88 nr_pads = (4 - (tlvbuflen % 4)) % 4;
89 memset (&tlvbuf[tlvbuflen], 0, nr_pads);
90 return (tlvbuflen + nr_pads);
91 }
92
93 static unsigned int
add_cmts_mic(unsigned char * tlvbuf,unsigned int tlvbuflen,unsigned char * key,int keylen)94 add_cmts_mic (unsigned char *tlvbuf, unsigned int tlvbuflen,
95 unsigned char *key, int keylen)
96 {
97
98 int i;
99 register unsigned char *cp, *dp;
100 unsigned char *cmts_tlvs;
101 unsigned char digest[17];
102
103 /* Only these configuration TLVs must be used to calculate the CMTS MIC */
104 #define NR_CMTS_MIC_TLVS 21
105 unsigned char digest_order[NR_CMTS_MIC_TLVS] =
106 { 1, 2, 3, 4, 17, 43, 6, 18, 19, 20, 22, 23, 24, 25, 28, 29, 26, 35, 36, 37, 40 };
107
108 if (tlvbuf == NULL || tlvbuflen == 0 )
109 return 0;
110
111 cmts_tlvs = (unsigned char *) malloc (tlvbuflen + 1); /* Plenty of space */
112 dp = cmts_tlvs;
113 for (i = 0; i < NR_CMTS_MIC_TLVS; i++)
114 {
115 cp = tlvbuf;
116 while ((unsigned int) (cp - tlvbuf) < tlvbuflen)
117 {
118 if (cp[0] == digest_order[i])
119 {
120 memcpy (dp, cp, cp[1] + 2);
121 dp = dp + cp[1] + 2;
122 cp = cp + cp[1] + 2;
123 }
124 else
125 {
126 if ( cp[0] == 64 ) {
127 cp = cp + (size_t) ntohs(*((unsigned short *)(cp+1))) + 3;
128 } else {
129 cp = cp + cp[1] + 2;
130 }
131 }
132 }
133 }
134 fprintf (stderr, "##### Calculating CMTS MIC using TLVs:\n");
135 decode_main_aggregate (cmts_tlvs, dp - cmts_tlvs);
136 fprintf (stderr, "##### End of CMTS MIC TLVs\n");
137 hmac_md5 (cmts_tlvs, dp - cmts_tlvs, key, keylen, digest);
138 md5_print_digest (digest);
139 tlvbuf[tlvbuflen] = 7; /* CMTS MIC */
140 tlvbuf[tlvbuflen + 1] = 16; /* length of MD5 digest */
141 memcpy (&tlvbuf[tlvbuflen + 2], digest, 16);
142 free (cmts_tlvs);
143 return (tlvbuflen + 18);
144 }
145
146 static unsigned int
add_mta_hash(unsigned char * tlvbuf,unsigned int tlvbuflen,unsigned int hash)147 add_mta_hash (unsigned char *tlvbuf, unsigned int tlvbuflen, unsigned int hash) {
148 SHA_CTX shactx;
149 unsigned char hash_value[SHA_DIGEST_LENGTH];
150
151 SHA1_Init(&shactx);
152 SHA1_Update(&shactx, tlvbuf, tlvbuflen);
153 SHA1_Final(hash_value, &shactx);
154
155 if (hash == 1) {
156 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);
157 tlvbuflen += 19;
158 }
159 if (hash == 2) {
160 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);
161 tlvbuflen += 17;
162 }
163
164 memcpy (tlvbuf + tlvbuflen, hash_value, SHA_DIGEST_LENGTH);
165 tlvbuflen += SHA_DIGEST_LENGTH;
166 memcpy (tlvbuf + tlvbuflen, "\xfe\x01\xff", 3);
167 tlvbuflen += 3;
168
169 return (tlvbuflen);
170 }
171
172 static unsigned int
add_dialplan(unsigned char * tlvbuf,unsigned int tlvbuflen)173 add_dialplan (unsigned char *tlvbuf, unsigned int tlvbuflen) {
174 FILE *dialplan_file;
175 char *dialplan_buffer;
176 unsigned int fileSize;
177 unsigned short local_v_len;
178 unsigned short *p_local_v_len = &local_v_len;
179 unsigned char local_char;
180 unsigned char *p_local_char = &local_char;
181
182 dialplan_file = fopen("dialplan.txt", "rb");
183 if (!dialplan_file) {
184 fprintf(stderr, "Cannot open dialplan.txt file, fatal error, closing.\n");
185 exit(-1);
186 }
187 fseek(dialplan_file, 0, SEEK_END);
188 fileSize = ftell (dialplan_file);
189 fseek(dialplan_file, 0, SEEK_SET);
190 dialplan_buffer = malloc(fileSize);
191 if (!dialplan_buffer) {
192 fprintf(stderr, "Fatal error allocating memory for dialplan buffer, closing.\n");
193 exit(-1);
194 }
195 fread(dialplan_buffer, fileSize, 1, dialplan_file);
196 fclose(dialplan_file);
197
198 tlvbuflen -= 3;
199 memcpy(tlvbuf + tlvbuflen, "\x40", 1);
200 tlvbuflen += 1;
201 if (fileSize > 0x7f) {
202 local_v_len = htons(2 + 2 + 20 + 2 + 2 + fileSize);
203 } else if (fileSize > 0x69) {
204 local_v_len = htons(2 + 2 + 20 + 1 + 1 + fileSize);
205 } else {
206 local_v_len = htons(1 + 1 + 20 + 1 + 1 + fileSize);
207 }
208 memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
209 tlvbuflen += sizeof(local_v_len);
210 memcpy(tlvbuf + tlvbuflen, "\x30", 1);
211 tlvbuflen += 1;
212 local_char = 0x16 + fileSize;
213 if (local_char < 0x80) {
214 memcpy(tlvbuf + tlvbuflen, p_local_char, sizeof(local_char));
215 tlvbuflen += sizeof(local_char);
216 } else {
217 memcpy(tlvbuf + tlvbuflen, "\x82", 1);
218 tlvbuflen += 1;
219 if (fileSize > 0x7f) {
220 local_v_len = htons(20 + 2 + 2 + fileSize);
221 } else {
222 local_v_len = htons(20 + 1 + 1 + fileSize);
223 }
224 memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
225 tlvbuflen += sizeof(local_v_len);
226 }
227 memcpy(tlvbuf + tlvbuflen, "\x06\x12\x2b\x06\x01\x04\x01\xa3\x0b\x02\x02\x08\x02\x01\x01\x03\x01\x01\x02\x00", 20);
228 tlvbuflen += 20;
229 memcpy(tlvbuf + tlvbuflen, "\x04", 1);
230 tlvbuflen += 1;
231
232 if (fileSize > 0x7f) {
233 memcpy(tlvbuf + tlvbuflen, "\x82", 1);
234 tlvbuflen += 1;
235 local_v_len = (unsigned short) htons(fileSize);
236 memcpy(tlvbuf + tlvbuflen, p_local_v_len, sizeof(local_v_len));
237 tlvbuflen += sizeof(local_v_len);
238 } else {
239 local_char = (char) fileSize;
240 memcpy(tlvbuf + tlvbuflen, p_local_char, sizeof(local_char));
241 tlvbuflen += sizeof(local_char);
242 }
243
244 memcpy(tlvbuf + tlvbuflen, dialplan_buffer, fileSize);
245 tlvbuflen = tlvbuflen + fileSize;
246
247 memcpy (tlvbuf + tlvbuflen, "\xfe\x01\xff", 3);
248 tlvbuflen += 3;
249 return (tlvbuflen);
250 }
251
252 #ifdef __GNUC__
253 static void usage () __attribute__((__noreturn__));
254 #endif
255
256 static void
usage()257 usage ()
258 {
259 fprintf(stderr, "DOCSIS Configuration File creator, version %s\n", VERSION);
260 fprintf(stderr, "Copyright (c) 1999,2000,2001 Cornel Ciocirlan, ctrl@users.sourceforge.net\n");
261 fprintf(stderr, "Copyright (c) 2002,2003,2004,2005 Evvolve Media SRL, docsis@evvolve.com\n");
262 fprintf(stderr, "Copyright (c) 2014 - 2015 Adrian Simionov, daniel.simionov@gmail.com\n\n");
263
264 fprintf(stderr, "To encode a cable modem configuration file: \n\tdocsis -e <modem_cfg_file> <key_file> <output_file>\n");
265 fprintf(stderr, "To encode multiple cable modem configuration files: \n\tdocsis -m <modem_cfg_file1> ... <key_file> <new_extension>\n");
266 fprintf(stderr, "To encode a MTA configuration file: \n\tdocsis -p <mta_cfg_file> <output_file>\n");
267 fprintf(stderr, "To encode multiple MTA configuration files: \n\tdocsis -m -p <mta_file1> ... <new_extension>\n");
268 fprintf(stderr, "To decode a CM or MTA config file: \n\tdocsis -d <binary_file>\n");
269 fprintf(stderr, "To decode a CM or MTA config file with OIDs: \n\tdocsis -o -d <binary_file>\n");
270 fprintf(stderr, "\nTo specify the MIBPATH encode or decode use:\n"
271 "\tdocsis -M \"PATH1:PATH2\" -d <binary_file>\n"
272 "\tdocsis -M \"PATH1:PATH2\" -e <modem_cfg_file> <key_file> <output_file>\n"
273 "\tdocsis -M \"PATH1:PATH2\" -m <modem_cfg_file1> ... <key_file> <new_extension>\n"
274 "\tdocsis -M \"PATH1:PATH2\" -p <mta_cfg_file> <output_file>\n"
275 "\tdocsis -M \"PATH1:PATH2\" -m -p <mta_file1> ... <new_extension>\n");
276 fprintf(stderr, "\nTo add SHA1 hash to mta config file, use -na or -eu options:\n");
277 fprintf(stderr, "\tdocsis -na|-eu -p <mta_cfg_file> <output_file>\n");
278 fprintf(stderr, "\tdocsis -na|-eu -m -p <mta_file1> ... <new_extension>\n");
279 fprintf(stderr, "\nTo add PC20 dialplan from external dialplan.txt file, use -dialplan option:\n");
280 fprintf(stderr, "\tdocsis -p -dialplan <mta_cfg_file> <output_file>\n");
281 fprintf(stderr, "\tdocsis -na|-eu -p -dialplan <mta_cfg_file> <output_file>\n");
282 fprintf(stderr, "\nTo remove hash from MTA config file, use -nohash option:\n");
283 fprintf(stderr, "\tdocsis -nohash -d <mta_cfg_file>\n");
284 fprintf(stderr, "\tdocsis -nohash -o -d <mta_cfg_file>\n");
285 fprintf(stderr, "\nWhere:\n<cfg_file>\t\t= name of text (human readable) cable modem or MTA \n"
286 "\t\t\t configuration file;\n"
287 "<key_file>\t\t= text file containing the authentication key\n"
288 "\t\t\t (shared secret) to be used for the CMTS MIC;\n"
289 "<output_file> \t\t= name of output file where"
290 " the binary data will\n\t\t\t be written to (if it does not exist it is created);\n"
291 "<binary_file>\t\t= name of binary file to be decoded;\n"
292 "<new_extension>\t\t= new extension to be used when encoding multiple files.\n");
293 fprintf(stderr, "\nSee examples/*.cfg for configuration file format.\n");
294 fprintf(stderr, "\nPlease report bugs or feature requests on GitHub.");
295 fprintf(stderr, "\nProject repository is https://github.com/rlaager/docsis\n\n");
296 exit (-10);
297 }
298
299 int
main(int argc,char * argv[])300 main (int argc, char *argv[])
301 {
302 unsigned char key[65];
303 FILE *kf;
304 char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL, *custom_mibs=NULL;
305 unsigned int keylen = 0;
306 unsigned int encode_docsis = FALSE, decode_bin = FALSE, hash = 0;
307 int i;
308 int resolve_oids = 1;
309 if (argc < 2 ) {
310 usage();
311 }
312
313 /* option: -nohash -o -d */
314 if (!strcmp (argv[1], "-nohash") ){
315 if (argc < 5) {
316 usage();
317 }
318 nohash = 1;
319 if (!strcmp (argv[2], "-o") ){
320 resolve_oids = 0;
321 if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC)) {
322 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC);
323 }
324 if (!strcmp (argv[3], "-d")) {
325 decode_bin = TRUE;
326 config_file = argv[4];
327 } else {
328 usage();
329 }
330 } else if (!strcmp (argv[2], "-d")) {
331 decode_bin = TRUE;
332 config_file = argv[3];
333 } else {
334 usage();
335 }
336 /* option -o -d */
337 } else if (!strcmp (argv[1], "-o") ) {
338 if (argc < 4 ) {
339 usage();
340 }
341 resolve_oids = 0;
342 if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC)) {
343 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC);
344 }
345 if (!strcmp (argv[2], "-d")) {
346 decode_bin = TRUE;
347 config_file = argv[3];
348 } else {
349 usage();
350 }
351 /* option -m -p */
352 } else if (!strcmp (argv[1], "-m") ) {
353 if (argc < 5 ) {
354 usage();
355 }
356 extension_string = argv[argc-1];
357 if (!strcmp ( argv[2], "-p")) {
358 key_file = NULL;
359 } else {
360 key_file = argv[argc-2];
361 encode_docsis = TRUE;
362 }
363 /* option -na -m -p*/
364 } else if (!strcmp (argv[1], "-na")) {
365 hash = 1;
366 if (!strcmp (argv[2], "-m") ) {
367 if (argc < 6) {
368 usage();
369 }
370 extension_string = argv[argc-1];
371 if (!strcmp ( argv[3], "-p")) {
372 key_file = NULL;
373 } else {
374 key_file = argv[argc-2];
375 encode_docsis = TRUE;
376 }
377 /* option -na -p */
378 } else if (!strcmp ( argv[2], "-p" )) {
379 if (argc < 5) {
380 usage ();
381 }
382 config_file = argv[3];
383 output_file = argv[4];
384 if (!strcmp ( argv[3], "-dialplan")) {
385 dialplan = 1;
386 config_file = argv[4];
387 output_file = argv[5];
388 }
389 }
390 /* option -eu -m -p*/
391 } else if (!strcmp (argv[1], "-eu")) {
392 hash = 2;
393 if (!strcmp (argv[2], "-m") ) {
394 if (argc < 6) {
395 usage();
396 }
397 extension_string = argv[argc-1];
398 if (!strcmp ( argv[3], "-p")) {
399 key_file = NULL;
400 } else {
401 key_file = argv[argc-2];
402 encode_docsis = TRUE;
403 }
404 /* option -eu -p */
405 } else if (!strcmp ( argv[2], "-p" )) {
406 if (argc < 5) {
407 usage ();
408 }
409 config_file = argv[3];
410 output_file = argv[4];
411 if (!strcmp ( argv[3], "-dialplan")) {
412 dialplan = 1;
413 config_file = argv[4];
414 output_file = argv[5];
415 }
416 }
417 /* option -M */
418 } else if (!strcmp (argv[1], "-M") ) {
419 if (argc < 4 ) {
420 usage();
421 }
422 custom_mibs=argv[2];
423 /* option -M -d */
424 if (!strcmp (argv[3], "-d")) {
425 decode_bin = TRUE;
426 config_file = argv[4];
427 /* option -M -m */
428 } else if (!strcmp (argv[3], "-m")) {
429 if (argc < 5 ) {
430 usage();
431 }
432 extension_string = argv[argc-1];
433 /* option -M -m -p */
434 if (!strcmp ( argv[4], "-p")) {
435 key_file = NULL;
436 } else {
437 key_file = argv[argc-2];
438 encode_docsis = TRUE;
439 }
440 /* option -M -p */
441 } else if (!strcmp (argv[3], "-p")) {
442 if (argc < 6) {
443 usage();
444 }
445 config_file = argv[4];
446 output_file = argv[5];
447 /* option -M -e */
448 } else if (!strcmp (argv[3], "-e")) {
449 encode_docsis = TRUE;
450 config_file = argv[4];
451 key_file = argv[5];
452 output_file = argv[6];
453 } else {
454 usage();
455 }
456 /* option -p */
457 } else if (!strcmp (argv[1], "-p") ) {
458 if (argc < 4) {
459 usage();
460 }
461 config_file = argv[2];
462 output_file = argv[3];
463 if (!strcmp (argv[2], "-dialplan")) {
464 if (argc < 5) {
465 usage();
466 } else {
467 dialplan = 1;
468 config_file = argv[3];
469 output_file = argv[4];
470 }
471 }
472 /* option -d */
473 } else if (!strcmp (argv[1], "-d") ) {
474 if (argc < 3 ) {
475 usage();
476 }
477 decode_bin = TRUE;
478 config_file = argv[2];
479 /* option -e */
480 } else if (!strcmp (argv[1], "-e") ) {
481 if (argc < 5 ) {
482 usage();
483 }
484 encode_docsis = TRUE;
485 config_file = argv[2];
486 key_file = argv[3];
487 output_file = argv[4];
488 } else {
489 usage ();
490 }
491
492 if (encode_docsis)
493 {
494 if ((kf = fopen (key_file, "r")) == NULL)
495 {
496 fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file);
497 exit (-5);
498 }
499 keylen = fread (key, sizeof (unsigned char), 64, kf);
500 while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13))
501 {
502 keylen--; /* eliminate trailing \n or \r */
503 }
504 }
505
506 init_global_symtable ();
507 setup_mib_flags(resolve_oids,custom_mibs);
508
509 if (decode_bin)
510 {
511 decode_file (config_file);
512 exit(0); // TODO: clean shutdown
513 }
514
515 if (extension_string) { /* encoding multiple files */
516 if (encode_docsis) {
517 /* encode argv[argc-3] to argv[2] */
518 for (i=2; i<argc-2; i++) {
519 if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
520 fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
521 continue;
522 }
523
524 fprintf(stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
525 if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
526 exit(2);
527 }
528 free (output_file);
529 output_file = NULL;
530 }
531 } else {
532 /* encode argv[argc-2] to argv[3] */
533 for (i=3; i<argc-1; i++) {
534 if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
535 fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
536 continue;
537 }
538 fprintf (stderr, "Processing input file %s: output to %s\n",argv[i], output_file);
539 if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
540 exit(2);
541 }
542 free (output_file);
543 output_file = NULL;
544 }
545 }
546 } else {
547 if (encode_one_file (config_file, output_file, key, keylen, encode_docsis, hash)) {
548 exit(2);
549 }
550 /* encode argv[1] */
551 }
552 free(global_symtable);
553 shutdown_mib();
554 return 0;
555 }
556
encode_one_file(char * input_file,char * output_file,unsigned char * key,unsigned int keylen,int encode_docsis,unsigned int hash)557 int encode_one_file ( char *input_file, char *output_file,
558 unsigned char *key, unsigned int keylen, int encode_docsis, unsigned int hash)
559 {
560 int parse_result=0;
561 unsigned int buflen;
562 unsigned char *buffer;
563 FILE *of;
564
565 /* It's not an error to specify the input and output as "-". */
566 if (!strcmp (input_file, output_file) && strcmp (input_file, "-"))
567 {
568 fprintf(stderr, "docsis: Error: source file is the same as destination file\n");
569 return -1;
570 }
571
572 parse_result = parse_config_file (input_file, &global_tlvtree_head );
573
574 if (parse_result || global_tlvtree_head == NULL)
575 {
576 fprintf(stderr, "Error parsing config file %s\n", input_file);
577 return -1;
578 }
579 /* Check whether we're encoding PacketCable */
580
581 if (global_tlvtree_head->docs_code == 254) {
582 fprintf(stderr, "First TLV is MtaConfigDelimiter, forcing PacketCable MTA file.\n");
583 encode_docsis=0;
584 }
585
586 /* walk the tree to find out how much memory we need */
587 /* leave some room for CM MIC, CMTS MIC, pad, and a HUGE PC20 dialplan */
588 buflen = tlvtreelen (global_tlvtree_head);
589 buffer = (unsigned char *) malloc ( buflen + 255 + 8192 );
590 buflen = flatten_tlvsubtree(buffer, 0, global_tlvtree_head);
591
592
593 #ifdef DEBUG
594 fprintf(stderr, "TLVs found in parsed config file:\n");
595 decode_main_aggregate (buffer, buflen);
596 #endif
597
598 if (encode_docsis)
599 {
600 /* CM config file => add CM MIC, CMTS MIC, End-of-Data and pad */
601 buflen = add_cm_mic (buffer, buflen);
602 buflen = add_cmts_mic (buffer, buflen, key, keylen);
603 buflen = add_eod_and_pad (buffer, buflen);
604 }
605
606 if (dialplan == 1) {
607 printf("Adding PC20 dialplan from external file.\n");
608 buflen = add_dialplan (buffer, buflen);
609 }
610
611 if (hash == 1) {
612 printf("Adding NA ConfigHash to MTA file.\n");
613 buflen = add_mta_hash (buffer, buflen, hash);
614 }
615 if (hash == 2) {
616 printf("Adding EU ConfigHash to MTA file.\n");
617 buflen = add_mta_hash (buffer, buflen, hash);
618 }
619
620 fprintf (stderr, "Final content of config file:\n");
621
622 decode_main_aggregate (buffer, buflen);
623 if (!strcmp (output_file, "-"))
624 {
625 of = stdout;
626 }
627 else if ((of = fopen (output_file, "wb")) == NULL)
628 {
629 fprintf (stderr, "docsis: error: can't open output file %s\n", output_file);
630 return -2;
631 }
632 fwrite (buffer, sizeof (unsigned char), buflen, of);
633 fclose (of);
634 free(buffer);
635 return 0;
636
637 /*free(global_tlvlist->tlvlist); free(global_tlvlist); */ /* TODO free tree */
638 }
639
640 int
init_global_symtable(void)641 init_global_symtable (void)
642 {
643 global_symtable =
644 (symbol_type *) malloc (sizeof (symbol_type) * NUM_IDENTIFIERS);
645 if (global_symtable == NULL)
646 {
647 fprintf(stderr, "Error allocating memory\n");
648 exit (255);
649 }
650 memcpy (global_symtable, symtable, sizeof (symbol_type) * NUM_IDENTIFIERS);
651 return 1;
652 }
653
654 void
decode_file(char * file)655 decode_file (char *file)
656 {
657 int ifd;
658 unsigned char *buffer;
659 unsigned int buflen = 0;
660 int rv = 0;
661 struct stat st;
662 if ((ifd = open (file, O_RDONLY)) == -1)
663 {
664 fprintf(stderr, "Error opening binary file %s: %s\n", file, strerror (errno));
665 exit (-1);
666 }
667 if ((rv = fstat (ifd, &st)))
668 {
669 fprintf(stderr, "Can't stat file %s: %s\n", file, strerror (errno));
670 exit (-1);
671 }
672 buffer = (unsigned char *) malloc (st.st_size * sizeof (unsigned char) + 1);
673 buflen = read (ifd, buffer, st.st_size);
674 decode_main_aggregate (buffer, buflen);
675 free(buffer);
676 }
677
678
679 static void
setup_mib_flags(int resolve_oids,char * custom_mibs)680 setup_mib_flags(int resolve_oids, char *custom_mibs) {
681
682 #ifdef DEBUG
683 /* snmp_set_mib_warnings (2); */
684 #endif /* DEBUG */
685 /* We do not want warning for normal users. Should be set with an argument on the CLI maybe?
686 * snmp_set_mib_warnings (1); */
687
688 if (custom_mibs)
689 {
690 setenv ("MIBDIRS", custom_mibs, 1);
691 }
692
693 if (resolve_oids)
694 {
695 setenv ("MIBS", "ALL", 1);
696 }
697
698 #ifdef HAVE_NETSNMP_INIT_MIB
699 netsnmp_init_mib ();
700 #else
701 init_mib ();
702 #endif
703
704 if (!netsnmp_ds_get_boolean
705 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS))
706 {
707 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
708 NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
709 } /* we want OIDs to appear in numeric form */
710 if (!netsnmp_ds_get_boolean
711 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM))
712 {
713 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
714 NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
715 } /* we want enums to appear in numeric form as integers */
716 if (!netsnmp_ds_get_boolean
717 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID))
718 {
719 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
720 NETSNMP_DS_LIB_PRINT_FULL_OID);
721 } /* we want to full numeric OID to be printed, including prefix */
722 if (!netsnmp_ds_get_boolean
723 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS))
724 {
725 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
726 NETSNMP_DS_LIB_DONT_PRINT_UNITS);
727 }
728 if (!netsnmp_ds_get_boolean
729 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS))
730 {
731 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
732 NETSNMP_DS_LIB_RANDOM_ACCESS);
733 } /* so we can use sysContact.0 instead of system.sysContact.0 */
734 if (!netsnmp_ds_get_boolean
735 (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS))
736 {
737 netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
738 NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
739 } /* so we can use sysContact.0 instead of system.sysContact.0 */
740 }
741
742
743 /*
744 * Given a string representing a filename path and a new extension_string,
745 * returns the path with the extension part replaced by the new extension.
746 * The old filename must have an extension and the new extension cannot be
747 * longer than the old one.
748 */
749
get_output_name(char * input_path,char * extension_string)750 char *get_output_name ( char *input_path, char *extension_string )
751 {
752 size_t pathlen=0, i=0, old_ext_len=0;
753 char *new_path;
754
755 if (input_path == NULL || extension_string == NULL)
756 return NULL;
757 if ( (new_path = strdup(input_path) ) == NULL )
758 return NULL; /* out of memory */
759
760 pathlen = strlen(input_path);
761
762 /* Identify the length of the old extension */
763 for (i=pathlen; i > 0; i--) {
764 if ( input_path[i] == '/' || input_path[i] == '\\' )
765 break;
766 if ( input_path[i] == '.' ) {
767 old_ext_len = pathlen - i;
768 break;
769 }
770 }
771
772 if (old_ext_len < strlen (extension_string) )
773 return NULL;
774
775 memset (&new_path[pathlen - old_ext_len], 0, old_ext_len);
776 strncpy (&new_path[pathlen - old_ext_len], extension_string, strlen(extension_string) );
777
778 return new_path;
779 /* !!! caller has to free the new string after using it !!! */
780 }
781