Esempio n. 1
0
void add_handler(enum handler_type h_type,
                 enum input_type i_type,
                 const char * command,
                 char * const argv[]) {

    struct handler *h = gc_malloc(sizeof (struct handler));
    instantiate_handler(h, h_type, i_type, command, argv);

    if(handlers) {
        last->next = h;
        last = h;
    }
    else {
        handlers = h;
        last = h;
    }
}
Esempio n. 2
0
File: proxy.c Progetto: jmadhav/lib
char *
get_windows_internet_string (const DWORD dwOption, struct gc_arena *gc)
{
  DWORD size = 0;
  char *ret = NULL;

  /* Initially, get size of return buffer */
  InternetQueryOption (NULL, dwOption, NULL, &size);
  if (size)
    {
      /* Now get actual info */
      ret = (INTERNET_PROXY_INFO *) gc_malloc (size, false, gc);
      if (!InternetQueryOption (NULL, dwOption, (LPVOID) ret, &size))
	ret = NULL;
    }
  return ret;
}
Esempio n. 3
0
static INTERNET_PROXY_INFO *
get_windows_proxy_settings (struct gc_arena *gc)
{
  DWORD size = 0;
  INTERNET_PROXY_INFO *ret = NULL;

  /* Initially, get size of return buffer */
  InternetQueryOption (NULL, INTERNET_OPTION_PROXY, NULL, &size);
  if (size)
    {
      /* Now get actual info */
      ret = (INTERNET_PROXY_INFO *) gc_malloc (size, false, gc);
      if (!InternetQueryOption (NULL, INTERNET_OPTION_PROXY, (LPVOID) ret, &size))
	ret = NULL;
    }
  return ret;
}
Esempio n. 4
0
LispRef newFunction(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
    WITH_DEBUG(fprintf(stderr, "newFunction\n"));

    LispRef name = content(stream, eos_error_p, eos_value);
    LispRef domain = content(stream, eos_error_p, eos_value);
    LispRef setter = content(stream, eos_error_p, eos_value);
    LispRef env = content(stream, eos_error_p, eos_value);

    int n;
    read_int(n);

    WITH_DEBUG(fprintf(stderr, "size: %d\n", n));

    char *data = read_bytes(n);
    char *new_data = (char *)gc_malloc(n);
    memcpy(new_data, data, n);

    LispRef code;
    eul_allocate_nstring(code, new_data, n);

    LispRef res;
    eul_allocate_lambda1(res, name, domain, code);
    LAMBDA_SETTER(res) = setter;
    LAMBDA_ENV(res) = env;
    newHandle(stream, res);
    LispRef refs = content(stream, eos_error_p, eos_value);

    WITH_DEBUG(fprintf(stderr, "!!!Refs: "));
    WITH_DEBUG(fprint_ref(stderr, refs));
    WITH_DEBUG(fprintf(stderr, "\n"));

    if (refs != eul_nil)
    {
        if (eul_link_lambda_refs(res, refs) == eul_nil)
        {
            LispRef str;
            eul_allocate_string(str, "cannot serialize foreign function");
            eul_serial_error(stream, str, eul_nil);
            return eul_nil;
        }
    }

    return res;
}
Esempio n. 5
0
alloc_buf_gc (size_t size, struct gc_arena *gc)
#endif
{
  struct buffer buf;
  if (!buf_size_valid (size))
    buf_size_error (size);
  buf.capacity = (int)size;
  buf.offset = 0;
  buf.len = 0;
#ifdef DMALLOC
  buf.data = (uint8_t *) gc_malloc_debug (size, false, gc, file, line);
#else
  buf.data = (uint8_t *) gc_malloc (size, false, gc);
#endif
  if (size)
    *buf.data = 0;
  return buf;
}
Esempio n. 6
0
lref_t lcopy_structure(lref_t st)
{
     if (!STRUCTUREP(st))
          vmerror_wrong_type_n(1, st);

     lref_t new_st = new_cell(TC_STRUCTURE);

     size_t len = STRUCTURE_DIM(st);;

     SET_STRUCTURE_DIM(new_st, len);
     SET_STRUCTURE_LAYOUT(new_st, STRUCTURE_LAYOUT(st));
     SET_STRUCTURE_DATA(new_st, (lref_t *) gc_malloc(len * sizeof(lref_t)));

     for (size_t ii = 0; ii < len; ii++)
          SET_STRUCTURE_ELEM(new_st, ii, STRUCTURE_ELEM(st, ii));

     return new_st;
}
Esempio n. 7
0
static void
plugin_vlog (vpnconnect_plugin_log_flags_t flags, const char *name, const char *format, va_list arglist)
{
  unsigned int msg_flags = 0;

  if (!format)
    return;

  if (!name || name[0] == '\0')
    {
      msg (D_PLUGIN_DEBUG, "PLUGIN: suppressed log message from plugin with unknown name");
      return;
    }

  if (flags & PLOG_ERR)
    msg_flags = M_INFO | M_NONFATAL;
  else if (flags & PLOG_WARN)
    msg_flags = M_INFO | M_WARN;
  else if (flags & PLOG_NOTE)
    msg_flags = M_INFO;
  else if (flags & PLOG_DEBUG)
    msg_flags = D_PLUGIN_DEBUG;

  if (flags & PLOG_ERRNO)
    msg_flags |= M_ERRNO;
  if (flags & PLOG_NOMUTE)
    msg_flags |= M_NOMUTE;

  if (MSG_TEST (msg_flags))
    {
      struct gc_arena gc;
      char* msg_fmt;

      /* Never add instance prefix; not thread safe */
      msg_flags |= M_NOIPREFIX;

      gc_init (&gc);
      msg_fmt = gc_malloc (ERR_BUF_SIZE, false, &gc);
      vpnconnect_snprintf (msg_fmt, ERR_BUF_SIZE, "PLUGIN %s: %s", name, format);
      x_msg_va (msg_flags, msg_fmt, arglist);

      gc_free (&gc);
    }
}
Esempio n. 8
0
/* Name        : CC_MakeTrampCode
   Description : make corresponding trampoline code to CallContext instance
   Maintainer  : Junpyo Lee <*****@*****.**>
   Pre-condition: trampoline code of cc must be null
   Post-condition:
   Notes:  */
