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 *)
44 cdef import from "afs/pterror.h":
49 void initialize_PT_error_table()
52 cdef public afs_int32 flags
53 cdef public afs_int32 id
54 cdef public afs_int32 owner
55 cdef public afs_int32 creator
56 cdef public afs_int32 ngroups
57 cdef public afs_int32 count
59 cdef int _ptentry_from_c(PTEntry p_entry, prcheckentry c_entry) except -1:
64 p_entry.flags = c_entry.flags
65 p_entry.id = c_entry.id
66 p_entry.owner = c_entry.owner
67 p_entry.creator = c_entry.creator
68 p_entry.ngroups = c_entry.ngroups
69 p_entry.count = c_entry.count
72 cdef int _ptentry_to_c(prcheckentry * c_entry, PTEntry p_entry) except -1:
77 c_entry.flags = p_entry.flags
78 c_entry.id = p_entry.id
79 c_entry.owner = p_entry.owner
80 c_entry.creator = p_entry.creator
81 c_entry.ngroups = p_entry.ngroups
82 c_entry.count = p_entry.count
87 A PTS object is essentially a handle to talk to the server in a
90 cell defaults to None. If no argument is passed for cell, PTS
91 connects to the home cell.
93 sec is the security level, an integer from 0 to 3:
94 - 0: unauthenticated connection
95 - 1: try authenticated, then fall back to unauthenticated
96 - 2: fail if an authenticated connection can't be established
97 - 3: same as 2, plus encrypt all traffic to the protection
100 cdef ubik_client * client
102 def __cinit__(self, cell=None, sec=1):
104 cdef afsconf_dir *cdir
105 cdef afsconf_cell info
107 cdef ktc_principal prin
109 cdef rx_securityClass *sc
110 cdef rx_connection *serverconns[MAXSERVERS]
113 initialize_PT_error_table()
124 raise Exception(code, "Error initializing Rx")
126 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
129 "Error opening configuration directory (%s): %s" % \
130 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
131 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
133 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
136 strncpy(prin.cell, info.name, sizeof(prin.cell))
138 strncpy(prin.name, "afs", sizeof(prin.name))
140 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
143 # No really - we wanted authentication
144 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
151 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
152 token.kvno, token.ticketLen,
156 sc = rxnull_NewClientSecurityObject()
160 memset(serverconns, 0, sizeof(serverconns))
161 for 0 <= i < info.numServers:
162 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
163 info.hostAddr[i].sin_port,
168 code = ubik_ClientInit(serverconns, &self.client)
170 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
172 code = rxs_Release(sc)
174 def __dealloc__(self):
175 ubik_ClientDestroy(self.client)
178 def NameToId(self, name):
180 Converts a user or group to an AFS ID.
184 cdef afs_int32 code, id
188 lids.idlist_val = NULL
189 lnames.namelist_len = 1
190 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
191 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
192 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
193 if lids.idlist_val is not NULL:
194 id = lids.idlist_val[0]
195 free(lids.idlist_val)
196 if id == ANONYMOUSID:
199 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
202 def IdToName(self, id):
204 Convert an AFS ID to the name of a user or group.
209 cdef char name[PR_MAXNAMELEN]
212 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
213 lids.idlist_val[0] = id
214 lnames.namelist_len = 0
215 lnames.namelist_val = NULL
216 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
217 if lnames.namelist_val is not NULL:
218 strncpy(name, lnames.namelist_val[0], sizeof(name))
219 free(lnames.namelist_val)
220 if lids.idlist_val is not NULL:
221 free(lids.idlist_val)
225 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
228 def CreateUser(self, name, id=None):
230 Create a new user in the protection database. If an ID is
231 provided, that one will be used.
235 name = name[:PR_MAXNAMELEN].lower()
241 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
243 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
246 raise Exception("Failed to create user: %s" % afs_error_message(code))
249 def CreateGroup(self, name, owner, id=None):
251 Create a new group in the protection database. If an ID is
252 provided, that one will be used.
254 cdef afs_int32 code, cid
256 name = name[:PR_MAXNAMELEN].lower()
257 oid = self.NameToId(owner)
261 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
263 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
266 raise Exception("Failed to create group: %s" % afs_error_message(code))
269 def Delete(self, id):
271 Delete the protection database entry with the provided ID.
275 code = ubik_PR_Delete(self.client, 0, id)
277 raise Exception("Failed to delete user: %s" % afs_error_message(code))
279 def AddToGroup(self, uid, gid):
281 Add the user with the given ID to the group with the given ID.
285 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
287 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
289 def RemoveFromGroup(self, uid, gid):
291 Remove the user with the given ID from the group with the given ID.
295 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
297 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
299 def ListMembers(self, id):
301 Get the membership of an entity.
303 If id is a group ID, this returns the users that are in that
306 If id is a user ID, this returns the list of groups that user
309 This returns a list of PTS IDs.
311 cdef afs_int32 code, over
314 cdef object members = []
317 alist.prlist_val = NULL
319 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
321 if alist.prlist_val is not NULL:
322 for i in range(alist.prlist_len):
323 members.append(alist.prlist_val[i])
324 free(alist.prlist_val)
329 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
333 def ListOwned(self, oid):
335 Get all groups owned by an entity.
337 cdef afs_int32 code, over
340 cdef object owned = []
343 alist.prlist_val = NULL
345 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
347 if alist.prlist_val is not NULL:
348 for i in range(alist.prlist_len):
349 owned.append(alist.prlist_val[i])
350 free(alist.prlist_val)
355 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
359 def ListEntry(self, id):
361 Load a PTEntry instance with information about the provided
365 cdef prcheckentry centry
366 cdef object entry = PTEntry()
368 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
370 raise Exception("Error getting entity info: %s" % afs_error_message(code))
372 _ptentry_from_c(entry, centry)
375 def ChangeEntry(self, id, newname=None, newid=None, newoid=None):
377 Change the name, ID, and/or owner of a PTS entity.
379 For any of newname, newid, and newoid which aren't specified
380 or ar None, the value isn't changed.
383 cdef afs_int32 c_newid = 0, c_newoid = 0
384 cdef char * c_newname
387 newname = self.IdToName(id)
389 if newid is not None:
391 if newoid is not None:
394 code = ubik_PR_ChangeEntry(self.client, 0, id, c_newname, c_newoid, c_newid)
396 raise Exception("Error changing entity info: %s" % afs_error_message(code))
398 def IsAMemberOf(self, uid, gid):
400 Return True if the given uid is a member of the given gid.
405 code = ubik_PR_IsAMemberOf(self.client, 0, uid, gid, &flag)
407 raise Exception("Error testing membership: %s" % afs_error_message(code))