Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Written by: |
| 3 | Fred Gansevles <Fred.Gansevles@cs.utwente.nl> |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 4 | B&O group, |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 5 | Faculteit der Informatica, |
| 6 | Universiteit Twente, |
| 7 | Enschede, |
| 8 | the Netherlands. |
| 9 | ******************************************************************/ |
| 10 | |
| 11 | /* NIS module implementation */ |
| 12 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <rpc/rpc.h> |
| 18 | #include <rpcsvc/yp_prot.h> |
Guido van Rossum | 8a170cb | 1996-08-08 19:11:41 +0000 | [diff] [blame] | 19 | #include <rpcsvc/ypclnt.h> |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 259552d | 1996-12-09 18:46:28 +0000 | [diff] [blame] | 21 | #ifdef __sgi |
| 22 | /* This is missing from rpcsvc/ypclnt.h */ |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 23 | extern int yp_get_default_domain(char **); |
Guido van Rossum | 259552d | 1996-12-09 18:46:28 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 26 | static PyObject *NisError; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 27 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 28 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 29 | nis_error (int err) |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 30 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 31 | PyErr_SetString(NisError, yperr_string(err)); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 32 | return NULL; |
| 33 | } |
| 34 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 35 | static struct nis_map { |
| 36 | char *alias; |
| 37 | char *map; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 38 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 39 | } aliases [] = { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 40 | {"passwd", "passwd.byname", 0}, |
| 41 | {"group", "group.byname", 0}, |
| 42 | {"networks", "networks.byaddr", 0}, |
| 43 | {"hosts", "hosts.byname", 0}, |
| 44 | {"protocols", "protocols.bynumber", 0}, |
| 45 | {"services", "services.byname", 0}, |
| 46 | {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */ |
| 47 | {"ethers", "ethers.byname", 0}, |
| 48 | {0L, 0L, 0} |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static char * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 52 | nis_mapname (char *map, int *pfix) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 54 | int i; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 56 | *pfix = 0; |
| 57 | for (i=0; aliases[i].alias != 0L; i++) { |
| 58 | if (!strcmp (aliases[i].alias, map)) { |
| 59 | *pfix = aliases[i].fix; |
| 60 | return aliases[i].map; |
| 61 | } |
| 62 | if (!strcmp (aliases[i].map, map)) { |
| 63 | *pfix = aliases[i].fix; |
| 64 | return aliases[i].map; |
| 65 | } |
| 66 | } |
| 67 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 68 | return map; |
| 69 | } |
| 70 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 71 | typedef int (*foreachfunc)(int, char *, int, char *, int, char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 73 | struct ypcallback_data { |
| 74 | PyObject *dict; |
| 75 | int fix; |
| 76 | }; |
| 77 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 78 | static int |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 79 | nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, |
| 80 | int invallen, struct ypcallback_data *indata) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 81 | { |
| 82 | if (instatus == YP_TRUE) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 83 | PyObject *key; |
| 84 | PyObject *val; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 85 | int err; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 86 | |
| 87 | if (indata->fix) { |
Neal Norwitz | 6623906 | 2002-11-04 23:21:09 +0000 | [diff] [blame] | 88 | if (inkeylen > 0 && inkey[inkeylen-1] == '\0') |
| 89 | inkeylen--; |
| 90 | if (invallen > 0 && inval[invallen-1] == '\0') |
| 91 | invallen--; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 92 | } |
| 93 | key = PyString_FromStringAndSize(inkey, inkeylen); |
| 94 | val = PyString_FromStringAndSize(inval, invallen); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 95 | if (key == NULL || val == NULL) { |
| 96 | /* XXX error -- don't know how to handle */ |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 97 | PyErr_Clear(); |
| 98 | Py_XDECREF(key); |
| 99 | Py_XDECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 100 | return 1; |
| 101 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 102 | err = PyDict_SetItem(indata->dict, key, val); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 103 | Py_DECREF(key); |
| 104 | Py_DECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 105 | if (err != 0) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 106 | PyErr_Clear(); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 107 | return 1; |
| 108 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | return 1; |
| 112 | } |
| 113 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 114 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 115 | nis_match (PyObject *self, PyObject *args) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 116 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 117 | char *match; |
| 118 | char *domain; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 119 | int keylen, len; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 120 | char *key, *map; |
| 121 | int err; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 122 | PyObject *res; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 123 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 124 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 125 | if (!PyArg_ParseTuple(args, "t#s:match", &key, &keylen, &map)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 126 | return NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 127 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 128 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 129 | map = nis_mapname (map, &fix); |
| 130 | if (fix) |
| 131 | keylen++; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 132 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 133 | err = yp_match (domain, map, key, keylen, &match, &len); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 134 | Py_END_ALLOW_THREADS |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 135 | if (fix) |
| 136 | len--; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 137 | if (err != 0) |
| 138 | return nis_error(err); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 139 | res = PyString_FromStringAndSize (match, len); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 140 | free (match); |
| 141 | return res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 144 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 145 | nis_cat (PyObject *self, PyObject *args) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 147 | char *domain; |
| 148 | char *map; |
| 149 | struct ypall_callback cb; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 150 | struct ypcallback_data data; |
| 151 | PyObject *dict; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 152 | int err; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 153 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 154 | if (!PyArg_ParseTuple(args, "s:cat", &map)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 155 | return NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 156 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 157 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 158 | dict = PyDict_New (); |
| 159 | if (dict == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 160 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 161 | cb.foreach = (foreachfunc)nis_foreach; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 162 | data.dict = dict; |
| 163 | map = nis_mapname (map, &data.fix); |
| 164 | cb.data = (char *)&data; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 165 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 166 | err = yp_all (domain, map, &cb); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 167 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 168 | if (err != 0) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 169 | Py_DECREF(dict); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 170 | return nis_error(err); |
| 171 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 172 | return dict; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | /* These should be u_long on Sun h/w but not on 64-bit h/w. |
| 176 | This is not portable to machines with 16-bit ints and no prototypes */ |
| 177 | #ifndef YPPROC_MAPLIST |
| 178 | #define YPPROC_MAPLIST 11 |
| 179 | #endif |
| 180 | #ifndef YPPROG |
| 181 | #define YPPROG 100004 |
| 182 | #endif |
| 183 | #ifndef YPVERS |
| 184 | #define YPVERS 2 |
| 185 | #endif |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 186 | |
| 187 | typedef char *domainname; |
| 188 | typedef char *mapname; |
| 189 | |
| 190 | enum nisstat { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 191 | NIS_TRUE = 1, |
| 192 | NIS_NOMORE = 2, |
| 193 | NIS_FALSE = 0, |
| 194 | NIS_NOMAP = -1, |
| 195 | NIS_NODOM = -2, |
| 196 | NIS_NOKEY = -3, |
| 197 | NIS_BADOP = -4, |
| 198 | NIS_BADDB = -5, |
| 199 | NIS_YPERR = -6, |
| 200 | NIS_BADARGS = -7, |
| 201 | NIS_VERS = -8 |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 202 | }; |
| 203 | typedef enum nisstat nisstat; |
| 204 | |
| 205 | struct nismaplist { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 206 | mapname map; |
| 207 | struct nismaplist *next; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 208 | }; |
| 209 | typedef struct nismaplist nismaplist; |
| 210 | |
| 211 | struct nisresp_maplist { |
| 212 | nisstat stat; |
| 213 | nismaplist *maps; |
| 214 | }; |
| 215 | typedef struct nisresp_maplist nisresp_maplist; |
| 216 | |
| 217 | static struct timeval TIMEOUT = { 25, 0 }; |
| 218 | |
| 219 | static |
| 220 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 221 | nis_xdr_domainname(XDR *xdrs, domainname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 222 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 223 | if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { |
| 224 | return (FALSE); |
| 225 | } |
| 226 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static |
| 230 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 231 | nis_xdr_mapname(XDR *xdrs, mapname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 232 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 233 | if (!xdr_string(xdrs, objp, YPMAXMAP)) { |
| 234 | return (FALSE); |
| 235 | } |
| 236 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static |
| 240 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 241 | nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 242 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 243 | if (!nis_xdr_mapname(xdrs, &objp->map)) { |
| 244 | return (FALSE); |
| 245 | } |
| 246 | if (!xdr_pointer(xdrs, (char **)&objp->next, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 247 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 248 | { |
| 249 | return (FALSE); |
| 250 | } |
| 251 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static |
| 255 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 256 | nis_xdr_ypstat(XDR *xdrs, nisstat *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 257 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 258 | if (!xdr_enum(xdrs, (enum_t *)objp)) { |
| 259 | return (FALSE); |
| 260 | } |
| 261 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | static |
| 266 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 267 | nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 268 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 269 | if (!nis_xdr_ypstat(xdrs, &objp->stat)) { |
| 270 | return (FALSE); |
| 271 | } |
| 272 | if (!xdr_pointer(xdrs, (char **)&objp->maps, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 273 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 274 | { |
| 275 | return (FALSE); |
| 276 | } |
| 277 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | static |
| 282 | nisresp_maplist * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 283 | nisproc_maplist_2(domainname *argp, CLIENT *clnt) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 284 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 285 | static nisresp_maplist res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 286 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 287 | memset(&res, 0, sizeof(res)); |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 288 | if (clnt_call(clnt, YPPROC_MAPLIST, |
| 289 | (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, |
| 290 | (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, |
| 291 | TIMEOUT) != RPC_SUCCESS) |
| 292 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 293 | return (NULL); |
| 294 | } |
| 295 | return (&res); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static |
| 299 | nismaplist * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 300 | nis_maplist (void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 301 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 302 | nisresp_maplist *list; |
| 303 | char *dom; |
Thomas Wouters | e75e6d0 | 2001-01-21 23:34:12 +0000 | [diff] [blame] | 304 | CLIENT *cl; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 305 | char *server = NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 306 | int mapi = 0; |
Barry Warsaw | 1abda0f | 1996-12-11 01:00:46 +0000 | [diff] [blame] | 307 | int err; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 308 | |
Barry Warsaw | 1abda0f | 1996-12-11 01:00:46 +0000 | [diff] [blame] | 309 | if ((err = yp_get_default_domain (&dom)) != 0) { |
| 310 | nis_error(err); |
| 311 | return NULL; |
| 312 | } |
Barry Warsaw | 3696c52 | 1996-12-11 00:29:14 +0000 | [diff] [blame] | 313 | |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 314 | while (!server && aliases[mapi].map != 0L) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 315 | yp_master (dom, aliases[mapi].map, &server); |
| 316 | mapi++; |
| 317 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 318 | if (!server) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 319 | PyErr_SetString(NisError, "No NIS master found for any map"); |
| 320 | return NULL; |
| 321 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 322 | cl = clnt_create(server, YPPROG, YPVERS, "tcp"); |
| 323 | if (cl == NULL) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 324 | PyErr_SetString(NisError, clnt_spcreateerror(server)); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 325 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 326 | } |
| 327 | list = nisproc_maplist_2 (&dom, cl); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 328 | clnt_destroy(cl); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 329 | if (list == NULL) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 330 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 331 | if (list->stat != NIS_TRUE) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 332 | goto finally; |
| 333 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 334 | free(server); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 335 | return list->maps; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 336 | |
| 337 | finally: |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 338 | free(server); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 339 | return NULL; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 342 | static PyObject * |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 343 | nis_maps (PyObject *self) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 344 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 345 | nismaplist *maps; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 346 | PyObject *list; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 347 | |
| 348 | if ((maps = nis_maplist ()) == NULL) |
| 349 | return NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 350 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 351 | return NULL; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 352 | for (maps = maps; maps; maps = maps->next) { |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 353 | PyObject *str = PyString_FromString(maps->map); |
| 354 | if (!str || PyList_Append(list, str) < 0) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 355 | { |
| 356 | Py_DECREF(list); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 357 | list = NULL; |
| 358 | break; |
| 359 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 360 | Py_DECREF(str); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 361 | } |
| 362 | /* XXX Shouldn't we free the list of maps now? */ |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 363 | return list; |
| 364 | } |
| 365 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 366 | static PyMethodDef nis_methods[] = { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 367 | {"match", nis_match, METH_VARARGS}, |
| 368 | {"cat", nis_cat, METH_VARARGS}, |
| 369 | {"maps", (PyCFunction)nis_maps, METH_NOARGS}, |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 370 | {NULL, NULL} /* Sentinel */ |
| 371 | }; |
| 372 | |
| 373 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 374 | initnis (void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 375 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 376 | PyObject *m, *d; |
| 377 | m = Py_InitModule("nis", nis_methods); |
| 378 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 379 | NisError = PyErr_NewException("nis.error", NULL, NULL); |
| 380 | if (NisError != NULL) |
| 381 | PyDict_SetItemString(d, "error", NisError); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 382 | } |