3 cdef import from "afs/ptuser.h":
9 ctypedef char prname[PR_MAXNAMELEN]
12 unsigned int namelist_len
16 unsigned int prlist_len
20 unsigned int idlist_len
31 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
32 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
33 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
34 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
35 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
36 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
37 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
38 int ubik_PR_ListElements(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
39 int ubik_PR_ListOwned(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
40 int ubik_PR_ListEntry(ubik_client *, afs_int32, afs_int32, prcheckentry *)
41 int ubik_PR_ChangeEntry(ubik_client *, afs_int32, afs_int32, char *, afs_int32, afs_int32)
42 int ubik_PR_IsAMemberOf(ubik_client *, afs_int32, afs_int32, afs_int32, afs_int32 *)
43 int ubik_PR_ListMax(ubik_client *, afs_int32, afs_int32 *, afs_int32 *)
45 cdef import from "afs/pterror.h":
50 void initialize_PT_error_table()
53 cdef public afs_int32 flags
54 cdef public afs_int32 id
55 cdef public afs_int32 owner
56 cdef public afs_int32 creator
57 cdef public afs_int32 ngroups
58 cdef public afs_int32 count
60 cdef int _ptentry_from_c(PTEntry p_entry, prcheckentry c_entry) except -1:
65 p_entry.flags = c_entry.flags
66 p_entry.id = c_entry.id
67 p_entry.owner = c_entry.owner
68 p_entry.creator = c_entry.creator
69 p_entry.ngroups = c_entry.ngroups
70 p_entry.count = c_entry.count
73 cdef int _ptentry_to_c(prcheckentry * c_entry, PTEntry p_entry) except -1:
78 c_entry.flags = p_entry.flags
79 c_entry.id = p_entry.id
80 c_entry.owner = p_entry.owner
81 c_entry.creator = p_entry.creator
82 c_entry.ngroups = p_entry.ngroups
83 c_entry.count = p_entry.count
88 A PTS object is essentially a handle to talk to the server in a
91 cell defaults to None. If no argument is passed for cell, PTS
92 connects to the home cell.
94 sec is the security level, an integer from 0 to 3:
95 - 0: unauthenticated connection
96 - 1: try authenticated, then fall back to unauthenticated
97 - 2: fail if an authenticated connection can't be established
98 - 3: same as 2, plus encrypt all traffic to the protection
101 cdef ubik_client * client
103 def __cinit__(self, cell=None, sec=1):
105 cdef afsconf_dir *cdir
106 cdef afsconf_cell info
108 cdef ktc_principal prin
110 cdef rx_securityClass *sc
111 cdef rx_connection *serverconns[MAXSERVERS]
114 initialize_PT_error_table()
125 raise Exception(code, "Error initializing Rx")
127 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
130 "Error opening configuration directory (%s): %s" % \
131 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
132 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
134 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
137 strncpy(prin.cell, info.name, sizeof(prin.cell))
139 strncpy(prin.name, "afs", sizeof(prin.name))
141 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
144 # No really - we wanted authentication
145 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
152 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
153 token.kvno, token.ticketLen,
157 sc = rxnull_NewClientSecurityObject()
161 memset(serverconns, 0, sizeof(serverconns))
162 for 0 <= i < info.numServers:
163 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
164 info.hostAddr[i].sin_port,
169 code = ubik_ClientInit(serverconns, &self.client)
171 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
173 code = rxs_Release(sc)
175 def __dealloc__(self):
176 ubik_ClientDestroy(self.client)
179 def NameToId(self, name):
181 Converts a user or group to an AFS ID.
185 cdef afs_int32 code, id
189 lids.idlist_val = NULL
190 lnames.namelist_len = 1
191 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
192 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
193 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
194 if lids.idlist_val is not NULL:
195 id = lids.idlist_val[0]
196 free(lids.idlist_val)
197 if id == ANONYMOUSID:
200 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
203 def IdToName(self, id):
205 Convert an AFS ID to the name of a user or group.
210 cdef char name[PR_MAXNAMELEN]
213 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
214 lids.idlist_val[0] = id
215 lnames.namelist_len = 0
216 lnames.namelist_val = NULL
217 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
218 if lnames.namelist_val is not NULL:
219 strncpy(name, lnames.namelist_val[0], sizeof(name))
220 free(lnames.namelist_val)
221 if lids.idlist_val is not NULL:
222 free(lids.idlist_val)
226 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
229 def CreateUser(self, name, id=None):
231 Create a new user in the protection database. If an ID is
232 provided, that one will be used.
236 name = name[:PR_MAXNAMELEN].lower()
242 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
244 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
247 raise Exception("Failed to create user: %s" % afs_error_message(code))
250 def CreateGroup(self, name, owner, id=None):
252 Create a new group in the protection database. If an ID is
253 provided, that one will be used.
255 cdef afs_int32 code, cid
257 name = name[:PR_MAXNAMELEN].lower()
258 oid = self.NameToId(owner)
262 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
264 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
267 raise Exception("Failed to create group: %s" % afs_error_message(code))
270 def Delete(self, id):
272 Delete the protection database entry with the provided ID.
276 code = ubik_PR_Delete(self.client, 0, id)
278 raise Exception("Failed to delete user: %s" % afs_error_message(code))
280 def AddToGroup(self, uid, gid):
282 Add the user with the given ID to the group with the given ID.
286 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
288 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
290 def RemoveFromGroup(self, uid, gid):
292 Remove the user with the given ID from the group with the given ID.
296 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
298 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
300 def ListMembers(self, id):
302 Get the membership of an entity.
304 If id is a group ID, this returns the users that are in that
307 If id is a user ID, this returns the list of groups that user
310 This returns a list of PTS IDs.
312 cdef afs_int32 code, over
315 cdef object members = []
318 alist.prlist_val = NULL
320 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
322 if alist.prlist_val is not NULL:
323 for i in range(alist.prlist_len):
324 members.append(alist.prlist_val[i])
325 free(alist.prlist_val)
330 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
334 def ListOwned(self, oid):
336 Get all groups owned by an entity.
338 cdef afs_int32 code, over
341 cdef object owned = []
344 alist.prlist_val = NULL
346 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
348 if alist.prlist_val is not NULL:
349 for i in range(alist.prlist_len):
350 owned.append(alist.prlist_val[i])
351 free(alist.prlist_val)
356 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
360 def ListEntry(self, id):
362 Load a PTEntry instance with information about the provided
366 cdef prcheckentry centry
367 cdef object entry = PTEntry()
369 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
371 raise Exception("Error getting entity info: %s" % afs_error_message(code))
373 _ptentry_from_c(entry, centry)
376 def ChangeEntry(self, id, newname=None, newid=None, newoid=None):
378 Change the name, ID, and/or owner of a PTS entity.
380 For any of newname, newid, and newoid which aren't specified
381 or ar None, the value isn't changed.
384 cdef afs_int32 c_newid = 0, c_newoid = 0
385 cdef char * c_newname
388 newname = self.IdToName(id)
390 if newid is not None:
392 if newoid is not None:
395 code = ubik_PR_ChangeEntry(self.client, 0, id, c_newname, c_newoid, c_newid)
397 raise Exception("Error changing entity info: %s" % afs_error_message(code))
399 def IsAMemberOf(self, uid, gid):
401 Return True if the given uid is a member of the given gid.
406 code = ubik_PR_IsAMemberOf(self.client, 0, uid, gid, &flag)
408 raise Exception("Error testing membership: %s" % afs_error_message(code))
414 Return a tuple of the maximum user ID and the maximum group
415 ID currently assigned.
417 cdef afs_int32 code, uid, gid
419 code = ubik_PR_ListMax(self.client, 0, &uid, &gid)
421 raise Exception("Error looking up max uid/gid: %s" % afs_error_message(code))