void*
CC_MakeTrampCode(CallContext* cc) 
{
    methodTrampoline* tramp;

    assert(cc);
    assert(CC_GetTrampCode(cc) == NULL);

    tramp = (methodTrampoline*)gc_malloc(sizeof(methodTrampoline),
					 &gc_jit_code);
        
    FILL_IN_JIT_TRAMPOLINE(tramp, cc, dispatch_method_with_CC);

    CC_SetTrampCode(cc, &tramp->code[0]);

    FLUSH_DCACHE(&tramp->code[0], &tramp->code[3]);

    return (void*)&tramp->code[0];
}
Esempio n. 9
0
string_alloc (const char *str, struct gc_arena *gc)
#endif
{
  if (str)
    {
      const int n = strlen (str) + 1;
      char *ret;

#ifdef DMALLOC
      ret = (char *) gc_malloc_debug (n, false, gc, file, line);
#else
      ret = (char *) gc_malloc (n, false, gc);
#endif
      memcpy (ret, str, n);
      return ret;
    }
  else
    return NULL;
}
Esempio n. 10
0
enum sys_retcode_t sys_opendir(const char *path, struct sys_dir_t ** dir)
{
     *dir = gc_malloc(sizeof(**dir));

     if (*dir == NULL)
          return SYS_E_OUT_OF_MEMORY;

     (*dir)->_dir = opendir(path);

     if ((*dir)->_dir == NULL)
     {
          gc_free(*dir);
          *dir = NULL;

          return rc_to_sys_retcode_t(errno);
     }

     return SYS_OK;
}
Esempio n. 11
0
/* Name        : SM_MakeTrampCode
   Description : make corresponding trampoline code to SpecializedMethod 
                 instance
   Maintainer  : Junpyo Lee <*****@*****.**>
   Pre-condition:
   Post-condition:
   Notes:  */
void*
SM_MakeTrampCode(SpecializedMethod* sm) 
{
    methodTrampoline* tramp;

    assert(sm);
    if(SM_GetTrampCode(sm) != NULL)
      return SM_GetTrampCode(sm);

    tramp = (methodTrampoline*)gc_malloc(sizeof(methodTrampoline),
					 &gc_jit_code);
        
    FILL_IN_JIT_TRAMPOLINE(tramp, sm, dispatch_method_with_SM);

    SM_SetTrampCode(sm, &tramp->code[0]);

    FLUSH_DCACHE(&tramp->code[0], &tramp->code[3]);

    return (void*)&tramp->code[0];
}
Esempio n. 12
0
lref_t lstructurecons(lref_t slots, lref_t layout)
{
     if (!VECTORP(slots))
          vmerror_wrong_type_n(1, slots);

     size_t len = slots->as.vector.dim;

     validate_structure_layout(len, layout);

     lref_t st = new_cell(TC_STRUCTURE);

     SET_STRUCTURE_DIM(st, len);
     SET_STRUCTURE_LAYOUT(st, layout);
     SET_STRUCTURE_DATA(st, (lref_t *) gc_malloc(len * sizeof(lref_t)));

     for (size_t ii = 0; ii < len; ii++)
          SET_STRUCTURE_ELEM(st, ii, slots->as.vector.data[ii]);

     return st;
}
/**
 * Allocate a new sequence element.
 */
