| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Copyright (C) Matthieu Patou <[email protected]> 2010 |
| 4 |
# |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
__docformat__ = "restructuredText" |
| 20 |
|
| 21 |
|
| 22 |
import optparse |
| 23 |
import sys |
| 24 |
# Allow to run from s4 source directory (without installing samba) |
| 25 |
sys.path.insert(0, "bin/python") |
| 26 |
|
| 27 |
import samba.getopt as options |
| 28 |
from samba.credentials import DONT_USE_KERBEROS |
| 29 |
from samba.auth import system_session |
| 30 |
from samba import param |
| 31 |
from samba.provision import find_provision_key_parameters |
| 32 |
from samba.upgradehelpers import (get_paths, get_ldbs) |
| 33 |
from ldb import SCOPE_BASE, Message, MessageElement, Dn, FLAG_MOD_ADD |
| 34 |
|
| 35 |
parser = optparse.OptionParser("addlotscontacts [options]") |
| 36 |
sambaopts = options.SambaOptions(parser) |
| 37 |
parser.add_option_group(sambaopts) |
| 38 |
parser.add_option_group(options.VersionOptions(parser)) |
| 39 |
credopts = options.CredentialsOptions(parser) |
| 40 |
parser.add_option_group(credopts) |
| 41 |
|
| 42 |
(opts, args) = parser.parse_args() |
| 43 |
|
| 44 |
lp = sambaopts.get_loadparm() |
| 45 |
smbconf = lp.configfile |
| 46 |
creds = credopts.get_credentials(lp) |
| 47 |
creds.set_kerberos_state(DONT_USE_KERBEROS) |
| 48 |
|
| 49 |
if len(args) > 0: |
| 50 |
num_contacts = int(args[0]) |
| 51 |
else: |
| 52 |
num_contacts = 10000 |
| 53 |
|
| 54 |
if __name__ == '__main__': |
| 55 |
paths = get_paths(param, smbconf=smbconf) |
| 56 |
session = system_session() |
| 57 |
|
| 58 |
ldbs = get_ldbs(paths, creds, session, lp) |
| 59 |
ldbs.startTransactions() |
| 60 |
|
| 61 |
names = find_provision_key_parameters(ldbs.sam, ldbs.secrets, ldbs.idmap, |
| 62 |
paths, smbconf, lp) |
| 63 |
|
| 64 |
contactdn = "OU=Contacts,%s" % str(names.domaindn) |
| 65 |
res = ldbs.sam.search(expression="(distinguishedName=%s)" % contactdn, |
| 66 |
base=str(names.domaindn), |
| 67 |
scope=SCOPE_BASE) |
| 68 |
|
| 69 |
if (len(res) == 0): |
| 70 |
msg = Message() |
| 71 |
msg.dn = Dn(ldbs.sam, contactdn) |
| 72 |
msg["objectClass"] = MessageElement("organizationalUnit", FLAG_MOD_ADD, |
| 73 |
"objectClass") |
| 74 |
|
| 75 |
ldbs.sam.add(msg) |
| 76 |
|
| 77 |
print("Creating %d contacts" % num_contacts) |
| 78 |
count = 0 |
| 79 |
increment = num_contacts / 10 |
| 80 |
if increment > 5000: |
| 81 |
increment = 5000 |
| 82 |
|
| 83 |
while (count < num_contacts): |
| 84 |
msg = Message() |
| 85 |
msg.dn = Dn(ldbs.sam, "CN=contact%d,%s" % (count + 1, contactdn)) |
| 86 |
msg["objectClass"] = MessageElement("contact", FLAG_MOD_ADD, |
| 87 |
"objectClass") |
| 88 |
|
| 89 |
if count !=0 and (count % increment) == 0: |
| 90 |
print("Added contacts: %d" % count) |
| 91 |
|
| 92 |
ldbs.sam.add(msg) |
| 93 |
count += 1 |
| 94 |
|
| 95 |
ldbs.groupedCommit() |