source: trunk/debathena/debathena/python-moira/_moira.pyx @ 24442

Revision 24442, 4.6 KB checked in by broder, 14 years ago (diff)
In python-moira: * New upstream version
Line 
1cdef extern from "moira/moira.h":
2    int mr_krb5_auth(char * prog)
3    int mr_auth(char * prog)
4    int mr_connect(char * server)
5    int mr_disconnect()
6    int mr_host(char * host_buf, int buf_size)
7    int mr_motd(char ** motd)
8    int mr_noop()
9    int mr_query(char * handle, int argc, char ** argv,
10                 int (*callback)(int, char **, void *), object callarg)
11    int mr_access(char *handle, int argc, char ** argv)
12    int mr_proxy(char *principal, char *orig_authtype)
13
14    enum:
15        MR_SUCCESS
16        MR_CONT
17
18cdef extern from "com_err.h":
19    ctypedef long errcode_t
20    char * error_message(errcode_t)
21
22cdef extern from "stdlib.h":
23    ctypedef unsigned long size_t
24    void * malloc(size_t size)
25    void free(void * ptr)
26
27class MoiraException(Exception):
28    def code(self):
29        return self.args[0]
30    code = property(code)
31
32__connected = False
33
34def _error(code):
35    raise MoiraException, (code, error_message(code))
36
37def connect(server=''):
38    """
39    Establish a connection to a Moira server.
40   
41    A server may be specified, but it is optional. If specified, it
42    should be of the form hostname:portname. Portname will be looked
43    up in /etc/services if specified, but it is optional as well.
44   
45    If no server is specified, the server will be found from the
46    MOIRASERVER environment variable, Hesiod, or a compiled in default
47    (in that order).
48   
49    This function raises a MoiraException if the connection is
50    not successful.
51    """
52    global __connected
53   
54    if __connected:
55        disconnect()
56   
57    status = mr_connect(server)
58    if status != MR_SUCCESS:
59        _error(status)
60    else:
61        __connected = True
62
63def disconnect():
64    """
65    Disconnect from the active Moira server
66    """
67    global __connected
68    if __connected:
69        mr_disconnect()
70    __connected = False
71
72def auth(program, krb4=False):
73    """
74    Authenticate to the Moira server with Kerberos tickets. If krb4 is
75    True, then Kerberos version 4 will be used. Otherwise, Kerberos
76    version 5 is used.
77   
78    The program argument identifies the connecting program to the
79    Moira server. This is used for setting the modwith field when
80    modifications are made.
81   
82    Note that the use of Kerberos version 4 is deprecated and highly
83    discouraged
84    """
85    if krb4:
86        status = mr_auth(program)
87    else:
88        status = mr_krb5_auth(program)
89    if status != MR_SUCCESS:
90        _error(status)
91
92def host():
93    """
94    Return the name of the host the client is connected to.
95    """
96    cdef char buffer[512]
97    status = mr_host(buffer, 512)
98    if status != MR_SUCCESS:
99        _error(status)
100    return buffer
101
102def motd():
103    """
104    Retrieve the current message of the day from the server.
105    """
106    cdef char * motd
107    status = mr_motd(&motd)
108    if status != MR_SUCCESS:
109        _error(status)
110    if motd != NULL:
111        return motd
112
113def noop():
114    """
115    Does "no operation" to the server, just making sure it's still
116    there
117    """
118    status = mr_noop()
119    if status:
120        _error(status)
121
122def _access(handle, *args):
123    """
124    Verifies that the authenticated user has the access to perform the
125    given query.
126    """
127    cdef int argc, i
128    argc = len(args)
129    cdef char ** argv
130    argv = <char **>malloc(argc * sizeof(char *))
131
132    if argv != NULL:
133        for i in xrange(argc):
134            argv[i] = args[i]
135
136        status = mr_access(handle, argc, argv)
137        free(argv)
138
139        if status:
140            _error(status)
141
142def _query(handle, callback, *args):
143    cdef int argc, i
144    argc = len(args)
145    cdef char ** argv
146    argv = <char **>malloc(argc * sizeof(char *))
147   
148    if argv != NULL:
149        for i in xrange(argc):
150            argv[i] = args[i]
151       
152        status = mr_query(handle, argc, argv, _call_python_callback, callback)
153        free(argv)
154       
155        if status:
156            _error(status)
157
158def proxy(principal, orig_authtype):
159    """
160    Authenticate as a proxy for another principal.
161
162    For those with sufficient privilege, proxy allows an authenticated
163    user to impersonate another.
164
165    The principal argument contains the Kerberos principal for which
166    this user is proxying, and orig_authtype is the mechanism by which
167    the proxied user originally authenticated to the proxier.
168    """
169    status = mr_proxy(principal, orig_authtype)
170    if status != MR_SUCCESS:
171        _error(status)
172
173cdef int _call_python_callback(int argc, char ** argv, void * hint):
174    cdef object callback
175    callback = <object>hint
176    result = []
177    cdef int i
178    for i in xrange(argc):
179        result.append(argv[i])
180    callback(tuple(result))
181    return MR_CONT
Note: See TracBrowser for help on using the repository browser.