sequence*
nextSeq(void)
{
	sequence* ret;

	ret = currSeq;
	if (ret == 0) {
		sequencechunk *sc;
		
		int i;
		/* Allocate chunk of sequence elements */
		sc = gc_malloc(sizeof(sequencechunk), KGC_ALLOC_JIT_SEQ);
		assert(sc != NULL);

		sc->next = sequencechunks;
		sequencechunks = sc;
		
		ret = &sc->data[0];
		
		/* Attach to current chain */
		if (lastSeq == 0) {
			firstSeq = ret;
		}
		else {
			lastSeq->next = ret;
		}
		lastSeq = &sc->data[ALLOCSEQNR-1];

		/* Link elements into list */
		for (i = 0; i < ALLOCSEQNR-1; i++) {
			sc->data[i].next = &sc->data[i+1];
		}
	}
	currSeq = ret->next;
	ret->lastuse = 0;
	ret->refed = 1;
	ret->jflags = willcatch;
	activeSeq = ret;
	return (ret);
}
Esempio n. 14
0
static WCHAR *
wide_cmd_line (const struct argv *a, struct gc_arena *gc)
{
  size_t nchars = 1;
  size_t maxlen = 0;
  size_t i;
  struct buffer buf;
  char *work = NULL;

  if (!a)
    return NULL;

  for (i = 0; i < a->argc; ++i)
    {
      const char *arg = a->argv[i];
      const size_t len = strlen (arg);
      nchars += len + 3;
      if (len > maxlen)
	maxlen = len;
    }

  work = gc_malloc (maxlen + 1, false, gc);
  check_malloc_return (work);
  buf = alloc_buf_gc (nchars, gc);

  for (i = 0; i < a->argc; ++i)
    {
      const char *arg = a->argv[i];
      strcpy (work, arg);
      string_mod (work, CC_PRINT, CC_DOUBLE_QUOTE|CC_CRLF, '_');
      if (i)
	buf_printf (&buf, " ");
      if (string_class (work, CC_ANY, CC_SPACE))
	buf_printf (&buf, "%s", work);
      else
	buf_printf (&buf, "\"%s\"", work);
    }

  return wide_string (BSTR (&buf), gc);
}
Esempio n. 15
0
void
setenv_str_incr(struct env_set *es, const char *name, const char *value)
{
    unsigned int counter = 1;
    const size_t tmpname_len = strlen(name) + 5; /* 3 digits counter max */
    char *tmpname = gc_malloc(tmpname_len, true, NULL);
    strcpy(tmpname, name);
    while (NULL != env_set_get(es, tmpname) && counter < 1000)
    {
        ASSERT(openvpn_snprintf(tmpname, tmpname_len, "%s_%u", name, counter));
        counter++;
    }
    if (counter < 1000)
    {
        setenv_str(es, tmpname, value);
    }
    else
    {
        msg(D_TLS_DEBUG_MED, "Too many same-name env variables, ignoring: %s", name);
    }
    free(tmpname);
}
Esempio n. 16
0
File: a2c.c Progetto: sacado/arc2c
string * utf82str (char * s){
  int size     = strlen(s);
  string * res = (string *) gc_malloc(sizeof(string));
  int i = 0;
  int len = 0;
  unsigned char cur;
  long cpt;

  res->cpts = malloc (sizeof(long) * size); /* Can be too big */
  res->type = T_STR;

  while (i < size){
    cur = (unsigned char) s[i];
    if (cur < 128) /* An ASCII char */
      res->cpts[len++] = (long) s[i++];
    else if ((cur & 240) == 240){ /* 4 bytes */
      cpt = (((unsigned char) s[i++]) & ~240) * 262144;
      cpt += (((unsigned char) s[i++]) & ~128) * 4096;
      cpt += (((unsigned char) s[i++]) & ~128) * 64;
      cpt += (((unsigned char) s[i++]) & ~128);
    }
    else if ((cur & 224) == 224){ /* 3 bytes */
      cpt = (((unsigned char) s[i++]) & ~224) * 4096;
      cpt += (((unsigned char) s[i++]) & ~128) * 64;
      cpt += (((unsigned char) s[i++]) & ~128);
      res->cpts[len++] = cpt;
    }
    else{ /* 2 bytes */
      cpt = (((unsigned char) s[i++]) & ~192) * 64;
      cpt += (((unsigned char) s[i++]) & ~128);
      res->cpts[len++] = cpt;
    }
  }

  res->size = len;

  return res;
}
Esempio n. 17
0
char *
backend_x509_get_serial (openvpn_x509_cert_t *cert, struct gc_arena *gc)
{
  char *buf = NULL;
  size_t buflen = 0;
  mpi serial_mpi = { 0 };
  int retval = 0;

  /* Transform asn1 integer serial into PolarSSL MPI */
  mpi_init(&serial_mpi);
  retval = mpi_read_binary(&serial_mpi, cert->serial.p, cert->serial.len);
  if (retval < 0)
    {
      char errbuf[128];
      polarssl_strerror(retval, errbuf, sizeof(errbuf));

      msg(M_WARN, "Failed to retrieve serial from certificate: %s.", errbuf);
      return NULL;
    }

  /* Determine decimal representation length, allocate buffer */
  mpi_write_string(&serial_mpi, 10, buf, &buflen);
  buf = gc_malloc(buflen, true, gc);

  /* Write MPI serial as decimal string into buffer */
  retval = mpi_write_string(&serial_mpi, 10, buf, &buflen);
  if (retval < 0)
    {
      char errbuf[128];
      polarssl_strerror(retval, errbuf, sizeof(errbuf));

      msg(M_WARN, "Failed to write serial to string: %s.", errbuf);
      return NULL;
    }

  return buf;
}
Esempio n. 18
0
/*
 * Read a line from the prompt.
 */
extern char *prompt(bool silent, FILE *input, history_t *state)
{
    const char *prompt = (silent? "": "> ");

    while (true)
    {
#ifndef WINDOWS
        if (use_readline && input == stdin)
        {
            // Set the state:
            if (use_state)
            {
                if (*state != NULL)
                {
                    history_set_history_state((HISTORY_STATE *)*state);
                    free(*state);
                    *state = NULL;
                }
                else
                    history_set_history_state(empty_state);
            }

            // Readline:
            char *line = readline(prompt);

            if (line != NULL)
            {
                bool all_space = true;
                for (size_t i = 0; line[i]; i++)
                {
                    if (!isspace(line[i]))
                    {
                        all_space = false;
                        break;
                    }
                }
                if (!all_space)
                    add_history(line);
            }

            if (use_state)
                *state = (history_t)history_get_history_state();

            return line;
        }
#endif      /* WINDOWS */

        // Non-readline:
        size_t size = 128, len = 0;
        char *line = gc_malloc(size*sizeof(char));
        char c;
        fputs(prompt, stdout);
        while (!feof(input) && !ferror(input) && (c = getc(input)) != '\n')
        {
            if (len >= size-1)
            {
                size = (3 * size) / 2;
                line = (char *)gc_realloc(line, size*sizeof(char));
            }
            line[len++] = c;
        }
        if (feof(input) || ferror(input))
            return NULL;
        line[len] = '\0';
        return line;
    }
}
Esempio n. 19
0
File: parse.c Progetto: GJDuck/SMCHR
/*
 * Read a string.
 */
static char *token_readstring(context_t cxt, char end)
{
    size_t size = 128;
    size_t len = 0;
    char *buf = (char *)gc_malloc(size+1);
    char c;

    while (*cxt->str != end)
    {
        if (!isprint(*cxt->str))
            return NULL;
        if (len >= size)
        {
            size *= 2;
            buf = (char *)gc_realloc(buf, size);
        }
        c = *cxt->str;
        buf[len++] = *cxt->str++;
        if (c == '\\')
        {
            c = *cxt->str++;
            switch (c)
            {
                case '\0':
                    return NULL;
                case '0':
                    buf[len-1] = '\0';
                    break;
                case 'n':
                    buf[len-1] = '\n';
                    break;
                case 'r':
                    buf[len-1] = '\r';
                    break;
                case 't':
                    buf[len-1] = '\r';
                    break;
                case 'a':
                    buf[len-1] = '\a';
                    break;
                case 'b':
                    buf[len-1] = '\b';
                    break;
                case 'f':
                    buf[len-1] = '\f';
                    break;
                case 'x':
                {
                    char tmp[3];
                    tmp[0] = *cxt->str++;
                    if (!isxdigit(tmp[0]))
                        return NULL;
                    tmp[1] = *cxt->str++;
                    if (!isxdigit(tmp[1]))
                        return NULL;
                    tmp[2] = '\0';
                    unsigned x;
                    if (sscanf(tmp, "%x", &x) != 1)
                        return NULL;
                    buf[len-1] = (char)x;
                    break;
                }
                default:
                    if (c == '\n')
                        cxt->line++;
                    buf[len-1] = c;
                    break;
            }
        }
    }

    buf[len] = '\0';
    buf = gc_realloc(buf, len+1);
    cxt->str++;
    return buf;
}
Esempio n. 20
0
File: parse.c Progetto: GJDuck/SMCHR
/*
 * Parse a term (no operators).
 */
