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)
25 cdef import from "afs/pterror.h":
29 void initialize_PT_error_table()
32 cdef ubik_client * client
34 def __cinit__(self, cell=None, sec=1):
36 Open a connection to the protection server. A PTS object is
37 essentially a handle to talk to the server in a given cell.
39 cell defaults to None. If no argument is passed for cell, PTS
40 connects to the home cell.
42 sec is the security level, an integer from 0 to 3:
43 - 0: unauthenticated connection
44 - 1: try authenticated, then fall back to unauthenticated
45 - 2: fail if an authenticated connection can't be established
46 - 3: same as 2, plus encrypt all traffic to the protection
50 cdef afsconf_dir *cdir
51 cdef afsconf_cell info
53 cdef ktc_principal prin
55 cdef rx_securityClass *sc
56 cdef rx_connection *serverconns[MAXSERVERS]
59 initialize_PT_error_table()
70 raise Exception(code, "Error initializing Rx")
72 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
75 "Error opening configuration directory (%s): %s" % \
76 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
77 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
79 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
82 strncpy(prin.cell, info.name, sizeof(prin.cell))
84 strncpy(prin.name, "afs", sizeof(prin.name))
86 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
89 # No really - we wanted authentication
90 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
97 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
98 token.kvno, token.ticketLen,
102 sc = rxnull_NewClientSecurityObject()
106 memset(serverconns, 0, sizeof(serverconns))
107 for 0 <= i < info.numServers:
108 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
109 info.hostAddr[i].sin_port,
114 code = ubik_ClientInit(serverconns, &self.client)
116 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
118 code = rxs_Release(sc)
120 def __dealloc__(self):
121 ubik_ClientDestroy(self.client)
124 def NameToId(self, name):
126 Converts a user or group to an AFS ID.
130 cdef afs_int32 code, id
134 lids.idlist_val = NULL
135 lnames.namelist_len = 1
136 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
137 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
138 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
139 if lids.idlist_val is not NULL:
140 id = lids.idlist_val[0]
141 free(lids.idlist_val)
142 if id == ANONYMOUSID:
145 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
148 def IdToName(self, id):
150 Convert an AFS ID to the name of a user or group.
155 cdef char name[PR_MAXNAMELEN]
158 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
159 lids.idlist_val[0] = id
160 lnames.namelist_len = 0
161 lnames.namelist_val = NULL
162 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
163 if lnames.namelist_val is not NULL:
164 strncpy(name, lnames.namelist_val[0], sizeof(name))
165 free(lnames.namelist_val)
166 if lids.idlist_val is not NULL:
167 free(lids.idlist_val)
171 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
174 def CreateUser(self, name, id=None):
176 Create a new user in the protection database. If an ID is
177 provided, that one will be used.
181 name = name[:PR_MAXNAMELEN].lower()
187 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
189 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
192 raise Exception("Failed to create user: %s" % afs_error_message(code))
195 def CreateGroup(self, name, owner, id=None):
197 Create a new group in the protection database. If an ID is
198 provided, that one will be used.
200 cdef afs_int32 code, cid
202 name = name[:PR_MAXNAMELEN].lower()
203 oid = self.NameToId(owner)
207 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
209 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
212 raise Exception("Failed to create group: %s" % afs_error_message(code))
215 def Delete(self, id):
217 Delete the protection database entry with the provided ID.
221 code = ubik_PR_Delete(self.client, 0, id)
223 raise Exception("Failed to delete user: %s" % afs_error_message(code))