static int ejs_net_samsync_ldb(MprVarHandle eid, int argc, struct MprVar **argv) { TALLOC_CTX *mem_ctx; struct libnet_context *ctx; struct libnet_samsync_ldb *samsync; NTSTATUS status; ctx = mprGetThisPtr(eid, "ctx"); mem_ctx = talloc_new(mprMemCtx()); samsync = talloc(mem_ctx, struct libnet_samsync_ldb); if (!samsync) { talloc_free(mem_ctx); return -1; } /* prepare parameters for the samsync */ samsync->in.machine_account = NULL; samsync->in.session_info = NULL; samsync->in.binding_string = NULL; samsync->out.error_string = NULL; if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) { MprVar *credentials = mprGetProperty(argv[0], "machine_account", NULL); MprVar *session_info = mprGetProperty(argv[0], "session_info", NULL); if (credentials) { samsync->in.machine_account = talloc_get_type(mprGetPtr(credentials, "creds"), struct cli_credentials); } if (session_info) { samsync->in.session_info = talloc_get_type(mprGetPtr(session_info, "session_info"), struct auth_session_info); } }
/* set a mpr component, allowing for sub objects, using the '.' convention destroys 'val' after setting */ NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val) { const char *p = strchr(name, '.'); char *objname; struct MprVar *v2; NTSTATUS status; if (p == NULL) { v2 = mprSetProperty(v, name, &val); if (v2 == NULL) { DEBUG(1,("mprSetVar unable to set '%s'\n", name)); return NT_STATUS_INVALID_PARAMETER_MIX; } mprDestroyVar(&val); return NT_STATUS_OK; } objname = talloc_strndup(mprMemCtx(), name, p-name); if (objname == NULL) { return NT_STATUS_NO_MEMORY; } v2 = mprGetProperty(v, objname, NULL); if (v2 == NULL) { mprSetVar(v, objname, mprObject(objname)); v2 = mprGetProperty(v, objname, NULL); } status = mprSetVar(v2, p+1, val); talloc_free(objname); return status; }
static int ejs_net_join_domain(MprVarHandle eid, int argc, struct MprVar **argv) { TALLOC_CTX *mem_ctx; struct libnet_context *ctx; struct libnet_Join *join; NTSTATUS status; ctx = mprGetThisPtr(eid, "ctx"); mem_ctx = talloc_new(mprMemCtx()); join = talloc(mem_ctx, struct libnet_Join); if (!join) { talloc_free(mem_ctx); return -1; } /* prepare parameters for the join */ join->in.netbios_name = NULL; join->in.join_type = SEC_CHAN_WKSTA; join->in.domain_name = cli_credentials_get_domain(ctx->cred); join->in.level = LIBNET_JOIN_AUTOMATIC; join->out.error_string = NULL; if (argc == 1 && argv[0]->type == MPR_TYPE_OBJECT) { MprVar *netbios_name = mprGetProperty(argv[0], "netbios_name", NULL); MprVar *domain_name = mprGetProperty(argv[0], "domain_name", NULL); MprVar *join_type = mprGetProperty(argv[0], "join_type", NULL); if (netbios_name) { join->in.netbios_name = mprToString(netbios_name); } if (domain_name) { join->in.domain_name = mprToString(domain_name); } if (join_type) { join->in.join_type = mprToInt(join_type); } } if (!join->in.domain_name) { ejsSetErrorMsg(eid, "a domain must be specified for to join"); talloc_free(mem_ctx); return -1; } /* do the domain join */ status = libnet_Join(ctx, join, join); if (!NT_STATUS_IS_OK(status)) { MprVar error_string = mprString(join->out.error_string); mprSetPropertyValue(argv[0], "error_string", error_string); mpr_Return(eid, mprCreateBoolVar(False)); } else { mpr_Return(eid, mprCreateBoolVar(True)); } talloc_free(mem_ctx); return 0; }
/* set a pointer in a existing MprVar, freeing it when the property goes away */ void mprSetPtrChild(struct MprVar *v, const char *propname, const void *p) { mprSetVar(v, propname, mprCreatePtrVar(discard_const(p))); v = mprGetProperty(v, propname, NULL); v->allocatedData = 1; talloc_steal(mprMemCtx(), p); }
/* find a mpr component, allowing for sub objects, using the '.' convention */ NTSTATUS mprGetVar(struct MprVar **v, const char *name) { const char *p = strchr(name, '.'); char *objname; NTSTATUS status; if (p == NULL) { *v = mprGetProperty(*v, name, NULL); if (*v == NULL) { DEBUG(1,("mprGetVar unable to find '%s'\n", name)); return NT_STATUS_INVALID_PARAMETER; } return NT_STATUS_OK; } objname = talloc_strndup(mprMemCtx(), name, p-name); NT_STATUS_HAVE_NO_MEMORY(objname); *v = mprGetProperty(*v, objname, NULL); NT_STATUS_HAVE_NO_MEMORY(*v); status = mprGetVar(v, p+1); talloc_free(objname); return status; }
/* get a pointer from a MprVar */ void *mprGetPtr(struct MprVar *v, const char *propname) { struct MprVar *val; val = mprGetProperty(v, propname, NULL); if (val == NULL) { return NULL; } if (val->type != MPR_TYPE_PTR) { return NULL; } return val->ptr; }
int espGetVar(EspRequest *ep, EspEnvType oType, char *var, MprVar *value) { MprVar *vp; mprAssert(ep); mprAssert(var); vp = mprGetProperty(&ep->variables[oType], var, 0); if (vp == 0) { return -1; } *value = *vp; return 0; }
/* set a pointer as a child of the local object */ void mprSetThisPtr(int eid, const char *name, void *ptr) { struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); mprSetPtrChild(this, name, ptr); }
/* get a pointer in the current object */ void *mprGetThisPtr(int eid, const char *name) { struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); return mprGetPtr(this, name); }
/* turn a ldb_message into a ejs object variable */ static struct MprVar mprLdbMessage(struct ldb_context *ldb, struct ldb_message *msg) { struct MprVar var; int i; /* we force some attributes to always be an array in the returned structure. This makes the scripting easier, as you don't need a special case for the single value case */ const char *multivalued[] = { "objectClass", "memberOf", "privilege", "member", NULL }; var = mprObject(ldb_dn_alloc_linearized(msg, msg->dn)); for (i=0;i<msg->num_elements;i++) { struct ldb_message_element *el = &msg->elements[i]; struct MprVar val; const struct ldb_attrib_handler *attr; struct ldb_val v; attr = ldb_attrib_handler(ldb, el->name); if (attr == NULL) { goto failed; } if (el->num_values == 1 && !str_list_check_ci(multivalued, el->name)) { if (attr->ldif_write_fn(ldb, msg, &el->values[0], &v) != 0) { goto failed; } /* FIXME: nasty hack, remove me when ejs will support * arbitrary string and does not truncate on \0 */ if (strlen((char *)v.data) != v.length) { val = mprDataBlob(v); } else { val = mprData(v.data, v.length); } } else { int j; val = mprArray(el->name); for (j=0;j<el->num_values;j++) { if (attr->ldif_write_fn(ldb, msg, &el->values[j], &v) != 0) { goto failed; } /* FIXME: nasty hack, remove me when ejs will support * arbitrary string and does not truncate on \0 */ if (strlen((char *)v.data) != v.length) { mprAddArray(&val, j, mprDataBlob(v)); } else { mprAddArray(&val, j, mprData(v.data, v.length)); } } } mprSetVar(&var, el->name, val); } /* add the dn if it is not already specified */ if (mprGetProperty(&var, "dn", 0) == 0) { mprSetVar(&var, "dn", mprString(ldb_dn_alloc_linearized(msg, msg->dn))); } return var; failed: return mprCreateUndefinedVar(); }
MprVar *espGetProperty(MprVar *obj, char *property, MprVar *value) { return mprGetProperty(obj, property, value); }