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)
26 cdef import from "afs/pterror.h":
30 void initialize_PT_error_table()
33 cdef ubik_client * client
35 def __cinit__(self, cell=None, sec=1):
37 Open a connection to the protection server. A PTS object is
38 essentially a handle to talk to the server in a given cell.
40 cell defaults to None. If no argument is passed for cell, PTS
41 connects to the home cell.
43 sec is the security level, an integer from 0 to 3:
44 - 0: unauthenticated connection
45 - 1: try authenticated, then fall back to unauthenticated
46 - 2: fail if an authenticated connection can't be established
47 - 3: same as 2, plus encrypt all traffic to the protection
51 cdef afsconf_dir *cdir
52 cdef afsconf_cell info
54 cdef ktc_principal prin
56 cdef rx_securityClass *sc
57 cdef rx_connection *serverconns[MAXSERVERS]
60 initialize_PT_error_table()
71 raise Exception(code, "Error initializing Rx")
73 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
76 "Error opening configuration directory (%s): %s" % \
77 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
78 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
80 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
83 strncpy(prin.cell, info.name, sizeof(prin.cell))
85 strncpy(prin.name, "afs", sizeof(prin.name))
87 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
90 # No really - we wanted authentication
91 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
98 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
99 token.kvno, token.ticketLen,
103 sc = rxnull_NewClientSecurityObject()
107 memset(serverconns, 0, sizeof(serverconns))
108 for 0 <= i < info.numServers:
109 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
110 info.hostAddr[i].sin_port,
115 code = ubik_ClientInit(serverconns, &self.client)
117 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
119 code = rxs_Release(sc)
121 def __dealloc__(self):
122 ubik_ClientDestroy(self.client)
125 def NameToId(self, name):
127 Converts a user or group to an AFS ID.
131 cdef afs_int32 code, id
135 lids.idlist_val = NULL
136 lnames.namelist_len = 1
137 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
138 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
139 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
140 if lids.idlist_val is not NULL:
141 id = lids.idlist_val[0]
142 free(lids.idlist_val)
143 if id == ANONYMOUSID:
146 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
149 def IdToName(self, id):
151 Convert an AFS ID to the name of a user or group.
156 cdef char name[PR_MAXNAMELEN]
159 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
160 lids.idlist_val[0] = id
161 lnames.namelist_len = 0
162 lnames.namelist_val = NULL
163 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
164 if lnames.namelist_val is not NULL:
165 strncpy(name, lnames.namelist_val[0], sizeof(name))
166 free(lnames.namelist_val)
167 if lids.idlist_val is not NULL:
168 free(lids.idlist_val)
172 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
175 def CreateUser(self, name, id=None):
177 Create a new user in the protection database. If an ID is
178 provided, that one will be used.
182 name = name[:PR_MAXNAMELEN].lower()
188 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
190 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
193 raise Exception("Failed to create user: %s" % afs_error_message(code))
196 def CreateGroup(self, name, owner, id=None):
198 Create a new group in the protection database. If an ID is
199 provided, that one will be used.
201 cdef afs_int32 code, cid
203 name = name[:PR_MAXNAMELEN].lower()
204 oid = self.NameToId(owner)
208 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
210 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
213 raise Exception("Failed to create group: %s" % afs_error_message(code))
216 def Delete(self, id):
218 Delete the protection database entry with the provided ID.
222 code = ubik_PR_Delete(self.client, 0, id)
224 raise Exception("Failed to delete user: %s" % afs_error_message(code))
226 def AddToGroup(self, uid, gid):
228 Add the user with the given ID to the group with the given ID.
232 code = ubik_PR_AddToGroup(self.client, 0, uid, gid)
234 raise Exception("Failed to add user to group: %s" % afs_error_message(code))