r23680: Make it easier to setup a domain member server - the 'server role'
[ab/samba.git/.git] / source4 / scripting / ejs / smbcalls_auth.c
index 8ef04bec9795a37b3fa06a72230132a699d02efa..5509e78357c9c065ffc678127140c38113ad9ed7 100644 (file)
 #include "includes.h"
 #include "lib/appweb/ejs/ejs.h"
 #include "auth/auth.h"
+#include "auth/credentials/credentials.h"
 #include "scripting/ejs/smbcalls.h"
+#include "lib/events/events.h"
+#include "lib/messaging/irpc.h"
 
 static int ejs_doauth(MprVarHandle eid,
                      TALLOC_CTX *tmp_ctx, struct MprVar *auth, const char *username, 
-                     const char *password, const char *domain, const char *remote_host,
-                     const char *authtype)
+                     const char *password, const char *domain, const char *workstation,
+                     struct socket_address *remote_host, const char **auth_types)
 {
        struct auth_usersupplied_info *user_info = NULL;
        struct auth_serversupplied_info *server_info = NULL;
        struct auth_session_info *session_info = NULL;
        struct auth_context *auth_context;
-       const char *auth_types[] = { authtype, NULL };
+       struct MprVar *session_info_obj;
        NTSTATUS nt_status;
 
-       /*
-         darn, we need some way to get the right event_context here
-       */
-       nt_status = auth_context_create(tmp_ctx, auth_types, &auth_context, NULL);
+       struct smbcalls_context *c;
+       struct event_context *ev;
+       struct messaging_context *msg;
+
+       /* Hope we can find an smbcalls_context somewhere up there... */
+       c = talloc_find_parent_bytype(tmp_ctx, struct smbcalls_context);
+       if (c) {
+               ev = c->event_ctx;
+               msg = c->msg_ctx;
+       } else {
+               /* Hope we can find the event context somewhere up there... */
+               ev = event_context_find(tmp_ctx);
+               msg = messaging_client_init(tmp_ctx, ev);
+       }
+
+       nt_status = auth_context_create_methods(tmp_ctx, auth_types, ev, msg, &auth_context);
        if (!NT_STATUS_IS_OK(nt_status)) {
                mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
                mprSetPropertyValue(auth, "report", mprString("Auth System Failure"));
@@ -61,7 +76,7 @@ static int ejs_doauth(MprVarHandle eid,
        user_info->client.domain_name = domain;
        user_info->mapped.domain_name = domain;
 
-       user_info->workstation_name = remote_host;
+       user_info->workstation_name = workstation;
 
        user_info->remote_host = remote_host;
 
@@ -74,8 +89,14 @@ static int ejs_doauth(MprVarHandle eid,
        user_info->logon_parameters = 0;
 
        nt_status = auth_check_password(auth_context, tmp_ctx, user_info, &server_info);
+
+       /* Don't give the game away (any difference between no such
+        * user and wrong password) */
+       nt_status = auth_nt_status_squash(nt_status);
+
        if (!NT_STATUS_IS_OK(nt_status)) {
-               mprSetPropertyValue(auth, "report", mprString("Login Failed"));
+               mprSetPropertyValue(auth, "report", 
+                                   mprString(talloc_strdup(mprMemCtx(), get_friendly_nt_error_msg(nt_status))));
                mprSetPropertyValue(auth, "result", mprCreateBoolVar(False));
                goto done;
        }
@@ -87,9 +108,12 @@ static int ejs_doauth(MprVarHandle eid,
                goto done;
        }
 
+       session_info_obj = mprInitObject(eid, "session_info", 0, NULL);
+
+       mprSetPtrChild(session_info_obj, "session_info", session_info);
        talloc_steal(mprMemCtx(), session_info);
-       mprSetThisPtr(eid, "session_info", session_info);
 
+       mprSetProperty(auth, "session_info", session_info_obj);
        mprSetPropertyValue(auth, "result", mprCreateBoolVar(server_info->authenticated));
        mprSetPropertyValue(auth, "username", mprString(server_info->account_name));
        mprSetPropertyValue(auth, "domain", mprString(server_info->domain_name));
@@ -101,13 +125,6 @@ done:
 /*
   perform user authentication, returning an array of results
 
-  syntax:
-    var authinfo = new Object();
-    authinfo.username = myname;
-    authinfo.password = mypass;
-    authinfo.domain = mydom;
-    authinfo.rhost = request['REMOTE_HOST'];
-    auth = userAuth(authinfo);
 */
 static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
 {
@@ -115,11 +132,13 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
        const char *username;
        const char *password;
        const char *domain;
-       const char *remote_host;
+       const char *workstation;
        struct MprVar auth;
        struct cli_credentials *creds;
+       struct socket_address *remote_host;
+       const char *auth_types_unix[] = { "unix", NULL };
 
-       if (argc != 1 || argv[0]->type != MPR_TYPE_OBJECT) {
+       if (argc != 2 || argv[0]->type != MPR_TYPE_OBJECT || argv[1]->type != MPR_TYPE_OBJECT) {
                ejsSetErrorMsg(eid, "userAuth invalid arguments, this function requires an object.");
                return -1;
        }
@@ -127,7 +146,13 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
        /* get credential values from credentials object */
        creds = mprGetPtr(argv[0], "creds");
        if (creds == NULL) {
-               ejsSetErrorMsg(eid, "userAuth requires a 'creds' element");
+               ejsSetErrorMsg(eid, "userAuth requires a 'creds' first parameter");
+               return -1;
+       }
+
+       remote_host = mprGetPtr(argv[1], "socket_address");
+       if (remote_host == NULL) {
+               ejsSetErrorMsg(eid, "userAuth requires a socket address second parameter");
                return -1;
        }
 
@@ -136,7 +161,7 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
        username    = cli_credentials_get_username(creds);
        password    = cli_credentials_get_password(creds);
        domain      = cli_credentials_get_domain(creds);
-       remote_host = cli_credentials_get_workstation(creds);
+       workstation = cli_credentials_get_workstation(creds);
 
        if (username == NULL || password == NULL || domain == NULL) {
                mpr_Return(eid, mprCreateUndefinedVar());
@@ -146,10 +171,10 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
 
        auth = mprObject("auth");
 
-       if (domain && (strcmp("System User", domain) == 0)) {
-               ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, remote_host, "unix");
+       if (domain && (strcmp("SYSTEM USER", domain) == 0)) {
+               ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, auth_types_unix);
        } else {
-               ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, remote_host, "sam");
+               ejs_doauth(eid, tmp_ctx, &auth, username, password, domain, workstation, remote_host, lp_auth_methods());
        }
 
        mpr_Return(eid, auth);
@@ -157,10 +182,28 @@ static int ejs_userAuth(MprVarHandle eid, int argc, struct MprVar **argv)
        return 0;
 }
 
+/*
+  initialise credentials ejs object
+*/
+static int ejs_system_session(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       struct MprVar *obj = mprInitObject(eid, "session_info", argc, argv);
+       struct auth_session_info *session_info = system_session(mprMemCtx());
+
+       if (session_info == NULL) {
+               return -1;
+       }
+
+       mprSetPtrChild(obj, "session_info", session_info);
+       return 0;
+}
+
 /*
   setup C functions that be called from ejs
 */
-void smb_setup_ejs_auth(void)
+NTSTATUS smb_setup_ejs_auth(void)
 {
        ejsDefineCFunction(-1, "userAuth", ejs_userAuth, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineCFunction(-1, "system_session", ejs_system_session, NULL, MPR_VAR_SCRIPT_HANDLE);
+       return NT_STATUS_OK;
 }