This repository was archived by the owner on Oct 4, 2020. It is now read-only.
forked from stedolan/nd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmds.py
More file actions
181 lines (149 loc) · 4.43 KB
/
Copy pathcmds.py
File metadata and controls
181 lines (149 loc) · 4.43 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Command line interface to Netsoc Directory
import os
import sys
from nd import *
objtypes = {'user':User, 'group':Group }
def help(*args):
'''Information about commands'''
def shorthelp(f):
return f.__doc__.split("\n")[0]
def longhelp(f):
return f.__doc__
if args:
cmd = getfunc(args[0])
if not cmd:
err("Unknown command %s" % args[0])
else:
print longhelp(cmd)
else:
print "Available commands:"
for i in cmdfuncs:
print i.__name__ + ": " + shorthelp(i)
print "\nUse help <command> for information about a specific command"
def myself(*args):
'''Viewing or modifying your own user account
The myself command is equivalent to "user <current user>", see help user for details'''
return user(str(os.getuid()), *args)
def search(*args):
'''Search for particular users, groups, etc.'''
try:
type = objtypes[args[0]]
except:
err()
args = args[1:]
if len(args) == 1:
filter = SearchFilter.any(uid=args[0], cn=args[0])
else:
if len(args) % 2 != 0:
err("search called with an odd number of parameters")
#FIXME: validate special chars??
keys = args[::2]
values = args[1::2]
filters = []
for i in range(len(keys)):
if keys[i] == "ldap":
filters.append(SearchFilter.from_raw_filter(values[i]))
else:
filters.append(SearchFilter.attr_match(keys[i], values[i]))
filter = SearchFilter.all(*filters)
for i in type.search(filter):
print i
def searchuser(*args):
'''Searching for users, groups or hosts
Usage:
searchuser <type> <property> <value> [<property> <value>...]: Search by any property
<type> is user, group or host
If multiple properties are specified, all are matched.
The property "memberOf" specifies that the user must be in the specified group.
The property "ldap" specifies an arbitrary LDAP search filter
Examples:
search user cn stephen memberOf council
- Finds users whose full names contain "Stephen" and are in the "council" group
search user tcd-ISS-username sdolan
- Finds the user whose College username is "sdolan"
search group member mu
- Finds groups containing user "mu"
'''
try:
type = objtypes[args[0]]
except:
err("Invalid type (try 'user', 'group' or 'host')")
def query(objspec, type, args):
if len(args) == 0:
obj = type(objspec)
print obj.info()
else:
cmd = args[0]
if cmd == 'get':
obj = type(objspec)
if len(args) > 1:
for attr in args[1:]:
print obj.get_attribute(attr)
else:
for attr, value in obj.get_all_attribute_pairs():
print attr + ": " + str(value)
elif cmd == 'set':
obj = type(objspec)
obj.set_attribute(args[1], args[2])
else:
err("Unknown command %s" % cmd)
def user(*args): #FIXME: group modification ops, user creation/deletion, whitespace in vals
'''Creating, deleting, viewing and modifying user accounts
Usage:
user <uid> get: Prints out all information about a user
user <uid> get <property>: Gets a property (full name, email address, etc.) of a user
user <uid> set <property> <value>: Sets a property
user <uid> set <property>: Sets a property from stdin.
e.g. user someone set jpegPhoto < somePhoto.jpg
You might not have permission to perform some of these operations.
<uid> above is a username, a UID number, or an LDAP DN for a user.
<property> is one of the available user properties, interesting ones include:
cn: name
jpegPhoto: a photograph
tcdnetsoc-ISS-username: College username
mail: email address
and various others
To list all the user accounts, see "group list"
'''
if not args:
err()
uid = args[0]
query(uid, User, args[1:])
def group(*args):
'''Creating, deleting, viewing and modifying groups
Usage:
group <gid> list: Lists the users in a given group'''
pass
def host(*args):
'''Creating, deleting, viewing and modifying machine accounts'''
pass
cmdfuncs = [myself, user, group, host, help, search]
def getfunc(name):
for f in cmdfuncs:
if f.__name__ == name:
return f
return None
def run_command(args):
if not args:
help()
else:
# ldap_connect('cn=root,dc=netsoc,dc=tcd,dc=ie','foo')
func = getfunc(args.pop(0))
if not func:
help()
else:
# try:
func(*args)
# except Exception, e:
# help(func.__name__)
# raise e
def err_on(cond):
if cond:
err()
def err(message="Invalid command syntax"):
sys.stderr.write("Error: %s\n" % message)
raise Exception(message)
def warn(message):
sys.stderr.write("Warning: %s\n" % message)
if __name__=='__main__':
run_command(sys.argv[1:])