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
30 char name[PR_MAXNAMELEN]
32 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
33 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
34 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
35 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
36 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
37 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
38 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
39 int ubik_PR_ListElements(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
40 int ubik_PR_ListOwned(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
41 int ubik_PR_ListEntry(ubik_client *, afs_int32, afs_int32, prcheckentry *)
43 cdef import from "afs/pterror.h":
48 void initialize_PT_error_table()
51 cdef public afs_int32 flags
52 cdef public afs_int32 id
53 cdef public afs_int32 owner
54 cdef public afs_int32 creator
55 cdef public afs_int32 ngroups
56 cdef public afs_int32 count
57 cdef public char * name
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
70 p_entry.name = c_entry.name
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
84 strncpy(c_entry.name, p_entry.name, sizeof(c_entry.name))
89 A PTS object is essentially a handle to talk to the server in a
92 cell defaults to None. If no argument is passed for cell, PTS
93 connects to the home cell.
95 sec is the security level, an integer from 0 to 3:
96 - 0: unauthenticated connection
97 - 1: try authenticated, then fall back to unauthenticated
98 - 2: fail if an authenticated connection can't be established
99 - 3: same as 2, plus encrypt all traffic to the protection
102 cdef ubik_client * client
104 def __cinit__(self, cell=None, sec=1):
106 cdef afsconf_dir *cdir
107 cdef afsconf_cell info
109 cdef ktc_principal prin
111 cdef rx_securityClass *sc
112 cdef rx_connection *serverconns[MAXSERVERS]
115 initialize_PT_error_table()
126 raise Exception(code, "Error initializing Rx")
128 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
131 "Error opening configuration directory (%s): %s" % \
132 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
133 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
135 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
138 strncpy(prin.cell, info.name, sizeof(prin.cell))
140 strncpy(prin.name, "afs", sizeof(prin.name))
142 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
145 # No really - we wanted authentication
146 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
153 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
154 token.kvno, token.ticketLen,
158 sc = rxnull_NewClientSecurityObject()
162 memset(serverconns, 0, sizeof(serverconns))
163 for 0 <= i < info.numServers:
164 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
165 info.hostAddr[i].sin_port,
170 code = ubik_ClientInit(serverconns, &self.client)
172 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
174 code = rxs_Release(sc)
176 def __dealloc__(self):
177 ubik_ClientDestroy(self.client)
180 def NameToId(self, name):
182 Converts a user or group to an AFS ID.
186 cdef afs_int32 code, id
190 lids.idlist_val = NULL
191 lnames.namelist_len = 1
192 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
193 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
194 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
195 if lids.idlist_val is not NULL:
196 id = lids.idlist_val[0]
197 free(lids.idlist_val)
198 if id == ANONYMOUSID:
201 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
204 def IdToName(self, id):
206 Convert an AFS ID to the name of a user or group.
211 cdef char name[PR_MAXNAMELEN]
214 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
215 lids.idlist_val[0] = id
216 lnames.namelist_len = 0
217 lnames.namelist_val = NULL
218 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
219 if lnames.namelist_val is not NULL:
220 strncpy(name, lnames.namelist_val[0], sizeof(name))
221 free(lnames.namelist_val)
222 if lids.idlist_val is not NULL:
223 free(lids.idlist_val)
227 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
230 def CreateUser(self, name, id=None):
232 Create a new user in the protection database. If an ID is
233 provided, that one will be used.
237 name = name[:PR_MAXNAMELEN].lower()
243 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
245 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
248 raise Exception("Failed to create user: %s" % afs_error_message(code))
251 def CreateGroup(self, name, owner, id=None):
253 Create a new group in the protection database. If an ID is
254 provided, that one will be used.
256 cdef afs_int32 code, cid
258 name = name[:PR_MAXNAMELEN].lower()
259 oid = self.NameToId(owner)
263 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
265 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
268 raise Exception("Failed to create group: %s" % afs_error_message(code))
271 def Delete(self, id):
273 Delete the protection database entry with the provided ID.
277 code = ubik_PR_Delete(self.client, 0, id)
279 raise Exception("Failed to delete user: %s" % afs_error_message(code))
281 def AddToGroup(self, uid, gid):
283 Add the user with the given ID to the group with the given ID.
287 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
289 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
291 def RemoveFromGroup(self, uid, gid):
293 Remove the user with the given ID from the group with the given ID.
297 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
299 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
301 def ListMembers(self, id):
303 Get the membership of an entity.
305 If id is a group ID, this returns the users that are in that
308 If id is a user ID, this returns the list of groups that user
311 This returns a list of PTS IDs.
313 cdef afs_int32 code, over
316 cdef object members = []
319 alist.prlist_val = NULL
321 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
323 if alist.prlist_val is not NULL:
324 for i in range(alist.prlist_len):
325 members.append(alist.prlist_val[i])
326 free(alist.prlist_val)
331 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
335 def ListOwned(self, oid):
337 Get all groups owned by an entity.
339 cdef afs_int32 code, over
342 cdef object owned = []
345 alist.prlist_val = NULL
347 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
349 if alist.prlist_val is not NULL:
350 for i in range(alist.prlist_len):
351 owned.append(alist.prlist_val[i])
352 free(alist.prlist_val)
357 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
361 def ListEntry(self, id):
363 Load a PTEntry instance with information about the provided
367 cdef prcheckentry centry
368 cdef object entry = PTEntry()
370 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
372 raise Exception("Error getting entity info: %s" % afs_error_message(code))
374 _ptentry_from_c(entry, centry)