static bool parse_term_head(context_t cxt, term_t *val)
{
    char *tokstr = (char *)gc_malloc(TOKEN_MAXLEN+1);
    term_t tokval;
    token_t tok = token_get(cxt, &tokval, tokstr);

    unsigned priority;
    if (parse_maybe_op(tok) &&
        unop_lookup(cxt->opinfo, tokstr, &priority, NULL))
    {
        term_t lval;
        if (!parse_term_op(cxt, &lval, priority))
        {
            gc_free(tokstr);
            return false;
        }
        atom_t atom = make_atom(gc_strdup(tokstr), 1);
        func_t f = make_func(atom, lval);
        tokval = term_func(f);
        *val = tokval;
        gc_free(tokstr);
        return true;
    }

    bool ok = true;
    term_t *args = NULL;
    switch ((int)tok)
    {
        case '(':
            if (!parse_term_op(cxt, val, UINT32_MAX) || !token_expect(cxt, ')'))
                ok = false;
            break;
        case TOKEN_NIL: case TOKEN_BOOLEAN: case TOKEN_ATOM: case TOKEN_STRING:
        case TOKEN_NUMBER:
            *val = tokval;
            break;
        case TOKEN_VARIABLE:
        {
            if (token_peek(cxt, NULL, NULL) != '(')
            {
                // Really is a variable:
                *val = tokval;
                break;
            }
            // Otherwise this is a functor:
            var_t x = var(tokval);
            atom_t atom = make_atom(x->name, 0);
            if (!token_expect(cxt, '('))
            {
                ok = false;
                break;
            }
            args = (term_t *)gc_malloc(MAX_ARGS * sizeof(term_t));
            uint_t a = 0;
            if (token_peek(cxt, NULL, NULL) == ')')
                token_get(cxt, NULL, NULL);
            else
            {
                while (true)
                {
                    ok = parse_term_op(cxt, &tokval, UINT32_MAX);
                    if (!ok)
                        break;
                    if (a >= MAX_ARGS)
                    {
                        parse_error(cxt, "too many arguments; maximum is %zu",
                            MAX_ARGS);
                        ok = false;
                        break;
                    }
                    args[a++] = tokval;
                    tok = token_get(cxt, NULL, tokstr);
                    if (tok == ',')
                        continue;
                    if (tok == ')')
                        break;
                    parse_error(cxt, "expected token `,' or `)'; got token "
                        "`%s'", tokstr);
                    ok = false;
                    break;
                }
                if (!ok)
                    break;
            }
            atom = atom_set_arity(atom, a);
            func_t f = make_func_a(atom, args);
            tokval = term_func(f);
            *val = tokval;
            break;
        }
        default:
            if (tok != TOKEN_ERROR)
                parse_error(cxt, "unexpected token `%s'", tokstr);
            ok = false;
            break;
    }
    gc_free(tokstr);
    gc_free(args);
    return ok;
}
Esempio n. 21
0
const char *
format_extended_socket_error (int fd, int *mtu, struct gc_arena *gc)
{
  int res;
  struct probehdr rcvbuf;
  struct iovec iov;
  struct msghdr msg;
  struct cmsghdr *cmsg;
  struct sock_extended_err *e;
  struct sockaddr_in addr;
  struct buffer out = alloc_buf_gc (256, gc);
  char *cbuf = (char *) gc_malloc (256, false, gc);

  *mtu = 0;

  while (true)
    {
      memset (&rcvbuf, -1, sizeof (rcvbuf));
      iov.iov_base = &rcvbuf;
      iov.iov_len = sizeof (rcvbuf);
      msg.msg_name = (uint8_t *) &addr;
      msg.msg_namelen = sizeof (addr);
      msg.msg_iov = &iov;
      msg.msg_iovlen = 1;
      msg.msg_flags = 0;
      msg.msg_control = cbuf;
      msg.msg_controllen = 256; /* size of cbuf */

      res = recvmsg (fd, &msg, MSG_ERRQUEUE);
      if (res < 0)
	goto exit;

      e = NULL;

      for (cmsg = CMSG_FIRSTHDR (&msg); cmsg; cmsg = CMSG_NXTHDR (&msg, cmsg))
	{
	  if (cmsg->cmsg_level == SOL_IP)
	    {
	      if (cmsg->cmsg_type == IP_RECVERR)
		{
		  e = (struct sock_extended_err *) CMSG_DATA (cmsg);
		}
	      else
		{
		  buf_printf (&out ,"CMSG=%d|", cmsg->cmsg_type);
		}
	    }
	}
      if (e == NULL)
	{
	  buf_printf (&out, "NO-INFO|");
	  goto exit;
	}

      switch (e->ee_errno)
	{
	case ETIMEDOUT:
	  buf_printf (&out, "ETIMEDOUT|");
	  break;
	case EMSGSIZE:
	  buf_printf (&out, "EMSGSIZE Path-MTU=%d|", e->ee_info);
	  *mtu = e->ee_info;
	  break;
	case ECONNREFUSED:
	  buf_printf (&out, "ECONNREFUSED|");
	  break;
	case EPROTO:
	  buf_printf (&out, "EPROTO|");
	  break;
	case EHOSTUNREACH:
	  buf_printf (&out, "EHOSTUNREACH|");
	  break;
	case ENETUNREACH:
	  buf_printf (&out, "ENETUNREACH|");
	  break;
	case EACCES:
	  buf_printf (&out, "EACCES|");
	  break;
	default:
	  buf_printf (&out, "UNKNOWN|");
	  break;
	}
    }

 exit:
  buf_rmtail (&out, '|');
  return BSTR (&out);
}
Esempio n. 22
0
File: parse.c Progetto: GJDuck/SMCHR
/*
 * Get a name token.
 */
