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 char name[PR_MAXNAMELEN]
33 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
34 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
35 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
36 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
37 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
38 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
39 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
40 int ubik_PR_ListElements(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
41 int ubik_PR_ListOwned(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
42 int ubik_PR_ListEntry(ubik_client *, afs_int32, afs_int32, prcheckentry *)
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
58 cdef afs_int32 reserved[5]
59 cdef public char * name
61 cdef int _ptentry_from_c(PTEntry p_entry, prcheckentry c_entry) except -1:
66 p_entry.flags = c_entry.flags
67 p_entry.id = c_entry.id
68 p_entry.owner = c_entry.owner
69 p_entry.creator = c_entry.creator
70 p_entry.ngroups = c_entry.ngroups
71 p_entry.count = c_entry.count
72 memcpy(p_entry.reserved, c_entry.reserved, sizeof(p_entry.reserved))
73 p_entry.name = c_entry.name
76 cdef int _ptentry_to_c(prcheckentry * c_entry, PTEntry p_entry) except -1:
81 c_entry.flags = p_entry.flags
82 c_entry.id = p_entry.id
83 c_entry.owner = p_entry.owner
84 c_entry.creator = p_entry.creator
85 c_entry.ngroups = p_entry.ngroups
86 c_entry.count = p_entry.count
87 memcpy(c_entry.reserved, p_entry.reserved, sizeof(p_entry.reserved))
88 strncpy(c_entry.name, p_entry.name, sizeof(c_entry.name))
93 A PTS object is essentially a handle to talk to the server in a
96 cell defaults to None. If no argument is passed for cell, PTS
97 connects to the home cell.
99 sec is the security level, an integer from 0 to 3:
100 - 0: unauthenticated connection
101 - 1: try authenticated, then fall back to unauthenticated
102 - 2: fail if an authenticated connection can't be established
103 - 3: same as 2, plus encrypt all traffic to the protection
106 cdef ubik_client * client
108 def __cinit__(self, cell=None, sec=1):
110 cdef afsconf_dir *cdir
111 cdef afsconf_cell info
113 cdef ktc_principal prin
115 cdef rx_securityClass *sc
116 cdef rx_connection *serverconns[MAXSERVERS]
119 initialize_PT_error_table()
130 raise Exception(code, "Error initializing Rx")
132 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
135 "Error opening configuration directory (%s): %s" % \
136 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
137 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
139 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
142 strncpy(prin.cell, info.name, sizeof(prin.cell))
144 strncpy(prin.name, "afs", sizeof(prin.name))
146 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
149 # No really - we wanted authentication
150 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
157 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
158 token.kvno, token.ticketLen,
162 sc = rxnull_NewClientSecurityObject()
166 memset(serverconns, 0, sizeof(serverconns))
167 for 0 <= i < info.numServers:
168 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
169 info.hostAddr[i].sin_port,
174 code = ubik_ClientInit(serverconns, &self.client)
176 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
178 code = rxs_Release(sc)
180 def __dealloc__(self):
181 ubik_ClientDestroy(self.client)
184 def NameToId(self, name):
186 Converts a user or group to an AFS ID.
190 cdef afs_int32 code, id
194 lids.idlist_val = NULL
195 lnames.namelist_len = 1
196 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
197 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
198 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
199 if lids.idlist_val is not NULL:
200 id = lids.idlist_val[0]
201 free(lids.idlist_val)
202 if id == ANONYMOUSID:
205 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
208 def IdToName(self, id):
210 Convert an AFS ID to the name of a user or group.
215 cdef char name[PR_MAXNAMELEN]
218 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
219 lids.idlist_val[0] = id
220 lnames.namelist_len = 0
221 lnames.namelist_val = NULL
222 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
223 if lnames.namelist_val is not NULL:
224 strncpy(name, lnames.namelist_val[0], sizeof(name))
225 free(lnames.namelist_val)
226 if lids.idlist_val is not NULL:
227 free(lids.idlist_val)
231 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
234 def CreateUser(self, name, id=None):
236 Create a new user in the protection database. If an ID is
237 provided, that one will be used.
241 name = name[:PR_MAXNAMELEN].lower()
247 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
249 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
252 raise Exception("Failed to create user: %s" % afs_error_message(code))
255 def CreateGroup(self, name, owner, id=None):
257 Create a new group in the protection database. If an ID is
258 provided, that one will be used.
260 cdef afs_int32 code, cid
262 name = name[:PR_MAXNAMELEN].lower()
263 oid = self.NameToId(owner)
267 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
269 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
272 raise Exception("Failed to create group: %s" % afs_error_message(code))
275 def Delete(self, id):
277 Delete the protection database entry with the provided ID.
281 code = ubik_PR_Delete(self.client, 0, id)
283 raise Exception("Failed to delete user: %s" % afs_error_message(code))
285 def AddToGroup(self, uid, gid):
287 Add the user with the given ID to the group with the given ID.
291 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
293 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
295 def RemoveFromGroup(self, uid, gid):
297 Remove the user with the given ID from the group with the given ID.
301 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
303 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
305 def ListMembers(self, id):
307 Get the membership of an entity.
309 If id is a group ID, this returns the users that are in that
312 If id is a user ID, this returns the list of groups that user
315 This returns a list of PTS IDs.
317 cdef afs_int32 code, over
320 cdef object members = []
323 alist.prlist_val = NULL
325 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
327 if alist.prlist_val is not NULL:
328 for i in range(alist.prlist_len):
329 members.append(alist.prlist_val[i])
330 free(alist.prlist_val)
335 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
339 def ListOwned(self, oid):
341 Get all groups owned by an entity.
343 cdef afs_int32 code, over
346 cdef object owned = []
349 alist.prlist_val = NULL
351 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
353 if alist.prlist_val is not NULL:
354 for i in range(alist.prlist_len):
355 owned.append(alist.prlist_val[i])
356 free(alist.prlist_val)
361 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
365 def ListEntry(self, id):
367 Load a PTEntry instance with information about the provided
371 cdef prcheckentry centry
372 cdef object entry = PTEntry()
374 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
376 raise Exception("Error getting entity info: %s" % afs_error_message(code))
378 _ptentry_from_c(entry, centry)