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 *)
32 cdef import from "afs/pterror.h":
37 void initialize_PT_error_table()
40 cdef ubik_client * client
42 def __cinit__(self, cell=None, sec=1):
44 Open a connection to the protection server. A PTS object is
45 essentially a handle to talk to the server in a given cell.
47 cell defaults to None. If no argument is passed for cell, PTS
48 connects to the home cell.
50 sec is the security level, an integer from 0 to 3:
51 - 0: unauthenticated connection
52 - 1: try authenticated, then fall back to unauthenticated
53 - 2: fail if an authenticated connection can't be established
54 - 3: same as 2, plus encrypt all traffic to the protection
58 cdef afsconf_dir *cdir
59 cdef afsconf_cell info
61 cdef ktc_principal prin
63 cdef rx_securityClass *sc
64 cdef rx_connection *serverconns[MAXSERVERS]
67 initialize_PT_error_table()
78 raise Exception(code, "Error initializing Rx")
80 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
83 "Error opening configuration directory (%s): %s" % \
84 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
85 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
87 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
90 strncpy(prin.cell, info.name, sizeof(prin.cell))
92 strncpy(prin.name, "afs", sizeof(prin.name))
94 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
97 # No really - we wanted authentication
98 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
105 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
106 token.kvno, token.ticketLen,
110 sc = rxnull_NewClientSecurityObject()
114 memset(serverconns, 0, sizeof(serverconns))
115 for 0 <= i < info.numServers:
116 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
117 info.hostAddr[i].sin_port,
122 code = ubik_ClientInit(serverconns, &self.client)
124 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
126 code = rxs_Release(sc)
128 def __dealloc__(self):
129 ubik_ClientDestroy(self.client)
132 def NameToId(self, name):
134 Converts a user or group to an AFS ID.
138 cdef afs_int32 code, id
142 lids.idlist_val = NULL
143 lnames.namelist_len = 1
144 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
145 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
146 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
147 if lids.idlist_val is not NULL:
148 id = lids.idlist_val[0]
149 free(lids.idlist_val)
150 if id == ANONYMOUSID:
153 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
156 def IdToName(self, id):
158 Convert an AFS ID to the name of a user or group.
163 cdef char name[PR_MAXNAMELEN]
166 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
167 lids.idlist_val[0] = id
168 lnames.namelist_len = 0
169 lnames.namelist_val = NULL
170 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
171 if lnames.namelist_val is not NULL:
172 strncpy(name, lnames.namelist_val[0], sizeof(name))
173 free(lnames.namelist_val)
174 if lids.idlist_val is not NULL:
175 free(lids.idlist_val)
179 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
182 def CreateUser(self, name, id=None):
184 Create a new user in the protection database. If an ID is
185 provided, that one will be used.
189 name = name[:PR_MAXNAMELEN].lower()
195 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
197 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
200 raise Exception("Failed to create user: %s" % afs_error_message(code))
203 def CreateGroup(self, name, owner, id=None):
205 Create a new group in the protection database. If an ID is
206 provided, that one will be used.
208 cdef afs_int32 code, cid
210 name = name[:PR_MAXNAMELEN].lower()
211 oid = self.NameToId(owner)
215 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
217 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
220 raise Exception("Failed to create group: %s" % afs_error_message(code))
223 def Delete(self, id):
225 Delete the protection database entry with the provided ID.
229 code = ubik_PR_Delete(self.client, 0, id)
231 raise Exception("Failed to delete user: %s" % afs_error_message(code))
233 def AddToGroup(self, uid, gid):
235 Add the user with the given ID to the group with the given ID.
239 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
241 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
243 def RemoveFromGroup(self, uid, gid):
245 Remove the user with the given ID from the group with the given ID.
249 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
251 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
253 def ListMembers(self, id):
255 Get the membership of an entity.
257 If id is a group ID, this returns the users that are in that
260 If id is a user ID, this returns the list of groups that user
263 This returns a list of PTS IDs.
265 cdef afs_int32 code, over
268 cdef object members = []
271 alist.prlist_val = NULL
273 code = ubik_PR_ListElements(self.client, 0, id, &alist, &over)
275 if alist.prlist_val is not NULL:
276 for i in range(alist.prlist_len):
277 members.append(alist.prlist_val[i])
278 free(alist.prlist_val)
283 raise Exception("Failed to get group membership: %s" % afs_error_message(code))