[24605] | 1 | cdef extern from "moira/mrclient.h": |
---|
| 2 | int mrcl_connect(char * server, char * client, int version, int auth) |
---|
| 3 | char * mrcl_krb_user() |
---|
| 4 | char * c_partial_canonicalize_hostname "partial_canonicalize_hostname" (char * s) |
---|
| 5 | char * mrcl_get_message() |
---|
| 6 | void mrcl_com_err(char * whoami) |
---|
| 7 | int mrcl_validate_pobox_smtp(char * user, char * address, char ** ret) |
---|
| 8 | |
---|
| 9 | enum: |
---|
| 10 | MRCL_SUCCESS |
---|
| 11 | MRCL_FAIL |
---|
| 12 | MRCL_REJECT |
---|
| 13 | MRCL_WARN |
---|
| 14 | MRCL_ENOMEM |
---|
| 15 | MRCL_MOIRA_ERROR |
---|
| 16 | MRCL_AUTH_ERROR |
---|
| 17 | |
---|
| 18 | cdef extern from "stdlib.h": |
---|
| 19 | void free(void * ptr) |
---|
| 20 | |
---|
| 21 | cdef extern from "string.h": |
---|
| 22 | char * strdup(char * s) |
---|
| 23 | |
---|
| 24 | cdef public char * whoami |
---|
| 25 | |
---|
| 26 | class MoiraClientException(Exception): |
---|
| 27 | def code(self): |
---|
| 28 | return self.args[0] |
---|
| 29 | code = property(code) |
---|
| 30 | |
---|
| 31 | def _error(code, text=None): |
---|
| 32 | if code == MRCL_ENOMEM: |
---|
| 33 | text = "Out of memory" |
---|
| 34 | if code == MRCL_MOIRA_ERROR: |
---|
| 35 | text = "Moira Error" |
---|
| 36 | if code == MRCL_AUTH_ERROR: |
---|
| 37 | text = "Authentication error" |
---|
| 38 | if mrcl_get_message(): |
---|
| 39 | if text == None: |
---|
| 40 | text = mrcl_get_message() |
---|
| 41 | else: |
---|
| 42 | text = "%s (%s)" % (text, mrcl_get_message()) |
---|
| 43 | raise MoiraClientException(code, text) |
---|
| 44 | |
---|
| 45 | def connect(server='', client='mrclient.py', version=-1, auth=1): |
---|
| 46 | status = mrcl_connect(server, client, version, auth) |
---|
| 47 | if status != MRCL_SUCCESS: |
---|
| 48 | _error(status) |
---|
| 49 | whoami = client |
---|
| 50 | |
---|
| 51 | def krb_user(): |
---|
| 52 | cdef char * c_krb_user |
---|
| 53 | cdef object py_krb_user |
---|
| 54 | c_krb_user = mrcl_krb_user() |
---|
| 55 | if c_krb_user == NULL: |
---|
| 56 | _error(-1, "Could not retrieve principal name") |
---|
| 57 | py_krb_user = c_krb_user |
---|
| 58 | free(c_krb_user) |
---|
| 59 | return py_krb_user |
---|
| 60 | |
---|
| 61 | def validate_pobox_smtp(user, address): |
---|
| 62 | cdef char * c_retval |
---|
| 63 | cdef object py_retval |
---|
| 64 | py_retval = address |
---|
| 65 | status = mrcl_validate_pobox_smtp(user, address, &c_retval) |
---|
| 66 | if status != MRCL_SUCCESS: |
---|
| 67 | _error(status) |
---|
| 68 | if c_retval != NULL: |
---|
| 69 | py_retval = c_retval |
---|
| 70 | free(c_retval) |
---|
| 71 | return py_retval |
---|
| 72 | |
---|
| 73 | def partial_canonicalize_hostname(host): |
---|
| 74 | cdef object py_hostname |
---|
| 75 | cdef char * c_hostname |
---|
| 76 | c_hostname = c_partial_canonicalize_hostname(strdup(host)) |
---|
| 77 | py_hostname = c_hostname |
---|
| 78 | free(c_hostname) |
---|
| 79 | return py_hostname |
---|
| 80 | |
---|