diff options
Diffstat (limited to 'source4/scripting')
43 files changed, 807 insertions, 1998 deletions
diff --git a/source4/scripting/bin/minschema.py b/source4/scripting/bin/minschema.py index fb9d7b05aa..111557126d 100755 --- a/source4/scripting/bin/minschema.py +++ b/source4/scripting/bin/minschema.py @@ -4,6 +4,12 @@ # import optparse + +import os, sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + import samba from samba import getopt as options import sys diff --git a/source4/scripting/bin/rpcclient b/source4/scripting/bin/rpcclient index 34efafdf73..aba4f9ddb3 100755 --- a/source4/scripting/bin/rpcclient +++ b/source4/scripting/bin/rpcclient @@ -1,6 +1,10 @@ #!/usr/bin/python import sys, os, string + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + from cmd import Cmd from optparse import OptionParser from pprint import pprint diff --git a/source4/scripting/bin/samba3dump b/source4/scripting/bin/samba3dump index 8f56d423d8..d89667233f 100755 --- a/source4/scripting/bin/samba3dump +++ b/source4/scripting/bin/samba3dump @@ -7,7 +7,10 @@ import optparse import os, sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../python")) + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + import samba import samba.samba3 diff --git a/source4/scripting/bin/samr.py b/source4/scripting/bin/samr.py new file mode 100755 index 0000000000..e91b5bc312 --- /dev/null +++ b/source4/scripting/bin/samr.py @@ -0,0 +1,114 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Unix SMB/CIFS implementation. +# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008 +# +# Based on samr.js © Andrew Tridgell <tridge@samba.org> +# +# 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 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# 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, see <http://www.gnu.org/licenses/>. +# + +import sys + +sys.path.insert(0, "bin/python") + +from samba.dcerpc import samr, security, lsa + +def FillUserInfo(samr, dom_handle, users, level): + """fill a user array with user information from samrQueryUserInfo""" + for i in range(len(users)): + user_handle = samr.OpenUser(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, users[i].idx) + info = samr.QueryUserInfo(user_handle, level) + info.name = users[i].name + info.idx = users[i].idx + users[i] = info + samr.Close(user_handle) + +def toArray((handle, array, num_entries)): + ret = [] + for x in range(num_entries): + ret.append((array.entries[x].idx, array.entries[x].name)) + return ret + + +def test_Connect(samr): + """test the samr_Connect interface""" + print "Testing samr_Connect" + return samr.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED) + +def test_LookupDomain(samr, handle, domain): + """test the samr_LookupDomain interface""" + print "Testing samr_LookupDomain" + return samr.LookupDomain(handle, domain) + +def test_OpenDomain(samr, handle, sid): + """test the samr_OpenDomain interface""" + print "Testing samr_OpenDomain" + return samr.OpenDomain(handle, security.SEC_FLAG_MAXIMUM_ALLOWED, sid) + +def test_EnumDomainUsers(samr, dom_handle): + """test the samr_EnumDomainUsers interface""" + print "Testing samr_EnumDomainUsers" + users = toArray(samr.EnumDomainUsers(dom_handle, 0, 0, -1)) + print "Found %d users" % len(users) + for idx, user in users: + print "\t%s\t(%d)" % (user, idx) + +def test_EnumDomainGroups(samr, dom_handle): + """test the samr_EnumDomainGroups interface""" + print "Testing samr_EnumDomainGroups" + groups = toArray(samr.EnumDomainGroups(dom_handle, 0, 0)) + print "Found %d groups" % len(groups) + for idx, group in groups: + print "\t%s\t(%d)" % (group, idx) + +def test_domain_ops(samr, dom_handle): + """test domain specific ops""" + test_EnumDomainUsers(samr, dom_handle) + test_EnumDomainGroups(samr, dom_handle) + +def test_EnumDomains(samr, handle): + """test the samr_EnumDomains interface""" + print "Testing samr_EnumDomains" + + domains = toArray(samr.EnumDomains(handle, 0, -1)) + print "Found %d domains" % len(domains) + for idx, domain in domains: + print "\t%s (%d)" % (domain, idx) + for idx, domain in domains: + print "Testing domain %s" % domain + sid = samr.LookupDomain(handle, domain) + dom_handle = test_OpenDomain(samr, handle, sid) + test_domain_ops(samr, dom_handle) + samr.Close(dom_handle) + +if len(sys.argv) != 2: + print "Usage: samr.js <BINDING>" + sys.exit(1) + +binding = sys.argv[1] + +print "Connecting to " + binding +try: + samr = samr.samr(binding) +except Exception, e: + print "Failed to connect to %s: %s" % (binding, e.message) + sys.exit(1) + +handle = test_Connect(samr) +test_EnumDomains(samr, handle) +samr.Close(handle) + +print "All OK" diff --git a/source4/scripting/bin/subunitrun b/source4/scripting/bin/subunitrun index 11ac426589..6f1086ad37 100755 --- a/source4/scripting/bin/subunitrun +++ b/source4/scripting/bin/subunitrun @@ -17,9 +17,32 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -from subunit import SubunitTestRunner import sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + +from subunit import SubunitTestRunner from unittest import TestProgram +import optparse +import os +from samba import param +import samba.getopt as options +import samba.tests + +parser = optparse.OptionParser("subunitrun [options] <tests>") +credopts = options.CredentialsOptions(parser) +parser.add_option_group(credopts) +sambaopts = options.SambaOptions(parser) +parser.add_option_group(sambaopts) +parser.add_option_group(options.VersionOptions(parser)) + +args = parser.parse_args()[1] + +samba.tests.cmdline_loadparm = sambaopts.get_loadparm() +samba.tests.cmdline_credentials = credopts.get_credentials(samba.tests.cmdline_loadparm) + +param.cvar.default_config = samba.tests.cmdline_loadparm runner = SubunitTestRunner() -TestProgram(module=None, argv=sys.argv, testRunner=runner) +program = TestProgram(module=None, argv=[sys.argv[0]] + args, testRunner=runner) diff --git a/source4/scripting/bin/winreg.py b/source4/scripting/bin/winreg.py index 1e39ee8f78..19d39e56ab 100755 --- a/source4/scripting/bin/winreg.py +++ b/source4/scripting/bin/winreg.py @@ -7,6 +7,10 @@ # import sys + +# Find right directory when running from source tree +sys.path.insert(0, "bin/python") + import winreg import optparse import samba.getopt as options diff --git a/source4/scripting/ejs/config.mk b/source4/scripting/ejs/config.mk index 33a4ac915f..c1a1ca1f0f 100644 --- a/source4/scripting/ejs/config.mk +++ b/source4/scripting/ejs/config.mk @@ -1,84 +1,86 @@ [SUBSYSTEM::EJSRPC] -OBJ_FILES = \ - ejsrpc.o + +EJSRPC_OBJ_FILES = $(ejsscriptsrcdir)/ejsrpc.o [MODULE::smbcalls_config] -OBJ_FILES = smbcalls_config.o OUTPUT_TYPE = MERGED_OBJ SUBSYSTEM = smbcalls INIT_FUNCTION = smb_setup_ejs_config +smbcalls_config_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_config.o + [MODULE::smbcalls_ldb] -OBJ_FILES = smbcalls_ldb.o OUTPUT_TYPE = MERGED_OBJ SUBSYSTEM = smbcalls INIT_FUNCTION = smb_setup_ejs_ldb PRIVATE_DEPENDENCIES = LIBLDB SAMDB LIBNDR +smbcalls_ldb_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_ldb.o + [MODULE::smbcalls_reg] -OBJ_FILES = smbcalls_reg.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_reg PRIVATE_DEPENDENCIES = registry SAMDB LIBNDR +smbcalls_reg_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_reg.o + [MODULE::smbcalls_nbt] -OBJ_FILES = smbcalls_nbt.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_nbt +smbcalls_nbt_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_nbt.o + [MODULE::smbcalls_rand] -OBJ_FILES = smbcalls_rand.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_random +smbcalls_rand_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_rand.o + [MODULE::smbcalls_nss] -OBJ_FILES = smbcalls_nss.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_nss PRIVATE_DEPENDENCIES = NSS_WRAPPER +smbcalls_nss_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_nss.o + [MODULE::smbcalls_data] -OBJ_FILES = smbcalls_data.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_datablob +smbcalls_data_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_data.o + [MODULE::smbcalls_auth] -OBJ_FILES = smbcalls_auth.o OUTPUT_TYPE = MERGED_OBJ SUBSYSTEM = smbcalls INIT_FUNCTION = smb_setup_ejs_auth -PRIVATE_DEPENDENCIES = auth +PRIVATE_DEPENDENCIES = service_auth + +smbcalls_auth_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_auth.o + +smbcalls_auth_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_auth.o [MODULE::smbcalls_string] -OBJ_FILES = smbcalls_string.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_string +smbcalls_string_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_string.o + [MODULE::smbcalls_sys] -OBJ_FILES = smbcalls_sys.o SUBSYSTEM = smbcalls OUTPUT_TYPE = MERGED_OBJ INIT_FUNCTION = smb_setup_ejs_system +smbcalls_sys_OBJ_FILES = $(ejsscriptsrcdir)/smbcalls_sys.o + mkinclude ejsnet/config.mk [SUBSYSTEM::smbcalls] -PRIVATE_PROTO_HEADER = proto.h -OBJ_FILES = \ - smbcalls.o \ - smbcalls_cli.o \ - smbcalls_rpc.o \ - smbcalls_options.o \ - smbcalls_creds.o \ - smbcalls_param.o \ - mprutil.o \ - literal.o PRIVATE_DEPENDENCIES = \ EJS LIBSAMBA-UTIL \ EJSRPC MESSAGING \ @@ -87,12 +89,23 @@ PRIVATE_DEPENDENCIES = \ dcerpc \ NDR_TABLE +smbcalls_OBJ_FILES = $(addprefix $(ejsscriptsrcdir)/, \ + smbcalls.o \ + smbcalls_cli.o \ + smbcalls_rpc.o \ + smbcalls_options.o \ + smbcalls_creds.o \ + smbcalls_param.o \ + mprutil.o \ + literal.o) + +$(eval $(call proto_header_template,$(ejsscriptsrcdir)/proto.h,$(smbcalls_OBJ_FILES:.o=.c))) + ####################### # Start BINARY SMBSCRIPT [BINARY::smbscript] -INSTALLDIR = BINDIR -OBJ_FILES = \ - smbscript.o PRIVATE_DEPENDENCIES = EJS LIBSAMBA-UTIL smbcalls LIBSAMBA-HOSTCONFIG # End BINARY SMBSCRIPT ####################### + +smbscript_OBJ_FILES = $(ejsscriptsrcdir)/smbscript.o diff --git a/source4/scripting/ejs/ejsnet/config.mk b/source4/scripting/ejs/ejsnet/config.mk index 249a33121d..710221e37d 100644 --- a/source4/scripting/ejs/ejsnet/config.mk +++ b/source4/scripting/ejs/ejsnet/config.mk @@ -1,11 +1,13 @@ [MODULE::smbcalls_net] -OBJ_FILES = \ +SUBSYSTEM = smbcalls +INIT_FUNCTION = smb_setup_ejs_net +PRIVATE_DEPENDENCIES = LIBSAMBA-NET LIBCLI_SMB CREDENTIALS + +smbcalls_net_OBJ_FILES = $(addprefix $(ejsscriptsrcdir)/ejsnet/, \ net_ctx.o \ net_user.o \ mpr_user.o \ net_host.o \ - mpr_host.o -SUBSYSTEM = smbcalls -INIT_FUNCTION = smb_setup_ejs_net -PRIVATE_PROTO_HEADER = proto.h -PRIVATE_DEPENDENCIES = LIBSAMBA-NET LIBCLI_SMB CREDENTIALS + mpr_host.o) + +$(eval $(call proto_header_template,$(ejsscriptsrcdir)/ejsnet/proto.h,$(smbcalls_net_OBJ_FILES:.o=.c))) diff --git a/source4/scripting/ejs/ejsnet/net_ctx.c b/source4/scripting/ejs/ejsnet/net_ctx.c index 99be1c4ef8..cbe163552e 100644 --- a/source4/scripting/ejs/ejsnet/net_ctx.c +++ b/source4/scripting/ejs/ejsnet/net_ctx.c @@ -50,7 +50,7 @@ static int ejs_net_context(MprVarHandle eid, int argc, struct MprVar **argv) ejsSetErrorMsg(eid, "talloc_new() failed"); return -1; } - ev = event_context_find(event_mem_ctx); + ev = mprEventCtx(); ctx = libnet_context_init(ev, mprLpCtx()); /* IF we generated a new event context, it will be under here, diff --git a/source4/scripting/ejs/ejsnet/net_user.c b/source4/scripting/ejs/ejsnet/net_user.c index 57e538d3b4..0c32035c6c 100644 --- a/source4/scripting/ejs/ejsnet/net_user.c +++ b/source4/scripting/ejs/ejsnet/net_user.c @@ -268,8 +268,9 @@ static int ejs_net_userinfo(MprVarHandle eid, int argc, char **argv) /* call the libnet function */ req.in.domain_name = userman_domain; - req.in.user_name = username; - + req.in.data.user_name = username; + req.in.level = USER_INFO_BY_NAME; + status = libnet_UserInfo(ctx, mem_ctx, &req); if (!NT_STATUS_IS_OK(status)) { ejsSetErrorMsg(eid, "%s", req.out.error_string); diff --git a/source4/scripting/ejs/smbcalls.c b/source4/scripting/ejs/smbcalls.c index 63a80e17db..98d6be07bf 100644 --- a/source4/scripting/ejs/smbcalls.c +++ b/source4/scripting/ejs/smbcalls.c @@ -23,7 +23,6 @@ #include "includes.h" #include "param/param.h" #include "scripting/ejs/smbcalls.h" -#include "build.h" #include "version.h" /* @@ -173,6 +172,32 @@ _PUBLIC_ void ejs_exception(const char *reason) */ void smb_setup_ejs_functions(void (*exception_handler)(const char *)) { + extern NTSTATUS ejs_init_security(void); + extern NTSTATUS ejs_init_initshutdown(void); + extern NTSTATUS smb_setup_ejs_reg(void); + extern NTSTATUS smb_setup_ejs_string(void); + extern NTSTATUS ejs_init_lsarpc(void); + extern NTSTATUS ejs_init_rpcecho(void); + extern NTSTATUS ejs_init_winreg(void); + extern NTSTATUS smb_setup_ejs_random(void); + extern NTSTATUS smb_setup_ejs_config(void); + extern NTSTATUS ejs_init_misc(void); + extern NTSTATUS ejs_init_netdfs(void); + extern NTSTATUS smb_setup_ejs_datablob(void); + extern NTSTATUS smb_setup_ejs_auth(void); + extern NTSTATUS smb_setup_ejs_nss(void); + extern NTSTATUS ejs_init_samr(void); + extern NTSTATUS ejs_init_wkssvc(void); + extern NTSTATUS smb_setup_ejs_system(void); + extern NTSTATUS smb_setup_ejs_ldb(void); + extern NTSTATUS ejs_init_svcctl(void); + extern NTSTATUS smb_setup_ejs_nbt(void); + extern NTSTATUS smb_setup_ejs_net(void); + extern NTSTATUS ejs_init_srvsvc(void); + extern NTSTATUS ejs_init_netlogon(void); + extern NTSTATUS ejs_init_drsuapi(void); + extern NTSTATUS ejs_init_irpc(void); + extern NTSTATUS ejs_init_eventlog(void); init_module_fn static_init[] = { STATIC_smbcalls_MODULES }; init_module_fn *shared_init; diff --git a/source4/scripting/ejs/smbcalls_auth.c b/source4/scripting/ejs/smbcalls_auth.c index 908a009159..b67bb7ed5b 100644 --- a/source4/scripting/ejs/smbcalls_auth.c +++ b/source4/scripting/ejs/smbcalls_auth.c @@ -55,7 +55,7 @@ static int ejs_doauth(MprVarHandle eid, msg = c->msg_ctx; } else { /* Hope we can find the event context somewhere up there... */ - ev = event_context_find(tmp_ctx); + ev = mprEventCtx(); msg = messaging_client_init(tmp_ctx, lp_messaging_path(tmp_ctx, mprLpCtx()), lp_iconv_convenience(mprLpCtx()), ev); } @@ -109,7 +109,7 @@ static int ejs_doauth(MprVarHandle eid, goto done; } - nt_status = auth_generate_session_info(tmp_ctx, mprLpCtx(), server_info, &session_info); + nt_status = auth_generate_session_info(tmp_ctx, mprEventCtx(), mprLpCtx(), server_info, &session_info); if (!NT_STATUS_IS_OK(nt_status)) { mprSetPropertyValue(auth, "report", mprString("Session Info generation failed")); mprSetPropertyValue(auth, "result", mprCreateBoolVar(false)); diff --git a/source4/scripting/ejs/smbcalls_ldb.c b/source4/scripting/ejs/smbcalls_ldb.c index f47920b9bb..4a157945af 100644 --- a/source4/scripting/ejs/smbcalls_ldb.c +++ b/source4/scripting/ejs/smbcalls_ldb.c @@ -453,7 +453,7 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) dbfile = argv[0]; - ldb = ldb_wrap_connect(mprMemCtx(), mprLpCtx(), dbfile, + ldb = ldb_wrap_connect(mprMemCtx(), mprEventCtx(), mprLpCtx(), dbfile, session_info, creds, 0, (const char **)(argv+1)); if (ldb == NULL) { diff --git a/source4/scripting/ejs/smbcalls_nbt.c b/source4/scripting/ejs/smbcalls_nbt.c index 67a85414ca..8c555bf821 100644 --- a/source4/scripting/ejs/smbcalls_nbt.c +++ b/source4/scripting/ejs/smbcalls_nbt.c @@ -70,7 +70,7 @@ static int ejs_resolve_name(MprVarHandle eid, int argc, struct MprVar **argv) result = 0; - nt_status = resolve_name(lp_resolve_context(mprLpCtx()), &name, tmp_ctx, &reply_addr, event_context_find(tmp_ctx)); + nt_status = resolve_name(lp_resolve_context(mprLpCtx()), &name, tmp_ctx, &reply_addr, mprEventCtx()); if (NT_STATUS_IS_OK(nt_status)) { mprSetPropertyValue(argv[0], "value", mprString(reply_addr)); diff --git a/source4/scripting/ejs/smbcalls_reg.c b/source4/scripting/ejs/smbcalls_reg.c index e20d91ad2e..ed8653d3a7 100644 --- a/source4/scripting/ejs/smbcalls_reg.c +++ b/source4/scripting/ejs/smbcalls_reg.c @@ -70,7 +70,7 @@ static int ejs_reg_open(MprVarHandle eid, int argc, struct MprVar **argv) struct registry_context *rctx; WERROR error; - error = reg_open_samba(mprMemCtx(), &rctx, mprLpCtx(), NULL, NULL); + error = reg_open_samba(mprMemCtx(), &rctx, mprEventCtx(), mprLpCtx(), NULL, NULL); SMB_ASSERT(W_ERROR_IS_OK(error)); mprSetPtrChild(reg, "registry", rctx); diff --git a/source4/scripting/ejs/smbcalls_rpc.c b/source4/scripting/ejs/smbcalls_rpc.c index d1e49b4348..94774d708b 100644 --- a/source4/scripting/ejs/smbcalls_rpc.c +++ b/source4/scripting/ejs/smbcalls_rpc.c @@ -73,7 +73,7 @@ static int ejs_irpc_connect(MprVarHandle eid, int argc, char **argv) p->server_name = argv[0]; - ev = event_context_find(p); + ev = mprEventCtx(); /* create a messaging context, looping as we have no way to allocate temporary server ids automatically */ @@ -158,7 +158,7 @@ static int ejs_rpc_connect(MprVarHandle eid, int argc, char **argv) cli_credentials_set_anonymous(creds); } - ev = event_context_find(mprMemCtx()); + ev = mprEventCtx(); status = dcerpc_pipe_connect(this, &p, binding, iface, creds, ev, mprLpCtx()); diff --git a/source4/scripting/libjs/provision.js b/source4/scripting/libjs/provision.js deleted file mode 100644 index 51e2785762..0000000000 --- a/source4/scripting/libjs/provision.js +++ /dev/null @@ -1,1254 +0,0 @@ -/* - backend code for provisioning a Samba4 server - Copyright Andrew Tridgell 2005 - Released under the GNU GPL version 3 or later -*/ - -sys = sys_init(); - -/* - return true if the current install seems to be OK -*/ -function install_ok(session_info, credentials) -{ - var lp = loadparm_init(); - var ldb = ldb_init(); - ldb.session_info = session_info; - ldb.credentials = credentials; - if (lp.get("realm") == "") { - return false; - } - var ok = ldb.connect(lp.get("sam database")); - if (!ok) { - return false; - } - var res = ldb.search("(cn=Administrator)"); - if (res.error != 0 || res.msgs.length != 1) { - return false; - } - return true; -} - -/* - find a user or group from a list of possibilities -*/ -function findnss() -{ - var i; - assert(arguments.length >= 2); - var nssfn = arguments[0]; - for (i=1;i<arguments.length;i++) { - if (nssfn(arguments[i]) != undefined) { - return arguments[i]; - } - } - printf("Unable to find user/group for %s\n", arguments[1]); - assert(i<arguments.length); -} - -/* - add a foreign security principle - */ -function add_foreign(ldb, subobj, sid, desc) -{ - var add = sprintf(" -dn: CN=%s,CN=ForeignSecurityPrincipals,%s -objectClass: top -objectClass: foreignSecurityPrincipal -description: %s -", - sid, subobj.DOMAINDN, desc); - /* deliberately ignore errors from this, as the records may - already exist */ - ldb.add(add); -} - - -/* - setup a mapping between a sam name and a unix name - */ -function setup_name_mapping(info, ldb, sid, unixname) -{ - var attrs = new Array("dn"); - var res = ldb.search(sprintf("objectSid=%s", sid), - info.subobj.DOMAINDN, ldb.SCOPE_SUBTREE, attrs); - if (res.error != 0 || res.msgs.length != 1) { - info.message("Failed to find record for objectSid %s\n", sid); - return false; - } - var mod = sprintf(" -dn: %s -changetype: modify -replace: unixName -unixName: %s -", - res.msgs[0].dn, unixname); - var ok = ldb.modify(mod); - if (ok.error != 0) { - info.message("name mapping for %s failed - %s\n", - sid, ldb.errstring()); - return false; - } - return true; -} - -/* - return current time as a nt time string -*/ -function nttime() -{ - return "" + sys.nttime(); -} - -/* - return current time as a ldap time string -*/ -function ldaptime() -{ - return sys.ldaptime(sys.nttime()); -} - -/* - return a date string suitable for a dns zone serial number -*/ -function datestring() -{ - var t = sys.ntgmtime(sys.nttime()); - return sprintf("%04u%02u%02u%02u", - t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour); -} - -/* - return first host IP -*/ -function hostip() -{ - var list = sys.interfaces(); - return list[0]; -} - - -/* - return first part of hostname -*/ -function hostname() -{ - var s = split(".", sys.hostname()); - return s[0]; -} - -/* the ldb is in bad shape, possibly due to being built from an - incompatible previous version of the code, so delete it - completely */ -function ldb_delete(info, ldb) -{ - info.message("Deleting " + ldb.filename + "\n"); - var lp = loadparm_init(); - sys.unlink(sprintf("%s/%s", lp.get("private dir"), ldb.filename)); - ldb.transaction_cancel(); - ldb.close(); - var ok = ldb.connect(ldb.filename); - ldb.transaction_start(); - assert(ok); -} - -/* - erase an ldb, removing all records -*/ -function ldb_erase(info, ldb) -{ - var res; - - /* delete the specials */ - ldb.del("@INDEXLIST"); - ldb.del("@ATTRIBUTES"); - ldb.del("@OPTIONS"); - ldb.del("@MODULES"); - ldb.del("@PARTITION"); - ldb.del("@KLUDGEACL"); - - /* and the rest */ - attrs = new Array("dn"); - var basedn = ""; - var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs); - var i; - if (res.error != 0) { - ldb_delete(info, ldb); - return; - } - for (i=0;i<res.msgs.length;i++) { - ldb.del(res.msgs[i].dn); - } - - var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs); - if (res.error != 0 || res.msgs.length != 0) { - ldb_delete(info, ldb); - return; - } - assert(res.msgs.length == 0); -} - -/* - erase an ldb, removing all records -*/ -function ldb_erase_partitions(info, ldb, ldapbackend) -{ - var rootDSE_attrs = new Array("namingContexts"); - var lp = loadparm_init(); - var j; - - var res = ldb.search("(objectClass=*)", "", ldb.SCOPE_BASE, rootDSE_attrs); - if (res.error != 0) { - info.message("rootdse search failed: " + res.errstr + "\n"); - assert(res.error == 0); - } - assert(res.msgs.length == 1); - if (typeof(res.msgs[0].namingContexts) == "undefined") { - return; - } - for (j=0; j<res.msgs[0].namingContexts.length; j++) { - var anything = "(|(objectclass=*)(distinguishedName=*))"; - var attrs = new Array("distinguishedName"); - var basedn = res.msgs[0].namingContexts[j]; - var k; - var previous_remaining = 1; - var current_remaining = 0; - - if (ldapbackend && (basedn == info.subobj.DOMAINDN)) { - /* Only delete objects that were created by provision */ - anything = "(objectcategory=*)"; - } - - for (k=0; k < 10 && (previous_remaining != current_remaining); k++) { - /* and the rest */ - var res2 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs); - var i; - if (res2.error != 0) { - if (res2.error == 32) { - break; - } else { - info.message("ldb search(2) failed: " + res2.errstr + "\n"); - continue; - } - } - previous_remaining = current_remaining; - current_remaining = res2.msgs.length; - for (i=0;i<res2.msgs.length;i++) { - ldb.del(res2.msgs[i].dn); - } - - var res3 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs); - if (res3.error != 0) { - info.message("ldb search(3) failed: " + res3.errstr + "\n"); - continue; - } - if (res3.msgs.length != 0) { - info.message("Failed to delete all records under " + basedn + ", " + res3.msgs.length + " records remaining\n"); - } - } - } -} - -function open_ldb(info, dbname, erase) -{ - var ldb = ldb_init(); - ldb.session_info = info.session_info; - ldb.credentials = info.credentials; - ldb.filename = dbname; - - var connect_ok = ldb.connect(dbname); - if (!connect_ok) { - var lp = loadparm_init(); - sys.unlink(sprintf("%s/%s", lp.get("private dir"), dbname)); - connect_ok = ldb.connect(dbname); - assert(connect_ok); - } - - ldb.transaction_start(); - - if (erase) { - ldb_erase(info, ldb); - } - return ldb; -} - - -/* - setup a ldb in the private dir - */ -function setup_add_ldif(ldif, info, ldb, failok) -{ - var lp = loadparm_init(); - var src = lp.get("setup directory") + "/" + ldif; - - var data = sys.file_load(src); - data = substitute_var(data, info.subobj); - - var add_res = ldb.add(data); - if (add_res.error != 0) { - info.message("ldb load failed: " + add_res.errstr + "\n"); - if (!failok) { - assert(add_res.error == 0); - } - } - return (add_res.error == 0); -} - -function setup_modify_ldif(ldif, info, ldb, failok) -{ - var lp = loadparm_init(); - var src = lp.get("setup directory") + "/" + ldif; - - var data = sys.file_load(src); - data = substitute_var(data, info.subobj); - - var mod_res = ldb.modify(data); - if (mod_res.error != 0) { - info.message("ldb load failed: " + mod_res.errstr + "\n"); - if (!failok) { - assert(mod_res.error == 0); - } - } - return (mod_res.error == 0); -} - - -function setup_ldb(ldif, info, dbname) -{ - var erase = true; - var failok = false; - - if (arguments.length >= 4) { - erase = arguments[3]; - } - if (arguments.length == 5) { - failok = arguments[4]; - } - var ldb = open_ldb(info, dbname, erase); - if (setup_add_ldif(ldif, info, ldb, failok)) { - var commit_ok = ldb.transaction_commit(); - if (!commit_ok) { - info.message("ldb commit failed: " + ldb.errstring() + "\n"); - assert(commit_ok); - } - } -} - -/* - setup a ldb in the private dir - */ -function setup_ldb_modify(ldif, info, ldb) -{ - var lp = loadparm_init(); - - var src = lp.get("setup directory") + "/" + ldif; - - var data = sys.file_load(src); - data = substitute_var(data, info.subobj); - - var mod_res = ldb.modify(data); - if (mod_res.error != 0) { - info.message("ldb load failed: " + mod_res.errstr + "\n"); - return (mod_res.error == 0); - } - return (mod_res.error == 0); -} - -/* - setup a file in the private dir - */ -function setup_file(template, message, fname, subobj) -{ - var lp = loadparm_init(); - var f = fname; - var src = lp.get("setup directory") + "/" + template; - - if (! sys.stat(src)) { - message("Template file not found: %s\n",src); - assert(0); - } - - sys.unlink(f); - - var data = sys.file_load(src); - data = substitute_var(data, subobj); - - ok = sys.file_save(f, data); - if (!ok) { - message("failed to create file: " + f + "\n"); - assert(ok); - } -} - -function provision_default_paths(subobj) -{ - /* subobj.DNSDOMAIN isn't available at this point */ - var dnsdomain = strlower(subobj.REALM); - var lp = loadparm_init(); - var paths = new Object(); - paths.smbconf = lp.filename() - paths.shareconf = lp.get("private dir") + "/" + "share.ldb"; - paths.samdb = lp.get("sam database"); - paths.idmapdb = lp.get("idmap database"); - paths.secrets = lp.get("secrets database"); - paths.templates = lp.get("private dir") + "/" + "templates.ldb"; - paths.keytab = "secrets.keytab"; - paths.dns_keytab = "dns.keytab"; - paths.dns_keytab_abs = lp.get("private dir") + "/" + paths.dns_keytab; - paths.dns = lp.get("private dir") + "/" + dnsdomain + ".zone"; - paths.named_conf = lp.get("private dir") + "/named.conf"; - paths.winsdb = "wins.ldb"; - paths.ldapdir = lp.get("private dir") + "/ldap"; - - paths.s4_ldapi_socket = lp.get("private dir") + "/ldapi"; - paths.phpldapadminconfig = lp.get("private dir") + "/phpldapadmin-config.php"; - - paths.sysvol = lp.get("sysvol", "path"); - - if (paths.sysvol == undefined) { - paths.sysvol = lp.get("lock dir") + "/sysvol"; - } - - paths.netlogon = lp.get("netlogon", "path"); - - if (paths.netlogon == undefined) { - paths.netlogon = paths.sysvol + "/" + dnsdomain + "/scripts"; - } - - return paths; -} - - -/* - setup reasonable name mappings for sam names to unix names -*/ -function setup_name_mappings(info, ldb) -{ - var lp = loadparm_init(); - var attrs = new Array("objectSid"); - var subobj = info.subobj; - - res = ldb.search("objectSid=*", subobj.DOMAINDN, ldb.SCOPE_BASE, attrs); - assert(res.error == 0); - assert(res.msgs.length == 1 && res.msgs[0].objectSid != undefined); - var sid = res.msgs[0].objectSid; - - /* add some foreign sids if they are not present already */ - add_foreign(ldb, subobj, "S-1-5-7", "Anonymous"); - add_foreign(ldb, subobj, "S-1-1-0", "World"); - add_foreign(ldb, subobj, "S-1-5-2", "Network"); - add_foreign(ldb, subobj, "S-1-5-18", "System"); - add_foreign(ldb, subobj, "S-1-5-11", "Authenticated Users"); - - /* some well known sids */ - setup_name_mapping(info, ldb, "S-1-5-7", subobj.NOBODY); - setup_name_mapping(info, ldb, "S-1-1-0", subobj.NOGROUP); - setup_name_mapping(info, ldb, "S-1-5-2", subobj.NOGROUP); - setup_name_mapping(info, ldb, "S-1-5-18", subobj.ROOT); - setup_name_mapping(info, ldb, "S-1-5-11", subobj.USERS); - setup_name_mapping(info, ldb, "S-1-5-32-544", subobj.WHEEL); - setup_name_mapping(info, ldb, "S-1-5-32-545", subobj.USERS); - setup_name_mapping(info, ldb, "S-1-5-32-546", subobj.NOGROUP); - setup_name_mapping(info, ldb, "S-1-5-32-551", subobj.BACKUP); - - /* and some well known domain rids */ - setup_name_mapping(info, ldb, sid + "-500", subobj.ROOT); - setup_name_mapping(info, ldb, sid + "-518", subobj.WHEEL); - setup_name_mapping(info, ldb, sid + "-519", subobj.WHEEL); - setup_name_mapping(info, ldb, sid + "-512", subobj.WHEEL); - setup_name_mapping(info, ldb, sid + "-513", subobj.USERS); - setup_name_mapping(info, ldb, sid + "-520", subobj.WHEEL); - - return true; -} - -function provision_fix_subobj(subobj, paths) -{ - var ldb = ldb_init(); - - subobj.REALM = strupper(subobj.REALM); - subobj.HOSTNAME = strlower(subobj.HOSTNAME); - subobj.DOMAIN = strupper(subobj.DOMAIN); - subobj.NETBIOSNAME = strupper(subobj.HOSTNAME); - subobj.DNSDOMAIN = strlower(subobj.REALM); - subobj.DNSNAME = sprintf("%s.%s", - strlower(subobj.HOSTNAME), - subobj.DNSDOMAIN); - var rdn_list = split(".", subobj.DNSDOMAIN); - subobj.DOMAINDN = "DC=" + join(",DC=", rdn_list); - subobj.ROOTDN = subobj.DOMAINDN; - subobj.CONFIGDN = "CN=Configuration," + subobj.ROOTDN; - subobj.SCHEMADN = "CN=Schema," + subobj.CONFIGDN; - - subobj.MACHINEPASS_B64 = ldb.encode(subobj.MACHINEPASS); - subobj.KRBTGTPASS_B64 = ldb.encode(subobj.KRBTGTPASS); - subobj.ADMINPASS_B64 = ldb.encode(subobj.ADMINPASS); - subobj.DNSPASS_B64 = ldb.encode(subobj.DNSPASS); - - subobj.SAM_LDB = "tdb://" + paths.samdb; - subobj.SECRETS_KEYTAB = paths.keytab; - subobj.DNS_KEYTAB = paths.dns_keytab; - subobj.DNS_KEYTAB_ABS = paths.dns_keytab_abs; - - subobj.LDAPDIR = paths.ldapdir; - var ldap_path_list = split("/", paths.ldapdir); - subobj.LDAPI_URI = "ldapi://" + join("%2F", ldap_path_list) + "%2Fldapi"; - - var s4ldap_path_list = split("/", paths.s4_ldapi_socket); - subobj.S4_LDAPI_URI = "ldapi://" + join("%2F", s4ldap_path_list); - - subobj.LDAPMANAGERDN = "cn=Manager," + subobj.DOMAINDN; - - subobj.NETLOGONPATH = paths.netlogon; - subobj.SYSVOLPATH = paths.sysvol; - - if (subobj.DOMAIN_CONF == undefined) { - subobj.DOMAIN_CONF = subobj.DOMAIN; - } - if (subobj.REALM_CONF == undefined) { - subobj.REALM_CONF = subobj.REALM; - } - if (strlower(subobj.SERVERROLE) != strlower("domain controller")) { - subobj.REALM = subobj.HOSTNAME; - subobj.DOMAIN = subobj.HOSTNAME; - } - - return true; -} - -function provision_become_dc(subobj, message, erase, paths, session_info) -{ - var lp = loadparm_init(); - var sys = sys_init(); - var info = new Object(); - - var ok = provision_fix_subobj(subobj, paths); - assert(ok); - - if (subobj.BACKEND_MOD == undefined) { - subobj.BACKEND_MOD = "repl_meta_data"; - } - - info.subobj = subobj; - info.message = message; - info.session_info = session_info; - - message("Setting up templates into " + paths.templates + "\n"); - setup_ldb("provision_templates.ldif", info, paths.templates); - - /* Also wipes the database */ - message("Setting up " + paths.samdb + " partitions\n"); - setup_ldb("provision_partitions.ldif", info, paths.samdb); - - var samdb = open_ldb(info, paths.samdb, false); - - message("Setting up " + paths.samdb + " attributes\n"); - setup_add_ldif("provision_init.ldif", info, samdb, false); - - message("Setting up " + paths.samdb + " rootDSE\n"); - setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false); - - if (erase) { - message("Erasing data from partitions\n"); - ldb_erase_partitions(info, samdb, undefined); - } - - message("Setting up " + paths.samdb + " indexes\n"); - setup_add_ldif("provision_index.ldif", info, samdb, false); - - ok = samdb.transaction_commit(); - assert(ok); - - message("Setting up " + paths.secrets + "\n"); - setup_ldb("secrets_init.ldif", info, paths.secrets); - - setup_ldb("secrets.ldif", info, paths.secrets, false); - - setup_ldb("secrets_dc.ldif", info, paths.secrets, false); - - return true; -} - -function load_schema(subobj, message, samdb) -{ - var lp = loadparm_init(); - var src = lp.get("setup directory") + "/" + "schema.ldif"; - - if (! sys.stat(src)) { - message("Template file not found: %s\n",src); - assert(0); - } - - var schema_data = sys.file_load(src); - - src = lp.get("setup directory") + "/" + "schema_samba4.ldif"; - - if (! sys.stat(src)) { - message("Template file not found: %s\n",src); - assert(0); - } - - schema_data = schema_data + sys.file_load(src); - - schema_data = substitute_var(schema_data, subobj); - - src = lp.get("setup directory") + "/" + "provision_schema_basedn_modify.ldif"; - - if (! sys.stat(src)) { - message("Template file not found: %s\n",src); - assert(0); - } - - var head_data = sys.file_load(src); - head_data = substitute_var(head_data, subobj); - - var ok = samdb.attach_dsdb_schema_from_ldif(head_data, schema_data); - return ok; -} - - -/* - provision samba4 - caution, this wipes all existing data! -*/ -function provision(subobj, message, blank, paths, session_info, credentials, ldapbackend) -{ - var lp = loadparm_init(); - var sys = sys_init(); - var info = new Object(); - random_init(local); - - var ok = provision_fix_subobj(subobj, paths); - assert(ok); - - if (strlower(subobj.SERVERROLE) == strlower("domain controller")) { - if (subobj.BACKEND_MOD == undefined) { - subobj.BACKEND_MOD = "repl_meta_data"; - } - } else { - if (subobj.BACKEND_MOD == undefined) { - subobj.BACKEND_MOD = "objectguid"; - } - } - - if (subobj.DOMAINGUID != undefined) { - subobj.DOMAINGUID_MOD = sprintf("replace: objectGUID\nobjectGUID: %s\n-", subobj.DOMAINGUID); - } else { - subobj.DOMAINGUID_MOD = ""; - } - - if (subobj.HOSTGUID != undefined) { - subobj.HOSTGUID_ADD = sprintf("objectGUID: %s", subobj.HOSTGUID); - } else { - subobj.HOSTGUID_ADD = ""; - } - - info.subobj = subobj; - info.message = message; - info.credentials = credentials; - info.session_info = session_info; - - /* only install a new smb.conf if there isn't one there already */ - var st = sys.stat(paths.smbconf); - if (st == undefined) { - var smbconfsuffix; - if (strlower(subobj.SERVERROLE) == strlower("domain controller")) { - smbconfsuffix = "dc"; - } else if (strlower(subobj.SERVERROLE) == strlower("member server")) { - smbconfsuffix = "member"; - } else { - smbconfsuffix = subobj.SERVERROLE; - } - message("Setting up " + paths.smbconf +"\n"); - setup_file("provision.smb.conf." + smbconfsuffix, info.message, paths.smbconf, subobj); - lp.reload(); - } - /* only install a new shares config db if there is none */ - st = sys.stat(paths.shareconf); - if (st == undefined) { - message("Setting up share.ldb\n"); - setup_ldb("share.ldif", info, paths.shareconf); - } - - message("Setting up " + paths.secrets + "\n"); - setup_ldb("secrets_init.ldif", info, paths.secrets); - setup_ldb("secrets.ldif", info, paths.secrets, false); - - message("Setting up the registry\n"); - var reg = reg_open(); - reg.apply_patchfile(lp.get("setup directory") + "/provision.reg") - - message("Setting up templates into " + paths.templates + "\n"); - setup_ldb("provision_templates.ldif", info, paths.templates); - - message("Setting up " + paths.idmapdb +"\n"); - setup_ldb("idmap_init.ldif", info, paths.idmapdb); - - message("Setting up sam.ldb partitions\n"); - /* Also wipes the database */ - setup_ldb("provision_partitions.ldif", info, paths.samdb); - - var samdb = open_ldb(info, paths.samdb, false); - - message("Setting up sam.ldb attributes\n"); - setup_add_ldif("provision_init.ldif", info, samdb, false); - - message("Setting up sam.ldb rootDSE\n"); - setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false); - - message("Erasing data from partitions\n"); - ldb_erase_partitions(info, samdb, ldapbackend); - - // (hack) Reload, now we have the partitions and rootdse loaded. - var commit_ok = samdb.transaction_commit(); - if (!commit_ok) { - info.message("samdb commit failed: " + samdb.errstring() + "\n"); - assert(commit_ok); - } - samdb.close(); - - message("Pre-loading the Samba4 and AD schema\n"); - - samdb = open_ldb(info, paths.samdb, false); - - samdb.set_domain_sid(subobj.DOMAINSID); - - if (strlower(subobj.SERVERROLE) == strlower("domain controller")) { - if (subobj.INVOCATIONID == undefined) { - subobj.INVOCATIONID = randguid(); - } - samdb.set_ntds_invocationId(subobj.INVOCATIONID); - if (subobj.BACKEND_MOD == undefined) { - subobj.BACKEND_MOD = "repl_meta_data"; - } - } else { - if (subobj.BACKEND_MOD == undefined) { - subobj.BACKEND_MOD = "objectguid"; - } - } - - var load_schema_ok = load_schema(subobj, message, samdb); - assert(load_schema_ok.is_ok); - - message("Adding DomainDN: " + subobj.DOMAINDN + " (permitted to fail)\n"); - var add_ok = setup_add_ldif("provision_basedn.ldif", info, samdb, true); - message("Modifying DomainDN: " + subobj.DOMAINDN + "\n"); - var modify_basedn_ok = setup_ldb_modify("provision_basedn_modify.ldif", info, samdb); - if (!modify_basedn_ok) { - if (!add_ok) { - message("%s", "Failed to both add and modify " + subobj.DOMAINDN + " in target " + subobj.DOMAINDN_LDB + ": " + samdb.errstring() + "\n"); - message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); - }; - assert(modify_basedn_ok); - }; - - message("Adding configuration container (permitted to fail)\n"); - var add_config_ok = setup_add_ldif("provision_configuration_basedn.ldif", info, samdb, true); - message("Modifying configuration container\n"); - var modify_config_ok = setup_ldb_modify("provision_configuration_basedn_modify.ldif", info, samdb); - if (!modify_config_ok) { - if (!add_config_ok) { - message("%s", "Failed to both add and modify " + subobj.CONFIGDN + " in target " + subobj.CONFIGDN_LDB + ": " + samdb.errstring() + "\n"); - message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); - } - assert(modify_config_ok); - } - - message("Adding schema container (permitted to fail)\n"); - var add_schema_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true); - message("Modifying schema container\n"); - var modify_schema_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb); - if (!modify_schema_ok) { - if (!add_schema_ok) { - message("%s", "Failed to both add and modify " + subobj.SCHEMADN + " in target " + subobj.SCHEMADN_LDB + ": " + samdb.errstring() + "\n"); - message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); - } - message("Failed to modify the schema container: " + samdb.errstring() + "\n"); - assert(modify_schema_ok); - } - - message("Setting up sam.ldb Samba4 schema\n"); - setup_add_ldif("schema_samba4.ldif", info, samdb, false); - message("Setting up sam.ldb AD schema\n"); - setup_add_ldif("schema.ldif", info, samdb, false); - - message("Setting up sam.ldb configuration data\n"); - setup_add_ldif("provision_configuration.ldif", info, samdb, false); - - message("Setting up display specifiers\n"); - setup_add_ldif("display_specifiers.ldif", info, samdb, false); - - message("Adding users container (permitted to fail)\n"); - var add_users_ok = setup_add_ldif("provision_users_add.ldif", info, samdb, true); - message("Modifying users container\n"); - var modify_users_ok = setup_ldb_modify("provision_users_modify.ldif", info, samdb); - if (!modify_users_ok) { - if (!add_users_ok) { - message("Failed to both add and modify the users container\n"); - } - assert(modify_users_ok); - } - message("Adding computers container (permitted to fail)\n"); - var add_computers_ok = setup_add_ldif("provision_computers_add.ldif", info, samdb, true); - message("Modifying computers container\n"); - var modify_computers_ok = setup_ldb_modify("provision_computers_modify.ldif", info, samdb); - if (!modify_computers_ok) { - if (!add_computers_ok) { - message("Failed to both add and modify the computers container\n"); - } - assert(modify_computers_ok); - } - - message("Setting up sam.ldb data\n"); - setup_add_ldif("provision.ldif", info, samdb, false); - - if (blank != false) { - message("Setting up sam.ldb index\n"); - setup_add_ldif("provision_index.ldif", info, samdb, false); - - message("Setting up sam.ldb rootDSE marking as syncronized\n"); - setup_modify_ldif("provision_rootdse_modify.ldif", info, samdb, false); - - var commit_ok = samdb.transaction_commit(); - if (!commit_ok) { - info.message("ldb commit failed: " + samdb.errstring() + "\n"); - assert(commit_ok); - } - return true; - } - -// message("Activate schema module"); -// setup_modify_ldif("schema_activation.ldif", info, samdb, false); -// -// // (hack) Reload, now we have the schema loaded. -// var commit_ok = samdb.transaction_commit(); -// if (!commit_ok) { -// info.message("samdb commit failed: " + samdb.errstring() + "\n"); -// assert(commit_ok); -// } -// samdb.close(); -// -// samdb = open_ldb(info, paths.samdb, false); -// - message("Setting up sam.ldb users and groups\n"); - setup_add_ldif("provision_users.ldif", info, samdb, false); - - if (strlower(subobj.SERVERROLE) == strlower("domain controller")) { - message("Setting up self join\n"); - setup_add_ldif("provision_self_join.ldif", info, samdb, false); - setup_add_ldif("provision_group_policy.ldif", info, samdb, false); - - sys.mkdir(paths.sysvol, 0755); - sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN, 0755); - sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies", 0755); - sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}", 0755); - sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}/Machine", 0755); - sys.mkdir(paths.sysvol + "/"+ subobj.DNSDOMAIN + "/Policies/{" + subobj.POLICYGUID + "}/User", 0755); - - sys.mkdir(paths.netlogon, 0755); - - setup_ldb("secrets_dc.ldif", info, paths.secrets, false); - - } - - if (setup_name_mappings(info, samdb) == false) { - return false; - } - - message("Setting up sam.ldb index\n"); - setup_add_ldif("provision_index.ldif", info, samdb, false); - - message("Setting up sam.ldb rootDSE marking as syncronized\n"); - setup_modify_ldif("provision_rootdse_modify.ldif", info, samdb, false); - - var commit_ok = samdb.transaction_commit(); - if (!commit_ok) { - info.message("samdb commit failed: " + samdb.errstring() + "\n"); - assert(commit_ok); - } - - message("Setting up phpLDAPadmin configuration\n"); - setup_file("phpldapadmin-config.php", info.message, paths.phpldapadminconfig, subobj); - message("Please install the phpLDAPadmin configuration located at " + paths.phpldapadminconfig + " into /etc/phpldapadmin/config.php\n"); - - return true; -} - -/* - provision just the schema into a temporary ldb, so we can run ad2oLschema on it -*/ -function provision_schema(subobj, message, tmp_schema_path, paths) -{ - var lp = loadparm_init(); - var sys = sys_init(); - var info = new Object(); - - var ok = provision_fix_subobj(subobj, paths); - assert(ok); - - info.subobj = subobj; - info.message = message; - - message("Setting up sam.ldb partitions\n"); - - /* This will erase anything in the tmp db */ - var samdb = open_ldb(info, tmp_schema_path, true); - - message("Setting up sam.ldb attributes\n"); - setup_add_ldif("provision_init.ldif", info, samdb, false); - - message("Setting up sam.ldb rootDSE\n"); - setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false); - - message("Adding schema container (permitted to fail)\n"); - var add_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true); - message("Modifying schema container\n"); - var modify_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb); - if (!modify_ok) { - if (!add_ok) { - message("Failed to both add and modify schema dn: " + samdb.errstring() + "\n"); - message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); - assert(modify_ok); - } - message("Failed to modify the schema container: " + samdb.errstring() + "\n"); - assert(modify_ok); - } - - message("Setting up sam.ldb Samba4 schema\n"); - setup_add_ldif("schema_samba4.ldif", info, samdb, false); - message("Setting up sam.ldb AD schema\n"); - setup_add_ldif("schema.ldif", info, samdb, false); - - var commit_ok = samdb.transaction_commit(); - if (!commit_ok) { - info.message("samdb commit failed: " + samdb.errstring() + "\n"); - assert(commit_ok); - } - samdb.close(); -} - -/* Write out a DNS zone file, from the info in the current database */ -function provision_dns(subobj, message, paths, session_info, credentials) -{ - var lp = loadparm_init(); - if (strlower(subobj.SERVERROLE) != strlower("domain controller")) { - message("No DNS zone required for role %s\n", subobj.SERVERROLE); - return; - } - message("Setting up DNS zone: " + subobj.DNSDOMAIN + " \n"); - var ldb = ldb_init(); - ldb.session_info = session_info; - ldb.credentials = credentials; - - /* connect to the sam */ - var ok = ldb.connect(paths.samdb); - assert(ok); - - /* These values may have changed, due to an incoming SamSync, - or may not have been specified, so fetch them from the database */ - - var attrs = new Array("objectGUID"); - res = ldb.search("objectGUID=*", subobj.DOMAINDN, ldb.SCOPE_BASE, attrs); - assert(res.error == 0); - assert(res.msgs.length == 1); - assert(res.msgs[0].objectGUID != undefined); - subobj.DOMAINGUID = res.msgs[0].objectGUID; - - subobj.HOSTGUID = searchone(ldb, subobj.DOMAINDN, "(&(objectClass=computer)(cn=" + subobj.NETBIOSNAME + "))", "objectGUID"); - assert(subobj.HOSTGUID != undefined); - - setup_file("provision.zone", - message, paths.dns, - subobj); - - setup_file("named.conf", - message, paths.named_conf, - subobj); - - message("Please install the zone located in " + paths.dns + " into your DNS server. A sample BIND configuration snippit is at " + paths.named_conf + "\n"); -} - - -/* - guess reasonably default options for provisioning -*/ -function provision_guess() -{ - var subobj = new Object(); - var nss = nss_init(); - var lp = loadparm_init(); - var rdn_list; - random_init(local); - - subobj.SERVERROLE = strlower(lp.get("server role")); - subobj.REALM = strupper(lp.get("realm")); - subobj.DOMAIN = lp.get("workgroup"); - subobj.HOSTNAME = hostname(); - - assert(subobj.REALM); - assert(subobj.DOMAIN); - assert(subobj.HOSTNAME); - - subobj.VERSION = version(); - subobj.HOSTIP = hostip(); - subobj.DOMAINSID = randsid(); - subobj.POLICYGUID = randguid(); - subobj.KRBTGTPASS = randpass(12); - subobj.MACHINEPASS = randpass(12); - subobj.DNSPASS = randpass(12); - subobj.ADMINPASS = randpass(12); - subobj.LDAPMANAGERPASS = randpass(12); - subobj.DEFAULTSITE = "Default-First-Site-Name"; - subobj.DATESTRING = datestring; - subobj.ROOT = findnss(nss.getpwnam, "root"); - subobj.NOBODY = findnss(nss.getpwnam, "nobody"); - subobj.NOGROUP = findnss(nss.getgrnam, "nogroup", "nobody"); - subobj.WHEEL = findnss(nss.getgrnam, "wheel", "root", "staff", "adm"); - subobj.BACKUP = findnss(nss.getgrnam, "backup", "wheel", "root", "staff"); - subobj.USERS = findnss(nss.getgrnam, "users", "guest", "other", "unknown", "usr"); - - //Add modules to the list to activate them by default - //beware often order is important - // - // Some Known ordering constraints: - // - rootdse must be first, as it makes redirects from "" -> cn=rootdse - // - objectclass must be before password_hash, because password_hash checks - // that the objectclass is of type person (filled in by the objectclass - // module when expanding the objectclass list) - // - partition must be last - // - each partition has its own module list then - var modules_list = new Array("rootdse", - "paged_results", - "ranged_results", - "anr", - "server_sort", - "extended_dn", - "asq", - "samldb", - "rdn_name", - "objectclass", - "kludge_acl", - "operational"); - var tdb_modules_list = new Array("subtree_rename", - "subtree_delete", - "linked_attributes"); - var modules_list2 = new Array("show_deleted", - "partition"); - subobj.MODULES_LIST = join(",", modules_list); - subobj.TDB_MODULES_LIST = "," + join(",", tdb_modules_list); - subobj.MODULES_LIST2 = join(",", modules_list2); - subobj.DOMAINDN_LDB = "users.ldb"; - subobj.CONFIGDN_LDB = "configuration.ldb"; - subobj.SCHEMADN_LDB = "schema.ldb"; - subobj.DOMAINDN_MOD = "pdc_fsmo,password_hash,instancetype"; - subobj.CONFIGDN_MOD = "naming_fsmo,instancetype"; - subobj.SCHEMADN_MOD = "schema_fsmo,instancetype"; - - subobj.ACI = "# no aci for local ldb"; - - return subobj; -} - -/* - search for one attribute as a string - */ -function searchone(ldb, basedn, expression, attribute) -{ - var attrs = new Array(attribute); - res = ldb.search(expression, basedn, ldb.SCOPE_SUBTREE, attrs); - if (res.error != 0 || - res.msgs.length != 1 || - res.msgs[0][attribute] == undefined) { - return undefined; - } - return res.msgs[0][attribute]; -} - -/* - modify an account to remove the -*/ -function enable_account(ldb, user_dn) -{ - var attrs = new Array("userAccountControl"); - var res = ldb.search(NULL, user_dn, ldb.SCOPE_ONELEVEL, attrs); - assert(res.error == 0); - assert(res.msgs.length == 1); - var userAccountControl = res.msgs[0].userAccountControl; - userAccountControl = userAccountControl - 2; /* remove disabled bit */ - var mod = sprintf(" -dn: %s -changetype: modify -replace: userAccountControl -userAccountControl: %u -", - user_dn, userAccountControl); - var ok = ldb.modify(mod); - return (ok.error == 0); -} - - -/* - add a new user record -*/ -function newuser(username, unixname, password, message, session_info, credentials) -{ - var lp = loadparm_init(); - var samdb = lp.get("sam database"); - var ldb = ldb_init(); - random_init(local); - ldb.session_info = session_info; - ldb.credentials = credentials; - - /* connect to the sam */ - var ok = ldb.connect(samdb); - assert(ok); - - ldb.transaction_start(); - - /* find the DNs for the domain and the domain users group */ - var attrs = new Array("defaultNamingContext"); - res = ldb.search("defaultNamingContext=*", "", ldb.SCOPE_BASE, attrs); - assert(res.error == 0); - assert(res.msgs.length == 1 && res.msgs[0].defaultNamingContext != undefined); - var domain_dn = res.msgs[0].defaultNamingContext; - assert(domain_dn != undefined); - var dom_users = searchone(ldb, domain_dn, "name=Domain Users", "dn"); - assert(dom_users != undefined); - - var user_dn = sprintf("CN=%s,CN=Users,%s", username, domain_dn); - - - /* - the new user record. note the reliance on the samdb module to fill - in a sid, guid etc - */ - var ldif = sprintf(" -dn: %s -sAMAccountName: %s -unixName: %s -sambaPassword: %s -objectClass: user -", - user_dn, username, - unixname, password); - /* - add the user to the users group as well - */ - var modgroup = sprintf(" -dn: %s -changetype: modify -add: member -member: %s -", - dom_users, user_dn); - - - /* - now the real work - */ - message("Adding user %s\n", user_dn); - ok = ldb.add(ldif); - if (ok.error != 0) { - message("Failed to add %s - %s\n", user_dn, ok.errstr); - return false; - } - - message("Modifying group %s\n", dom_users); - ok = ldb.modify(modgroup); - if (ok.error != 0) { - message("Failed to modify %s - %s\n", dom_users, ok.errstr); - return false; - } - - /* - modify the userAccountControl to remove the disabled bit - */ - ok = enable_account(ldb, user_dn); - if (ok) { - ldb.transaction_commit(); - } - return ok; -} - -// Check whether a name is valid as a NetBIOS name. -// FIXME: There are probably more constraints here. -// crh has a paragraph on this in his book (1.4.1.1) -function valid_netbios_name(name) -{ - if (strlen(name) > 15) return false; - return true; -} - -function provision_validate(subobj, message) -{ - var lp = loadparm_init(); - - if (!valid_netbios_name(subobj.DOMAIN)) { - message("Invalid NetBIOS name for domain\n"); - return false; - } - - if (!valid_netbios_name(subobj.NETBIOSNAME)) { - message("Invalid NetBIOS name for host\n"); - return false; - } - - - if (strupper(lp.get("workgroup")) != strupper(subobj.DOMAIN_CONF)) { - message("workgroup '%s' in smb.conf must match chosen domain '%s'\n", - lp.get("workgroup"), subobj.DOMAIN_CONF); - return false; - } - - if (strupper(lp.get("realm")) != strupper(subobj.REALM_CONF)) { - message("realm '%s' in smb.conf must match chosen realm '%s'\n", - lp.get("realm"), subobj.REALM_CONF); - return false; - } - - if (strlower(lp.get("server role")) != strlower(subobj.SERVERROLE)) { - message("server role '%s' in smb.conf must match chosen role '%s'\n", - lp.get("server role"), subobj.SERVERROLE); - return false; - } - - return true; -} - -function join_domain(domain, netbios_name, join_type, creds, message) -{ - var ctx = NetContext(creds); - var joindom = new Object(); - joindom.domain = domain; - joindom.join_type = join_type; - joindom.netbios_name = netbios_name; - if (!ctx.JoinDomain(joindom)) { - message("Domain Join failed: " + joindom.error_string); - return false; - } - return true; -} - -/* Vampire a remote domain. Session info and credentials are required for for - * access to our local database (might be remote ldap) - */ - -function vampire(domain, session_info, credentials, message) { - var ctx = NetContext(credentials); - var vampire_ctx = new Object(); - var machine_creds = credentials_init(); - machine_creds.set_domain(form.DOMAIN); - if (!machine_creds.set_machine_account()) { - message("Failed to access domain join information!"); - return false; - } - vampire_ctx.machine_creds = machine_creds; - vampire_ctx.session_info = session_info; - if (!ctx.SamSyncLdb(vampire_ctx)) { - message("Migration of remote domain to Samba failed: " + vampire_ctx.error_string); - return false; - } - - return true; -} - -return 0; diff --git a/source4/scripting/libjs/samr.js b/source4/scripting/libjs/samr.js deleted file mode 100644 index 6e8c70af3c..0000000000 --- a/source4/scripting/libjs/samr.js +++ /dev/null @@ -1,170 +0,0 @@ -/* - samr rpc utility functions - Copyright Andrew Tridgell 2005 - released under the GNU GPL version 3 or later -*/ - -if (global["HAVE_SAMR_JS"] != undefined) { - return; -} -HAVE_SAMR_JS=1 - -/* - return a list of names and indexes from a samArray -*/ -function samArray(output) -{ - var list = new Array(output.num_entries); - if (output.sam == NULL) { - return list; - } - var i, entries = output.sam.entries; - for (i=0;i<output.num_entries;i++) { - list[i] = new Object(); - list[i].name = entries[i].name; - list[i].idx = entries[i].idx; - } - return list; -} - -/* - connect to the sam database -*/ -function samrConnect(conn) -{ - security_init(conn); - var io = irpcObj(); - io.input.system_name = NULL; - io.input.access_mask = conn.SEC_FLAG_MAXIMUM_ALLOWED; - var status = conn.samr_Connect2(io); - check_status_ok(status); - return io.output.connect_handle; -} - -/* - close a handle -*/ -function samrClose(conn, handle) -{ - var io = irpcObj(); - io.input.handle = handle; - var status = conn.samr_Close(io); - check_status_ok(status); -} - -/* - get the sid for a domain -*/ -function samrLookupDomain(conn, handle, domain) -{ - var io = irpcObj(); - io.input.connect_handle = handle; - io.input.domain_name = domain; - var status = conn.samr_LookupDomain(io); - check_status_ok(status); - return io.output.sid; -} - -/* - open a domain by sid -*/ -function samrOpenDomain(conn, handle, sid) -{ - var io = irpcObj(); - io.input.connect_handle = handle; - io.input.access_mask = conn.SEC_FLAG_MAXIMUM_ALLOWED; - io.input.sid = sid; - var status = conn.samr_OpenDomain(io); - check_status_ok(status); - return io.output.domain_handle; -} - -/* - open a user by rid -*/ -function samrOpenUser(conn, handle, rid) -{ - var io = irpcObj(); - io.input.domain_handle = handle; - io.input.access_mask = conn.SEC_FLAG_MAXIMUM_ALLOWED; - io.input.rid = rid; - var status = conn.samr_OpenUser(io); - check_status_ok(status); - return io.output.user_handle; -} - -/* - return a list of all users -*/ -function samrEnumDomainUsers(conn, dom_handle) -{ - var io = irpcObj(); - io.input.domain_handle = dom_handle; - io.input.resume_handle = 0; - io.input.acct_flags = 0; - io.input.max_size = -1; - var status = conn.samr_EnumDomainUsers(io); - check_status_ok(status); - return samArray(io.output); -} - -/* - return a list of all groups -*/ -function samrEnumDomainGroups(conn, dom_handle) -{ - var io = irpcObj(); - io.input.domain_handle = dom_handle; - io.input.resume_handle = 0; - io.input.acct_flags = 0; - io.input.max_size = -1; - var status = conn.samr_EnumDomainGroups(io); - check_status_ok(status); - return samArray(io.output); -} - -/* - return a list of domains -*/ -function samrEnumDomains(conn, handle) -{ - var io = irpcObj(); - io.input.connect_handle = handle; - io.input.resume_handle = 0; - io.input.buf_size = -1; - var status = conn.samr_EnumDomains(io); - check_status_ok(status); - return samArray(io.output); -} - -/* - return information about a user -*/ -function samrQueryUserInfo(conn, user_handle, level) -{ - var r, io = irpcObj(); - io.input.user_handle = user_handle; - io.input.level = level; - var status = conn.samr_QueryUserInfo(io); - check_status_ok(status); - return io.output.info.info3; -} - - -/* - fill a user array with user information from samrQueryUserInfo -*/ -function samrFillUserInfo(conn, dom_handle, users, level) -{ - var i; - for (i=0;i<users.length;i++) { - var r, user_handle, info; - user_handle = samrOpenUser(conn, dom_handle, users[i].idx); - info = samrQueryUserInfo(conn, user_handle, level); - info.name = users[i].name; - info.idx = users[i].idx; - users[i] = info; - samrClose(conn, user_handle); - } -} - diff --git a/source4/scripting/libjs/winreg.js b/source4/scripting/libjs/winreg.js deleted file mode 100644 index 9db415694d..0000000000 --- a/source4/scripting/libjs/winreg.js +++ /dev/null @@ -1,291 +0,0 @@ -/* - winreg rpc utility functions - Copyright Andrew Tridgell 2005 - released under the GNU GPL version 3 or later -*/ - -libinclude("base.js"); - -/* - close a handle -*/ -function __winreg_close(handle) -{ - var io = irpcObj(); - io.input.handle = handle; - this.winreg_CloseKey(io); -} - - -/* - open a hive -*/ -function __winreg_open_hive(hive) -{ - var io = irpcObj(); - io.input.system_name = NULL; - io.input.access_mask = this.SEC_FLAG_MAXIMUM_ALLOWED; - var status; - if (hive == "HKLM") { - status = this.winreg_OpenHKLM(io); - } else if (hive == "HKCR") { - status = this.winreg_OpenHKCR(io); - } else if (hive == "HKPD") { - status = this.winreg_OpenHKPD(io); - } else if (hive == "HKU") { - status = this.winreg_OpenHKU(io); - } else { - this._last_error = "Unknown hive " + hive; - return undefined; - } - if (!status.is_ok) { - return undefined; - } - return io.output.handle; -} - -/* - open a handle to a path -*/ -function __winreg_open_path(path) -{ - var s = string_init(); - var i, components = s.split('\\', path); - - /* cope with a leading slash */ - if (components[0] == '') { - for (i=0;i<(components.length-1);i++) { - components[i] = components[i+1]; - } - delete(components[i]); - } - - if (components.length == 0) { - return undefined; - } - - var handle = this.open_hive(components[0]); - if (handle == undefined) { - return undefined; - } - - if (components.length == 1) { - return handle; - } - - var hpath = components[1]; - - for (i=2;i<components.length;i++) { - hpath = hpath + "\\" + components[i]; - } - - io = irpcObj(); - io.input.parent_handle = handle; - io.input.keyname = hpath; - io.input.unknown = 0; - io.input.access_mask = this.SEC_FLAG_MAXIMUM_ALLOWED; - var status = this.winreg_OpenKey(io); - - this.close(handle); - - if (!status.is_ok) { - return undefined; - } - if (io.output.result != "WERR_OK") { - return undefined; - } - - return io.output.handle; -} - -/* - return a list of keys for a winreg server given a path - usage: - list = reg.enum_path(path); -*/ -function __winreg_enum_path(path) -{ - var list = new Array(0); - - if (path == null || path == "\\" || path == "") { - return new Array("HKLM", "HKU"); - } - - var handle = this.open_path(path); - if (handle == undefined) { - return undefined; - } - - var io = irpcObj(); - io.input.handle = handle; - io.input.name = new Object(); - io.input.name.length = 0; - io.input.name.size = 32; - io.input.name.name = NULL; - io.input.keyclass = new Object(); - io.input.keyclass.length = 0; - io.input.keyclass.size = 1024; - io.input.keyclass.name = NULL; - io.input.last_changed_time = 0; - - var idx = 0; - for (idx=0;idx >= 0;idx++) { - io.input.enum_index = idx; - var status = this.winreg_EnumKey(io); - if (!status.is_ok) { - this.close(handle); - return list; - } - var out = io.output; - if (out.result == "WERR_MORE_DATA") { - io.input.name.size = io.input.name.size * 2; - idx--; - if (io.input.name.size > 32000) { - this.close(handle); - return list; - } - continue; - } - if (out.result != "WERR_OK") { - this.close(handle); - return list; - } - list[list.length] = out.name.name; - } - - this.close(handle); - return list; -} - - -/* - return a list of values for a winreg server given a path - usage: - list = reg.enum_values(path); - - each returned list element is an object containing a name, a - type and a value -*/ -function __winreg_enum_values(path) -{ - var data = datablob_init(); - var list = new Array(0); - - var handle = this.open_path(path); - if (handle == undefined) { - return undefined; - } - - var io = irpcObj(); - io.input.handle = handle; - io.input.name = new Object(); - io.input.name.length = 0; - io.input.name.size = 128; - io.input.name.name = ""; - io.input.type = 0; - io.input.value = new Array(0); - io.input.size = 1024; - io.input.length = 0; - - var idx; - for (idx=0;idx >= 0;idx++) { - io.input.enum_index = idx; - var status = this.winreg_EnumValue(io); - if (!status.is_ok) { - this.close(handle); - return list; - } - var out = io.output; - if (out.result == "WERR_MORE_DATA") { - io.input.size = io.input.size * 2; - io.input.name.size = io.input.name.size * 2; - idx--; - /* limit blobs to 1M */ - if (io.input.size > 1000000) { - this.close(handle); - return list; - } - continue; - } - if (out.result != "WERR_OK") { - this.close(handle); - return list; - } - var el = new Object(); - el.name = out.name.name; - el.type = out.type; - el.rawvalue = out.value; - el.value = data.regToVar(el.rawvalue, el.type); - el.size = out.size; - list[list.length] = el; - } - - this.close(handle); - return list; -} - - -/* - create a new key - ok = reg.create_key(path, key); -*/ -function __winreg_create_key(path, key) -{ - var handle = this.open_path(path); - if (handle == undefined) { - return undefined; - } - - var io = irpcObj(); - io.input.handle = handle; - io.input.name = key; - io.input.keyclass = NULL; - io.input.options = 0; - io.input.access_mask = this.SEC_FLAG_MAXIMUM_ALLOWED; - io.input.secdesc = NULL; - io.input.action_taken = 0; - - var status = this.winreg_CreateKey(io); - this.close(handle); - if (!status.is_ok) { - return false; - } - if (io.output.result != "WERR_OK") { - return false; - } - this.close(io.output.new_handle); - return true; -} - - -/* - return a string for a winreg type -*/ -function __winreg_typestring(type) -{ - return this.typenames[type]; -} - -/* - initialise the winreg lib, returning an object -*/ -function winregObj() -{ - var reg = winreg_init(); - security_init(reg); - - reg.typenames = new Array("REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", - "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", "REG_MULTI_SZ", - "REG_RESOURCE_LIST", "REG_FULL_RESOURCE_DESCRIPTOR", - "REG_RESOURCE_REQUIREMENTS_LIST", "REG_QWORD"); - - reg.close = __winreg_close; - reg.open_hive = __winreg_open_hive; - reg.open_path = __winreg_open_path; - reg.enum_path = __winreg_enum_path; - reg.enum_values = __winreg_enum_values; - reg.create_key = __winreg_create_key; - reg.typestring = __winreg_typestring; - - return reg; -} diff --git a/source4/scripting/python/config.m4 b/source4/scripting/python/config.m4 index 3790071ba8..b599aaefb0 100644 --- a/source4/scripting/python/config.m4 +++ b/source4/scripting/python/config.m4 @@ -5,7 +5,7 @@ AC_ARG_VAR([PYTHON_VERSION],[The installed Python will be appended to the Python interpreter canonical name.]) -AC_PROG_SWIG(1.3.31) +AC_PROG_SWIG(1.3.35) AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]]) if test -z "$PYTHON"; then @@ -64,10 +64,14 @@ SMB_EXT_LIB(EXT_LIB_PYTHON, [$PYTHON_LDFLAGS], [$PYTHON_CFLAGS]) AC_MSG_CHECKING(working python module support) if test $working_python = yes; then SMB_ENABLE(EXT_LIB_PYTHON,YES) - SMB_ENABLE(smbpython,YES) SMB_ENABLE(LIBPYTHON,YES) AC_MSG_RESULT([yes]) else AC_MSG_ERROR([Python not found. Please install Python 2.x and its development headers/libraries.]) fi +AC_MSG_CHECKING(python library directory) +pythondir=`$PYTHON -c "from distutils import sysconfig; print sysconfig.get_python_lib(1, 0, '\\${prefix}')"` +AC_MSG_RESULT($pythondir) + +AC_SUBST(pythondir) diff --git a/source4/scripting/python/config.mk b/source4/scripting/python/config.mk index 09c77813ca..b494ee6e8d 100644 --- a/source4/scripting/python/config.mk +++ b/source4/scripting/python/config.mk @@ -1,49 +1,37 @@ -[BINARY::smbpython] -PRIVATE_DEPENDENCIES = LIBPYTHON -OBJ_FILES = smbpython.o - [SUBSYSTEM::LIBPYTHON] PUBLIC_DEPENDENCIES = EXT_LIB_PYTHON +PRIVATE_DEPENDENCIES = PYTALLOC INIT_FUNCTION_SENTINEL = { NULL, NULL } -OBJ_FILES = modules.o pytalloc.o + +LIBPYTHON_OBJ_FILES = $(addprefix $(pyscriptsrcdir)/, modules.o) + +[SUBSYSTEM::PYTALLOC] +PUBLIC_DEPENDENCIES = EXT_LIB_PYTHON LIBTALLOC + +PYTALLOC_OBJ_FILES = $(addprefix $(pyscriptsrcdir)/, pytalloc.o) [PYTHON::python_uuid] PRIVATE_DEPENDENCIES = LIBNDR -OBJ_FILES = uuidmodule.o + +python_uuid_OBJ_FILES = $(pyscriptsrcdir)/uuidmodule.o [PYTHON::python_misc] +LIBRARY_REALNAME = samba/_misc.$(SHLIBEXT) PRIVATE_DEPENDENCIES = LIBNDR LIBLDB SAMDB CREDENTIALS -SWIG_FILE = misc.i - -# Swig extensions -swig:: pythonmods - -.SUFFIXES: _wrap.c .i -.i_wrap.c: - [ "$(SWIG)" = "no" ] || $(SWIG) -O -Wall -I$(srcdir)/scripting/swig -python -keyword $< +python_misc_OBJ_FILES = $(pyscriptsrcdir)/misc_wrap.o -realdistclean:: - @echo "Removing SWIG output files" - # FIXME: Remove _wrap.c files +$(python_misc_OBJ_FILES): CFLAGS+=$(CFLAG_NO_UNUSED_MACROS) $(CFLAG_NO_CAST_QUAL) -pythonmods:: $(PYTHON_DSOS) $(PYTHON_PYS) +_PY_FILES = $(shell find $(pyscriptsrcdir)/samba $(pyscriptsrcdir)/subunit -name "*.py") -PYDOCTOR_MODULES=bin/python/ldb.py bin/python/auth.py bin/python/credentials.py bin/python/registry.py bin/python/tdb.py bin/python/security.py bin/python/events.py bin/python/net.py +$(foreach pyfile, $(_PY_FILES),$(eval $(call python_py_module_template,$(patsubst $(pyscriptsrcdir)/%,%,$(pyfile)),$(pyfile)))) -pydoctor:: pythonmods - LD_LIBRARY_PATH=bin/shared PYTHONPATH=bin/python pydoctor --project-name=Samba --make-html --docformat=restructuredtext --add-package scripting/python/samba/ $(addprefix --add-module , $(PYDOCTOR_MODULES)) +$(eval $(call python_py_module_template,samba/misc.py,$(pyscriptsrcdir)/misc.py)) -bin/python/%.py: - mkdir -p $(@D) - cp $< $@ +EPYDOC_OPTIONS = --no-private --url http://www.samba.org/ --no-sourcecode -installpython:: pythonmods - @$(SHELL) $(srcdir)/script/installpython.sh \ - $(INSTALLPERMS) \ - $(DESTDIR)$(PYTHONDIR) \ - scripting/python bin/python +epydoc:: pythonmods + PYTHONPATH=$(pythonbuilddir) epydoc $(EPYDOC_OPTIONS) samba tdb ldb subunit -clean:: - @echo "Removing python modules" - @rm -rf bin/python/* +install:: installpython diff --git a/source4/scripting/python/misc.i b/source4/scripting/python/misc.i index e04e6a6906..9a4c124121 100644 --- a/source4/scripting/python/misc.i +++ b/source4/scripting/python/misc.i @@ -40,6 +40,15 @@ %rename(random_password) generate_random_str; char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len); +%feature("docstring") ldb_set_credentials "S.set_credentials(credentials)\n" + "Set credentials to use when connecting."; + +%feature("docstring") ldb_set_session_info "S.set_session_info(session_info)\n" + "Set session info to use when connecting."; + +%feature("docstring") ldb_set_loadparm "S.set_loadparm(session_info)\n" + "Set loadparm context to use when connecting."; + %inline %{ void ldb_set_credentials(struct ldb_context *ldb, struct cli_credentials *creds) { @@ -58,14 +67,20 @@ void ldb_set_loadparm(struct ldb_context *ldb, struct loadparm_context *lp_ctx) %} +%feature("docstring") samdb_set_domain_sid "S.set_domain_sid(sid)\n" + "Set SID of domain to use."; bool samdb_set_domain_sid(struct ldb_context *ldb, const struct dom_sid *dom_sid_in); WERROR dsdb_attach_schema_from_ldif_file(struct ldb_context *ldb, const char *pf, const char *df); +%feature("docstring") samba_version_string "version()\n" + "Obtain the Samba version."; %rename(version) samba_version_string; const char *samba_version_string(void); int dsdb_set_global_schema(struct ldb_context *ldb); +%feature("docstring") ldb_register_samba_handlers "register_samba_handlers()\n" + "Register Samba-specific LDB modules and schemas."; int ldb_register_samba_handlers(struct ldb_context *ldb); %inline %{ @@ -78,3 +93,8 @@ bool dsdb_set_ntds_invocation_id(struct ldb_context *ldb, const char *guid) return samdb_set_ntds_invocation_id(ldb, &invocation_id_in); } %} + +char *private_path(TALLOC_CTX* mem_ctx, + struct loadparm_context *lp_ctx, + const char *name); + diff --git a/source4/scripting/python/misc.py b/source4/scripting/python/misc.py index 2fc7fe37e7..25e8d2de8c 100644 --- a/source4/scripting/python/misc.py +++ b/source4/scripting/python/misc.py @@ -1,5 +1,5 @@ # This file was automatically generated by SWIG (http://www.swig.org). -# Version 1.3.33 +# Version 1.3.35 # # Don't modify this file, modify the SWIG interface instead. @@ -62,14 +62,51 @@ import credentials import param import security random_password = _misc.random_password -ldb_set_credentials = _misc.ldb_set_credentials -ldb_set_session_info = _misc.ldb_set_session_info -ldb_set_loadparm = _misc.ldb_set_loadparm -samdb_set_domain_sid = _misc.samdb_set_domain_sid + +def ldb_set_credentials(*args, **kwargs): + """ + S.set_credentials(credentials) + Set credentials to use when connecting. + """ + return _misc.ldb_set_credentials(*args, **kwargs) + +def ldb_set_session_info(*args, **kwargs): + """ + S.set_session_info(session_info) + Set session info to use when connecting. + """ + return _misc.ldb_set_session_info(*args, **kwargs) + +def ldb_set_loadparm(*args, **kwargs): + """ + S.set_loadparm(session_info) + Set loadparm context to use when connecting. + """ + return _misc.ldb_set_loadparm(*args, **kwargs) + +def samdb_set_domain_sid(*args, **kwargs): + """ + S.set_domain_sid(sid) + Set SID of domain to use. + """ + return _misc.samdb_set_domain_sid(*args, **kwargs) dsdb_attach_schema_from_ldif_file = _misc.dsdb_attach_schema_from_ldif_file -version = _misc.version + +def version(*args): + """ + version() + Obtain the Samba version. + """ + return _misc.version(*args) dsdb_set_global_schema = _misc.dsdb_set_global_schema -ldb_register_samba_handlers = _misc.ldb_register_samba_handlers + +def ldb_register_samba_handlers(*args, **kwargs): + """ + register_samba_handlers() + Register Samba-specific LDB modules and schemas. + """ + return _misc.ldb_register_samba_handlers(*args, **kwargs) dsdb_set_ntds_invocation_id = _misc.dsdb_set_ntds_invocation_id +private_path = _misc.private_path diff --git a/source4/scripting/python/misc_wrap.c b/source4/scripting/python/misc_wrap.c index 579d1f379f..22a072fc6f 100644 --- a/source4/scripting/python/misc_wrap.c +++ b/source4/scripting/python/misc_wrap.c @@ -1,6 +1,6 @@ /* ---------------------------------------------------------------------------- * This file was automatically generated by SWIG (http://www.swig.org). - * Version 1.3.33 + * Version 1.3.35 * * This file is not intended to be easily readable and contains a number of * coding conventions designed to improve portability and efficiency. Do not make @@ -126,7 +126,7 @@ /* This should only be incremented when either the layout of swig_type_info changes, or for whatever reason, the runtime changes incompatibly */ -#define SWIG_RUNTIME_VERSION "3" +#define SWIG_RUNTIME_VERSION "4" /* define SWIG_TYPE_TABLE_NAME as "SWIG_TYPE_TABLE" */ #ifdef SWIG_TYPE_TABLE @@ -161,6 +161,7 @@ /* Flags for pointer conversions */ #define SWIG_POINTER_DISOWN 0x1 +#define SWIG_CAST_NEW_MEMORY 0x2 /* Flags for new pointer objects */ #define SWIG_POINTER_OWN 0x1 @@ -301,10 +302,10 @@ SWIGINTERNINLINE int SWIG_CheckState(int r) { extern "C" { #endif -typedef void *(*swig_converter_func)(void *); +typedef void *(*swig_converter_func)(void *, int *); typedef struct swig_type_info *(*swig_dycast_func)(void **); -/* Structure to store inforomation on one type */ +/* Structure to store information on one type */ typedef struct swig_type_info { const char *name; /* mangled name of this type */ const char *str; /* human readable name of this type */ @@ -431,8 +432,8 @@ SWIG_TypeCheckStruct(swig_type_info *from, swig_type_info *into) { Cast a pointer up an inheritance hierarchy */ SWIGRUNTIMEINLINE void * -SWIG_TypeCast(swig_cast_info *ty, void *ptr) { - return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr); +SWIG_TypeCast(swig_cast_info *ty, void *ptr, int *newmemory) { + return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr, newmemory); } /* @@ -856,7 +857,7 @@ SWIG_Python_AddErrorMsg(const char* mesg) Py_DECREF(old_str); Py_DECREF(value); } else { - PyErr_Format(PyExc_RuntimeError, mesg); + PyErr_SetString(PyExc_RuntimeError, mesg); } } @@ -1416,7 +1417,7 @@ PySwigObject_dealloc(PyObject *v) { PySwigObject *sobj = (PySwigObject *) v; PyObject *next = sobj->next; - if (sobj->own) { + if (sobj->own == SWIG_POINTER_OWN) { swig_type_info *ty = sobj->ty; PySwigClientData *data = ty ? (PySwigClientData *) ty->clientdata : 0; PyObject *destroy = data ? data->destroy : 0; @@ -1434,12 +1435,13 @@ PySwigObject_dealloc(PyObject *v) res = ((*meth)(mself, v)); } Py_XDECREF(res); - } else { - const char *name = SWIG_TypePrettyName(ty); + } #if !defined(SWIG_PYTHON_SILENT_MEMLEAK) - printf("swig/python detected a memory leak of type '%s', no destructor found.\n", name); -#endif + else { + const char *name = SWIG_TypePrettyName(ty); + printf("swig/python detected a memory leak of type '%s', no destructor found.\n", (name ? name : "unknown")); } +#endif } Py_XDECREF(next); PyObject_DEL(v); @@ -1944,7 +1946,7 @@ SWIG_Python_GetSwigThis(PyObject *pyobj) SWIGRUNTIME int SWIG_Python_AcquirePtr(PyObject *obj, int own) { - if (own) { + if (own == SWIG_POINTER_OWN) { PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); if (sobj) { int oldown = sobj->own; @@ -1965,6 +1967,8 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int return SWIG_OK; } else { PySwigObject *sobj = SWIG_Python_GetSwigThis(obj); + if (own) + *own = 0; while (sobj) { void *vptr = sobj->ptr; if (ty) { @@ -1978,7 +1982,15 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int if (!tc) { sobj = (PySwigObject *)sobj->next; } else { - if (ptr) *ptr = SWIG_TypeCast(tc,vptr); + if (ptr) { + int newmemory = 0; + *ptr = SWIG_TypeCast(tc,vptr,&newmemory); + if (newmemory == SWIG_CAST_NEW_MEMORY) { + assert(own); + if (own) + *own = *own | SWIG_CAST_NEW_MEMORY; + } + } break; } } @@ -1988,7 +2000,8 @@ SWIG_Python_ConvertPtrAndOwn(PyObject *obj, void **ptr, swig_type_info *ty, int } } if (sobj) { - if (own) *own = sobj->own; + if (own) + *own = *own | sobj->own; if (flags & SWIG_POINTER_DISOWN) { sobj->own = 0; } @@ -2053,8 +2066,13 @@ SWIG_Python_ConvertFunctionPtr(PyObject *obj, void **ptr, swig_type_info *ty) { } if (ty) { swig_cast_info *tc = SWIG_TypeCheck(desc,ty); - if (!tc) return SWIG_ERROR; - *ptr = SWIG_TypeCast(tc,vptr); + if (tc) { + int newmemory = 0; + *ptr = SWIG_TypeCast(tc,vptr,&newmemory); + assert(!newmemory); /* newmemory handling not yet implemented */ + } else { + return SWIG_ERROR; + } } else { *ptr = vptr; } @@ -2519,7 +2537,7 @@ static swig_module_info swig_module = {swig_types, 27, 0, 0, 0, 0}; #define SWIG_name "_misc" -#define SWIGVERSION 0x010333 +#define SWIGVERSION 0x010335 #define SWIG_VERSION SWIGVERSION @@ -3153,17 +3171,80 @@ fail: } +SWIGINTERN PyObject *_wrap_private_path(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) { + PyObject *resultobj = 0; + TALLOC_CTX *arg1 = (TALLOC_CTX *) 0 ; + struct loadparm_context *arg2 = (struct loadparm_context *) 0 ; + char *arg3 = (char *) 0 ; + char *result = 0 ; + void *argp2 = 0 ; + int res2 = 0 ; + int res3 ; + char *buf3 = 0 ; + int alloc3 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + char * kwnames[] = { + (char *) "lp_ctx",(char *) "name", NULL + }; + + arg2 = loadparm_init(NULL); + arg1 = NULL; + if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"|OO:private_path",kwnames,&obj0,&obj1)) SWIG_fail; + if (obj0) { + res2 = SWIG_ConvertPtr(obj0, &argp2,SWIGTYPE_p_loadparm_context, 0 | 0 ); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "private_path" "', argument " "2"" of type '" "struct loadparm_context *""'"); + } + arg2 = (struct loadparm_context *)(argp2); + } + if (obj1) { + res3 = SWIG_AsCharPtrAndSize(obj1, &buf3, NULL, &alloc3); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "private_path" "', argument " "3"" of type '" "char const *""'"); + } + arg3 = (char *)(buf3); + } + result = (char *)private_path(arg1,arg2,(char const *)arg3); + resultobj = SWIG_FromCharPtr((const char *)result); + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + return resultobj; +fail: + if (alloc3 == SWIG_NEWOBJ) free((char*)buf3); + return NULL; +} + + static PyMethodDef SwigMethods[] = { { (char *)"random_password", (PyCFunction) _wrap_random_password, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"ldb_set_credentials", (PyCFunction) _wrap_ldb_set_credentials, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"ldb_set_session_info", (PyCFunction) _wrap_ldb_set_session_info, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"ldb_set_loadparm", (PyCFunction) _wrap_ldb_set_loadparm, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"samdb_set_domain_sid", (PyCFunction) _wrap_samdb_set_domain_sid, METH_VARARGS | METH_KEYWORDS, NULL}, + { (char *)"ldb_set_credentials", (PyCFunction) _wrap_ldb_set_credentials, METH_VARARGS | METH_KEYWORDS, (char *)"\n" + "S.set_credentials(credentials)\n" + "Set credentials to use when connecting.\n" + ""}, + { (char *)"ldb_set_session_info", (PyCFunction) _wrap_ldb_set_session_info, METH_VARARGS | METH_KEYWORDS, (char *)"\n" + "S.set_session_info(session_info)\n" + "Set session info to use when connecting.\n" + ""}, + { (char *)"ldb_set_loadparm", (PyCFunction) _wrap_ldb_set_loadparm, METH_VARARGS | METH_KEYWORDS, (char *)"\n" + "S.set_loadparm(session_info)\n" + "Set loadparm context to use when connecting.\n" + ""}, + { (char *)"samdb_set_domain_sid", (PyCFunction) _wrap_samdb_set_domain_sid, METH_VARARGS | METH_KEYWORDS, (char *)"\n" + "S.set_domain_sid(sid)\n" + "Set SID of domain to use.\n" + ""}, { (char *)"dsdb_attach_schema_from_ldif_file", (PyCFunction) _wrap_dsdb_attach_schema_from_ldif_file, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"version", (PyCFunction)_wrap_version, METH_NOARGS, NULL}, + { (char *)"version", (PyCFunction)_wrap_version, METH_NOARGS, (char *)"\n" + "version()\n" + "Obtain the Samba version.\n" + ""}, { (char *)"dsdb_set_global_schema", (PyCFunction) _wrap_dsdb_set_global_schema, METH_VARARGS | METH_KEYWORDS, NULL}, - { (char *)"ldb_register_samba_handlers", (PyCFunction) _wrap_ldb_register_samba_handlers, METH_VARARGS | METH_KEYWORDS, NULL}, + { (char *)"ldb_register_samba_handlers", (PyCFunction) _wrap_ldb_register_samba_handlers, METH_VARARGS | METH_KEYWORDS, (char *)"\n" + "register_samba_handlers()\n" + "Register Samba-specific LDB modules and schemas.\n" + ""}, { (char *)"dsdb_set_ntds_invocation_id", (PyCFunction) _wrap_dsdb_set_ntds_invocation_id, METH_VARARGS | METH_KEYWORDS, NULL}, + { (char *)"private_path", (PyCFunction) _wrap_private_path, METH_VARARGS | METH_KEYWORDS, NULL}, { NULL, NULL, 0, NULL } }; @@ -3180,7 +3261,7 @@ static swig_type_info _swigt__p_ldb_context = {"_p_ldb_context", "struct ldb_con static swig_type_info _swigt__p_ldb_dn = {"_p_ldb_dn", "struct ldb_dn *|ldb_dn *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_ldif = {"_p_ldb_ldif", "struct ldb_ldif *|ldb_ldif *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_message = {"_p_ldb_message", "ldb_msg *|struct ldb_message *", 0, 0, (void*)0, 0}; -static swig_type_info _swigt__p_ldb_message_element = {"_p_ldb_message_element", "struct ldb_message_element *|ldb_msg_element *", 0, 0, (void*)0, 0}; +static swig_type_info _swigt__p_ldb_message_element = {"_p_ldb_message_element", "struct ldb_message_element *|ldb_message_element *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_ldb_result = {"_p_ldb_result", "struct ldb_result *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_loadparm_context = {"_p_loadparm_context", "struct loadparm_context *|loadparm_context *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_loadparm_service = {"_p_loadparm_service", "struct loadparm_service *|loadparm_service *", 0, 0, (void*)0, 0}; @@ -3352,7 +3433,7 @@ SWIGRUNTIME void SWIG_InitializeModule(void *clientdata) { size_t i; swig_module_info *module_head, *iter; - int found; + int found, init; clientdata = clientdata; @@ -3362,6 +3443,9 @@ SWIG_InitializeModule(void *clientdata) { swig_module.type_initial = swig_type_initial; swig_module.cast_initial = swig_cast_initial; swig_module.next = &swig_module; + init = 1; + } else { + init = 0; } /* Try and load any already created modules */ @@ -3390,6 +3474,12 @@ SWIG_InitializeModule(void *clientdata) { module_head->next = &swig_module; } + /* When multiple interpeters are used, a module could have already been initialized in + a different interpreter, but not yet have a pointer in this interpreter. + In this case, we do not want to continue adding types... everything should be + set up already */ + if (init == 0) return; + /* Now work on filling in swig_module.types */ #ifdef SWIGRUNTIME_DEBUG printf("SWIG_InitializeModule: size %d\n", swig_module.size); diff --git a/source4/scripting/python/modules.c b/source4/scripting/python/modules.c index 2ecad20b8e..0fe15b2fda 100644 --- a/source4/scripting/python/modules.c +++ b/source4/scripting/python/modules.c @@ -19,7 +19,6 @@ #include "includes.h" #include <Python.h> -#include "build.h" extern void init_ldb(void); extern void init_security(void); @@ -35,17 +34,19 @@ extern void init_events(void); extern void inituuid(void); extern void init_net(void); extern void initecho(void); +extern void initdfs(void); +extern void initdrsuapi(void); extern void initwinreg(void); extern void initepmapper(void); extern void initinitshutdown(void); -static void initdcerpc_misc(void) {} extern void initmgmt(void); +extern void initnet(void); extern void initatsvc(void); extern void initsamr(void); -static void initdcerpc_security(void) {} extern void initlsa(void); extern void initsvcctl(void); extern void initwkssvc(void); +extern void initunixinfo(void); extern void init_libcli_nbt(void); extern void init_libcli_smb(void); diff --git a/source4/scripting/python/pyrpc.h b/source4/scripting/python/pyrpc.h index 3a5d235cfc..93d583c10a 100644 --- a/source4/scripting/python/pyrpc.h +++ b/source4/scripting/python/pyrpc.h @@ -17,6 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#ifndef _PYRPC_H_ +#define _PYRPC_H_ + #define PY_CHECK_TYPE(type, var, fail) \ if (!type ## _Check(var)) {\ PyErr_Format(PyExc_TypeError, "Expected type %s", type ## _Type.tp_name); \ @@ -26,9 +29,11 @@ #define dom_sid2_Type dom_sid_Type #define dom_sid28_Type dom_sid_Type #define dom_sid2_Check dom_sid_Check -#define dom_sid28_Check dom_sid28_Check +#define dom_sid28_Check dom_sid_Check /* This macro is only provided by Python >= 2.3 */ #ifndef PyAPI_DATA # define PyAPI_DATA(RTYPE) extern RTYPE #endif + +#endif /* _PYRPC_H_ */ diff --git a/source4/scripting/python/pytalloc.c b/source4/scripting/python/pytalloc.c index d8d3efe69c..ca476e9604 100644 --- a/source4/scripting/python/pytalloc.c +++ b/source4/scripting/python/pytalloc.c @@ -24,6 +24,7 @@ void py_talloc_dealloc(PyObject* self) { py_talloc_Object *obj = (py_talloc_Object *)self; talloc_free(obj->talloc_ctx); + obj->talloc_ctx = NULL; PyObject_Del(self); } @@ -31,7 +32,13 @@ PyObject *py_talloc_import_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr) { py_talloc_Object *ret = PyObject_New(py_talloc_Object, py_type); - ret->talloc_ctx = talloc_reference(NULL, mem_ctx); + ret->talloc_ctx = talloc_new(NULL); + if (ret->talloc_ctx == NULL) { + return NULL; + } + if (talloc_reference(ret->talloc_ctx, mem_ctx) == NULL) { + return NULL; + } ret->ptr = ptr; return (PyObject *)ret; } @@ -39,7 +46,7 @@ PyObject *py_talloc_import_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, PyObject *py_talloc_default_repr(PyObject *py_obj) { py_talloc_Object *obj = (py_talloc_Object *)py_obj; + PyTypeObject *type = (PyTypeObject*)PyObject_Type((PyObject *)obj); - return PyString_FromFormat("<talloc: %s>", - talloc_get_name(obj->talloc_ctx)); + return PyString_FromFormat("<%s talloc object at 0x%x>", type->tp_name, (intptr_t)py_obj); } diff --git a/source4/scripting/python/samba/__init__.py b/source4/scripting/python/samba/__init__.py index b9d81c6c3c..94f9e4d005 100644 --- a/source4/scripting/python/samba/__init__.py +++ b/source4/scripting/python/samba/__init__.py @@ -20,6 +20,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +__docformat__ = "restructuredText" + import os def _in_source_tree(): @@ -33,6 +35,8 @@ if _in_source_tree(): srcdir = "%s/../../.." % os.path.dirname(__file__) sys.path.append("%s/bin/python" % srcdir) default_ldb_modules_dir = "%s/bin/modules/ldb" % srcdir +else: + default_ldb_modules_dir = None import ldb @@ -69,15 +73,15 @@ class Ldb(ldb.Ldb): self.set_modules_dir(default_ldb_modules_dir) if credentials is not None: - self.set_credentials(self, credentials) + self.set_credentials(credentials) if session_info is not None: - self.set_session_info(self, session_info) + self.set_session_info(session_info) assert misc.ldb_register_samba_handlers(self) == 0 if lp is not None: - self.set_loadparm(self, lp) + self.set_loadparm(lp) def msg(l,text): print text diff --git a/source4/scripting/python/samba/getopt.py b/source4/scripting/python/samba/getopt.py index 82cb004b62..9ecb66e21c 100644 --- a/source4/scripting/python/samba/getopt.py +++ b/source4/scripting/python/samba/getopt.py @@ -17,10 +17,15 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +"""Support for parsing Samba-related command-line options.""" + import optparse from credentials import Credentials, AUTO_USE_KERBEROS, DONT_USE_KERBEROS, MUST_USE_KERBEROS +__docformat__ = "restructuredText" + class SambaOptions(optparse.OptionGroup): + """General Samba-related command line options.""" def __init__(self, parser): optparse.OptionGroup.__init__(self, parser, "Samba Common Options") self.add_option("-s", "--configfile", action="callback", @@ -29,26 +34,33 @@ class SambaOptions(optparse.OptionGroup): self._configfile = None def get_loadparm_path(self): + """Return the path to the smb.conf file specified on the command line. """ return self._configfile def _load_configfile(self, option, opt_str, arg, parser): self._configfile = arg def get_loadparm(self): - import param + """Return a loadparm object with data specified on the command line. """ + import os, param lp = param.LoadParm() - if self._configfile is None: - lp.load_default() - else: + if self._configfile is not None: lp.load(self._configfile) + elif os.getenv("SMB_CONF_PATH") is not None: + lp.load(os.getenv("SMB_CONF_PATH")) + else: + lp.load_default() return lp + class VersionOptions(optparse.OptionGroup): + """Command line option for printing Samba version.""" def __init__(self, parser): optparse.OptionGroup.__init__(self, parser, "Version Options") class CredentialsOptions(optparse.OptionGroup): + """Command line options for specifying credentials.""" def __init__(self, parser): self.no_pass = False optparse.OptionGroup.__init__(self, parser, "Credentials Options") @@ -89,6 +101,11 @@ class CredentialsOptions(optparse.OptionGroup): self.creds.set_bind_dn(arg) def get_credentials(self, lp): + """Obtain the credentials set on the command-line. + + :param lp: Loadparm object to use. + :return: Credentials object + """ self.creds.guess(lp) if not self.no_pass: self.creds.set_cmdline_callbacks() diff --git a/source4/scripting/python/samba/idmap.py b/source4/scripting/python/samba/idmap.py index 355565968a..755ec52c7b 100644 --- a/source4/scripting/python/samba/idmap.py +++ b/source4/scripting/python/samba/idmap.py @@ -20,7 +20,10 @@ """Convenience functions for using the idmap database.""" +__docformat__ = "restructuredText" + import samba +import misc import ldb class IDmapDB(samba.Ldb): @@ -37,11 +40,17 @@ class IDmapDB(samba.Ldb): :param url: URL of the database. """ + self.lp = lp + super(IDmapDB, self).__init__(session_info=session_info, credentials=credentials, modules_dir=modules_dir, lp=lp) if url: self.connect(url) + else: + self.connect(lp.get("idmap database")) + def connect(self, url): + super(IDmapDB, self).connect(misc.private_path(self.lp, url)) def setup_name_mapping(self, sid, type, unixid): """Setup a mapping between a sam name and a unix name. diff --git a/source4/scripting/python/samba/provision.py b/source4/scripting/python/samba/provision.py index 4d5a9cb1f1..fe9b582d56 100644 --- a/source4/scripting/python/samba/provision.py +++ b/source4/scripting/python/samba/provision.py @@ -43,6 +43,8 @@ from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE, LdbError, \ """Functions for setting up a Samba configuration.""" +__docformat__ = "restructuredText" + DEFAULTSITE = "Default-First-Site-Name" class InvalidNetbiosName(Exception): @@ -87,6 +89,7 @@ class ProvisionNames: self.domain = None self.hostname = None self.sitename = None + self.smbconf = None class ProvisionResult: def __init__(self): @@ -239,6 +242,8 @@ def provision_paths_from_lp(lp, dnsdomain): paths.secrets = os.path.join(paths.private_dir, lp.get("secrets database") or "secrets.ldb") paths.templates = os.path.join(paths.private_dir, "templates.ldb") paths.dns = os.path.join(paths.private_dir, dnsdomain + ".zone") + paths.namedconf = os.path.join(paths.private_dir, "named.conf") + paths.krb5conf = os.path.join(paths.private_dir, "krb5.conf") paths.winsdb = os.path.join(paths.private_dir, "wins.ldb") paths.s4_ldapi_path = os.path.join(paths.private_dir, "ldapi") paths.phpldapadminconfig = os.path.join(paths.private_dir, @@ -266,12 +271,15 @@ def provision_paths_from_lp(lp, dnsdomain): paths.netlogon = lp.get("path", "netlogon") + paths.smbconf = lp.configfile() + return paths -def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None, - serverrole=None, rootdn=None, domaindn=None, configdn=None, - schemadn=None, sitename=None): +def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None, serverrole=None, + rootdn=None, domaindn=None, configdn=None, schemadn=None, serverdn=None, + sitename=None): + """Guess configuration settings to use.""" if hostname is None: hostname = socket.gethostname().split(".")[0].lower() @@ -292,8 +300,8 @@ def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None, realm = dnsdomain.upper() if lp.get("realm").upper() != realm: - raise Exception("realm '%s' must match chosen realm '%s'" % - (lp.get("realm"), realm)) + raise Exception("realm '%s' in %s must match chosen realm '%s'" % + (lp.get("realm"), lp.configfile(), realm)) dnsdomain = dnsdomain.lower() @@ -338,6 +346,7 @@ def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None, names.netbiosname = netbiosname names.hostname = hostname names.sitename = sitename + names.serverdn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (netbiosname, sitename, configdn) return names @@ -390,6 +399,7 @@ def make_smbconf(smbconf, setup_path, hostname, domain, realm, serverrole, }) + def setup_name_mappings(samdb, idmap, sid, domaindn, root_uid, nobody_uid, users_gid, wheel_gid): """setup reasonable name mappings for sam names to unix names. @@ -536,10 +546,7 @@ def setup_samdb_partitions(samdb_path, setup_path, message, lp, session_info, samdb.load_ldif_file_add(setup_path("provision_init.ldif")) message("Setting up sam.ldb rootDSE") - setup_samdb_rootdse(samdb, setup_path, names.schemadn, names.domaindn, - names.hostname, names.dnsdomain, names.realm, - names.rootdn, names.configdn, names.netbiosname, - names.sitename) + setup_samdb_rootdse(samdb, setup_path, names) if erase: message("Erasing data from partitions") @@ -652,25 +659,22 @@ def setup_idmapdb(path, setup_path, session_info, credentials, lp): return idmap_ldb -def setup_samdb_rootdse(samdb, setup_path, schemadn, domaindn, hostname, - dnsdomain, realm, rootdn, configdn, netbiosname, - sitename): +def setup_samdb_rootdse(samdb, setup_path, names): """Setup the SamDB rootdse. :param samdb: Sam Database handle :param setup_path: Obtain setup path """ setup_add_ldif(samdb, setup_path("provision_rootdse_add.ldif"), { - "SCHEMADN": schemadn, - "NETBIOSNAME": netbiosname, - "DNSDOMAIN": dnsdomain, - "DEFAULTSITE": sitename, - "REALM": realm, - "DNSNAME": "%s.%s" % (hostname, dnsdomain), - "DOMAINDN": domaindn, - "ROOTDN": rootdn, - "CONFIGDN": configdn, - "VERSION": samba.version(), + "SCHEMADN": names.schemadn, + "NETBIOSNAME": names.netbiosname, + "DNSDOMAIN": names.dnsdomain, + "REALM": names.realm, + "DNSNAME": "%s.%s" % (names.hostname, names.dnsdomain), + "DOMAINDN": names.domaindn, + "ROOTDN": names.rootdn, + "CONFIGDN": names.configdn, + "SERVERDN": names.serverdn, }) @@ -679,10 +683,12 @@ def setup_self_join(samdb, names, domainsid, invocationid, setup_path, policyguid): """Join a host to its own domain.""" + assert isinstance(invocationid, str) setup_add_ldif(samdb, setup_path("provision_self_join.ldif"), { "CONFIGDN": names.configdn, "SCHEMADN": names.schemadn, "DOMAINDN": names.domaindn, + "SERVERDN": names.serverdn, "INVOCATIONID": invocationid, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": names.sitename, @@ -730,8 +736,6 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, return samdb message("Pre-loading the Samba 4 and AD schema") - samdb = SamDB(path, session_info=session_info, - credentials=credentials, lp=lp) samdb.set_domain_sid(domainsid) if serverrole == "domain controller": samdb.set_invocation_id(invocationid) @@ -767,6 +771,7 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": names.sitename, "CONFIGDN": names.configdn, + "SERVERDN": names.serverdn, "POLICYGUID": policyguid, "DOMAINDN": names.domaindn, "DOMAINGUID_MOD": domainguid_mod, @@ -797,6 +802,7 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": names.sitename, "CONFIGDN": names.configdn, + "SERVERDN": names.serverdn }) message("Setting up sam.ldb Samba4 schema") @@ -815,6 +821,7 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, "DOMAIN": names.domain, "SCHEMADN": names.schemadn, "DOMAINDN": names.domaindn, + "SERVERDN": names.serverdn }) message("Setting up display specifiers") @@ -839,6 +846,7 @@ def setup_samdb(path, setup_path, session_info, credentials, lp, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": names.sitename, "CONFIGDN": names.configdn, + "SERVERDN": names.serverdn }) if fill == FILL_FULL: @@ -875,15 +883,15 @@ FILL_NT4SYNC = "NT4SYNC" FILL_DRS = "DRS" def provision(setup_dir, message, session_info, - credentials, smbconf=None, targetdir=None, samdb_fill=FILL_FULL, - realm=None, rootdn=None, domaindn=None, schemadn=None, - configdn=None, domain=None, hostname=None, hostip=None, - hostip6=None, domainsid=None, adminpass=None, krbtgtpass=None, - domainguid=None, policyguid=None, invocationid=None, - machinepass=None, dnspass=None, root=None, nobody=None, - nogroup=None, users=None, wheel=None, backup=None, aci=None, - serverrole=None, ldap_backend=None, ldap_backend_type=None, - sitename=None): + credentials, smbconf=None, targetdir=None, samdb_fill=FILL_FULL, realm=None, + rootdn=None, domaindn=None, schemadn=None, configdn=None, + serverdn=None, + domain=None, hostname=None, hostip=None, hostip6=None, + domainsid=None, adminpass=None, krbtgtpass=None, domainguid=None, + policyguid=None, invocationid=None, machinepass=None, + dnspass=None, root=None, nobody=None, nogroup=None, users=None, + wheel=None, backup=None, aci=None, serverrole=None, + ldap_backend=None, ldap_backend_type=None, sitename=None): """Provision samba4 :note: caution, this wipes all existing data! @@ -898,7 +906,7 @@ def provision(setup_dir, message, session_info, domainsid = security.Sid(domainsid) if policyguid is None: - policyguid = uuid.random() + policyguid = str(uuid.uuid4()) if adminpass is None: adminpass = misc.random_password(12) if krbtgtpass is None: @@ -930,9 +938,9 @@ def provision(setup_dir, message, session_info, lp.load(smbconf) names = guess_names(lp=lp, hostname=hostname, domain=domain, - dnsdomain=realm, serverrole=serverrole, - sitename=sitename, rootdn=rootdn, domaindn=domaindn, - configdn=configdn, schemadn=schemadn) + dnsdomain=realm, serverrole=serverrole, sitename=sitename, + rootdn=rootdn, domaindn=domaindn, configdn=configdn, schemadn=schemadn, + serverdn=serverdn) paths = provision_paths_from_lp(lp, names.dnsdomain) @@ -950,7 +958,7 @@ def provision(setup_dir, message, session_info, assert serverrole in ("domain controller", "member server", "standalone") if invocationid is None and serverrole == "domain controller": - invocationid = uuid.random() + invocationid = str(uuid.uuid4()) if not os.path.exists(paths.private_dir): os.mkdir(paths.private_dir) @@ -1000,20 +1008,25 @@ def provision(setup_dir, message, session_info, ldap_backend_type=ldap_backend_type) if lp.get("server role") == "domain controller": - policy_path = os.path.join(paths.sysvol, names.dnsdomain, "Policies", - "{" + policyguid + "}") - os.makedirs(policy_path, 0755) - os.makedirs(os.path.join(policy_path, "Machine"), 0755) - os.makedirs(os.path.join(policy_path, "User"), 0755) - if not os.path.isdir(paths.netlogon): + if paths.netlogon is None: + message("Existing smb.conf does not have a [netlogon] share, but you are configuring a DC.") + message("Please either remove %s or see the template at %s" % + ( paths.smbconf, setup_path("provision.smb.conf.dc"))) + assert(paths.netlogon is not None) + + if paths.sysvol is None: + message("Existing smb.conf does not have a [sysvol] share, but you are configuring a DC.") + message("Please either remove %s or see the template at %s" % + (paths.smbconf, setup_path("provision.smb.conf.dc"))) + assert(paths.sysvol is not None) + + policy_path = os.path.join(paths.sysvol, names.dnsdomain, "Policies", + "{" + policyguid + "}") + os.makedirs(policy_path, 0755) + os.makedirs(os.path.join(policy_path, "Machine"), 0755) + os.makedirs(os.path.join(policy_path, "User"), 0755) + if not os.path.isdir(paths.netlogon): os.makedirs(paths.netlogon, 0755) - secrets_ldb = Ldb(paths.secrets, session_info=session_info, - credentials=credentials, lp=lp) - secretsdb_become_dc(secrets_ldb, setup_path, domain=domain, realm=names.realm, - netbiosname=names.netbiosname, domainsid=domainsid, - keytab_path=paths.keytab, samdb_url=paths.samdb, - dns_keytab_path=paths.dns_keytab, dnspass=dnspass, - machinepass=machinepass, dnsdomain=names.dnsdomain) if samdb_fill == FILL_FULL: setup_name_mappings(samdb, idmap, str(domainsid), names.domaindn, @@ -1025,6 +1038,14 @@ def provision(setup_dir, message, session_info, # Only make a zone file on the first DC, it should be replicated with DNS replication if serverrole == "domain controller": + secrets_ldb = Ldb(paths.secrets, session_info=session_info, + credentials=credentials, lp=lp) + secretsdb_become_dc(secrets_ldb, setup_path, domain=domain, realm=names.realm, + netbiosname=names.netbiosname, domainsid=domainsid, + keytab_path=paths.keytab, samdb_url=paths.samdb, + dns_keytab_path=paths.dns_keytab, dnspass=dnspass, + machinepass=machinepass, dnsdomain=names.dnsdomain) + samdb = SamDB(paths.samdb, session_info=session_info, credentials=credentials, lp=lp) @@ -1034,21 +1055,30 @@ def provision(setup_dir, message, session_info, expression="(&(objectClass=computer)(cn=%s))" % names.hostname, scope=SCOPE_SUBTREE) assert isinstance(hostguid, str) - - create_zone_file(paths.dns, setup_path, samdb, - hostname=names.hostname, hostip=hostip, - hostip6=hostip6, dnsdomain=names.dnsdomain, - domaindn=names.domaindn, dnspass=dnspass, realm=names.realm, + + create_zone_file(paths.dns, setup_path, dnsdomain=names.dnsdomain, + domaindn=names.domaindn, hostip=hostip, + hostip6=hostip6, hostname=names.hostname, + dnspass=dnspass, realm=names.realm, domainguid=domainguid, hostguid=hostguid) message("Please install the zone located in %s into your DNS server" % paths.dns) - + + create_named_conf(paths.namedconf, setup_path, realm=names.realm, + dnsdomain=names.dnsdomain, private_dir=paths.private_dir, + keytab_name=paths.dns_keytab) + message("See %s for example configuration statements for secure GSS-TSIG updates" % paths.namedconf) + + create_krb5_conf(paths.krb5conf, setup_path, dnsdomain=names.dnsdomain, + hostname=names.hostname, realm=names.realm) + message("A Kerberos configuration suitable for Samba 4 has been generated at %s" % paths.krb5conf) + create_phpldapadmin_config(paths.phpldapadminconfig, setup_path, ldapi_url) message("Please install the phpLDAPadmin configuration located at %s into /etc/phpldapadmin/config.php" % paths.phpldapadminconfig) - message("Once the above files are installed, your server will be ready to use") - message("Server Type: %s" % serverrole) + message("Once the above files are installed, your Samba4 server will be ready to use") + message("Server Role: %s" % serverrole) message("Hostname: %s" % names.hostname) message("NetBIOS Domain: %s" % names.domain) message("DNS Domain: %s" % names.dnsdomain) @@ -1062,27 +1092,33 @@ def provision(setup_dir, message, session_info, result.samdb = samdb return result + def provision_become_dc(setup_dir=None, smbconf=None, targetdir=None, realm=None, rootdn=None, domaindn=None, schemadn=None, configdn=None, + serverdn=None, domain=None, hostname=None, domainsid=None, adminpass=None, krbtgtpass=None, domainguid=None, policyguid=None, invocationid=None, machinepass=None, dnspass=None, root=None, nobody=None, nogroup=None, users=None, wheel=None, backup=None, aci=None, serverrole=None, - ldap_backend=None, ldap_backend_type=None, sitename=DEFAULTSITE): + ldap_backend=None, ldap_backend_type=None, sitename=None): def message(text): """print a message if quiet is not set.""" print text - provision(setup_dir, message, system_session(), None, + return provision(setup_dir, message, system_session(), None, smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS, realm=realm, - rootdn=rootdn, domaindn=domaindn, schemadn=schemadn, configdn=configdn, + rootdn=rootdn, domaindn=domaindn, schemadn=schemadn, configdn=configdn, serverdn=serverdn, domain=domain, hostname=hostname, hostip="127.0.0.1", domainsid=domainsid, machinepass=machinepass, serverrole="domain controller", sitename=sitename); -def setup_db_config(setup_path, file, dbdir): +def setup_db_config(setup_path, dbdir): + """Setup a Berkeley database. + + :param setup_path: Setup path function. + :param dbdir: Database directory.""" if not os.path.isdir(os.path.join(dbdir, "bdb-logs")): os.makedirs(os.path.join(dbdir, "bdb-logs"), 0700); if not os.path.isdir(os.path.join(dbdir, "tmp")): @@ -1097,7 +1133,7 @@ def provision_backend(setup_dir=None, message=None, smbconf=None, targetdir=None, realm=None, rootdn=None, domaindn=None, schemadn=None, configdn=None, domain=None, hostname=None, adminpass=None, root=None, serverrole=None, - ldap_backend_type=None): + ldap_backend_type=None, ldap_backend_port=None): def setup_path(file): return os.path.join(setup_dir, file) @@ -1148,6 +1184,7 @@ def provision_backend(setup_dir=None, message=None, "NETBIOSNAME": names.netbiosname, "DEFAULTSITE": DEFAULTSITE, "CONFIGDN": names.configdn, + "SERVERDN": names.serverdn }) setup_add_ldif(schemadb, setup_path("schema_samba4.ldif"), @@ -1156,7 +1193,12 @@ def provision_backend(setup_dir=None, message=None, {"SCHEMADN": names.schemadn}) if ldap_backend_type == "fedora-ds": - setup_file(setup_path("fedora-ds.inf"), paths.fedoradsinf, + if ldap_backend_port is not None: + serverport = "ServerPort=%d" % ldap_backend_port + else: + serverport = "" + + setup_file(setup_path("fedorads.inf"), paths.fedoradsinf, {"ROOT": root, "HOSTNAME": hostname, "DNSDOMAIN": names.dnsdomain, @@ -1164,19 +1206,18 @@ def provision_backend(setup_dir=None, message=None, "DOMAINDN": names.domaindn, "LDAPMANAGERDN": names.ldapmanagerdn, "LDAPMANAGERPASS": adminpass, - "SERVERPORT": ""}) + "SERVERPORT": serverport}) - setup_file(setup_path("fedora-partitions.ldif"), paths.fedoradspartitions, + setup_file(setup_path("fedorads-partitions.ldif"), paths.fedoradspartitions, {"CONFIGDN": names.configdn, "SCHEMADN": names.schemadn, }) - setup_file(setup_path("fedora-partitions.ldif"), paths.fedoradspartitions, - {"CONFIGDN": names.configdn, - "SCHEMADN": names.schemadn, - }) mapping = "schema-map-fedora-ds-1.0" backend_schema = "99_ad.ldif" + + slapdcommand="Initailise Fedora DS with: setup-ds.pl --file=%s" % paths.fedoradsinf + elif ldap_backend_type == "openldap": attrs = ["linkID", "lDAPDisplayName"] res = schemadb.search(expression="(&(&(linkID=*)(!(linkID:1.2.840.113556.1.4.803:=1)))(objectclass=attributeSchema))", base=names.schemadn, scope=SCOPE_SUBTREE, attrs=attrs); @@ -1219,22 +1260,33 @@ refint_attributes""" + refint_attributes + "\n"; setup_file(setup_path("modules.conf"), paths.modulesconf, {"REALM": names.realm}) - setup_db_config(setup_path, file, os.path.join(paths.ldapdir, "db", "user")) - setup_db_config(setup_path, file, os.path.join(paths.ldapdir, "db", "config")) - setup_db_config(setup_path, file, os.path.join(paths.ldapdir, "db", "schema")) - mapping = "schema-map-openldap-2.3" - backend_schema = "backend-schema.schema" - - - ldapi_uri = "ldapi://" + urllib.quote(os.path.join(paths.private_dir, "ldap", "ldapi"), safe="") - message("Start slapd with: slapd -f " + paths.ldapdir + "/slapd.conf -h " + ldapi_uri) - + setup_db_config(setup_path, os.path.join(paths.ldapdir, os.path.join("db", "user"))) + setup_db_config(setup_path, os.path.join(paths.ldapdir, os.path.join("db", "config"))) + setup_db_config(setup_path, os.path.join(paths.ldapdir, os.path.join("db", "schema"))) + mapping = "schema-map-openldap-2.3" + backend_schema = "backend-schema.schema" + + ldapi_uri = "ldapi://" + urllib.quote(os.path.join(paths.private_dir, "ldap", "ldapi"), safe="") + if ldap_backend_port is not None: + server_port_string = " -h ldap://0.0.0.0:%d" % ldap_backend_port + else: + server_port_string = "" + slapdcommand="Start slapd with: slapd -f " + paths.ldapdir + "/slapd.conf -h " + ldapi_uri + server_port_string schema_command = "bin/ad2oLschema --option=convert:target=" + ldap_backend_type + " -I " + setup_path(mapping) + " -H tdb://" + schemadb_path + " -O " + os.path.join(paths.ldapdir, backend_schema); os.system(schema_command) + message("Your %s Backend for Samba4 is now configured, and is ready to be started" % ( ldap_backend_type) ) + message("Server Role: %s" % serverrole) + message("Hostname: %s" % names.hostname) + message("DNS Domain: %s" % names.dnsdomain) + message("Base DN: %s" % names.domaindn) + message("LDAP admin DN: %s" % names.ldapmanagerdn) + message("LDAP admin password: %s" % adminpass) + message(slapdcommand) + def create_phpldapadmin_config(path, setup_path, ldapi_uri): """Create a PHP LDAP admin configuration file. @@ -1246,13 +1298,12 @@ def create_phpldapadmin_config(path, setup_path, ldapi_uri): {"S4_LDAPI_URI": ldapi_uri}) -def create_zone_file(path, setup_path, samdb, dnsdomain, domaindn, - hostip, hostip6, hostname, dnspass, realm, domainguid, hostguid): +def create_zone_file(path, setup_path, dnsdomain, domaindn, + hostip, hostip6, hostname, dnspass, realm, domainguid, hostguid): """Write out a DNS zone file, from the info in the current database. - - :param path: Path of the new file. - :param setup_path": Setup path function. - :param samdb: SamDB object + + :param path: Path of the new zone file. + :param setup_path: Setup path function. :param dnsdomain: DNS Domain name :param domaindn: DN of the Domain :param hostip: Local IPv4 IP @@ -1286,6 +1337,44 @@ def create_zone_file(path, setup_path, samdb, dnsdomain, domaindn, "HOSTIP6_HOST_LINE": hostip6_host_line, }) +def create_named_conf(path, setup_path, realm, dnsdomain, + private_dir, keytab_name): + """Write out a file containing zone statements suitable for inclusion in a + named.conf file (including GSS-TSIG configuration). + + :param path: Path of the new named.conf file. + :param setup_path: Setup path function. + :param realm: Realm name + :param dnsdomain: DNS Domain name + :param private_dir: Path to private directory + :param keytab_name: File name of DNS keytab file + """ + + setup_file(setup_path("named.conf"), path, { + "DNSDOMAIN": dnsdomain, + "REALM": realm, + "REALM_WC": "*." + ".".join(realm.split(".")[1:]), + "DNS_KEYTAB": keytab_name, + "DNS_KEYTAB_ABS": os.path.join(private_dir, keytab_name), + }) + +def create_krb5_conf(path, setup_path, dnsdomain, hostname, realm): + """Write out a file containing zone statements suitable for inclusion in a + named.conf file (including GSS-TSIG configuration). + + :param path: Path of the new named.conf file. + :param setup_path: Setup path function. + :param dnsdomain: DNS Domain name + :param hostname: Local hostname + :param realm: Realm name + """ + + setup_file(setup_path("krb5.conf"), path, { + "DNSDOMAIN": dnsdomain, + "HOSTNAME": hostname, + "REALM": realm, + }) + def load_schema(setup_path, samdb, schemadn, netbiosname, configdn, sitename): """Load schema for the SamDB. diff --git a/source4/scripting/python/samba/samba3.py b/source4/scripting/python/samba/samba3.py index cffedb54af..c1340b7760 100644 --- a/source4/scripting/python/samba/samba3.py +++ b/source4/scripting/python/samba/samba3.py @@ -19,6 +19,8 @@ """Support for reading Samba 3 data files.""" +__docformat__ = "restructuredText" + REGISTRY_VALUE_PREFIX = "SAMBA_REGVAL" REGISTRY_DB_VERSION = 1 @@ -307,6 +309,7 @@ class ShareInfoDatabase(TdbDatabase): class Shares: + """Container for share objects.""" def __init__(self, lp, shareinfo): self.lp = lp self.shareinfo = shareinfo @@ -492,6 +495,7 @@ class TdbSam(TdbDatabase): assert self.version in (0, 1, 2) def usernames(self): + """Iterate over the usernames in this Tdb database.""" for k in self.tdb.keys(): if k.startswith(TDBSAM_USER_PREFIX): yield k[len(TDBSAM_USER_PREFIX):].rstrip("\0") @@ -633,6 +637,7 @@ class WinsDatabase: return iter(self.entries) def items(self): + """Return the entries in this WINS database.""" return self.entries.items() def close(self): # for consistency diff --git a/source4/scripting/python/samba/samdb.py b/source4/scripting/python/samba/samdb.py index bc3eef7879..6465f49519 100644 --- a/source4/scripting/python/samba/samdb.py +++ b/source4/scripting/python/samba/samdb.py @@ -25,20 +25,31 @@ import samba import misc import ldb +from samba.idmap import IDmapDB +import pwd + +__docformat__ = "restructuredText" class SamDB(samba.Ldb): """The SAM database.""" + def __init__(self, url=None, session_info=None, credentials=None, modules_dir=None, lp=None): """Open the Sam Database. :param url: URL of the database. """ + self.lp = lp super(SamDB, self).__init__(session_info=session_info, credentials=credentials, modules_dir=modules_dir, lp=lp) assert misc.dsdb_set_global_schema(self) == 0 if url: self.connect(url) + else: + self.connect(lp.get("sam database")) + + def connect(self, url): + super(SamDB, self).connect(misc.private_path(self.lp, url)) def add_foreign(self, domaindn, sid, desc): """Add a foreign security principle.""" @@ -101,10 +112,27 @@ userAccountControl: %u # now the real work self.add({"dn": user_dn, "sAMAccountName": username, - "unixName": unixname, "sambaPassword": password, "objectClass": "user"}) + res = self.search(user_dn, scope=ldb.SCOPE_BASE, + expression="objectclass=*", + attrs=["objectSid"]) + assert(len(res) == 1) + user_sid = self.schema_format_value("objectSid", res[0]["objectSid"][0]) + + + try: + idmap = IDmapDB(lp=self.lp) + + user = pwd.getpwnam(unixname) + # setup ID mapping for this UID + + idmap.setup_name_mapping(user_sid, idmap.TYPE_UID, user[2]) + + except KeyError: + pass + # modify the userAccountControl to remove the disabled bit self.enable_account(user_dn) self.transaction_commit() diff --git a/source4/scripting/python/samba/tests/__init__.py b/source4/scripting/python/samba/tests/__init__.py index c8673d3fae..e29b4a87d5 100644 --- a/source4/scripting/python/samba/tests/__init__.py +++ b/source4/scripting/python/samba/tests/__init__.py @@ -85,8 +85,13 @@ class LdbExtensionTests(TestCaseInTempDir): os.unlink(path) -def get_loadparm(): - import param - lp = param.LoadParm() - lp.load(os.getenv("SMB_CONF_PATH")) - return lp +cmdline_loadparm = None +cmdline_credentials = None + +class RpcInterfaceTestCase(unittest.TestCase): + def get_loadparm(self): + assert cmdline_loadparm is not None + return cmdline_loadparm + + def get_credentials(self): + return cmdline_credentials diff --git a/source4/scripting/python/samba/tests/dcerpc/registry.py b/source4/scripting/python/samba/tests/dcerpc/registry.py index 147acc5098..81133ff641 100644 --- a/source4/scripting/python/samba/tests/dcerpc/registry.py +++ b/source4/scripting/python/samba/tests/dcerpc/registry.py @@ -17,15 +17,14 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -import winreg -from param import LoadParm +from samba.dcerpc import winreg import unittest -from samba.tests import get_loadparm +from samba.tests import RpcInterfaceTestCase -class WinregTests(unittest.TestCase): +class WinregTests(RpcInterfaceTestCase): def setUp(self): - lp_ctx = get_loadparm() - self.conn = winreg.winreg("ncalrpc:", lp_ctx) + self.conn = winreg.winreg("ncalrpc:", self.get_loadparm(), + self.get_credentials()) def get_hklm(self): return self.conn.OpenHKLM(None, diff --git a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py index 8c1a8bec71..3b37f8a9bc 100644 --- a/source4/scripting/python/samba/tests/dcerpc/rpcecho.py +++ b/source4/scripting/python/samba/tests/dcerpc/rpcecho.py @@ -17,20 +17,19 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -import echo +from samba.dcerpc import echo import unittest -from samba.tests import get_loadparm +from samba.tests import RpcInterfaceTestCase -class RpcEchoTests(unittest.TestCase): +class RpcEchoTests(RpcInterfaceTestCase): def setUp(self): - lp_ctx = get_loadparm() - self.conn = echo.rpcecho("ncalrpc:", lp_ctx) + self.conn = echo.rpcecho("ncalrpc:", self.get_loadparm()) def test_addone(self): self.assertEquals(2, self.conn.AddOne(1)) def test_echodata(self): - self.assertEquals([1,2,3], self.conn.EchoData(3, [1, 2, 3])) + self.assertEquals([1,2,3], self.conn.EchoData([1, 2, 3])) def test_call(self): self.assertEquals(u"foobar", self.conn.TestCall(u"foobar")) diff --git a/source4/scripting/python/samba/tests/dcerpc/sam.py b/source4/scripting/python/samba/tests/dcerpc/sam.py index 96348f2f69..50e00a3f9e 100644 --- a/source4/scripting/python/samba/tests/dcerpc/sam.py +++ b/source4/scripting/python/samba/tests/dcerpc/sam.py @@ -1,7 +1,8 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Unix SMB/CIFS implementation. -# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008 +# Copyright © Jelmer Vernooij <jelmer@samba.org> 2008 # # 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 @@ -17,13 +18,29 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -import samr -import unittest -from samba.tests import get_loadparm +from samba.dcerpc import samr, security +from samba.tests import RpcInterfaceTestCase -class SamrTests(unittest.TestCase): +# FIXME: Pidl should be doing this for us +def toArray((handle, array, num_entries)): + ret = [] + for x in range(num_entries): + ret.append((array.entries[x].idx, array.entries[x].name)) + return ret + + +class SamrTests(RpcInterfaceTestCase): def setUp(self): - self.conn = samr.samr("ncalrpc:", get_loadparm()) + self.conn = samr.samr("ncalrpc:", self.get_loadparm()) def test_connect5(self): (level, info, handle) = self.conn.Connect5(None, 0, 1, samr.ConnectInfo1()) + + def test_connect2(self): + handle = self.conn.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED) + + def test_EnumDomains(self): + handle = self.conn.Connect2(None, security.SEC_FLAG_MAXIMUM_ALLOWED) + domains = toArray(self.conn.EnumDomains(handle, 0, -1)) + self.conn.Close(handle) + diff --git a/source4/scripting/python/samba/tests/dcerpc/unix.py b/source4/scripting/python/samba/tests/dcerpc/unix.py new file mode 100644 index 0000000000..aa47b71b16 --- /dev/null +++ b/source4/scripting/python/samba/tests/dcerpc/unix.py @@ -0,0 +1,37 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008 +# +# 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 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# 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, see <http://www.gnu.org/licenses/>. +# + +from samba.dcerpc import unixinfo +from samba.tests import RpcInterfaceTestCase + +class UnixinfoTests(RpcInterfaceTestCase): + def setUp(self): + self.conn = unixinfo.unixinfo("ncalrpc:", self.get_loadparm()) + + def test_getpwuid(self): + infos = self.conn.GetPWUid(range(512)) + self.assertEquals(512, len(infos)) + self.assertEquals("/bin/false", infos[0].shell) + self.assertTrue(isinstance(infos[0].homedir, unicode)) + + def test_gidtosid(self): + self.conn.GidToSid(1000) + + def test_uidtosid(self): + self.conn.UidToSid(1000) diff --git a/source4/scripting/python/samba/tests/provision.py b/source4/scripting/python/samba/tests/provision.py index 514582cbe4..76c10145f0 100644 --- a/source4/scripting/python/samba/tests/provision.py +++ b/source4/scripting/python/samba/tests/provision.py @@ -21,10 +21,10 @@ import os from samba.provision import setup_secretsdb, secretsdb_become_dc, findnss import samba.tests from ldb import Dn -import param +from samba import param import unittest -lp = samba.tests.get_loadparm() +lp = samba.tests.cmdline_loadparm setup_dir = "setup" def setup_path(file): diff --git a/source4/scripting/python/samba/tests/samdb.py b/source4/scripting/python/samba/tests/samdb.py index 3745dba6fc..0d4f7bde0e 100644 --- a/source4/scripting/python/samba/tests/samdb.py +++ b/source4/scripting/python/samba/tests/samdb.py @@ -21,7 +21,7 @@ from credentials import Credentials import os from samba.provision import setup_samdb from samba.samdb import SamDB -from samba.tests import get_loadparm, TestCaseInTempDir +from samba.tests import cmdline_loadparm, TestCaseInTempDir import security from unittest import TestCase import uuid @@ -29,21 +29,21 @@ import uuid class SamDBTestCase(TestCaseInTempDir): def setUp(self): super(SamDBTestCase, self).setUp() - invocationid = uuid.random() + invocationid = str(uuid.uuid4()) domaindn = "DC=COM,DC=EXAMPLE" self.domaindn = domaindn configdn = "CN=Configuration," + domaindn schemadn = "CN=Schema," + configdn - domainguid = uuid.random() - policyguid = uuid.random() + domainguid = str(uuid.uuid4()) + policyguid = str(uuid.uuid4()) setup_path = lambda x: os.path.join("setup", x) creds = Credentials() creds.set_anonymous() domainsid = security.random_sid() - hostguid = uuid.random() + hostguid = str(uuid.uuid4()) path = os.path.join(self.tempdir, "samdb.ldb") self.samdb = setup_samdb(path, setup_path, system_session(), creds, - get_loadparm(), schemadn, configdn, + cmdline_loadparm, schemadn, configdn, self.domaindn, "example.com", "EXAMPLE.COM", "FOO", lambda x: None, "foo", domaindn, False, domainsid, "# no aci", domainguid, diff --git a/source4/scripting/python/samba/upgrade.py b/source4/scripting/python/samba/upgrade.py index f40f2cffe7..0c83604e82 100644 --- a/source4/scripting/python/samba/upgrade.py +++ b/source4/scripting/python/samba/upgrade.py @@ -7,6 +7,8 @@ """Support code for upgrading from Samba 3 to Samba 4.""" +__docformat__ = "restructuredText" + from provision import findnss, provision, FILL_DRS import grp import ldb diff --git a/source4/scripting/python/smbpython.c b/source4/scripting/python/smbpython.c deleted file mode 100644 index c5de53fd60..0000000000 --- a/source4/scripting/python/smbpython.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Samba utility functions - Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007 - - 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 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - 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, see <http://www.gnu.org/licenses/>. -*/ - -#include "includes.h" -#include <Python.h> -#include "scripting/python/modules.h" - -int main(int argc, char **argv) -{ - py_load_samba_modules(); - Py_Initialize(); - if (strchr(argv[0], '/') != NULL) { - char *bindir = strndup(argv[0], strrchr(argv[0], '/')-argv[0]); - py_update_path(bindir); - free(bindir); - } - return Py_Main(argc,argv); -} diff --git a/source4/scripting/python/uuidmodule.c b/source4/scripting/python/uuidmodule.c index cd9a1cb4d5..98ef9adaa9 100644 --- a/source4/scripting/python/uuidmodule.c +++ b/source4/scripting/python/uuidmodule.c @@ -27,7 +27,7 @@ static PyObject *uuid_random(PyObject *self, PyObject *args) PyObject *pyobj; char *str; - if (!PyArg_ParseTuple(args, (char *)"")) + if (!PyArg_ParseTuple(args, "")) return NULL; guid = GUID_random(); @@ -46,13 +46,13 @@ static PyObject *uuid_random(PyObject *self, PyObject *args) } static PyMethodDef methods[] = { - { "random", (PyCFunction)uuid_random, METH_VARARGS, NULL}, + { "uuid4", (PyCFunction)uuid_random, METH_VARARGS, NULL}, { NULL, NULL } }; void inituuid(void) { - PyObject *mod = Py_InitModule3((char *)"uuid", methods, "UUID helper routines"); + PyObject *mod = Py_InitModule3("uuid", methods, "UUID helper routines"); if (mod == NULL) return; } |