3 cdef import from "afs/ptuser.h":
13 ctypedef char prname[PR_MAXNAMELEN]
16 unsigned int namelist_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)
30 cdef ubik_client * client
32 def __cinit__(self, cell=None, sec=1):
34 Open a connection to the protection server. A PTS object is
35 essentially a handle to talk to the server in a given cell.
37 cell defaults to None. If no argument is passed for cell, PTS
38 connects to the home cell.
40 sec is the security level, an integer from 0 to 3:
41 - 0: unauthenticated connection
42 - 1: try authenticated, then fall back to unauthenticated
43 - 2: fail if an authenticated connection can't be established
44 - 3: same as 2, plus encrypt all traffic to the protection
48 cdef afsconf_dir *cdir
49 cdef afsconf_cell info
51 cdef ktc_principal prin
53 cdef rx_securityClass *sc
54 cdef rx_connection *serverconns[MAXSERVERS]
66 raise Exception(code, "Error initializing Rx")
68 cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
71 "Error opening configuration directory (%s): %s" % \
72 (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
73 code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
75 raise Exception(code, "GetCellInfo: %s" % afs_error_message(code))
78 strncpy(prin.cell, info.name, sizeof(prin.cell))
80 strncpy(prin.name, "afs", sizeof(prin.name))
82 code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
85 # No really - we wanted authentication
86 raise Exception(code, "Failed to get token for service AFS: %s" % afs_error_message(code))
93 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
94 token.kvno, token.ticketLen,
98 sc = rxnull_NewClientSecurityObject()
102 memset(serverconns, 0, sizeof(serverconns))
103 for 0 <= i < info.numServers:
104 serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
105 info.hostAddr[i].sin_port,
110 code = ubik_ClientInit(serverconns, &self.client)
112 raise Exception("Failed to initialize ubik connection to Protection server: %s" % afs_error_message(code))
114 code = rxs_Release(sc)
116 def __dealloc__(self):
117 ubik_ClientDestroy(self.client)
120 def NameToId(self, name):
122 Converts a user or group to an AFS ID.
126 cdef afs_int32 code, id
130 lids.idlist_val = NULL
131 lnames.namelist_len = 1
132 lnames.namelist_val = <prname *>malloc(PR_MAXNAMELEN)
133 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN)
134 code = ubik_PR_NameToID(self.client, 0, &lnames, &lids)
135 if lids.idlist_val is not NULL:
136 id = lids.idlist_val[0]
137 free(lids.idlist_val)
139 raise Exception("Failed to lookup PTS name: %s" % afs_error_message(code))
142 def IdToName(self, id):
144 Convert an AFS ID to the name of a user or group.
149 cdef char name[PR_MAXNAMELEN]
152 lids.idlist_val = <afs_int32 *>malloc(sizeof(afs_int32))
153 lids.idlist_val[0] = id
154 lnames.namelist_len = 0
155 lnames.namelist_val = NULL
156 code = ubik_PR_IDToName(self.client, 0, &lids, &lnames)
157 if lnames.namelist_val is not NULL:
158 strncpy(name, lnames.namelist_val[0], sizeof(name))
159 free(lnames.namelist_val)
160 if lids.idlist_val is not NULL:
161 free(lids.idlist_val)
163 raise Exception("Failed to lookup PTS ID: %s" % afs_error_message(code))
166 def CreateUser(self, name, id=None):
168 Create a new user in the protection database. If an ID is
169 provided, that one will be used.
173 name = name[:PR_MAXNAMELEN].lower()
179 code = ubik_PR_INewEntry(self.client, 0, name, cid, 0)
181 code = ubik_PR_NewEntry(self.client, 0, name, 0, 0, &cid)
184 raise Exception("Failed to create user: %s" % afs_error_message(code))
187 def CreateGroup(self, name, owner, id=None):
189 Create a new group in the protection database. If an ID is
190 provided, that one will be used.
192 cdef afs_int32 code, cid
194 name = name[:PR_MAXNAMELEN].lower()
195 oid = self.NameToId(owner)
196 if oid == ANONYMOUSID:
197 raise Exception("Error creating group: owner does not exist")
201 code = ubik_PR_INewEntry(self.client, 0, name, cid, oid)
203 code = ubik_PR_NewEntry(self.client, 0, name, PRGRP, oid, &cid)
206 raise Exception("Failed to create group: %s" % afs_error_message(code))
209 def Delete(self, id):
211 Delete the protection database entry with the provided ID.
215 code = ubik_PR_Delete(self.client, 0, id)
217 raise Exception("Failed to delete user: %s" % afs_error_message(code))