Пример #1
0
static void respond_okstr(const str* s)
{
  obuf_putu(&outbuf, s->len + 1);
  obuf_putc(&outbuf, ':');
  obuf_putc(&outbuf, 'K');
  obuf_putstr(&outbuf, s);
  obuf_putc(&outbuf, ',');
  obuf_flush(&outbuf);
  exit(0);
}
Пример #2
0
void log_start(const char* tagstr)
{
  obuf_puts(&errbuf, program);
  obuf_putc(&errbuf, '[');
  obuf_putu(&errbuf, getpid());
  obuf_puts(&errbuf, "]: ");
  if (tagstr) {
    obuf_puts(&errbuf, tagstr);
    obuf_putc(&errbuf, ' ');
  }
}
Пример #3
0
int cli_main(int argc, char* argv[])
{
  nistp224key sec;
  nistp224key pub;
  obuf out;
  str str = {0,0,0};
  const char* home;
  const char* keypath;
  
  uskey_path = "secret";
  upkey_path = "public";
  
  if (argc > 0)
    keypath = argv[0];
  else {
    if ((home = getenv("HOME")) == 0) die1(1, "$HOME is not set.");
    if (chdir(home) != 0)
      die3sys(1, "Could not change directory to '", home, "'");
    mkdir(".srcmd", 0700);
    keypath = ".srcmd/key";
  }
  
  mkdir(keypath, 0755);
  if (chdir(keypath) != 0)
    die3sys(1, "Could not chdir to '", keypath, "'");
  
  random_key(sec);
  nistp224wrap(pub, BASEP224, sec);

  base64_encode_line(sec, sizeof sec, &str);
  if (!obuf_open(&out, uskey_path, OBUF_CREATE|OBUF_EXCLUSIVE, 0400, 0) ||
      !obuf_putstr(&out, &str) ||
      !obuf_putc(&out, '\n') ||
      !obuf_close(&out))
    die3sys(1, "Could not create secret key file '", uskey_path, "'");

  str_truncate(&str, 0);
  base64_encode_line(pub, sizeof pub, &str);
  if (!obuf_open(&out, upkey_path, OBUF_CREATE|OBUF_EXCLUSIVE, 0444, 0) ||
      !obuf_putstr(&out, &str) ||
      !obuf_putc(&out, '\n') ||
      !obuf_close(&out))
    die3sys(1, "Could not create public key file '", upkey_path, "'");
  
  msg3("Your public key is '", str.s, "'");
  
  return 0;
  argc = 1;
}
Пример #4
0
void respond_start(const char* tagstr)
{
  if (tagstr == 0) tagstr = tag.s;
  log_start(tagstr);
  if (!obuf_puts(&outbuf, tagstr) ||
      !obuf_putc(&outbuf, ' '))
    exit(1);
}
Пример #5
0
static int read_request(void)
/* Returns number of bytes read before the LF, or -1 for EOF */
{
  unsigned offset;
  int saw_esc;
  int saw_esc_respond;
  int saw_esc_ignore;
  char byte[1];

  saw_esc = saw_esc_respond = saw_esc_ignore = 0;
  offset = 0;
  while (offset < sizeof request - 1) {
    if (!ibuf_getc(&inbuf, byte)) { inbuf_errmsg(); return -1; }
    if (saw_esc) {
      saw_esc = 0;
      switch (*byte) {
      case TELNET_DONT:
      case TELNET_WONT:
	saw_esc_ignore = *byte; break;
      case TELNET_WILL: saw_esc_respond = TELNET_DONT; break;
      case TELNET_DO  : saw_esc_respond = TELNET_WONT; break;
      case TELNET_IAC : request[offset++] = TELNET_IAC; break;
      }
    }
    else if (saw_esc_ignore) {
      saw_esc_ignore = 0;
    }
    else if (saw_esc_respond) {
      obuf_putc(&outbuf, TELNET_IAC);
      obuf_putc(&outbuf, saw_esc_respond);
      obuf_putc(&outbuf, *byte);
      obuf_flush(&outbuf);
      saw_esc_respond = 0;
    }
    else if (*byte == TELNET_IAC)
      saw_esc = 1;
    else if (*byte == LF)
      break;
    else
      request[offset++] = *byte ? *byte : LF;
  }
  while (*byte != LF)
    if (!ibuf_getc(&inbuf, byte)) { inbuf_errmsg(); return -1; }
  return offset;
}
Пример #6
0
void test(long long num, unsigned width, char pad)
{
  char buf[FMT_ULONG_LEN];
  obuf_putu(&outbuf, fmt_slldecw(0, num, width, pad));
  obuf_putc(&outbuf, ':');
  buf[fmt_sdecw(buf, num, width, pad)] = 0;
  obuf_puts(&outbuf, buf);
  NL();
}
Пример #7
0
static void test(unsigned (*fn)(char*, unsigned long, unsigned, char),
		 unsigned long num, unsigned width, char pad)
{
  char buf[FMT_ULONG_LEN];
  obuf_putu(&outbuf, fn(0, num, width, pad));
  obuf_putc(&outbuf, ':');
  buf[fn(buf, num, width, pad)] = 0;
  obuf_puts(&outbuf, buf);
  NL();
}
Пример #8
0
static void obuf_putstream(obuf* out, const stream* s, const char* end)
{
  obuf_puts(out, "stream ");
  obuf_putuw(out, s->strnum, 10, ' ');
  obuf_puts(out, " record ");
  obuf_putuw(out, s->recnum, 10, ' ');
  obuf_puts(out, " offset ");
  obuf_putuw(out, s->offset, 10, ' ');
  obuf_putc(out, ' ');
  obuf_puts(out, end);
}
Пример #9
0
static void cmd_list(void)
{
  long i;
  respond(ok);
  for (i = 0; i < msg_count; i++) {
    if (!msgs[i].deleted) {
      obuf_putu(&outbuf, i+1);
      obuf_putc(&outbuf, SPACE);
      obuf_putu(&outbuf, msgs[i].size);
      obuf_puts(&outbuf, CRLF);
    }
  }
  respond(".");
}
Пример #10
0
static void cmd_uidl(void)
{
  long i;
  respond(ok);
  for (i = 0; i < msg_count; i++) {
    msg* m = &msgs[i];
    if (!m->deleted) {
      const char* fn = m->filename + 4;
      obuf_putu(&outbuf, i+1);
      obuf_putc(&outbuf, SPACE);
      obuf_write(&outbuf, fn, m->uid_len);
      obuf_puts(&outbuf, CRLF);
    }
  }
  respond(".");
}
Пример #11
0
static void dump_msg(long num, long bodylines)
{
  ibuf in;
  static char buf[4096];
  int in_header;		/* True until a blank line is seen */
  int sol;			/* True if at start of line */
  
  if (!ibuf_open(&in, msgs[num-1].filename, 0))
    return respond("-ERR Could not open that message");
  respond(ok);

  sol = in_header = 1;
  while (ibuf_read(&in, buf, sizeof buf) || in.count) {
    const char* ptr = buf;
    const char* end = buf + in.count;
    while (ptr < end) {
      const char* lfptr;
      if (sol) {
	if (!in_header)
	  if (--bodylines < 0) break;
	if (*ptr == '.')
	  obuf_putc(&outbuf, '.');
      }
      if ((lfptr = memchr(ptr, LF, end-ptr)) == 0) {
	obuf_write(&outbuf, ptr, end-ptr);
	ptr = end;
	sol = 0;
      }
      else {
	if (in_header && lfptr == ptr)
	  in_header = 0;
	obuf_write(&outbuf, ptr, lfptr-ptr);
	obuf_puts(&outbuf, CRLF);
	ptr = lfptr + 1;
	sol = 1;
      }
    }
  }
  ibuf_close(&in);
  obuf_puts(&outbuf, CRLF);
  respond(".");
}
Пример #12
0
static void test(const char* start)
{
  ipv4addr ip;
  int i;
  const char* end;
  obuf_put2s(&outbuf, start, ": ");
  end = ipv4_scan(start, &ip);
  if (end == 0)
    obuf_puts(&outbuf, "NULL");
  else {
    for (i = 0; i < 4; ++i) {
      if (i > 0) obuf_putc(&outbuf, '.');
      obuf_puti(&outbuf, ip.addr[i]);
    }
    if (*end != 0)
      obuf_put2s(&outbuf, " + ", end);
  }
  NL();
  obuf_flush(&outbuf);
}
Пример #13
0
void append_stream(stream* s, const char* buf, uint32 reclen)
{
  obuf_putstream(&outbuf, s, "append bytes ");
  obuf_putu(&outbuf, reclen);
  obuf_putc(&outbuf, LF);
}