Ejemplo n.º 1
0
int
main (int argc, char *argv[])
{
  unsigned char key[65];
  FILE *kf;
  char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL, *custom_mibs=NULL;
  unsigned int keylen = 0;
  unsigned int encode_docsis = FALSE, decode_bin = FALSE, hash = 0;
  int i;
  int resolve_oids = 1;

  while (argc > 0) {
    argc--; argv++;

    if (!argc) {
      usage();
    }

    /* the initial command-line parameters are flags / modifiers */
    if (!strcmp (argv[0], "-nohash")) {
      nohash = 1;

      continue;
    }

    if (!strcmp (argv[0], "-o")) {
      resolve_oids = 0;
      continue;
    }

    if (!strcmp (argv[0], "-M")) {
      if (argc < 2 ) {
        usage();
      }

      custom_mibs=argv[1];

      argc--; argv++;
      continue;
    }

    if (!strcmp (argv[0], "-na")) {
      if (hash) {
        usage();
      }
      hash = 1;
      continue;
    }

    if (!strcmp (argv[0], "-eu")) {
      if (hash) {
        usage();
      }
      hash = 2;
      continue;
    }

    if (!strcmp (argv[0], "-dialplan")) {
      dialplan = 1;
      continue;
    }

    /* the following command-line parameters are actions */

    if (!strcmp (argv[0], "-d")) {
      if (argc < 2 ) {
        usage();
      }

      decode_bin = TRUE;
      config_file = argv[1];

      break;
    }

    if (!strcmp (argv[0], "-e")) {
      if (argc < 4 ) {
        usage();
      }

      encode_docsis = TRUE;
      config_file = argv[1];
      key_file = argv[2];
      output_file = argv[3];

      break;
    }

    if (!strcmp (argv[0], "-m")) {
      extension_string = argv[argc-1];
      key_file = argv[argc-2];
      encode_docsis = TRUE;

      continue;
    }

    if (!strcmp (argv[0], "-p")) {
      /* encode_docsis may already have been set via the "-m" option */
      encode_docsis = 0;

      argc--; argv++;

      if (argc < 2 ) {
        usage();
      }

      /* -p might be followed by -dialplan.  This is allowed for backwards
       * compatibility */
      if (!strcmp (argv[0], "-dialplan")) {
        dialplan = 1;
        argc--; argv++;
      }

      if (argc < 2 ) {
        usage();
      }

      /* if -m has not already been specified, then we expect "<mta_cfg_file> <output_file>" */
      if (extension_string == NULL) {
        config_file = argv[0];
        output_file = argv[1];
      }

      break;
    }

    /* no more recognisable options means that we've either finished parsing
     * all arguments or else that the remaining arguments refer to a list of
     * config files */
    if (argc) {
      break;
    }
  }

  if (encode_docsis)
    {
      if ((kf = fopen (key_file, "r")) == NULL)
        {
          fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file);
          exit (-5);
        }
      keylen = fread (key, sizeof (unsigned char), 64, kf);
      while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13))
        {
          keylen--;		/* eliminate trailing \n or \r */
        }
      fclose(kf);
    }

  init_global_symtable ();
  setup_mib_flags(resolve_oids,custom_mibs);

  if (decode_bin)
  {
      decode_file (config_file);
      exit(0); // TODO: clean shutdown
  }

  if (extension_string) { /* encoding multiple files */
	if (encode_docsis) {
		/* encode argv[argc-3] to argv[0] */
		for (i=0; i<argc-2; i++) {
			if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
				fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
				continue;
			}

			fprintf(stderr, "Processing input file %s: output to  %s\n",argv[i], output_file);
			if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
				exit(2);
			}
			free (output_file);
			output_file = NULL;
		}
	} else {
		/* encode argv[argc-2] to argv[0] */
		for (i=0; i<argc-1; i++) {
			if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
				fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
				continue;
			}
			fprintf (stderr, "Processing input file %s: output to  %s\n",argv[i], output_file);
			if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis, hash)) {
				exit(2);
			}
			free (output_file);
			output_file = NULL;
		}
	}
  } else {
	if (encode_one_file (config_file, output_file, key, keylen, encode_docsis, hash)) {
		exit(2);
	}
	/* encode argv[1] */
  }
  free(global_symtable);
  shutdown_mib();
  return 0;
}
Ejemplo n.º 2
0
int
main (int argc, char *argv[])
{
  unsigned char key[65];
  FILE *kf;
  char *config_file=NULL, *key_file=NULL, *output_file=NULL, *extension_string=NULL;
  unsigned int keylen = 0;
  unsigned int encode_docsis = FALSE, decode_bin = FALSE;
  int i;
  int resolve_oids = 1;

  if (argc < 2 ) {
	usage();
  }

  if (!strcmp (argv[1], "-o") ){
	resolve_oids = 0;
	if (!strcmp (argv[2], "-d")) {
		decode_bin = TRUE;
		config_file = argv[3];
	} else {
		usage();
	}
  }else if (!strcmp (argv[1], "-m") ){ /* variable number of args, encoding multiple files */
	if (argc < 5 ) {
		usage();
	}
    	extension_string = argv[argc-1];
        if (!strcmp ( argv[2], "-p")) {
		key_file = NULL;
	} else {
		key_file = argv[argc-2];
		encode_docsis = TRUE;
	}
  } else {
  	switch (argc)
    	{
    	case 3:
      		if (strcmp (argv[1], "-d"))
			usage ();
      		decode_bin = TRUE;
      		config_file = argv[2];
      		break;
      		;;
    	case 4:
      		if (strcmp (argv[1], "-p"))
			usage ();
      		config_file = argv[2];
      		output_file = argv[3];
      		break;
      		;;
    	case 5:
      		if (strcmp (argv[1], "-e"))
			usage ();
      		encode_docsis = TRUE;
      		config_file = argv[2];
      		key_file = argv[3];
      		output_file = argv[4];
      		break;
      		;;
    	default:
		usage ();
    }
  }

  if (encode_docsis)
    {
      if ((kf = fopen (key_file, "r")) == NULL)
	{
	  fprintf (stderr, "docsis: error: can't open keyfile %s\n", key_file);
	  exit (-5);
	}
      keylen = fread (key, sizeof (unsigned char), 64, kf);
      while (keylen > 0 && (key[keylen - 1] == 10 || key[keylen - 1] == 13))
	{
	  keylen--;		/* eliminate trailing \n or \r */
	}
    }

  init_global_symtable ();
  setup_mib_flags(resolve_oids);

  if (decode_bin)
  {
      decode_file (config_file);
      exit(0); // TODO: clean shutdown
  }

  if (extension_string) { /* encoding multiple files */
	if (encode_docsis) {
		/* encode argv[argc-3] to argv[2] */
		for (i=2; i<argc-2; i++)  {
			if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
				fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
				continue;
			}

			fprintf(stderr, "Processing input file %s: output to  %s\n",argv[i], output_file);
			if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) {
				exit(2);
			}
			free (output_file);
			output_file = NULL;
		}
	} else {
		/* encode argv[argc-2] to argv[3] */
		for (i=3; i<argc-1; i++)  {
			if ( (output_file = get_output_name (argv[i], extension_string)) == NULL ) {
				fprintf(stderr, "Cannot process input file %s, extension too short ?\n",argv[i] );
				continue;
			}
			fprintf (stderr, "Processing input file %s: output to  %s\n",argv[i], output_file);
			if (encode_one_file (argv[i], output_file, key, keylen, encode_docsis)) {
				exit(2);
			}
			free (output_file);
			output_file = NULL;
		}
	}
  } else {
	if (encode_one_file (config_file, output_file, key, keylen, encode_docsis)) {
		exit(2);
	}
	/* encode argv[1] */
  }
  free(global_symtable);
  shutdown_mib();
  return 0;
}