static token_t token_getname(context_t cxt, term_t *val)
{
    char buf0[TOKEN_MAXLEN+1];
    char *buf = buf0;
    size_t len = 0;

    if (*cxt->str == '\'')
    {
        cxt->str++;
        buf = token_readstring(cxt, '\'');
        if (buf == NULL)
            return TOKEN_ERROR;
    }
    else
    {
        while (isalnum(*cxt->str) || *cxt->str == '_')
        {
            buf[len++] = *cxt->str++;
            if (len >= TOKEN_MAXLEN)
                return TOKEN_ERROR;
        }
        buf[len] = '\0';
    }

    // Check for anonymous variable:
    if (buf[0] == '_' && buf[1] == '\0')
    {
        var_t v = make_var(NULL);
        term_t t = term_var(v);
        *val = t;
        return TOKEN_VARIABLE;
    }

    // Check for special name:
    name_t entry = name_lookup(buf);
    if (entry != NULL)
    {
        *val = entry->val;
        return entry->token;
    }

    // Check for variable:
    term_t t;
    if (varset_search(cxt->vars, buf, &t))
    {
        *val = t;
        return TOKEN_VARIABLE;
    }
    
    // By default, create a new variable:
    char *name;
    if (buf == buf0)
    {
        name = (char *)gc_malloc(len+1);
        strcpy(name, buf);
    }
    else
        name = buf;
    register_name(name);
    var_t v = make_var(name);
    t = term_var(v);
    cxt->vars = varset_insert(cxt->vars, name, t);
    *val = t;
    return TOKEN_VARIABLE;
}
Esempio n. 23
0
void x_msg_va (const unsigned int flags, const char *format, va_list arglist)
{
  struct gc_arena gc;
#if SYSLOG_CAPABILITY
  int level;
#endif
  char *m1;
  char *m2;
  char *tmp;
  int e;
  const char *prefix;
  const char *prefix_sep;

  void usage_small (void);

#ifndef HAVE_VARARG_MACROS
  /* the macro has checked this otherwise */
  if (!MSG_TEST (flags))
    return;
#endif

  e = openvpn_errno ();

  /*
   * Apply muting filter.
   */
#ifndef HAVE_VARARG_MACROS
  /* the macro has checked this otherwise */
  if (!dont_mute (flags))
    return;
#endif

  gc_init (&gc);

  m1 = (char *) gc_malloc (ERR_BUF_SIZE, false, &gc);
  m2 = (char *) gc_malloc (ERR_BUF_SIZE, false, &gc);

  vsnprintf (m1, ERR_BUF_SIZE, format, arglist);
  m1[ERR_BUF_SIZE - 1] = 0; /* windows vsnprintf needs this */

  if ((flags & M_ERRNO) && e)
    {
      openvpn_snprintf (m2, ERR_BUF_SIZE, "%s: %s (errno=%d)",
			m1, strerror_ts (e, &gc), e);
      SWAP;
    }

#ifdef ENABLE_CRYPTO
#ifdef ENABLE_CRYPTO_OPENSSL
  if (flags & M_SSL)
    {
      int nerrs = 0;
      int err;
      while ((err = ERR_get_error ()))
	{
	  openvpn_snprintf (m2, ERR_BUF_SIZE, "%s: %s",
			    m1, ERR_error_string (err, NULL));
	  SWAP;
	  ++nerrs;
	}
      if (!nerrs)
	{
	  openvpn_snprintf (m2, ERR_BUF_SIZE, "%s (OpenSSL)", m1);
	  SWAP;
	}
    }
#endif
#endif

  if (flags & M_OPTERR)
    {
      openvpn_snprintf (m2, ERR_BUF_SIZE, "Options error: %s", m1);
      SWAP;
    }

#if SYSLOG_CAPABILITY
  if (flags & (M_FATAL|M_NONFATAL|M_USAGE_SMALL))
    level = LOG_ERR;
  else if (flags & M_WARN)
    level = LOG_WARNING;
  else
    level = LOG_NOTICE;
#endif

  /* set up client prefix */
  if (flags & M_NOIPREFIX)
    prefix = NULL;
  else
    prefix = msg_get_prefix ();
  prefix_sep = " ";
  if (!prefix)
    prefix_sep = prefix = "";

  /* virtual output capability used to copy output to management subsystem */
  if (!forked)
    {
      const struct virtual_output *vo = msg_get_virtual_output ();
      if (vo)
	{
	  openvpn_snprintf (m2, ERR_BUF_SIZE, "%s%s%s",
			    prefix,
			    prefix_sep,
			    m1);
	  virtual_output_print (vo, flags, m2);
	}
    }

  if (!(flags & M_MSG_VIRT_OUT))
    {
      if (use_syslog && !std_redir && !forked)
	{
#if SYSLOG_CAPABILITY
	  syslog (level, "%s%s%s",
		  prefix,
		  prefix_sep,
		  m1);
#endif
	}
      else
	{
	  FILE *fp = msg_fp(flags);
	  const bool show_usec = check_debug_level (DEBUG_LEVEL_USEC_TIME);

	  if ((flags & M_NOPREFIX) || suppress_timestamps)
	    {
	      fprintf (fp, "%s%s%s%s",
		       prefix,
		       prefix_sep,
		       m1,
		       (flags&M_NOLF) ? "" : "\n");
	    }
	  else
	    {
	      fprintf (fp, "%s %s%s%s%s",
		       time_string (0, 0, show_usec, &gc),
		       prefix,
		       prefix_sep,
		       m1,
		       (flags&M_NOLF) ? "" : "\n");
	    }
	  fflush(fp);
	  ++x_msg_line_num;
	}
    }

  if (flags & M_FATAL)
    msg (M_INFO, "Exiting due to fatal error");

  if (flags & M_FATAL)
    openvpn_exit (OPENVPN_EXIT_STATUS_ERROR); /* exit point */

  if (flags & M_USAGE_SMALL)
    usage_small ();

  gc_free (&gc);
}
Esempio n. 24
0
LispRef eul_mpi_receive(LispRef eul_source, LispRef eul_tag, int n)
{
    MPI_Status status;

    int source, tag;

    LispRef res = eul_nil;

    if (eul_source == eul_nil)
        source = MPI_ANY_SOURCE;
    else
        source = eul_fpi_as_c_int(eul_source);

    if (eul_tag == eul_nil)
        tag = (int)MPI_ANY_TAG;
    else if (eul_tag == eul_true)
        tag = (int)MPI_EUL_OBJECT;
    else
        tag = eul_fpi_as_c_int(eul_tag);

    switch (tag)
    {
        case ((int)MPI_BYTE):
            {
                char val;

                fprintf(stderr, "Before 1\n");
                MPI_Recv(&val, n, MPI_BYTE, source, tag, MPI_COMM_WORLD,
                    &status);
                if ((status.MPI_ERROR) == 0)
                    res = c_char_as_eul_char(val);
                break;
            }
        case ((int)MPI_INT):
            {
                int val;

                fprintf(stderr, "Before 2 n: %i\n", n);
                MPI_Recv(&val, n, MPI_INT, source, tag, MPI_COMM_WORLD,
                    &status);
                if ((status.MPI_ERROR) == 0)
                    res = c_int_as_eul_fpi(val);
                break;
            }
        case ((int)MPI_DOUBLE):
            {
                double val;

                fprintf(stderr, "Before 3\n");
                MPI_Recv(&val, n, MPI_DOUBLE, source, tag, MPI_COMM_WORLD,
                    &status);
                if ((status.MPI_ERROR) == 0)
                    eul_allocate_double(res, val);
                break;
            }
        case ((int)MPI_EUL_STRING):
        case ((int)MPI_EUL_OBJECT):
            {
                char *val;

                fprintf(stderr, "Before 4\n");
                val = (char *)gc_malloc(n + 1);
                MPI_Recv(val, n, MPI_BYTE, source, tag, MPI_COMM_WORLD,
                    &status);
                if ((status.MPI_ERROR) == 0)
                {
                    *(val + n) = '\0';
                    eul_allocate_nstring(res, val, n);
                }
                break;
            }
    }

    return res;
}
Esempio n. 25
0
bool
get_user_pass_cr (struct user_pass *up,
		  const char *auth_file,
		  const char *prefix,
		  const unsigned int flags,
		  const char *auth_challenge)
{
  struct gc_arena gc = gc_new ();

  if (!up->defined)
    {
      const bool from_stdin = (!auth_file || !strcmp (auth_file, "stdin"));

      if (flags & GET_USER_PASS_PREVIOUS_CREDS_FAILED)
	msg (M_WARN, "Note: previous '%s' credentials failed", prefix);

#ifdef ENABLE_MANAGEMENT
      /*
       * Get username/password from management interface?
       */
      if (management
	  && ((auth_file && streq (auth_file, "management")) || (from_stdin && (flags & GET_USER_PASS_MANAGEMENT)))
	  && management_query_user_pass_enabled (management))
	{
	  const char *sc = NULL;

	  if (flags & GET_USER_PASS_PREVIOUS_CREDS_FAILED)
	    management_auth_failure (management, prefix, "previous auth credentials failed");

#ifdef ENABLE_CLIENT_CR
	  if (auth_challenge && (flags & GET_USER_PASS_STATIC_CHALLENGE))
	    sc = auth_challenge;
#endif
	  if (!management_query_user_pass (management, up, prefix, flags, sc))
	    {
	      if ((flags & GET_USER_PASS_NOFATAL) != 0)
		return false;
	      else
		msg (M_FATAL, "ERROR: could not read %s username/password/ok/string from management interface", prefix);
	    }
	}
      else
#endif
      /*
       * Get NEED_OK confirmation from the console
       */
      if (flags & GET_USER_PASS_NEED_OK)
	{
	  struct buffer user_prompt = alloc_buf_gc (128, &gc);

	  buf_printf (&user_prompt, "NEED-OK|%s|%s:", prefix, up->username);
	  
	  if (!get_console_input (BSTR (&user_prompt), true, up->password, USER_PASS_LEN))
	    msg (M_FATAL, "ERROR: could not read %s ok-confirmation from stdin", prefix);
	  
	  if (!strlen (up->password))
	    strcpy (up->password, "ok");
	}
	  
      /*
       * Get username/password from standard input?
       */
      else if (from_stdin)
	{
#ifndef WIN32
	  /* did we --daemon'ize before asking for passwords? */
	  if ( !isatty(0) && !isatty(2) )
	    { msg(M_FATAL, "neither stdin nor stderr are a tty device, can't ask for %s password.  If you used --daemon, you need to use --askpass to make passphrase-protected keys work, and you can not use --auth-nocache.", prefix ); }
#endif

#ifdef ENABLE_CLIENT_CR
	  if (auth_challenge && (flags & GET_USER_PASS_DYNAMIC_CHALLENGE))
	    {
	      struct auth_challenge_info *ac = get_auth_challenge (auth_challenge, &gc);
	      if (ac)
		{
		  char *response = (char *) gc_malloc (USER_PASS_LEN, false, &gc);
		  struct buffer packed_resp;

		  buf_set_write (&packed_resp, (uint8_t*)up->password, USER_PASS_LEN);
		  msg (M_INFO|M_NOPREFIX, "CHALLENGE: %s", ac->challenge_text);
		  if (!get_console_input ("Response:", BOOL_CAST(ac->flags&CR_ECHO), response, USER_PASS_LEN))
		    msg (M_FATAL, "ERROR: could not read challenge response from stdin");
		  strncpynt (up->username, ac->user, USER_PASS_LEN);
		  buf_printf (&packed_resp, "CRV1::%s::%s", ac->state_id, response);
		}
	      else
		{
		  msg (M_FATAL, "ERROR: received malformed challenge request from server");
		}
	    }
	  else
#endif
	    {
	      struct buffer user_prompt = alloc_buf_gc (128, &gc);
	      struct buffer pass_prompt = alloc_buf_gc (128, &gc);

	      buf_printf (&user_prompt, "Enter %s Username:"******"Enter %s Password:"******"ERROR: could not read %s username from stdin", prefix);
		  if (strlen (up->username) == 0)
		    msg (M_FATAL, "ERROR: %s username is empty", prefix);
		}

	      if (!get_console_input (BSTR (&pass_prompt), false, up->password, USER_PASS_LEN))
		msg (M_FATAL, "ERROR: could not not read %s password from stdin", prefix);

#ifdef ENABLE_CLIENT_CR
	      if (auth_challenge && (flags & GET_USER_PASS_STATIC_CHALLENGE))
		{
		  char *response = (char *) gc_malloc (USER_PASS_LEN, false, &gc);
		  struct buffer packed_resp;
		  char *pw64=NULL, *resp64=NULL;

		  msg (M_INFO|M_NOPREFIX, "CHALLENGE: %s", auth_challenge);
		  if (!get_console_input ("Response:", BOOL_CAST(flags & GET_USER_PASS_STATIC_CHALLENGE_ECHO), response, USER_PASS_LEN))
		    msg (M_FATAL, "ERROR: could not read static challenge response from stdin");
		  if (openvpn_base64_encode(up->password, strlen(up->password), &pw64) == -1
		      || openvpn_base64_encode(response, strlen(response), &resp64) == -1)
		    msg (M_FATAL, "ERROR: could not base64-encode password/static_response");
		  buf_set_write (&packed_resp, (uint8_t*)up->password, USER_PASS_LEN);
		  buf_printf (&packed_resp, "SCRV1:%s:%s", pw64, resp64);
		  string_clear(pw64);
		  free(pw64);
		  string_clear(resp64);
		  free(resp64);
		}
#endif
	    }
	}
      else
	{
	  /*
	   * Get username/password from a file.
	   */
	  FILE *fp;
      
#ifndef ENABLE_PASSWORD_SAVE
	  /*
	   * Unless ENABLE_PASSWORD_SAVE is defined, don't allow sensitive passwords
	   * to be read from a file.
	   */
	  if (flags & GET_USER_PASS_SENSITIVE)
	    msg (M_FATAL, "Sorry, '%s' password cannot be read from a file", prefix);
#endif

	  warn_if_group_others_accessible (auth_file);

	  fp = platform_fopen (auth_file, "r");
	  if (!fp)
	    msg (M_ERR, "Error opening '%s' auth file: %s", prefix, auth_file);

	  if (flags & GET_USER_PASS_PASSWORD_ONLY)
	    {
	      if (fgets (up->password, USER_PASS_LEN, fp) == NULL)
		msg (M_FATAL, "Error reading password from %s authfile: %s",
		     prefix,
		     auth_file);
	    }
	  else
	    {
	      if (fgets (up->username, USER_PASS_LEN, fp) == NULL
		  || fgets (up->password, USER_PASS_LEN, fp) == NULL)
		msg (M_FATAL, "Error reading username and password (must be on two consecutive lines) from %s authfile: %s",
		     prefix,
		     auth_file);
	    }
      
	  fclose (fp);
      
	  chomp (up->username);
	  chomp (up->password);
      
	  if (!(flags & GET_USER_PASS_PASSWORD_ONLY) && strlen (up->username) == 0)
	    msg (M_FATAL, "ERROR: username from %s authfile '%s' is empty", prefix, auth_file);
	}

      string_mod (up->username, CC_PRINT, CC_CRLF, 0);
      string_mod (up->password, CC_PRINT, CC_CRLF, 0);

      up->defined = true;
    }

