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
32 char name[PR_MAXNAMELEN]
34 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
35 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
36 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
37 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
38 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
39 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
40 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
41 int ubik_PR_ListElements(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
42 int ubik_PR_ListOwned(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
43 int ubik_PR_ListEntry(ubik_client *, afs_int32, afs_int32, prcheckentry *)
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 nusers
59 cdef public afs_int32 count
60 cdef afs_int32 reserved[5]
61 cdef public char * name
63 cdef int _ptentry_from_c(PTEntry p_entry, prcheckentry c_entry) except -1:
68 p_entry.flags = c_entry.flags
69 p_entry.id = c_entry.id
70 p_entry.owner = c_entry.owner
71 p_entry.creator = c_entry.creator
72 p_entry.ngroups = c_entry.ngroups
73 p_entry.nusers = c_entry.nusers
74 p_entry.count = c_entry.count
75 memcpy(p_entry.reserved, c_entry.reserved, sizeof(p_entry.reserved))
76 p_entry.name = c_entry.name
79 cdef int _ptentry_to_c(prcheckentry * c_entry, PTEntry p_entry) except -1:
84 c_entry.flags = p_entry.flags
85 c_entry.id = p_entry.id
86 c_entry.owner = p_entry.owner
87 c_entry.creator = p_entry.creator
88 c_entry.ngroups = p_entry.ngroups
89 c_entry.nusers = p_entry.nusers
90 c_entry.count = p_entry.count
91 memcpy(c_entry.reserved, p_entry.reserved, sizeof(p_entry.reserved))
92 strncpy(c_entry.name, p_entry.name, sizeof(c_entry.name))
97 A PTS object is essentially a handle to talk to the server in a
100 cell defaults to None. If no argument is passed for cell, PTS
101 connects to the home cell.
103 sec is the security level, an integer from 0 to 3:
104 - 0: unauthenticated connection
105 - 1: try authenticated, then fall back to unauthenticated
106 - 2: fail if an authenticated connection can't be established
107 - 3: same as 2, plus encrypt all traffic to the protection
110 cdef ubik_client * client
112 def __cinit__(self, cell=None, sec=1):
114 cdef afsconf_dir *cdir
115 cdef afsconf_cell info
117 cdef ktc_principal prin
119 cdef rx_securityClass *sc
120 cdef rx_connection *serverconns[MAXSERVERS]
123 initialize_PT_error_table()
134 raise Exception(code, "Error initializing Rx")
136 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
139 "Error opening configuration directory (%s): %s" % \
140 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
141 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
143 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
146 strncpy(prin.cell, info.name, sizeof(prin.cell))
148 strncpy(prin.name, "afs", sizeof(prin.name))
150 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
153 # No really - we wanted authentication
154 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
161 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
162 token.kvno, token.ticketLen,
166 sc = rxnull_NewClientSecurityObject()
170 memset(serverconns, 0, sizeof(serverconns))
171 for 0 <= i < info.numServers:
172 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
173 info.hostAddr[i].sin_port,
178 code = ubik_ClientInit(serverconns, &self.client)
180 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
182 code = rxs_Release(sc)
184 def __dealloc__(self):
185 ubik_ClientDestroy(self.client)
188 def NameToId(self, name):
190 Converts a user or group to an AFS ID.
194 cdef afs_int32 code, id
198 lids.idlist_val = NULL
199 lnames.namelist_len = 1
200 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
201 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
202 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
203 if lids.idlist_val is not NULL:
204 id = lids.idlist_val[0]
205 free(lids.idlist_val)
206 if id == ANONYMOUSID:
209 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
212 def IdToName(self, id):
214 Convert an AFS ID to the name of a user or group.
219 cdef char name[PR_MAXNAMELEN]
222 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
223 lids.idlist_val[0] = id
224 lnames.namelist_len = 0
225 lnames.namelist_val = NULL
226 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
227 if lnames.namelist_val is not NULL:
228 strncpy(name, lnames.namelist_val[0], sizeof(name))
229 free(lnames.namelist_val)
230 if lids.idlist_val is not NULL:
231 free(lids.idlist_val)
235 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
238 def CreateUser(self, name, id=None):
240 Create a new user in the protection database. If an ID is
241 provided, that one will be used.
245 name = name[:PR_MAXNAMELEN].lower()
251 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
253 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
256 raise Exception("Failed to create user: %s" % afs_error_message(code))
259 def CreateGroup(self, name, owner, id=None):
261 Create a new group in the protection database. If an ID is
262 provided, that one will be used.
264 cdef afs_int32 code, cid
266 name = name[:PR_MAXNAMELEN].lower()
267 oid = self.NameToId(owner)
271 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
273 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
276 raise Exception("Failed to create group: %s" % afs_error_message(code))
279 def Delete(self, id):
281 Delete the protection database entry with the provided ID.
285 code = ubik_PR_Delete(self.client, 0, id)
287 raise Exception("Failed to delete user: %s" % afs_error_message(code))
289 def AddToGroup(self, uid, gid):
291 Add the user with the given ID to the group with the given ID.
295 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
297 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
299 def RemoveFromGroup(self, uid, gid):
301 Remove the user with the given ID from the group with the given ID.
305 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
307 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
309 def ListMembers(self, id):
311 Get the membership of an entity.
313 If id is a group ID, this returns the users that are in that
316 If id is a user ID, this returns the list of groups that user
319 This returns a list of PTS IDs.
321 cdef afs_int32 code, over
324 cdef object members = []
327 alist.prlist_val = NULL
329 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
331 if alist.prlist_val is not NULL:
332 for i in range(alist.prlist_len):
333 members.append(alist.prlist_val[i])
334 free(alist.prlist_val)
339 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
343 def ListOwned(self, oid):
345 Get all groups owned by an entity.
347 cdef afs_int32 code, over
350 cdef object owned = []
353 alist.prlist_val = NULL
355 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
357 if alist.prlist_val is not NULL:
358 for i in range(alist.prlist_len):
359 owned.append(alist.prlist_val[i])
360 free(alist.prlist_val)
365 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
369 def ListEntry(self, id):
371 Load a PTEntry instance with information about the provided
375 cdef prcheckentry centry
376 cdef object entry = PTEntry()
378 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
380 raise Exception("Error getting entity info: %s" % afs_error_message(code))
382 _ptentry_from_c(entry, centry)