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 *)
42 cdef import from "afs/pterror.h":
47 void initialize_PT_error_table()
50 cdef public afs_int32 flags
51 cdef public afs_int32 id
52 cdef public afs_int32 owner
53 cdef public afs_int32 creator
54 cdef public afs_int32 ngroups
55 cdef public afs_int32 count
57 cdef int _ptentry_from_c(PTEntry p_entry, prcheckentry c_entry) except -1:
62 p_entry.flags = c_entry.flags
63 p_entry.id = c_entry.id
64 p_entry.owner = c_entry.owner
65 p_entry.creator = c_entry.creator
66 p_entry.ngroups = c_entry.ngroups
67 p_entry.count = c_entry.count
70 cdef int _ptentry_to_c(prcheckentry * c_entry, PTEntry p_entry) except -1:
75 c_entry.flags = p_entry.flags
76 c_entry.id = p_entry.id
77 c_entry.owner = p_entry.owner
78 c_entry.creator = p_entry.creator
79 c_entry.ngroups = p_entry.ngroups
80 c_entry.count = p_entry.count
85 A PTS object is essentially a handle to talk to the server in a
88 cell defaults to None. If no argument is passed for cell, PTS
89 connects to the home cell.
91 sec is the security level, an integer from 0 to 3:
92 - 0: unauthenticated connection
93 - 1: try authenticated, then fall back to unauthenticated
94 - 2: fail if an authenticated connection can't be established
95 - 3: same as 2, plus encrypt all traffic to the protection
98 cdef ubik_client * client
100 def __cinit__(self, cell=None, sec=1):
102 cdef afsconf_dir *cdir
103 cdef afsconf_cell info
105 cdef ktc_principal prin
107 cdef rx_securityClass *sc
108 cdef rx_connection *serverconns[MAXSERVERS]
111 initialize_PT_error_table()
122 raise Exception(code, "Error initializing Rx")
124 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
127 "Error opening configuration directory (%s): %s" % \
128 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
129 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
131 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
134 strncpy(prin.cell, info.name, sizeof(prin.cell))
136 strncpy(prin.name, "afs", sizeof(prin.name))
138 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
141 # No really - we wanted authentication
142 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
149 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
150 token.kvno, token.ticketLen,
154 sc = rxnull_NewClientSecurityObject()
158 memset(serverconns, 0, sizeof(serverconns))
159 for 0 <= i < info.numServers:
160 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
161 info.hostAddr[i].sin_port,
166 code = ubik_ClientInit(serverconns, &self.client)
168 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
170 code = rxs_Release(sc)
172 def __dealloc__(self):
173 ubik_ClientDestroy(self.client)
176 def NameToId(self, name):
178 Converts a user or group to an AFS ID.
182 cdef afs_int32 code, id
186 lids.idlist_val = NULL
187 lnames.namelist_len = 1
188 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
189 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
190 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
191 if lids.idlist_val is not NULL:
192 id = lids.idlist_val[0]
193 free(lids.idlist_val)
194 if id == ANONYMOUSID:
197 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
200 def IdToName(self, id):
202 Convert an AFS ID to the name of a user or group.
207 cdef char name[PR_MAXNAMELEN]
210 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
211 lids.idlist_val[0] = id
212 lnames.namelist_len = 0
213 lnames.namelist_val = NULL
214 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
215 if lnames.namelist_val is not NULL:
216 strncpy(name, lnames.namelist_val[0], sizeof(name))
217 free(lnames.namelist_val)
218 if lids.idlist_val is not NULL:
219 free(lids.idlist_val)
223 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
226 def CreateUser(self, name, id=None):
228 Create a new user in the protection database. If an ID is
229 provided, that one will be used.
233 name = name[:PR_MAXNAMELEN].lower()
239 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
241 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
244 raise Exception("Failed to create user: %s" % afs_error_message(code))
247 def CreateGroup(self, name, owner, id=None):
249 Create a new group in the protection database. If an ID is
250 provided, that one will be used.
252 cdef afs_int32 code, cid
254 name = name[:PR_MAXNAMELEN].lower()
255 oid = self.NameToId(owner)
259 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
261 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
264 raise Exception("Failed to create group: %s" % afs_error_message(code))
267 def Delete(self, id):
269 Delete the protection database entry with the provided ID.
273 code = ubik_PR_Delete(self.client, 0, id)
275 raise Exception("Failed to delete user: %s" % afs_error_message(code))
277 def AddToGroup(self, uid, gid):
279 Add the user with the given ID to the group with the given ID.
283 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
285 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
287 def RemoveFromGroup(self, uid, gid):
289 Remove the user with the given ID from the group with the given ID.
293 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
295 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
297 def ListMembers(self, id):
299 Get the membership of an entity.
301 If id is a group ID, this returns the users that are in that
304 If id is a user ID, this returns the list of groups that user
307 This returns a list of PTS IDs.
309 cdef afs_int32 code, over
312 cdef object members = []
315 alist.prlist_val = NULL
317 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
319 if alist.prlist_val is not NULL:
320 for i in range(alist.prlist_len):
321 members.append(alist.prlist_val[i])
322 free(alist.prlist_val)
327 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
331 def ListOwned(self, oid):
333 Get all groups owned by an entity.
335 cdef afs_int32 code, over
338 cdef object owned = []
341 alist.prlist_val = NULL
343 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
345 if alist.prlist_val is not NULL:
346 for i in range(alist.prlist_len):
347 owned.append(alist.prlist_val[i])
348 free(alist.prlist_val)
353 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))
357 def ListEntry(self, id):
359 Load a PTEntry instance with information about the provided
363 cdef prcheckentry centry
364 cdef object entry = PTEntry()
366 code = ubik_PR_ListEntry(self.client, 0, id, ¢ry)
368 raise Exception("Error getting entity info: %s" % afs_error_message(code))
370 _ptentry_from_c(entry, centry)