#if 0
  msg (M_INFO, "GET_USER_PASS %s u='%s' p='%s'", prefix, up->username, up->password);
#endif

  gc_free (&gc);

  return true;
}
Esempio n. 26
0
int
test_store(struct tf_test *thiz)
{
	_gc_cap struct node *n;
	size_t i;

	n = gc_malloc(2004);
	n->p = gc_cheri_ptr((void *)0x5678, 0x7890);
	for (i=0; i<2004-sizeof(*n); i++)
		n->v[i] = 99;
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	n->n = gc_malloc(2006);
	for (i=0; i<2004-sizeof(*n); i++)
		n->n->v[i] = 22;
	printf("n: %s\n", gc_cap_str(n));
	printf("n->n: %s\n", gc_cap_str(n->n));
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	gc_malloc(2005);
	//printf("addr: 0x%llx\n", &n); /* force n to stack */

	/*for (i = 0; i < 1000; i++)
	{
		n->n = gc_malloc(254);
		n = n->n;
	}*/
	gc_extern_collect();
	printf("n: %s\n", gc_cap_str(n));
	gc_extern_collect();
	printf("n: %s\n", gc_cap_str(n));
	gc_extern_collect();

	printf("n: %s\n", gc_cap_str(n));
	printf("n->n: %s\n", gc_cap_str(n->n));
	printf("n->p: %s\n", gc_cap_str(n->p));
	for (i=0; i<2004-sizeof(*n); i++)
		if (n->v[i] != 99) printf("n->v[%zu] is not 99!\n", i);
	for (i=0; i<2004-sizeof(*n); i++)
		if (n->n->v[i] != 22) printf("n->n->v[%zu] = 0x%x is not 22!\n", i, n->n->v[i]);
	return (TF_SUCC);
}
Esempio n. 27
0
int
test_ll(struct tf_test *thiz)
{
#define	LLHASH(i,j,t,p) (			\
	    (uint8_t)(((i)+(j)+(t)+(p))>>(j))	\
	)

