-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldapAddGroup
executable file
·110 lines (92 loc) · 3.66 KB
/
ldapAddGroup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /usr/bin/python
import sys
import ldap
from ldapConf import *
from optparse import OptionParser
from ldapConf import *
from ldapUtil import *
####################################################################################
# global constants
####################################################################################
version="%prog: v0.82 (2011-Nov-27)"
modifiedBy="Garry Thuna"
###################################################################################
# parse command line options
###################################################################################
usage = "usage: %prog [options] [hostUri] [bindDN] [bindPW]"
description = "Create a user in " + BASE_DN
parser = OptionParser(usage=usage, version=version, description=description)
parser.add_option("-H", action="store", type="string", dest="hostUri",
help="LDAP Uniform Resource Identifier (eg. ldaps://ldapServer:port)")
parser.add_option("-D", action="store", type="string", dest="bindDN", help="bind DN")
parser.add_option("-w", action="store", type="string", dest="bindPass", help="bind password")
(options, args) = parser.parse_args()
####################################################################################
# gather the user input
####################################################################################
if options.hostUri:
hostUri = options.hostUri
elif len(args) >= 1:
hostUri = args[0]
else:
hostUri = "ldaps://example.com:636"
input = raw_input('host URI [{0}]: '.format(hostUri)).strip()
if len(input) != 0:
hostUri = input
if options.bindDN:
bindDN = options.bindDN
elif len(args) >= 2:
bindDN = args[1]
else:
bindDN = ""
input = raw_input('bind DN [{0}]: '.format(bindDN)).strip()
if len(input) != 0:
bindDN = input
if options.bindPass:
bindPass = options.bindPass
elif len(args) >= 3:
bindPass = args[2]
else:
bindPass = raw_input('bind password: ')
gid = raw_input('group ID: ')
displayName = raw_input('display name: ')
description = raw_input('description: ')
####################################################################################
# bind to the ldap server
# do the preliminary procssing of users and groups
# do the preliminary procssing of workspaces
####################################################################################
con = ldap.initialize(hostUri)
con.start_tls_s()
con.simple_bind_s(bindDN, bindPass)
groups, \
users, \
gnum2idx, \
gid2idx, \
uid2idx, \
belongsTo, \
workspaces, \
awsName2ws, \
awsName2path, \
gnum2awsName, \
servers, \
asName2server = preProcessLdapObjects(con)
# calc the maximum gidNumber
maxGidNumber = 0
for g in groups:
cur = int(g[1]['gidNumber'][0])
if cur > maxGidNumber:
maxGidNumber = cur
gidNumber = maxGidNumber + 1
# setup the entry's attributes
dn = GROUP_DN_FMT.format(gid)
ur = list()
ur.append( ('objectClass', ['sambaGroupMapping', 'posixGroup']) )
ur.append( ('cn', [gid]) )
ur.append( ('gidNumber', ['{0}'.format(gidNumber)]) )
ur.append( ('sambaGroupType', ['2']) )
ur.append( ('sambaSID', ['S-1-5-21-12345-1-{0}'.format(gidNumber * 2 + 1)]) )
ur.append( ('displayName', [displayName]) )
ur.append( ('description', [description]) )
con.add_s(dn, ur)
con.unbind_s()