示例#1
0
文件: evidence.c 项目: utrhira/mpik
/* added by Wakamori */
static kbool_t method_isPermissionAllowed(CTX ctx, kMethod *mtd)
{
	kDictMap *dmap = ctx->share->securityDictMap;
	CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf);
	CWB_write(ctx, cwb, S_tobytes(ClassTBL(mtd->cid)->sname));
	CWB_putc(ctx, cwb, '.');
	CWB_write(ctx, cwb, B(MN__(mtd->mn)));
	DBG_P("[methodname] : %s", CWB_tobytes(cwb).text);
	const char *idx, *p = role;
	while (p[0] != '\0') {
		idx = strchr((const char *)p, ',');
		kArray *a = NULL;
		if (idx != NULL) {
			a = (kArray *)knh_DictMap_getNULL(ctx, dmap, new_bytes2(p, idx - p));
			p = idx + 1;
		} else {
			a = (kArray *)knh_DictMap_getNULL(ctx, dmap, B(p));
			p += knh_strlen(p);
		}
		if (a != NULL) {
			int i;
			for (i = 0; i < knh_Array_size(a); i++) {
				const char *s = S_totext((kString*)knh_Array_n(a, i));
				if (strstr(s, CWB_tobytes(cwb).text) != NULL
					|| strstr(s, "all") != NULL) {
					// this method is allowed to be executed by this role
					CWB_close0(ctx, cwb);
					return 1;
				}
			}
		}
	}
	CWB_close0(ctx, cwb);
	return 0;
}
示例#2
0
static void knh_getToken(ksfp_t *sfp, AccessToken_t *token)
{
    kObject *consumer = sfp[0].ox->fields[0];
    kObject *rtoken = sfp[0].ox->fields[1];
    if (IS_NOTNULL(consumer)) {
        kObject *of = (kObject *)consumer;
        token->key = S_totext((kString*)of->fields[0]);
        token->secret = S_totext((kString*)of->fields[1]);
    }
    if (IS_NOTNULL(rtoken)) {
        kObject *of = (kObject *)rtoken;
        token->rtoken = S_totext((kString *)of->fields[0]);
        token->rtoken_secret = S_totext((kString *)of->fields[1]);
    }
    DBG_P("consumer_key: %s, consumer_secret: %s", token->key, token->secret);
    DBG_P("request_token: %s, request_token_secret: %s", token->rtoken, token->rtoken_secret);
}
示例#3
0
const char* knh_getmnname(CTX ctx, kmethodn_t mn)
{
    mn = MN_toFN(mn);
    if(mn < MN_OPSIZE) {
        return knh_getopname(mn);
    }
    return S_totext(knh_getFieldName(ctx, mn));
}
示例#4
0
文件: runtime.c 项目: utrhira/mpik
static void knh_shell(CTX ctx)
{
	void *shell_status = NULL;
	BEGIN_LOCAL(ctx, lsfp, 2);
//	LOCAL_NEW(ctx, lsfp, 0, kInputStream *, bin, new_BytesInputStream(ctx, new_Bytes(ctx, "shell", K_PAGESIZE)));
	{
		CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
		knh_showWelcome(ctx, cwb->w);
		knh_showSecurityAlert(ctx, cwb->w);
		shell_status = shell_init(ctx, CWB_totext(ctx, cwb), NULL);
		CWB_close(ctx, cwb);
	}
	while(1) {
		{
			CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
			kstatus_t status = readstmt(ctx, cwb);
			if(status == K_BREAK) {
				CWB_close(ctx, cwb);
				break;
			}
			if(CWB_size(cwb) == 0) {
				CWB_close(ctx, cwb);
				continue;
			}
			status = shell_command(ctx, CWB_totext(ctx, cwb));
			if(status == K_BREAK) {
				CWB_close(ctx, cwb);
				break;
			}
			if(status == K_REDO) {
				CWB_close(ctx, cwb);
				continue;
			}
#ifdef K_USING_SUGAR
			kString *script = CWB_newString(ctx, cwb, 0);
			KNH_SETv(ctx, lsfp[0].o, script);
			knh_beval2(ctx, S_totext(script), 1);
#else
			kInputStream *bin = new_BytesInputStream(ctx, CWB_totext(ctx, cwb), CWB_size(cwb));
			KNH_SETv(ctx, lsfp[0].o, bin);
			knh_beval(ctx, bin, 1);
#endif
		}
		knh_OutputStream_flush(ctx, ctx->out);
		if(ctx->isEvaled == 1) {
			CWB_t cwbbuf, *cwb = CWB_open(ctx, &cwbbuf);
			knh_write_Object(ctx, cwb->w, ctx->evaled, FMT_dump);
			knh_showSecurityAlert(ctx, cwb->w);
			if(CWB_size(cwb) !=0) {
				shell_display(ctx, shell_status, CWB_totext(ctx, cwb));
			}
			CWB_close(ctx, cwb);
			WCTX(ctx)->isEvaled = 0;
		}
	}
	shell_cleanup(ctx, shell_status);
	END_LOCAL(ctx, lsfp);
}
示例#5
0
void test_classdef_load(CTX)
{
    kclass_t *ct;
    ct = class_load(_ctx, BUILD_DIR "classdef_sample.dylib", "Sample");
    assert(ct);
    assert(strcmp(S_totext(ct->name), "Sample") == 0);
    assert(ct->cflag == 2);
    assert(ct->cstruct_size == 64/*sizeof(struct Sample)*/);
    sample_cid = ct->cid;
}
示例#6
0
knh_Fmethod knh_gluefunc(CTX ctx, kMethod *mtd, kNameSpace *ns, kDictMap *mdata)
{
	knh_Fmethod gluefunc = NULL;
	kObject *gluedata = knh_DictMap_getNULL(ctx, mdata, STEXT("gluefunc"));
	if(gluedata != NULL && IS_bString(gluedata)) {
		if(ns->gluehdr == NULL) {
			DBG_P("gluehdr is not open");
		}
		else {
			gluefunc = (knh_Fmethod)knh_dlsym(ctx, ns->gluehdr, S_totext((kString*)gluedata), NULL, 0);
			if(gluefunc == NULL) {
				DBG_P("gluefunc is not found: %s", S_totext((kString*)gluedata));
			}
		}
	}
	//if(gluefunc == NULL) {
	//	gluefunc = Fmethod_FFI;
	//}
	return gluefunc;
}
示例#7
0
static void knh_setArgs(CTX ctx, Args_t *args, kMap *m)
{
    kDictMap *dmap = knh_toDictMap(ctx, m, 0);
    ksfp_t *lsfp = ctx->esp;
    knitr_t mitrbuf = K_NITR_INIT, *mitr = &mitrbuf;
    klr_setesp(ctx, lsfp+1);
    char buf[256] = {0};
    while(m->spi->next(ctx, m->mapptr, mitr, lsfp)) {
        const char *key = S_totext(lsfp[0].s);
        Object *o = knh_DictMap_getNULL(ctx, dmap, S_tobytes(lsfp[0].s));
        switch (O_cid(o)) {
        case CLASS_Int:
            knh_snprintf(buf, sizeof(buf), "%s=" KINT_FMT, key, N_toint(o));
            break;
        case CLASS_Float:
            knh_snprintf(buf, sizeof(buf), "%s=" KFLOAT_FMT, key, N_tofloat(o));
            break;
        case CLASS_Boolean:
            knh_snprintf(buf, sizeof(buf), "%s=%s",
                         key, N_tobool(o) ? "true" : "false");
            break;
        case CLASS_String:
            knh_snprintf(buf, sizeof(buf), "%s=%s", key, S_totext((kString *)o));
            break;
        default:
            TODO();
            break;
        }
        DBG_P("m[%s] = {class:%s, struct:%s, o:%s}", S_totext(lsfp[0].s),
              CLASS__(O_cid(o)), STRUCT__(O_bcid(o)), O__(o));
        if (knh_strlen(buf) > 0) {
            DBG_P("param: \"%s\"", buf);
            oauth_add_param_to_array(&args->argc, &args->argv, buf);
        }
        buf[0] = '\0';
        klr_setesp(ctx, lsfp+1);
    }
}
示例#8
0
kuri_t knh_getURI(CTX ctx, kbytes_t t)
{
    OLD_LOCK(ctx, LOCK_SYSTBL, NULL);
    kindex_t idx = knh_DictSet_index(ctx->share->urnDictSet, t);
    if(idx == -1) {
        kString *s = new_String2(ctx, CLASS_String, t.text, t.len, SPOL_POOLALWAYS);
        idx = knh_Array_size(ctx->share->urns);
        knh_DictSet_set(ctx, ctx->share->urnDictSet, s, idx);
        knh_Array_add(ctx, ctx->share->urns, s);
        KNH_NTRACE2(ctx, "konoha:newuri", K_OK, KNH_LDATA(LOG_s("urn", S_totext(s)), LOG_i("uri", idx)));
    }
    else {
        idx = knh_DictSet_valueAt(ctx->share->urnDictSet, idx);
    }
    OLD_UNLOCK(ctx, LOCK_SYSTBL, NULL);
    return (kuri_t)idx;
}
示例#9
0
kPath* new_CurrentPath(CTX ctx)
{
	char buf[K_PATHMAX];
#if defined(K_USING_WINDOWS_)
	char *ptr = _fullpath(buf, ".", K_PATHMAX);
#elif defined(K_USING_POSIX_)
	char *ptr = realpath(".", buf);
#else
	char *ptr = NULL;
	buf[0] = '.'; buf[1] = 0;
#endif
	kPath *pth = new_(Path);
	KNH_SETv(ctx, pth->urn, new_String2(ctx, CLASS_String, buf, knh_strlen(buf), SPOL_POOLALWAYS));
	pth->ospath = S_totext(pth->urn);
	if(ptr != buf && ptr != NULL) {
		free(ptr);
	}
	kObjectoNULL(ctx, pth);
	return pth;
}
示例#10
0
文件: runtime.c 项目: utrhira/mpik
static void opt_help(CTX ctx, int mode, const char *optstr)
{
	kString* home = knh_getPropertyNULL(ctx, STEXT("konoha.home.path"));
	fprintf(stdout, HELPMSG, S_totext(home), knh_getSystemEncoding());
	exit(0);
}
示例#11
0
};