#define	ALLOCATE_JUNK do {				\
	    junk = gc_malloc(junksz);			\
	    thiz->t_assert(junk != NULL);		\
	    for (tmp = 0; tmp < junksz / 4; tmp++)	\
		junk[tmp] = junkfill;			\
	} while (0)

	_gc_cap struct node * _gc_cap *np;
	_gc_cap struct node *hd, *p, *t;
	_gc_cap uint32_t *junk;
	size_t nsz, junksz, tmp;
	int nmax, i, j, junkn;
	uint32_t junkfill;
	uint8_t h;

	/* Configurable */
	nmax = 100;
	nsz = 201;
	junksz = 200;
	junkfill = 0x0BADDEAD;
	junkn = 3;

	if (nsz < sizeof(struct node))
		nsz = sizeof(struct node);

	/* Align junk size to 4 bytes */
	junksz &= ~(size_t)3;

	/* Allocate LL. */
	np = gc_cheri_ptr(&hd, sizeof(hd));
	p = NULL;
	for (i = 0; i < nmax; i++) {
		for (j = 0; j < junkn; j++)
			ALLOCATE_JUNK;
		*np = gc_malloc(nsz);
		t = *np;
		thiz->t_assert(t != NULL);
		ALLOCATE_JUNK;
		thiz->t_assert(t != NULL);
		thiz->t_assert(gc_cheri_getlen(t) >= nsz);
		t->p = p;
		p = t;
		np = &t->n;

		for (j = 0; j < nsz - sizeof(struct node); j++) {
			t->v[j] = LLHASH(i, j,
			    (int)(uintptr_t)t, (int)(uintptr_t)t->p);
		}
		ALLOCATE_JUNK;
	}

	/* Check LL. */
	p = NULL;
	t = hd;
	for (i = 0; i < nmax; i++) {
		thiz->t_pf("checking linked list node %d\n", i);
		thiz->t_assert(t != NULL);
		thiz->t_pf("current: %p, actual prev: %p, stored prev: %p, stored next: %p\n",
		   (void *)t, (void *)p, (void *)t->p, (void *)t->n);
		thiz->t_assert(t->p == p);
		for (j = 0; j < nsz - sizeof(struct node); j++) {
			h = LLHASH(i, j,
			    (int)(uintptr_t)t, (int)(uintptr_t)t->p);
			if (t->v[j] != h)
				thiz->t_pf("position %d: stored hash: 0x%x; actual hash: 0x%x\n", j, t->v[j], h);
			thiz->t_assert(t->v[j] == h);
		}
		p = t;
		t = t->n;
	}

	return (TF_SUCC);
}
Esempio n. 28
0
File: show.c Progetto: GJDuck/SMCHR
/*
 * Show a nil.
 */
