static char *
rw_serialize_pub (const dckey *key)
{
  const rw_pub *pk = (const rw_pub *) key;
  char *res = NULL;

  if (cat_str (&res, rabin_1.name)
      || cat_str (&res, ":Pub,n=")
      || cat_mpz (&res, pk->n)) {
    free (res);
    res = NULL;
  }
  return res;
}
Ejemplo n.º 2
0
static char *fill_formatted_bytes(char *str, uint64_t bytes){
  const static char *sizes[]={"Bytes", "KB", "MB", "GB", "TB"};
  uint32_t rem, sz;
  rem=0;
  sz=0;
  while (bytes>=1024 && sz<ARRAY_SIZE(sizes)-1){
    rem=bytes%1024;
    bytes/=1024;
    sz++;
  }
  str=cat_uint32(str, (uint32_t)bytes);
  if (sz && bytes<100){
    rem=rem*1000/1024;
    assert(rem<=999);
    *str++='.';
    if (bytes<10){
      rem/=10;
      *str++='0'+rem/10;
      *str++='0'+rem%10;
    }
    else
      *str++='0'+rem/100;
  }
  return cat_str(str, sizes[sz]);
}
Ejemplo n.º 3
0
void uart_report(char *line)
{
	to_print = line;
#if 0
	char *buf = report;
	unsigned int cnt1_A;

	_DINT();
	cnt1_A = cnt1_a;
	cnt1_a = 0;
	_EINT();
	
	buf = cat_ul(buf, jiffies);
	buf = cat_str(buf, "\t");
	buf = cat_ul(buf, cnt1_A);
	buf = cat_str(buf, "\t");
	buf = cat_ul(buf, cnt1_b);
# if 0
	buf = cat_ul(buf, curr_state);
	buf = cat_str(buf, "\t");
	buf = cat_ul(buf, temp_up);
	buf = cat_str(buf, "\t");
	buf = cat_ul(buf, temp_bottom);
	buf = cat_str(buf, "\t");
	buf = cat_ul(buf, temp_ctl);
#endif

	buf = cat_str(buf, "\r\n");
	*buf = 0; 
#endif
	i = 0;
	IE2 |= UCA0TXIE;
}
Ejemplo n.º 4
0
void LibRnrsUnicode::CharGeneralCategory::func(KevesVM* vm, const_KevesIterator pc) {
  KevesChar chr(vm->acc_);

  const char* cat[] { "XX", "Mn", "Mc", "Me", "Nd", "Nl", "No", "Zs",
		      "Zp", "Zl", "Cc", "Cf", "Cs", "Co", "Cn", "Lu",
		      "Ll", "Lt", "Lm", "Lo", "Pc", "Pd", "Ps", "Pe",
		      "Pi", "Pf", "Po", "Sm", "Sc", "Sk", "So", };

  QChar::Category cat_n(chr.category());
  StringKevWithArray<0x4> cat_str(cat[cat_n]);
  SymbolKev cat_sym(cat_str);
  vm->acc_ = &cat_sym;
  return KevesVM::returnValueSafe(vm, pc);
}
Ejemplo n.º 5
0
Archivo: client.c Proyecto: malaise/c
int main(int argc, char *argv[]) {

  command_number number;
  typedef enum {None, Udp, Tcp, Ipm} proto_list;
  proto_list proto;
  soc_token soc = NULL;
  int port_no;
  char buff[500];
  request_message_t request;
  report_message_t report;
  int i, n, res;
  soc_host  my_host;
  soc_port  my_port;


  /* printf("Req_size: %d\n", sizeof(request_message_t)); */
  /* printf("Rep_size: %d\n", sizeof(report_message_t)); */

  proto = None;
  if (argc == 4) {
    if (strcmp (argv[1], "-u") == 0) {
      proto = Udp;
    } else if (strcmp (argv[1], "-t") == 0) {
      proto = Tcp;
    } else if (strcmp (argv[1], "-m") == 0) {
      proto = Ipm;
    }
  }
  if (proto == None) {
    fprintf(stderr, "Error. Three args -u/-t/-m <hostname> <port_name/num> expected.\n");
    exit(1);
  }

  /* Socket stuff */
  res = soc_open(&soc, (proto == Tcp ? tcp_header_socket : udp_socket));
  if (res != SOC_OK) {
    perror("soc_open");
    terror("soc_open", res);
    exit(1);
  }

  /* Dest to host (tcp/udp) or lan (ipm) */
  port_no = atoi(argv[3]);
  if (port_no <= 0) {
    res  = soc_set_dest_name_service(soc, argv[2], proto == Ipm, argv[3]);
    if (res != SOC_OK) {
      perror("soc_set_dest_name_service");
      terror("soc_set_dest_name_service", res);
      exit (1);
    }
  } else {
    res  = soc_set_dest_name_port(soc, argv[2], proto == Ipm, port_no);
    if (res != SOC_OK) {
      perror("soc_set_dest_name_port");
      terror("soc_open", res);
      exit (1);
    }
  }
  if (proto != Tcp) {
    res = soc_link_dynamic(soc);
    if (res != SOC_OK) {
      perror("soc_link_dynamic");
      terror("soc_link_dynamic", res);
      exit(1);
    }
  }
  res = soc_set_blocking(soc, FALSE);
  if (res != SOC_OK) {
    perror("soc_set_blocking");
    terror("soc_set_blocking", res);
    exit(1);
  }

  /* Get current host and port */
  res = soc_get_local_host_id(&my_host);
  if (res != SOC_OK) {
    perror("soc_get_local_host");
    terror("soc_get_local_host", res);
    exit(1);
  }
  my_port = 0;
  if (proto != Tcp) {
    res = soc_get_linked_port(soc, &my_port);
    if (res != SOC_OK) {
      perror("soc_get_linked_port");
      terror("soc_get_linked_port", res);
      exit(1);
    }
  }
  printf ("I am host %u port %u\n", my_host.integer, my_port);

  number = 0;
  for (;;) {
    printf ("\n");

    printf ("Start Kill Exit Ping Read (s k e p r) ? ");
    i = get_line (NULL, buff, sizeof(buff));

    if (strcmp(buff, "s") == 0) {
      /* Start */
      memset(request.start_req.command_text, 0,
             sizeof(request.start_req.command_text));
      memset(request.start_req.environ_variables, 0,
             sizeof(request.start_req.environ_variables));
      request.kind = start_command;
      printf ("Number: %d\n", number);
      request.start_req.number = number;
      number += 1;
      printf ("Command ? ");
      i = get_line (NULL, request.start_req.command_text,
                  sizeof(request.start_req.command_text));
      for (;;) {
        printf ("Argument (Empty to end) ? ");
        i = get_line (NULL, buff, sizeof(buff));
        if (i == 0) break;
        cat_str (request.start_req.command_text, buff);
      }
      for (n = 1;;n++) {
        printf ("Environ (Empty to end) ? ");
        i = get_line (NULL, buff, sizeof(buff));
        if (i == 0) break;
        if (n == 1) {
          strcpy (request.start_req.environ_variables, buff);
        } else {
          cat_str (request.start_req.environ_variables, buff);
        }
      }

      printf ("Current dir ? ");
      i = get_line (NULL, request.start_req.currend_dir,
                    sizeof(request.start_req.currend_dir));

      printf ("Output flow ? ");
      i = get_line (NULL, request.start_req.output_flow,
                    sizeof(request.start_req.output_flow));
      if (i != 0) {
        for (;;) {
          printf ("  Append (t or [f]) ? ");
          i = get_line (NULL, buff, sizeof(buff));
          if (strcmp(buff, "t") == 0) {
            request.start_req.append_output = 1;
            break;
          } else if ( (i == 0) || (strcmp(buff, "f") == 0) ) {
            request.start_req.append_output = 0;
            break;
          }
        }
      }

      printf ("Error flow ? ");
      i = get_line (NULL, request.start_req.error_flow,
                    sizeof(request.start_req.error_flow));
      if (i != 0) {
        for (;;) {
          printf ("  Append (t or [f]) ? ");
          i = get_line (NULL, buff, sizeof(buff));
          if (strcmp(buff, "t") == 0) {
            request.start_req.append_error = 1;
            break;
          } else if ( (i == 0) || (strcmp(buff, "f") == 0) ) {
            request.start_req.append_error = 0;
            break;
          }
        }
      }

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "k") == 0) {
      /* Kill */
      request.kind = kill_command;
      for (;;) {
        printf ("Number ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.start_req.number = i;
      for (;;) {
        printf ("Signal ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.kill_req.signal_number = i;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "e") == 0) {
      /* Exit */
      request.kind = fexit_command;
      for (;;) {
        printf ("Exit code ? ");
        i = get_line (NULL, buff, sizeof(buff));
        i = atoi(buff);
        if (i >= 0) break;
      }
      request.fexit_req.exit_code = i;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "p") == 0) {
      /* Exit */
      request.kind = ping_command;

      res = soc_send(soc, (char*)&request, sizeof(request));
      if (res != SOC_OK) {
        perror("soc_send");
        terror("soc_send", res);
      }

    } else if (strcmp(buff, "r") == 0) {
      /* Read report */
      n = soc_receive(soc, &report, sizeof(report), FALSE);
      if (n == sizeof(report)) {
        switch (report.kind) {
          case start_report :
            printf ("Start: command %d pid %d\n", report.start_rep.number,
                    report.start_rep.started_pid);
          break;
          case kill_report :
            printf ("Kill: command %d pid %d\n", report.kill_rep.number,
                    report.kill_rep.killed_pid);
          break;
          case exit_report :
            printf ("Exit: command %d pid %d code %d ", report.exit_rep.number,
                    report.exit_rep.exit_pid, report.exit_rep.exit_status);
            if (WIFEXITED(report.exit_rep.exit_status)) {
              printf ("exit normally code %d\n",
                      WEXITSTATUS(report.exit_rep.exit_status));
            } else if (WIFSIGNALED(report.exit_rep.exit_status)) {
              printf ("exit on signal %d\n",
                      WTERMSIG(report.exit_rep.exit_status));
            } else {
              printf ("stopped on signal %d\n",
                      WSTOPSIG(report.exit_rep.exit_status));
            }
          break;
          case fexit_report :
            printf ("Forker exited\n");
          break;
          case pong_report :
            printf ("Pong\n");
          break;
          default :
           printf ("Unknown report kind\n");
          break;
        }
      } else if (n == SOC_WOULD_BLOCK) {
        printf ("No report\n");
      } else if (n == SOC_READ_0) {
        printf ("Disconnected\n");
        exit (0);
      } else {
        perror("soc_receive");
      }
    }
  }
}
Ejemplo n.º 6
0
int 
main (int argc, char **argv)
{
  int fdca, fdpk;
  dckey *ca = NULL, *pk = NULL;
  char *id = NULL;
  char *cert_file = NULL, *pk_file = NULL;
  int duration = -1;

  ri ();

  if (argc < 2) 
    usage (argv[0]);
  else if (argc == 2) {
    if (strcmp (argv[1], "init") != 0)
      usage (argv[0]);
    else {
      setprogname (argv[0]);
      pki_init ();      
    }
  }
  else if (argc == 5) {
    if (strcmp (argv[1], "check") != 0)
      usage (argv[0]);
    else {
      setprogname (argv[0]);
      pki_check (argv[2], argv[3], argv[4]);      
    }
  }
  else if (strcmp (argv[1], "cert") != 0) {
    usage (argv[0]);
  }
  else {
    /* cert commnad */
    setprogname (argv[0]);

    /* first, let's take care of the options, if any */
    parse_options (&pk, &cert_file, &duration, argc, argv);

    /* the last two args are ID and PK-FILE */
    pk_file = argv[argc - 2];
    id = argv[argc - 1];
    /* set up default values for parameters not affected by the options */
    if (!cert_file) {
      /* default cert_file is ID.cert */
      if (cat_str (&cert_file, id)
	  || cat_str (&cert_file, ".cert")) {
	xfree (cert_file);
	exit (1);	    
      }
    }
      
    if (duration == -1) 
      /* default duration is 30 days */
      duration = 30;

    /* take care of the public key that we are certifying */
    /* if the -g option was used, we have to write the pk to pk_file */
    if (pk) 
      write_pkfile (pk_file, pk); 
    /* otherwise, import pk from pk_file */
    else {
      if ((fdpk = open (pk_file, O_RDONLY)) == -1) {
	if (errno == ENOENT) {
	  usage (argv[0]);
	}
	else {
	  perror (argv[0]);
	  
	  exit (1);
	}
      }
      else if (!(pk = import_pub_from_file (fdpk))) {
	fprintf (stderr, "%s: no public key found in %s\n", argv[0], pk_file);
      
	close (fdpk);
	exit (1);
      }
      close (fdpk);
      fdpk = -1;
    }
    /* now read the ca private key from ./.pki/ca.priv */
    if ((fdca = open ("./.pki/ca.priv", O_RDONLY)) == -1) {
      if (errno == ENOENT) {
	usage (argv[0]);
      }
      else {
	perror (argv[0]);
	
	exit (1);
      }
    }   
    else {
      if (!(ca = import_priv_from_file (fdca))) {
	fprintf (stderr, "%s: no private key found in %s\n", 
		argv[0], "./.pki/ca.priv");
	
	close (fdca);
	exit (1);
      }
      close (fdca);
      fdca = -1;

      /* prepare a cert, sign it and write it to cert_file */
      switch (cert_sign_n_write (ca, id, pk, duration, cert_file)) {
      case 0:
	/* no error */
	/* the ca signing key is not needed anymore: wipe it out */
	dcfree (ca);
	ca = NULL;
	break;
      case -1:
	/* trouble with the write system call */
	check_n_free (&cert_file);
	dcfree (ca);
	exit (1);
      case -2:
	/* trouble preparing/signinig the certificate */
	check_n_free (&cert_file);
	dcfree (ca);
	exit (1);
      default:
	check_n_free (&cert_file);
	dcfree (ca);
	exit (1);
      }

      assert (cert_verify (cert_read (cert_file)));
      
      dcfree (pk);
      pk = NULL;
    }
  }
  check_n_free (&cert_file);
  
  return 0;
}
Ejemplo n.º 7
0
char *
cert_export (const cert *c, int with_sig)
{
  char *res = NULL;

  if (!c || cat_str (&res, c->version)
      || cat_str (&res, ":ca=(")
      || cat_str (&res, dcexport_pub (c->issuer))
      || cat_str (&res, "),id=")
      || cat_str (&res, c->identity)
      || cat_str (&res, ",pk=(")
      || cat_str (&res, dcexport_pub (c->public_key))
      || cat_str (&res, "),issued=")
      || cat_str (&res, ctime (&(c->day_issued)))
      || (res[strlen(res) - 1] = ',',     /* replace '\n' by ',' */
	  cat_str (&res, "expires="))
      || cat_str (&res, ((c->day_expires == c->day_issued) ? "NEVER\n" 
			 : ctime (&(c->day_expires))))
      /* this one always eval to false; done just to remove trailing '\n' */
      || (res [strlen (res) - 1] = ((with_sig && c->sig) ? ',' : '\0'), 0)
      || (with_sig && c->sig && (cat_str (&res, "sig=") 
				 || cat_str (&res, c->sig)))) {
    xfree (res);
    res = NULL;
  }

  return res;
}