Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[kai/samba.git] / source / scripting / ejs / smbcalls_ldb.c
index 33f371cd4ef1832a742ece957b38cc40ea1ab939..4a157945af5cdf9184792e3187addc87e05b2454 100644 (file)
@@ -8,7 +8,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/appweb/ejs/ejs.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
-#include "db_wrap.h"
+#include "ldb_wrap.h"
 #include "dsdb/samdb/samdb.h"
 #include "librpc/ndr/libndr.h"
+#include "libcli/security/security.h"
 
 /*
   get the connected db
  */
 static struct ldb_context *ejs_get_ldb_context(int eid)
 {
-       struct ldb_context *ldb = mprGetThisPtr(eid, "db");
+       struct ldb_context *ldb = (struct ldb_context *)mprGetThisPtr(eid, "db");
        if (ldb == NULL) {
                ejsSetErrorMsg(eid, "invalid ldb connection");
        }
@@ -453,7 +453,7 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv)
 
        dbfile = argv[0];
 
-       ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 
+       ldb = ldb_wrap_connect(mprMemCtx(), mprEventCtx(), mprLpCtx(), dbfile, 
                               session_info, creds,
                               0, (const char **)(argv+1));
        if (ldb == NULL) {
@@ -484,7 +484,7 @@ static int ejs_ldbClose(MprVarHandle eid, int argc, struct MprVar **argv)
        }
 
        mprSetThisPtr(eid, "db", NULL);
-       mpr_Return(eid, mprCreateBoolVar(True));
+       mpr_Return(eid, mprCreateBoolVar(true));
        return 0;
 }
 
@@ -576,8 +576,6 @@ static int ejs_ldb_attach_dsdb_schema_from_ldif(MprVarHandle eid, int argc, char
 {
        struct ldb_context *ldb;
        WERROR status;
-       char *pf_name;
-       char *df_name;
        const char *pf;
        const char *df;
 
@@ -601,7 +599,7 @@ static int ejs_ldb_attach_dsdb_schema_from_ldif(MprVarHandle eid, int argc, char
 }
 
 /*
-  commit a ldb attach a dsdb_schema from ldif files
+  set a particular invocationId against the running LDB
   usage:
    ok = ldb.set_ntds_invocationId("7729aa4b-f990-41ad-b81a-8b6a14090f41");
 */
@@ -643,9 +641,9 @@ static int ejs_ldb_set_ntds_invocationId(MprVarHandle eid, int argc, char **argv
 }
 
 /*
-  commit a ldb attach a dsdb_schema from ldif files
+  attach a particular ntds objectGUID against the current ldb
   usage:
-   ok = ldb.get_ntds_objectGUID("7729aa4b-f990-41ad-b81a-8b6a14090f41");
+   ok = ldb.set_ntds_objectGUID("7729aa4b-f990-41ad-b81a-8b6a14090f41");
 */
 static int ejs_ldb_set_ntds_objectGUID(MprVarHandle eid, int argc, char **argv)
 {
@@ -684,6 +682,48 @@ static int ejs_ldb_set_ntds_objectGUID(MprVarHandle eid, int argc, char **argv)
        return 0;
 }
 
+/*
+  attach a particular domain SID against the current ldb
+  usage:
+   ok = ldb.set_domain_sid("S-S-1-5-21-3065342217-3567412576-2214182334");
+*/
+static int ejs_ldb_set_domain_sid(MprVarHandle eid, int argc, char **argv)
+{
+       struct ldb_context *ldb;
+       struct dom_sid *dom_sid;
+       char *dom_sid_str;
+       bool ok;
+
+       if (argc != 1) {
+               ejsSetErrorMsg(eid, "ldb.set_domain_sid invalid arguments");
+               return -1;
+       }
+
+       ldb = ejs_get_ldb_context(eid);
+       if (ldb == NULL) {
+               return -1;
+       }
+
+       dom_sid_str = argv[0];
+
+       dom_sid = dom_sid_parse_talloc(NULL, dom_sid_str);
+       if (!dom_sid) {
+               ejsSetErrorMsg(eid, "ldb.set_domain_sid - failed to parse domain sid '%s'\n",
+                               dom_sid_str);
+               return -1;
+       }
+
+       ok = samdb_set_domain_sid(ldb, dom_sid);
+       talloc_free(dom_sid);
+       if (!ok) {
+               ejsSetErrorMsg(eid, "ldb.set_domain_sid - failed to set cached ntds invocationId\n");
+               return -1;
+       }
+
+       mpr_Return(eid, mprCreateBoolVar(ok));
+       return 0;
+}
+
 /*
   initialise ldb ejs subsystem
 */
@@ -711,6 +751,8 @@ static int ejs_ldb_init(MprVarHandle eid, int argc, struct MprVar **argv)
                              ejs_ldb_set_ntds_invocationId);
        mprSetStringCFunction(ldb, "set_ntds_objectGUID",
                              ejs_ldb_set_ntds_objectGUID);
+       mprSetStringCFunction(ldb, "set_domain_sid",
+                             ejs_ldb_set_domain_sid);
        mprSetVar(ldb, "SCOPE_BASE", mprCreateNumberVar(LDB_SCOPE_BASE));
        mprSetVar(ldb, "SCOPE_ONE", mprCreateNumberVar(LDB_SCOPE_ONELEVEL));
        mprSetVar(ldb, "SCOPE_SUBTREE", mprCreateNumberVar(LDB_SCOPE_SUBTREE));