Setup the rx connections for the Ubik client.
[invirt/packages/python-afs.git] / afs / _pts.pyx
1 from afs cimport *
2
3 cdef class PTS:
4     cdef ubik_client * client
5     
6     def __cinit__(self, cell=None, sec=1):
7         cdef afs_int32 code
8         cdef afsconf_dir *cdir
9         cdef afsconf_cell info
10         cdef char * c_cell
11         cdef ktc_principal prin
12         cdef ktc_token token
13         cdef rx_securityClass *sc
14         cdef rx_connection *serverconns[MAXSERVERS]
15         cdef int i
16         
17         if cell is None:
18             c_cell = NULL
19         else:
20             c_cell = cell
21         
22         self.client = NULL
23         
24         code = rx_Init(0)
25         if code != 0:
26             raise Exception(code, "Error initializing Rx")
27         
28         cdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)
29         if cdir is NULL:
30             raise OSError(errno,
31                           "Error opening configuration directory (%s): %s" % \
32                               (AFSDIR_CLIENT_ETC_DIRPATH, strerror(errno)))
33         code = afsconf_GetCellInfo(cdir, c_cell, "afsprot", &info)
34         if code != 0:
35             raise Exception(code, "GetCellInfo: %s" % error_message(code))
36         
37         if sec > 0:
38             strncpy(prin.cell, info.name, sizeof(prin.cell))
39             prin.instance[0] = 0
40             strncpy(prin.name, "afs", sizeof(prin.name))
41             
42             code = ktc_GetToken(&prin, &token, sizeof(token), NULL);
43             if code != 0:
44                 if sec >= 2:
45                     # No really - we wanted authentication
46                     raise Exception(code, "Failed to get token for service AFS: %s" % error_message(code))
47                 sec = 0
48             else:
49                 if sec == 3:
50                     level = rxkad_crypt
51                 else:
52                     level = rxkad_clear
53                 sc = rxkad_NewClientSecurityObject(level, &token.sessionKey,
54                                                    token.kvno, token.ticketLen,
55                                                    token.ticket)
56         
57         if sec == 0:
58             sc = rxnull_NewClientSecurityObject()
59         else:
60             sec = 2
61         
62         memset(serverconns, 0, sizeof(serverconns))
63         for 0 <= i < info.numServers:
64             serverconns[i] = rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
65                                               info.hostAddr[i].sin_port,
66                                               PRSRV,
67                                               sc,
68                                               sec)
69         
70         code = rxs_Release(sc)
71     
72     def __dealloc__(self):
73         rx_Finalize()