DEFAPI(void) constXmlReader(CTX ctx, kclass_t cid, const knh_LoaderAPI_t *kapi)
{
	kapi->loadClassIntConst(ctx, cid, XmlReaderConstint);
}

//## @Native @Throwable XmlReader XmlReader.new(Path _);
KMETHOD XmlReader_new(CTX ctx, ksfp_t *sfp _RIX)
{
	kPath *pth = sfp[1].pth;
	xmlTextReaderPtr reader = xmlNewTextReaderFilename(pth->ospath);
	kRawPtr *po = new_ReturnCppObject(ctx, sfp, reader, NULL);
	if(reader == NULL) {
		KNH_NTHROW2(ctx, sfp, "IO!!", "xmlNewTextReader", K_FAILED,
				KNH_LDATA(LOG_s("urn", S_totext(pth->urn)), LOG_s("ospath", pth->ospath)));
	}
	RETURN_(po);
}

//## @Native void XmlReader.close();
KMETHOD XmlReader_close(CTX ctx, ksfp_t *sfp _RIX)
{
	xmlTextReaderPtr reader = (xmlTextReaderPtr)(sfp[0].p)->rawptr;
	if(reader != NULL) {
		xmlTextReaderClose(reader);
	}
	RETURNvoid_();
}

//## @Native String XmlReader.getQuoteChar();
示例#12
0
文件: evidence.c 项目: utrhira/mpik
/* added by Wakamori */
void loadPolicy(CTX ctx)
{
	if (enforce_security == 0) return;
	// load $konoha.home.path/policy
	knh_setProperty(ctx, new_String(ctx, "role"), (dynamic *)new_String(ctx, role));
	CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf);
	kString *s = knh_getPropertyNULL(ctx, STEXT("konoha.home.path"));
	CWB_write(ctx, cwb, S_tobytes(s));
	CWB_write(ctx, cwb, STEXT("/policy"));
	kInputStream *is = new_InputStream(ctx, NULL, new_Path(ctx, CWB_newString0(ctx, cwb)));

	if (is == NULL) {
		DBG_P("policy file not found. All @Restricted annotated method is rescricted");
	}
	else {
		/*
		if (enforce_security == 0) {
			enforce_security = 1;
			knh_memcpy(role, "Default", 7);
			role[7] = '\0';
		}
		*/
		// parse policy file written in JSON
		// it must be refactored in the future
		kDictMap *dmap = ctx->share->securityDictMap;
		kString *line = knh_InputStream_readLine(ctx, is);
		while (IS_NOTNULL(line)) {
			//fprintf(stderr, "line=%s\n", S_totext(line));
			if (S_equals(line, STEXT("[")) || S_equals(line, STEXT("]"))) {
				/* ignore */
			} else {
				kString *key = NULL;
				kArray *a = new_Array(ctx, CLASS_String, 0);
				const char *idx = NULL;
				char *p = strstr(S_totext(line), "\"name\": \"");
				if (p != NULL) {
					p += 9; // == strlen("\"name\": \"")
					idx = strchr((const char *)p, '"');
					if (idx != NULL) {
						p[idx - p] = '\0';
						//fprintf(stderr, "name: %s\n", p);
						//knh_DictMap_set(ctx, dmap, new_String(ctx, "name"), new_String(ctx, p));
						key = new_String(ctx, p);
						p = (char *)idx + 1;
					}
				}
				p = strstr((const char *)p, "\"permission\": [");
				if (p != NULL) {
					p += 16; // == strlen("\"permission\": \[\"")
					idx = strchr((const char *)p, '"');
					while (idx != NULL) {
						p[idx - p] = '\0';
						if (strstr((const char *)p, ", ") == NULL) {
							//fprintf(stderr, "permission: %s\n", p);
							knh_Array_add(ctx, a, new_String(ctx, p));
						}
						p = (char *)idx + 1;
						idx = strchr((const char *)p, '"');
					}
				}
				if (key != NULL) {
					knh_DictMap_set(ctx, dmap, key, a);
				}
			}
			line = knh_InputStream_readLine(ctx, is);
		}
		knh_InputStream_close(ctx, is);
	}
}