Ejemplo n.º 1
0
int
cmsg(const char *msgid)
{
	Item *itemp;
	int len;

	if (msgs_cache == (Cache *) NULL)
		if (init_cache(&msgs_cache, HASHSIZE, BSZ,
		    (int (*)())NULL, (int (*)())NULL) == -1) {
			(void) fprintf(stderr,
			    gettext("cmsg(): init_cache() failed.\n"));
			exit(1);
		}

	len = strlen(msgid) + 1;

	if ((itemp = lookup_cache(msgs_cache, (void *) msgid, len)) ==
	    Null_Item) {
		if ((itemp = (Item *) malloc(sizeof (*itemp))) == Null_Item) {
			(void) fprintf(stderr,
			    gettext("cmsg(): itemp=malloc(%d)\n"),
			    sizeof (*itemp));
			exit(1);
		}

		if ((itemp->key = (char *) malloc(len)) == NULL) {
			(void) fprintf(stderr,
			    gettext("cmsg(): itemp->key=malloc(%d)\n"),
			    len);
			exit(1);
		}
		(void) memmove(itemp->key, msgid, len);
		itemp->keyl = len;

		itemp->data = NULL;
		itemp->datal = 0;

		if (add_cache(msgs_cache, itemp) == -1)
			(void) fprintf(stderr,
			    gettext("cmsg(): add_cache() failed.\n"));

		return (0);
	} else {
		return (1);
	}
}
Ejemplo n.º 2
0
struct cached_block *
get_cached_block (block_sector_t sector, bool dirty)
{
  lock_acquire (&cache_lock);

  struct cached_block *cb = lookup_cache (sector);

  if (cb == NULL)
  {
    if (entry_count < MAX_CACHE_SIZE)
    {
      cb = (struct cached_block *) malloc (sizeof (struct cached_block));
      if (cb == NULL)
      {
        lock_release (&cache_lock);
        PANIC ("ERROR : Main and Cache memory full.");
      }
      cb->open = 0;
      entry_count++;
    }
    else
    {
      cb = evict_cache_block ();
      list_remove (&cb->elem);
    }
    cb->sector = sector;
    block_read (fs_device, sector, cb->data);
    cb->accessed = true;
    cb->open++;
    cb->dirty = dirty;
    list_push_back (&cache_list, &cb->elem);
  }
  else
  {
    cb->accessed = true;
    cb->open++;
    cb->dirty |= dirty;
  }
  lock_release (&cache_lock);
  return cb;
}
Ejemplo n.º 3
0
int __collatz(long long N)
{
	int t=0;
	long long n = N;
	
	if ((t = lookup_cache(N)) > 0)
		goto done;

	while (!(n & 0x1)) {
		n >>= 1;
		++t;
	}
	
	if (n != 1)
		t = __collatz(3*n+1);
	++t;
	
add_to_cache_return:
	add_to_cache(N, t);
done:	
	return t;
}
Ejemplo n.º 4
0
rep_struct_node *
rep_search_imports (rep_struct *s, repv var)
{
    rep_struct_node *n = lookup_cache (s, var);
    if (n != 0)
	return n;
    else
    {
	repv imports = s->imports;
	while (rep_CONSP (imports))
	{
	    n = lookup_recursively (rep_CAR (imports), var);
	    if (n != 0)
	    {
		enter_cache (s, n);
		return n;
	    }
	    imports = rep_CDR (imports);
	}
	return 0;
    }
}
Ejemplo n.º 5
0
void compile_dispatcher(NNF_NODE* node, Clause** learned_clause, DVtree* vtree, VtreeManager* vtree_manager, NnfManager* nnf_manager, SatState* sat_state) {

  //check cache
  VtreeCV item;
  if(lookup_cache(&item,vtree,vtree_manager)) {
    *node = item.node;
    *learned_clause = NULL;
    return;
  }

  //need to compile
  if(vtree_is_leaf(vtree)) 
    compile_vtree_leaf(node,learned_clause,vtree,nnf_manager);
  else if(vtree_is_shannon_node(vtree))
    compile_vtree_shannon(node,learned_clause,vtree,vtree_manager,nnf_manager,sat_state);
  else
    compile_vtree_decomposed(node,learned_clause,vtree,vtree_manager,nnf_manager,sat_state);

  //cache if a node is returned
  if(*learned_clause==NULL) { //otherwise, a node has not been returned
    item.node = *node;
    insert_cache(item,vtree,vtree_manager);
  }
}
Ejemplo n.º 6
0
/*
 * __gtxt(catname, id, dflt): Return a pointer to a message.
 *	catname is the name of the catalog. If null, the default catalog is
 *		used.
 *	id is the numeric id of the message in the catalogue
 *	dflt is the default message.
 *
 *	Information about non-existent catalogues is kept in db_info, in
 *	such a way that subsequent calls with the same catalogue do not
 *	try to open the catalogue again.
 */
