int maSetupTrace(MaHost *host, cchar *ext) { if (host->traceLevel > mprGetLogLevel(host)) { return 0; } if (ext) { if (host->traceInclude && !mprLookupHash(host->traceInclude, ext)) { return 0; } if (host->traceExclude && mprLookupHash(host->traceExclude, ext)) { return 0; } } return host->traceMask; }
int maAddGroup(MaAuth *auth, char *group, MaAcl acl, bool enabled) { MaGroup *gp; mprAssert(auth); mprAssert(group && *group); gp = maCreateGroup(auth, group, acl, enabled); if (gp == 0) { return MPR_ERR_NO_MEMORY; } /* * Create the index on demand */ if (auth->groups == 0) { auth->groups = mprCreateHash(auth, -1); } if (mprLookupHash(auth->groups, group)) { return MPR_ERR_ALREADY_EXISTS; } if (mprAddHash(auth->groups, group, gp) == 0) { return MPR_ERR_NO_MEMORY; } return 0; }
void *maLookupStageData(MaHttp *http, cchar *name) { MaStage *stage; stage = (MaStage*) mprLookupHash(http->stages, name); return stage->stageData; }
int maAddUser(MaAuth *auth, cchar *realm, cchar *user, cchar *password, bool enabled) { MaUser *up; char *key; up = maCreateUser(auth, realm, user, password, enabled); if (up == 0) { return MPR_ERR_NO_MEMORY; } if (auth->users == 0) { auth->users = mprCreateHash(auth, -1); } key = mprStrcat(auth, -1, realm, ":", user, NULL); if (mprLookupHash(auth->users, key)) { mprFree(key); return MPR_ERR_ALREADY_EXISTS; } if (mprAddHash(auth->users, key, up) == 0) { mprFree(key); return MPR_ERR_NO_MEMORY; } mprFree(key); return 0; }
static MprRomInode *lookup(MprRomFileSystem *rfs, cchar *path) { if (path == 0) { return 0; } /* * Remove "./" segments */ while (*path == '.') { if (path[1] == '\0') { path++; } else if (path[1] == '/') { path += 2; } else { break; } } /* * Skip over the leading "/" */ if (*path == '/') { path++; } return (MprRomInode*) mprLookupHash(rfs->fileIndex, path); }
int maRemoveGroup(MaAuth *auth, cchar *group) { if (auth->groups == 0 || !mprLookupHash(auth->groups, group)) { return MPR_ERR_CANT_ACCESS; } mprRemoveHash(auth->groups, group); return 0; }
static cchar *getHeader(void *handle, cchar *key) { MaRequest *req; MaConn *conn; conn = handle; req = conn->request; return (cchar*) mprLookupHash(req->headers, key); }
bool maIsGroupEnabled(MaAuth *auth, cchar *group) { MaGroup *gp; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return 0; } return gp->enabled; }
cvoid *maGetStageData(MaConn *conn, cchar *key) { MaRequest *req; req = conn->request; if (req->requestData == 0) { return NULL; } return mprLookupHash(req->requestData, key); }
MaAcl maGetGroupAcl(MaAuth *auth, char *group) { MaGroup *gp; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return MPR_ERR_CANT_ACCESS; } return gp->acl; }
int maSetGroupAcl(MaAuth *auth, cchar *group, MaAcl acl) { MaGroup *gp; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return MPR_ERR_CANT_ACCESS; } gp->acl = acl; return 0; }
int maEnableGroup(MaAuth *auth, cchar *group) { MaGroup *gp; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return MPR_ERR_CANT_ACCESS; } gp->enabled = 1; return 0; }
int maRemoveUser(MaAuth *auth, cchar *realm, cchar *user) { char *key; key = mprStrcat(auth, -1, realm, ":", user, NULL); if (auth->users == 0 || !mprLookupHash(auth->users, key)) { mprFree(key); return MPR_ERR_CANT_ACCESS; } mprRemoveHash(auth->users, key); mprFree(key); return 0; }
int maEnableUser(MaAuth *auth, cchar *realm, cchar *user) { MaUser *up; char *key; up = 0; key = mprStrcat(auth, -1, realm, ":", user, NULL); if (auth->users == 0 || (up = (MaUser*) mprLookupHash(auth->users, key)) == 0) { return MPR_ERR_CANT_ACCESS; } up->enabled = 1; return 0; }
bool maIsUserEnabled(MaAuth *auth, cchar *realm, cchar *user) { MaUser *up; char *key; up = 0; key = mprStrcat(auth, -1, realm, ":", user, NULL); if (auth->users == 0 || (up = (MaUser*) mprLookupHash(auth->users, key)) == 0) { mprFree(key); return 0; } mprFree(key); return up->enabled; }
cchar *maGetNativePassword(MaConn *conn, cchar *realm, cchar *user) { MaUser *up; MaAuth *auth; char *key; auth = conn->request->auth; up = 0; key = mprStrcat(conn, -1, realm, ":", user, NULL); if (auth->users) { up = (MaUser*) mprLookupHash(auth->users, key); } mprFree(key); if (up == 0) { return 0; } return up->password; }
int maRemoveUsersFromGroup(MaAuth *auth, cchar *group, cchar *users) { MaGroup *gp; cchar *tok; char ubuf[80], *user; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return MPR_ERR_CANT_ACCESS; } tok = NULL; user = mprGetWordTok(ubuf, sizeof(ubuf), users, " \t", &tok); while (user) { maRemoveUserFromGroup(gp, user); user = mprGetWordTok(ubuf, sizeof(ubuf), 0, " \t", &tok); } return 0; }
int maAddUsersToGroup(MaAuth *auth, cchar *group, cchar *users) { MaGroup *gp; cchar *tok; char ubuf[80], *user; gp = 0; if (auth->groups == 0 || (gp = (MaGroup*) mprLookupHash(auth->groups, group)) == 0) { return MPR_ERR_CANT_ACCESS; } tok = NULL; user = mprGetWordTok(ubuf, sizeof(ubuf), users, " \t", &tok); while (user) { /* Ignore already exists errors */ maAddUserToGroup(auth, gp, user); user = mprGetWordTok(ubuf, sizeof(ubuf), 0, " \t", &tok); } return 0; }
static int getAlphaToken(EcInput *input, EcToken *tp, int c) { ReservedWord *rp; EcStream *stream; /* * We know that c is an alpha already */ // TBD -- does ES4 allow $ stream = input->stream; while (isalnum(c) || c == '_' || c == '$' || c == '\\') { if (c == '\\') { c = getNextChar(stream); if (c == '\n' || c == '\r') { break; } else if (c == 'u') { c = decodeNumber(input, 16, 4); } } addCharToToken(tp, c); c = getNextChar(stream); } if (c) { putBackChar(stream, c); } // TODO - need to take into account contextually reserved and // full reserved words. rp = (ReservedWord*) mprLookupHash(input->lexer->keywords, (char*) tp->text); if (rp) { return finishToken(tp, rp->tokenId, rp->subId, rp->groupMask); } else { return finishToken(tp, T_ID, -1, 0); } }
/* * This runs when all input data has been received. The egi form must write all the data. * It currently does not support forms that return before writing all the data. */ static void runEgi(MaQueue *q) { MaConn *conn; MaRequest *req; MaEgiForm *form; MaEgi *egi; conn = q->conn; req = conn->request; egi = (MaEgi*) q->stage->stageData; maSetHeader(conn, 0, "Last-Modified", req->host->currentDate); maDontCacheResponse(conn); maPutForService(q, maCreateHeaderPacket(conn), 0); form = (MaEgiForm*) mprLookupHash(egi->forms, req->url); if (form == 0) { maFailRequest(conn, MPR_HTTP_CODE_NOT_FOUND, "Egi Form: \"%s\" is not defined", req->url); } else { (*form)(q); } maPutForService(q, maCreateEndPacket(conn), 1); }
void maLogRequest(MaConn *conn) { MaHost *logHost, *host; MaResponse *resp; MaRequest *req; MprBuf *buf; char keyBuf[80], *timeText, *fmt, *cp, *qualifier, *value, c; int len; resp = conn->response; req = conn->request; host = req->host; logHost = host->logHost; if (logHost == 0) { return; } fmt = logHost->logFormat; if (fmt == 0) { return; } if (req->method == 0) { return; } len = MPR_MAX_URL + 256; buf = mprCreateBuf(resp, len, len); while ((c = *fmt++) != '\0') { if (c != '%' || (c = *fmt++) == '%') { mprPutCharToBuf(buf, c); continue; } switch (c) { case 'a': /* Remote IP */ mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'A': /* Local IP */ mprPutStringToBuf(buf, conn->sock->listenSock->ipAddr); break; case 'b': if (resp->bytesWritten == 0) { mprPutCharToBuf(buf, '-'); } else { mprPutIntToBuf(buf, resp->bytesWritten); } break; case 'B': /* Bytes written (minus headers) */ mprPutIntToBuf(buf, resp->bytesWritten - resp->headerSize); break; case 'h': /* Remote host */ mprPutStringToBuf(buf, conn->remoteIpAddr); break; case 'n': /* Local host */ mprPutStringToBuf(buf, req->parsedUri->host); break; case 'l': /* Supplied in authorization */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case 'O': /* Bytes written (including headers) */ mprPutIntToBuf(buf, resp->bytesWritten); break; case 'r': /* First line of request */ mprPutFmtToBuf(buf, "%s %s %s", req->methodName, req->parsedUri->originalUri, req->httpProtocol); break; case 's': /* Response code */ mprPutIntToBuf(buf, resp->code); break; case 't': /* Time */ mprPutCharToBuf(buf, '['); timeText = mprFormatLocalTime(conn, mprGetTime(conn)); mprPutStringToBuf(buf, timeText); mprFree(timeText); mprPutCharToBuf(buf, ']'); break; case 'u': /* Remote username */ mprPutStringToBuf(buf, req->user ? req->user : "******"); break; case '{': /* Header line */ qualifier = fmt; if ((cp = strchr(qualifier, '}')) != 0) { fmt = &cp[1]; *cp = '\0'; c = *fmt++; mprStrcpy(keyBuf, sizeof(keyBuf), "HTTP_"); mprStrcpy(&keyBuf[5], sizeof(keyBuf) - 5, qualifier); mprStrUpper(keyBuf); switch (c) { case 'i': value = (char*) mprLookupHash(req->headers, keyBuf); mprPutStringToBuf(buf, value ? value : "-"); break; default: mprPutStringToBuf(buf, qualifier); } *cp = '}'; } else { mprPutCharToBuf(buf, c); } break; case '>': if (*fmt == 's') { fmt++; mprPutIntToBuf(buf, resp->code); } break; default: mprPutCharToBuf(buf, c); break; } } mprPutCharToBuf(buf, '\n'); mprAddNullToBuf(buf); mprWrite(logHost->accessLog, mprGetBufStart(buf), mprGetBufLength(buf)); }
static cchar *getHeader(void *handle, cchar *key) { return (cchar*) mprLookupHash(requestHeaders, key); }
/* * Get a request and parse environment and request URI. Given: * * http /simple.cgi/abc/def?a=b&c=d * DOCUMENT_ROOT=/Users/mob/hg/appweb/src/server/web * GATEWAY_INTERFACE=CGI/1.1 * CONTENT_LENGTH=NNN * CONTENT_TYPE * PATH_INFO=/abc/def * PATH_TRANSLATED=/Users/mob/hg/appweb/src/server/web/ab/def * QUERY_STRING=a=b&c=d * REMOTE_ADDR=10.0.0.1234 * REMOTE_HOST * REMOTE_PORT * REQUEST_URI=/simple.cgi * REQUEST_METHOD=GET * REQUEST_TRANSPORT=http * SCRIPT_FILENAME=/Users/mob/hg/appweb/src/server/web/simple.cgi * SERVER_NAME=127.0.0.1:7777 * SERVER_PORT=7777 * SERVER_SOFTWARE=Embedthis-Appweb/3.0A.1 * SERVER_PROTOCOL=http * HTTP_ACCEPT= * HTTP_COOKIE= * HTTP_REFERRER= * HTTP_CONNECTION=Keep-Alive * HTTP_HOST=127.0.0.1 * HTTP_USER_AGENT=Embedthis-http/3.0A.1 * HTTPS * HTTP_* * PATH= */ static int getRequest() { char key[MPR_MAX_STRING], *ep, *cp, *value; int len, i; formVars = mprCreateHash(mpr, EJS_CGI_HDR_HASH); requestHeaders = mprCreateHash(mpr, EJS_CGI_HDR_HASH); responseHeaders = mprCreateHash(mpr, EJS_CGI_HDR_HASH); if (debug) { debugFile = fopen("/tmp/ejscgi.log", "w+"); } for (i = 0; environ && environ[i]; i++) { if ((ep = environ[i]) == 0) { continue; } if ((cp = strchr(ep, '=')) != 0) { len = cp - ep; mprMemcpy(key, sizeof(key), ep, len); key[len] = '\0'; mprAddHash(requestHeaders, key, ++cp); if (debugFile) { fprintf(debugFile, "%-20s = %s\n", key, cp); } } } if (debugFile) { fclose(debugFile); debugFile = 0; } #if BLD_DEBUG if (dummy) { mprAddHash(requestHeaders, "SCRIPT_NAME", mprStrdup(mpr, "/cgi/ejscgi")); mprAddHash(requestHeaders, "DOCUMENT_ROOT", mprStrdup(mpr, "/Users/mob/hg/carmen")); mprAddHash(requestHeaders, "PATH_TRANSLATED", mprStrdup(mpr, "/Users/mob/hg/carmen")); mprAddHash(requestHeaders, "QUERY_STRING", mprStrdup(mpr, "a=b&c=d")); mprAddHash(requestHeaders, "PATH_INFO", mprStrdup(mpr, "/carmen/stock/")); mprAddHash(requestHeaders, "REQUEST_URI", mprStrdup(mpr, "/cgi-bin/ejscgi/carmen/stock/")); // mprAddHash(requestHeaders, "PATH_INFO", mprStrdup(mpr, "/carmen/web/style.css")); // mprAddHash(requestHeaders, "REQUEST_URI", mprStrdup(mpr, "/cgi-bin/ejscgi/carmen/web/style.css")); // mprAddHash(requestHeaders, "PATH_INFO", mprStrdup(mpr, "/carmen/web/images/banner.jpg")); // mprAddHash(requestHeaders, "REQUEST_URI", mprStrdup(mpr, "/cgi-bin/ejscgi/carmen/web/images/banner.jpg")); } #endif documentRoot = (char*) mprLookupHash(requestHeaders, "DOCUMENT_ROOT"); cookie = (char*) mprLookupHash(requestHeaders, "HTTP_COOKIE"); contentType = (char*) mprLookupHash(requestHeaders, "CONTENT_TYPE"); query = (char*) mprLookupHash(requestHeaders, "QUERY_STRING"); pathTranslated = (char*) mprLookupHash(requestHeaders, "PATH_TRANSLATED"); pathInfo = (char*) mprLookupHash(requestHeaders, "PATH_INFO"); scriptName = (char*) mprLookupHash(requestHeaders, "SCRIPT_NAME"); uri = (char*) mprLookupHash(requestHeaders, "REQUEST_URI"); if (documentRoot == 0 || pathInfo == 0 || scriptName == 0 || uri == 0) { error(NULL, 0, "CGI environment not setup correctly"); return EJS_ERR; } value = (char*) mprLookupHash(requestHeaders, "CONTENT_LENGTH"); if (value) { contentLength = atoi(value); } ext = strrchr(uri, '.'); if (createFormData() < 0) { return EJS_ERR; } return 0; }
MaStage *maLookupStage(MaHttp *http, cchar *name) { return (MaStage*) mprLookupHash(http->stages, name); }
/* * Determine if this user is specified as being eligible for this realm. We examine the requiredUsers and requiredGroups. */ static bool isUserValid(MaAuth *auth, cchar *realm, cchar *user) { MaGroup *gp; MaUser *up; cchar *tok, *gtok; char ubuf[80], gbuf[80], *key, *requiredUser, *group, *name; int rc, next; if (auth->anyValidUser) { key = mprStrcat(auth, -1, realm, ":", user, NULL); if (auth->users == 0) { return 0; } rc = mprLookupHash(auth->users, key) != 0; mprFree(key); return rc; } if (auth->requiredUsers) { tok = NULL; requiredUser = mprGetWordTok(ubuf, sizeof(ubuf), auth->requiredUsers, " \t", &tok); while (requiredUser) { if (strcmp(user, requiredUser) == 0) { return 1; } requiredUser = mprGetWordTok(ubuf, sizeof(ubuf), 0, " \t", &tok); } } if (auth->requiredGroups) { gtok = NULL; group = mprGetWordTok(gbuf, sizeof(gbuf), auth->requiredGroups, " \t", >ok); /* * For each group, check all the users in the group. */ while (group) { if (auth->groups == 0) { gp = 0; } else { gp = (MaGroup*) mprLookupHash(auth->groups, group); } if (gp == 0) { mprError(auth, "Can't find group %s", group); group = mprGetWordTok(gbuf, sizeof(gbuf), 0, " \t", >ok); continue; } for (next = 0; (name = mprGetNextItem(gp->users, &next)) != 0; ) { if (strcmp(user, name) == 0) { return 1; } } group = mprGetWordTok(gbuf, sizeof(gbuf), 0, " \t", >ok); } } if (auth->requiredAcl != 0) { key = mprStrcat(auth, -1, realm, ":", user, NULL); up = (MaUser*) mprLookupHash(auth->users, key); if (up) { mprLog(auth, 6, "UserRealm \"%s\" has ACL %lx, Required ACL %lx", key, up->acl, auth->requiredAcl); if (up->acl & auth->requiredAcl) { mprFree(key); return 1; } } mprFree(key); } return 0; }