extern char *show_nil(void)
{
    char *str = (char *)gc_malloc(4);
    show_buf_nil(str, str+4);
    return str;
}
Esempio n. 29
0
File: tex.c Progetto: Borf/CaveQuake
void
tex_loadall(void)
{
    int i;
    byte_t *rgb;
    int width, height, format, len, size;

    if (g->r_notextures)
      return;

    g->r_texture_data = (tex_data_t *) gc_malloc(g->r_numtextures * sizeof(tex_data_t));
    
    imgbuf = malloc(IMG_BUFSIZE);
   
    for (i=0; i < g->r_numtextures; ++i) {

        rgb = NULL;
        showProgress(i, g->r_numtextures-1);
	/* printf("loading...%s\n", g->r_texfiles[i].fname);  */

	len = strlen(g->r_texfiles[i].fname);
	if (!strcmp(&(g->r_texfiles[i].fname[len-4]), ".jpg"))
	{
	    if (!jpg_readtex(g->r_texfiles[i].fname, &rgb, &width, &height,
			     &format))
		Error("Could not open file %s", g->r_texfiles[i].fname);
		/* continue; */
                i = i; /* nothing */
	}
	else if (!strcmp(&(g->r_texfiles[i].fname[len-4]), ".tga"))
	{
	    if (!tga_readtex(g->r_texfiles[i].fname, &rgb, &width, &height,
			     &format))
	    {
		/* Might still be a jpg file !!! (compatibility with old
		   shader scripts?) */
		strcpy(&(g->r_texfiles[i].fname[len-3]), "jpg");
		if (!jpg_readtex(g->r_texfiles[i].fname, &rgb, &width,
				 &height, &format))
		{
		    /* FIXME: This should be an error, but still happens
		       in the demo levels ! */
		    strcpy(&(g->r_texfiles[i].fname[len-3]), "tga");
		    printf("Could not open file %s\n", g->r_texfiles[i].fname);
		    /* continue; */
		}
	    }
	}
	else {
	    Error("Unknown format for %s", g->r_texfiles[i].fname);
		/* continue;
 */
	}

        size = width*height* (format == GL_RGB ? 3 : 4);

        /* Not really a gamma: prelighten the texture to compensate for
           darkening after lightmap application. */
        if (rgb && (g->r_gamma != 1.0 || g->r_brightness != 0))
        {
    	    int i, val;
	    for (i=0; i<size; ++i)
	    {
	        val = (rgb[i] + g->r_brightness) * g->r_gamma;
                /*val = gammaCorrect(rgb[i], g->r_gamma);*/
	        if (val > 255) val = 255;
	        rgb[i] = val;
	    }
        }
	g->r_texture_data[i].rgb = rgb;
	g->r_texture_data[i].w = width;
	g->r_texture_data[i].h = height;
	g->r_texture_data[i].format = format;
    }
    free(imgbuf);
}
Esempio n. 30
0
/* Create a list node and return a pointer to it
 */
List *create_node(int v) {
    List *node = gc_malloc(sizeof(List));
    node->value = v;
    node->next = NULL;
    return node;
}