const char *
__gtxt(const char *catname, int id, const char *dflt)
{
	char	*curloc;
	struct db_info *db;
	int	err;

	/* Check for invalid message id */
	if (id < 0)
		return (not_found);
	if (id == 0)
		return ((dflt && *dflt) ? dflt : not_found);

	/*
	 * If catalogue is unspecified, use default catalogue.
	 * No catalogue at all is an error
	 */
	if (!catname || !*catname) {
		lrw_rdlock(&_rw_cur_cat);
		if (cur_cat == NULL || !*cur_cat) {
			lrw_unlock(&_rw_cur_cat);
			return (not_found);
		}
		catname = cur_cat;
		lrw_unlock(&_rw_cur_cat);
	}

	curloc = setlocale(LC_MESSAGES, NULL);

	/* First look up the cache */
	db = lookup_cache(NULL, curloc, catname);
	if (db != NULL) {
		/*
		 * The catalog has been loaded, and if id seems valid,
		 * then just return.
		 */
		if (valid_msg(db, id))
			return (msg(db, id));

		/*
		 * seems given id is out of bound or does not exist. In this
		 * case, we need to look up a message for the "C" locale as
		 * documented in the man page.
		 */
		db = lookup_cache(NULL, def_locale, catname);
		if (db == NULL) {
			/*
			 * Even the message catalog for the "C" has not been
			 * loaded.
			 */
			db = load_db(def_locale, catname, &err);
			if (err)
				return (not_found);
		}
		if (valid_msg(db, id))
			return (msg(db, id));
		/* no message found */
		return ((dflt && *dflt) ? dflt : not_found);
	}

	/*
	 * The catalog has not been loaded or even has not
	 * attempted to be loaded, invalidate all caches related to
	 * the catname for possibly different locale.
	 */
	db = NULL;
	while ((db = lookup_cache(db, NULL, catname)) != NULL)
		unload_db(db);

	/*
	 * load a message catalog for the requested locale.
	 */
	db = load_db(curloc, catname, &err);
	if (err)
		return (not_found);
	if (valid_msg(db, id))
		return (msg(db, id));

	/*
	 * If the requested catalog is either not exist or message
	 * id is invalid, then try to load from "C" locale.
	 */
	db = load_db(def_locale, catname, &err);
	if (err)
		return (not_found);

	if (valid_msg(db, id))
		return (msg(db, id));

	/* no message found */
	return ((dflt && *dflt) ? dflt : not_found);
}
Ejemplo n.º 7
0
errno_t name2ip( in_addr_t *out, const char *name, int flags )
{
    int    ia, ib, ic, id;
    if( 4 == sscanf( name, "%d.%d.%d.%d", &ia, &ib, &ic, &id ) )
    {
        // No resolver required, ip4 addr given
        ipv4_addr iaddr = IPV4_DOTADDR_TO_ADDR( ia, ib, ic, id);
        *out = htonl( iaddr );
        SHOW_FLOW( 2, "parsed %s to %s", name, inet_ntoa(* (struct in_addr*)out) );
        return 0;
    }

    if(!inited)
        return ENXIO;

    int tries = 20;

    if(flags & RESOLVER_FLAG_NORETRY)
        tries = 1;

    ipv4_addr 	result;
    //ipv4_addr 	next_servers[MAX_DNS_SERVERS];

    SHOW_FLOW( 1, "request '%s'", name );

    ipv4_addr *	sptr = servers;
    int         sleft = MAX_DNS_SERVERS;

    if( !(flags & RESOLVER_FLAG_NORCACHE) )
        if( lookup_cache( out, name ) == 0 )
        {
            SHOW_FLOW0( 1, "got from cache");
            return 0;
        }

    // On OS stop don't produce network traffic
    if( (flags & RESOLVER_FLAG_NOWAIT) || phantom_stop_level )
        return ESRCH;

    while(tries--)
    {
        ipv4_addr 	server = *sptr++;

        if(sleft-- <= 0 || server == 0)
        {
            SHOW_ERROR0( 1, "No more places to look in, give up\n");
            return ENOENT;
        }

        SHOW_FLOW( 2, "look in %s", inet_ntoa(* (struct in_addr*)&server) );
        errno_t res = dns_request( (const unsigned char *)name, server, &result );

        if( res == 0 )//|| result != 0 )
        {
            SHOW_FLOW( 1, "answer is %s", inet_ntoa(* (struct in_addr*)&result) );
            *out = result;
            if( !(flags & RESOLVER_FLAG_NOWCACHE) )
                store_to_cache( result, name );
            return 0;
        }
    }

    return ENOENT;
}
Ejemplo n.º 8
0
PAM_EXTERN int pam_sm_authenticate(pam_handle_t* pamh, int flags, int argc, const char **argv) {
	int ret = 0;

	const char* pUsername = NULL;
	const char* pUrl = NULL;
	const char* pCaFile = NULL;
	const char* pKey = NULL;
	const char* pTimeout = NULL;
	const char* pHost = NULL;
	const char* pSalt = NULL;
	const char* pCacheDir = NULL;
	const char* pCacheTimeout = NULL;

	struct pam_message msg;
	struct pam_conv* pItem;
	struct pam_response* pResp;
	const struct pam_message* pMsg = &msg;

	int connection_timeout = 10;
	int cache_timeout = 60 * 10; // 10 minutes

    openlog("pam_http", LOG_ODELAY, LOG_AUTH);


    if (flags & PAM_SILENT) {
        setlogmask(LOG_UPTO(LOG_EMERG));
    } else {
        if(_get_argument("debug", argc, argv)) {
            setlogmask(LOG_UPTO(LOG_DEBUG));
        } else {
            setlogmask(LOG_UPTO(LOG_WARNING));
        } 
    }
	syslog(LOG_DEBUG, "Entering pam_sm_authenticate.");

	msg.msg_style = PAM_PROMPT_ECHO_OFF;
	msg.msg = "Password: "******"url", argc, argv);

	if (!pUrl)
		pUrl = getenv("PAM_HTTP_URL");

	if (!pUrl) {
        syslog(LOG_ERR, "Authentication URL not provided via url parameter or PAM_HTTP_URL.");
		return PAM_AUTH_ERR;
	}

	pCaFile = _get_argument("cafile", argc, argv);

	if (!pCaFile)
		pCaFile = getenv("PAM_HTTP_CA");

	if (pam_get_item(pamh, PAM_CONV, (const void**)&pItem) != PAM_SUCCESS || !pItem) {
		syslog(LOG_ERR, "Couldn't obtain PAM_CONV.");
		return PAM_AUTH_ERR;
	}

	pTimeout = _get_argument("timeout", argc, argv);

	if (!pTimeout)
		pTimeout = getenv("PAM_HTTP_TIMEOUT");

	if (pTimeout) {
		connection_timeout = atoi(pTimeout);
		if (connection_timeout < 1) connection_timeout = 1;
	}

	pKey = _get_argument("key", argc, argv);

	if (!pKey)
		pKey = getenv("PAM_HTTP_KEY");


	pCacheDir = _get_argument("cache_dir", argc, argv);

	if (!pCacheDir)
		pCacheDir = getenv("PAM_HTTP_CACHE_DIR");

	if (!pCacheDir) {
        syslog(LOG_WARNING, "Cache dir not provided via cache_dir parameter or PAM_HTTP_CACHE_DIR.");
	}

    pSalt = _get_argument("cache_salt", argc, argv);

	if (!pSalt)
		pSalt = getenv("PAM_HTTP_SECRET");

	if (!pSalt) {
        syslog(LOG_WARNING, "Cache salt not provided via cache_salt parameter or PAM_HTTP_CACHE_SECRET, using default.");
		pSalt = DEFAULT_SALT;
	}

	pTimeout = _get_argument("cache_timeout", argc, argv);

	if (!pCacheTimeout)
		pCacheTimeout = getenv("PAM_HTTP_TIMEOUT");

	if (pCacheTimeout) {
		cache_timeout = atoi(pCacheTimeout);
		if (cache_timeout < 1) cache_timeout = 1;
	}


	pItem->conv(1, &pMsg, &pResp, pItem->appdata_ptr);

	if (pam_get_item(pamh, PAM_RHOST, (const void**)&pHost) != PAM_SUCCESS) {
		syslog(LOG_ERR, "Unable to obtain remote address.");
		pHost = NULL;
	}

	ret = PAM_SUCCESS;

    if (pCacheDir && lookup_cache(pCacheDir, pUsername, pResp[0].resp, pSalt, cache_timeout) == 0) {
        return ret;
    }

	if (perform_authentication(pUrl, pUsername, pResp[0].resp, pCaFile, pKey, pHost, connection_timeout) != 0) {
		ret = PAM_AUTH_ERR;
	} else {
        if (pCacheDir)
            store_cache(pCacheDir, pUsername, pResp[0].resp, pSalt);
    }

	memset(pResp[0].resp, 0, strlen(pResp[0].resp));
	free(pResp);

	return ret;
}