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
23 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
24 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
25 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
26 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
27 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
28 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
29 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
30 int ubik_PR_ListElements(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
31 int ubik_PR_ListOwned(ubik_client *, afs_int32, afs_int32, prlist *, afs_int32 *)
33 cdef import from "afs/pterror.h":
38 void initialize_PT_error_table()
41 cdef ubik_client * client
43 def __cinit__(self, cell=None, sec=1):
45 Open a connection to the protection server. A PTS object is
46 essentially a handle to talk to the server in a given cell.
48 cell defaults to None. If no argument is passed for cell, PTS
49 connects to the home cell.
51 sec is the security level, an integer from 0 to 3:
52 - 0: unauthenticated connection
53 - 1: try authenticated, then fall back to unauthenticated
54 - 2: fail if an authenticated connection can't be established
55 - 3: same as 2, plus encrypt all traffic to the protection
59 cdef afsconf_dir *cdir
60 cdef afsconf_cell info
62 cdef ktc_principal prin
64 cdef rx_securityClass *sc
65 cdef rx_connection *serverconns[MAXSERVERS]
68 initialize_PT_error_table()
79 raise Exception(code, "Error initializing Rx")
81 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
84 "Error opening configuration directory (%s): %s" % \
85 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
86 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
88 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
91 strncpy(prin.cell, info.name, sizeof(prin.cell))
93 strncpy(prin.name, "afs", sizeof(prin.name))
95 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
98 # No really - we wanted authentication
99 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
106 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
107 token.kvno, token.ticketLen,
111 sc = rxnull_NewClientSecurityObject()
115 memset(serverconns, 0, sizeof(serverconns))
116 for 0 <= i < info.numServers:
117 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
118 info.hostAddr[i].sin_port,
123 code = ubik_ClientInit(serverconns, &self.client)
125 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
127 code = rxs_Release(sc)
129 def __dealloc__(self):
130 ubik_ClientDestroy(self.client)
133 def NameToId(self, name):
135 Converts a user or group to an AFS ID.
139 cdef afs_int32 code, id
143 lids.idlist_val = NULL
144 lnames.namelist_len = 1
145 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
146 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
147 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
148 if lids.idlist_val is not NULL:
149 id = lids.idlist_val[0]
150 free(lids.idlist_val)
151 if id == ANONYMOUSID:
154 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
157 def IdToName(self, id):
159 Convert an AFS ID to the name of a user or group.
164 cdef char name[PR_MAXNAMELEN]
167 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
168 lids.idlist_val[0] = id
169 lnames.namelist_len = 0
170 lnames.namelist_val = NULL
171 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
172 if lnames.namelist_val is not NULL:
173 strncpy(name, lnames.namelist_val[0], sizeof(name))
174 free(lnames.namelist_val)
175 if lids.idlist_val is not NULL:
176 free(lids.idlist_val)
180 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
183 def CreateUser(self, name, id=None):
185 Create a new user in the protection database. If an ID is
186 provided, that one will be used.
190 name = name[:PR_MAXNAMELEN].lower()
196 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
198 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
201 raise Exception("Failed to create user: %s" % afs_error_message(code))
204 def CreateGroup(self, name, owner, id=None):
206 Create a new group in the protection database. If an ID is
207 provided, that one will be used.
209 cdef afs_int32 code, cid
211 name = name[:PR_MAXNAMELEN].lower()
212 oid = self.NameToId(owner)
216 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
218 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
221 raise Exception("Failed to create group: %s" % afs_error_message(code))
224 def Delete(self, id):
226 Delete the protection database entry with the provided ID.
230 code = ubik_PR_Delete(self.client, 0, id)
232 raise Exception("Failed to delete user: %s" % afs_error_message(code))
234 def AddToGroup(self, uid, gid):
236 Add the user with the given ID to the group with the given ID.
240 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
242 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
244 def RemoveFromGroup(self, uid, gid):
246 Remove the user with the given ID from the group with the given ID.
250 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
252 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
254 def ListMembers(self, id):
256 Get the membership of an entity.
258 If id is a group ID, this returns the users that are in that
261 If id is a user ID, this returns the list of groups that user
264 This returns a list of PTS IDs.
266 cdef afs_int32 code, over
269 cdef object members = []
272 alist.prlist_val = NULL
274 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
276 if alist.prlist_val is not NULL:
277 for i in range(alist.prlist_len):
278 members.append(alist.prlist_val[i])
279 free(alist.prlist_val)
284 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
288 def ListOwned(self, oid):
290 Get all groups owned by an entity.
292 cdef afs_int32 code, over
295 cdef object owned = []
298 alist.prlist_val = NULL
300 code = ubik_PR_ListOwned(self.client, 0, oid, &alist, &over)
302 if alist.prlist_val is not NULL:
303 for i in range(alist.prlist_len):
304 owned.append(alist.prlist_val[i])
305 free(alist.prlist_val)
310 raise Exception("Failed to get owned entities: %s" % afs_error_message(code))