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":
36 void initialize_PT_error_table()
39 cdef ubik_client * client
41 def __cinit__(self, cell=None, sec=1):
43 Open a connection to the protection server. A PTS object is
44 essentially a handle to talk to the server in a given cell.
46 cell defaults to None. If no argument is passed for cell, PTS
47 connects to the home cell.
49 sec is the security level, an integer from 0 to 3:
50 - 0: unauthenticated connection
51 - 1: try authenticated, then fall back to unauthenticated
52 - 2: fail if an authenticated connection can't be established
53 - 3: same as 2, plus encrypt all traffic to the protection
57 cdef afsconf_dir *cdir
58 cdef afsconf_cell info
60 cdef ktc_principal prin
62 cdef rx_securityClass *sc
63 cdef rx_connection *serverconns[MAXSERVERS]
66 initialize_PT_error_table()
77 raise Exception(code, "Error initializing Rx")
79 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
82 "Error opening configuration directory (%s): %s" % \
83 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
84 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
86 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
89 strncpy(prin.cell, info.name, sizeof(prin.cell))
91 strncpy(prin.name, "afs", sizeof(prin.name))
93 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
96 # No really - we wanted authentication
97 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
104 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
105 token.kvno, token.ticketLen,
109 sc = rxnull_NewClientSecurityObject()
113 memset(serverconns, 0, sizeof(serverconns))
114 for 0 <= i < info.numServers:
115 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
116 info.hostAddr[i].sin_port,
121 code = ubik_ClientInit(serverconns, &self.client)
123 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
125 code = rxs_Release(sc)
127 def __dealloc__(self):
128 ubik_ClientDestroy(self.client)
131 def NameToId(self, name):
133 Converts a user or group to an AFS ID.
137 cdef afs_int32 code, id
141 lids.idlist_val = NULL
142 lnames.namelist_len = 1
143 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
144 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
145 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
146 if lids.idlist_val is not NULL:
147 id = lids.idlist_val[0]
148 free(lids.idlist_val)
149 if id == ANONYMOUSID:
152 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
155 def IdToName(self, id):
157 Convert an AFS ID to the name of a user or group.
162 cdef char name[PR_MAXNAMELEN]
165 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
166 lids.idlist_val[0] = id
167 lnames.namelist_len = 0
168 lnames.namelist_val = NULL
169 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
170 if lnames.namelist_val is not NULL:
171 strncpy(name, lnames.namelist_val[0], sizeof(name))
172 free(lnames.namelist_val)
173 if lids.idlist_val is not NULL:
174 free(lids.idlist_val)
178 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
181 def CreateUser(self, name, id=None):
183 Create a new user in the protection database. If an ID is
184 provided, that one will be used.
188 name = name[:PR_MAXNAMELEN].lower()
194 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
196 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
199 raise Exception("Failed to create user: %s" % afs_error_message(code))
202 def CreateGroup(self, name, owner, id=None):
204 Create a new group in the protection database. If an ID is
205 provided, that one will be used.
207 cdef afs_int32 code, cid
209 name = name[:PR_MAXNAMELEN].lower()
210 oid = self.NameToId(owner)
214 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
216 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
219 raise Exception("Failed to create group: %s" % afs_error_message(code))
222 def Delete(self, id):
224 Delete the protection database entry with the provided ID.
228 code = ubik_PR_Delete(self.client, 0, id)
230 raise Exception("Failed to delete user: %s" % afs_error_message(code))
232 def AddToGroup(self, uid, gid):
234 Add the user with the given ID to the group with the given ID.
238 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
240 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
242 def RemoveFromGroup(self, uid, gid):
244 Remove the user with the given ID from the group with the given ID.
248 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
250 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))
252 def ListMembers(self, gid):
254 Get the membership of the list with the given ID.
256 This returns a list of PTS IDs.
258 cdef afs_int32 code, over
261 cdef object members = []
264 alist.prlist_val = NULL
266 code = ubik_PR_ListElements(self.client, 0, gid, &alist, &over)
268 raise Exception("Failed to get group membership: %s" % afs_error_message(code))
270 for i in range(alist.prlist_len):
271 members.append(alist.prlist_val[i])
272 if alist.prlist_val is not NULL:
273 free(alist.prlist_val)