3 cdef import from "afs/ptuser.h":
9 ctypedef char prname[PR_MAXNAMELEN]
12 unsigned int namelist_len
16 unsigned int idlist_len
19 int ubik_PR_NameToID(ubik_client *, afs_int32, namelist *, idlist *)
20 int ubik_PR_IDToName(ubik_client *, afs_int32, idlist *, namelist *)
21 int ubik_PR_INewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32)
22 int ubik_PR_NewEntry(ubik_client *, afs_int32, char *, afs_int32, afs_int32, afs_int32 *)
23 int ubik_PR_Delete(ubik_client *, afs_int32, afs_int32)
24 int ubik_PR_AddToGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
25 int ubik_PR_RemoveFromGroup(ubik_client *, afs_int32, afs_int32, afs_int32)
27 cdef import from "afs/pterror.h":
31 void initialize_PT_error_table()
34 cdef ubik_client * client
36 def __cinit__(self, cell=None, sec=1):
38 Open a connection to the protection server. A PTS object is
39 essentially a handle to talk to the server in a given cell.
41 cell defaults to None. If no argument is passed for cell, PTS
42 connects to the home cell.
44 sec is the security level, an integer from 0 to 3:
45 - 0: unauthenticated connection
46 - 1: try authenticated, then fall back to unauthenticated
47 - 2: fail if an authenticated connection can't be established
48 - 3: same as 2, plus encrypt all traffic to the protection
52 cdef afsconf_dir *cdir
53 cdef afsconf_cell info
55 cdef ktc_principal prin
57 cdef rx_securityClass *sc
58 cdef rx_connection *serverconns[MAXSERVERS]
61 initialize_PT_error_table()
72 raise Exception(code, "Error initializing Rx")
74 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
77 "Error opening configuration directory (%s): %s" % \
78 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
79 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
81 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
84 strncpy(prin.cell, info.name, sizeof(prin.cell))
86 strncpy(prin.name, "afs", sizeof(prin.name))
88 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
91 # No really - we wanted authentication
92 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
99 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
100 token.kvno, token.ticketLen,
104 sc = rxnull_NewClientSecurityObject()
108 memset(serverconns, 0, sizeof(serverconns))
109 for 0 <= i < info.numServers:
110 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
111 info.hostAddr[i].sin_port,
116 code = ubik_ClientInit(serverconns, &self.client)
118 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
120 code = rxs_Release(sc)
122 def __dealloc__(self):
123 ubik_ClientDestroy(self.client)
126 def NameToId(self, name):
128 Converts a user or group to an AFS ID.
132 cdef afs_int32 code, id
136 lids.idlist_val = NULL
137 lnames.namelist_len = 1
138 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
139 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
140 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
141 if lids.idlist_val is not NULL:
142 id = lids.idlist_val[0]
143 free(lids.idlist_val)
144 if id == ANONYMOUSID:
147 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
150 def IdToName(self, id):
152 Convert an AFS ID to the name of a user or group.
157 cdef char name[PR_MAXNAMELEN]
160 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
161 lids.idlist_val[0] = id
162 lnames.namelist_len = 0
163 lnames.namelist_val = NULL
164 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
165 if lnames.namelist_val is not NULL:
166 strncpy(name, lnames.namelist_val[0], sizeof(name))
167 free(lnames.namelist_val)
168 if lids.idlist_val is not NULL:
169 free(lids.idlist_val)
173 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
176 def CreateUser(self, name, id=None):
178 Create a new user in the protection database. If an ID is
179 provided, that one will be used.
183 name = name[:PR_MAXNAMELEN].lower()
189 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
191 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
194 raise Exception("Failed to create user: %s" % afs_error_message(code))
197 def CreateGroup(self, name, owner, id=None):
199 Create a new group in the protection database. If an ID is
200 provided, that one will be used.
202 cdef afs_int32 code, cid
204 name = name[:PR_MAXNAMELEN].lower()
205 oid = self.NameToId(owner)
209 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
211 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
214 raise Exception("Failed to create group: %s" % afs_error_message(code))
217 def Delete(self, id):
219 Delete the protection database entry with the provided ID.
223 code = ubik_PR_Delete(self.client, 0, id)
225 raise Exception("Failed to delete user: %s" % afs_error_message(code))
227 def AddToGroup(self, uid, gid):
229 Add the user with the given ID to the group with the given ID.
233 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
235 raise Exception("Failed to add user to group: %s" % afs_error_message(code))
237 def RemoveFromGroup(self, uid, gid):
239 Remove the user with the given ID from the group with the given ID.
243 code = ubik_PR_RemoveFromGroup(self.client, 0, uid, gid)
245 raise Exception("Failed to remove user from group: %s" % afs_error_message(code))