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 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 26 | PyDoc_STRVAR(get_default_domain__doc__, |
| 27 | "get_default_domain() -> str\n\ |
| 28 | Corresponds to the C library yp_get_default_domain() call, returning\n\ |
| 29 | the default NIS domain.\n"); |
| 30 | |
| 31 | PyDoc_STRVAR(match__doc__, |
| 32 | "match(key, map, domain = defaultdomain)\n\ |
| 33 | Corresponds to the C library yp_match() call, returning the value of\n\ |
| 34 | key in the given map. Optionally domain can be specified but it\n\ |
| 35 | defaults to the system default domain.\n"); |
| 36 | |
| 37 | PyDoc_STRVAR(cat__doc__, |
| 38 | "cat(map, domain = defaultdomain)\n\ |
| 39 | Returns the entire map as a dictionary. Optionally domain can be\n\ |
| 40 | specified but it defaults to the system default domain.\n"); |
| 41 | |
| 42 | PyDoc_STRVAR(maps__doc__, |
| 43 | "maps(domain = defaultdomain)\n\ |
| 44 | Returns an array of all available NIS maps within a domain. If domain\n\ |
| 45 | is not specified it defaults to the system default domain.\n"); |
| 46 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 47 | static PyObject *NisError; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 48 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 49 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 50 | nis_error (int err) |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 51 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 52 | PyErr_SetString(NisError, yperr_string(err)); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 56 | static struct nis_map { |
| 57 | char *alias; |
| 58 | char *map; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 59 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 60 | } aliases [] = { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 61 | {"passwd", "passwd.byname", 0}, |
| 62 | {"group", "group.byname", 0}, |
| 63 | {"networks", "networks.byaddr", 0}, |
| 64 | {"hosts", "hosts.byname", 0}, |
| 65 | {"protocols", "protocols.bynumber", 0}, |
| 66 | {"services", "services.byname", 0}, |
| 67 | {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */ |
| 68 | {"ethers", "ethers.byname", 0}, |
| 69 | {0L, 0L, 0} |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static char * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 73 | nis_mapname (char *map, int *pfix) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 75 | int i; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 77 | *pfix = 0; |
| 78 | for (i=0; aliases[i].alias != 0L; i++) { |
| 79 | if (!strcmp (aliases[i].alias, map)) { |
| 80 | *pfix = aliases[i].fix; |
| 81 | return aliases[i].map; |
| 82 | } |
| 83 | if (!strcmp (aliases[i].map, map)) { |
| 84 | *pfix = aliases[i].fix; |
| 85 | return aliases[i].map; |
| 86 | } |
| 87 | } |
| 88 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 89 | return map; |
| 90 | } |
| 91 | |
Brett Cannon | f6067ec | 2004-07-10 00:57:37 +0000 | [diff] [blame] | 92 | #ifdef __APPLE__ |
| 93 | typedef int (*foreachfunc)(unsigned long, char *, int, char *, int, void *); |
| 94 | #else |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 95 | typedef int (*foreachfunc)(int, char *, int, char *, int, char *); |
Brett Cannon | f6067ec | 2004-07-10 00:57:37 +0000 | [diff] [blame] | 96 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 98 | struct ypcallback_data { |
| 99 | PyObject *dict; |
| 100 | int fix; |
| 101 | }; |
| 102 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 103 | static int |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 104 | nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, |
| 105 | int invallen, struct ypcallback_data *indata) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 106 | { |
| 107 | if (instatus == YP_TRUE) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 108 | PyObject *key; |
| 109 | PyObject *val; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 110 | int err; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 111 | |
| 112 | if (indata->fix) { |
Neal Norwitz | 6623906 | 2002-11-04 23:21:09 +0000 | [diff] [blame] | 113 | if (inkeylen > 0 && inkey[inkeylen-1] == '\0') |
| 114 | inkeylen--; |
| 115 | if (invallen > 0 && inval[invallen-1] == '\0') |
| 116 | invallen--; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 117 | } |
Neal Norwitz | efaf629 | 2007-08-26 21:43:00 +0000 | [diff] [blame] | 118 | key = PyUnicode_FromStringAndSize(inkey, inkeylen); |
| 119 | val = PyUnicode_FromStringAndSize(inval, invallen); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 120 | if (key == NULL || val == NULL) { |
| 121 | /* XXX error -- don't know how to handle */ |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 122 | PyErr_Clear(); |
| 123 | Py_XDECREF(key); |
| 124 | Py_XDECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 127 | err = PyDict_SetItem(indata->dict, key, val); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 128 | Py_DECREF(key); |
| 129 | Py_DECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 130 | if (err != 0) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 131 | PyErr_Clear(); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 132 | return 1; |
| 133 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | return 1; |
| 137 | } |
| 138 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 139 | static PyObject * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 140 | nis_get_default_domain (PyObject *self) |
| 141 | { |
| 142 | char *domain; |
| 143 | int err; |
| 144 | PyObject *res; |
| 145 | |
| 146 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 147 | return nis_error(err); |
| 148 | |
Neal Norwitz | efaf629 | 2007-08-26 21:43:00 +0000 | [diff] [blame] | 149 | res = PyUnicode_FromStringAndSize (domain, strlen(domain)); |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 150 | return res; |
| 151 | } |
| 152 | |
| 153 | static PyObject * |
| 154 | nis_match (PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 155 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 156 | char *match; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 157 | char *domain = NULL; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 158 | int keylen, len; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 159 | char *key, *map; |
| 160 | int err; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 161 | PyObject *res; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 162 | int fix; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 163 | static char *kwlist[] = {"key", "map", "domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 164 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 165 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, |
Martin v. Löwis | d24fdbc | 2007-08-30 10:21:51 +0000 | [diff] [blame] | 166 | "s#s|s:match", kwlist, |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 167 | &key, &keylen, &map, &domain)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 168 | return NULL; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 169 | if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 170 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 171 | map = nis_mapname (map, &fix); |
| 172 | if (fix) |
| 173 | keylen++; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 174 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 175 | err = yp_match (domain, map, key, keylen, &match, &len); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 176 | Py_END_ALLOW_THREADS |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 177 | if (fix) |
| 178 | len--; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 179 | if (err != 0) |
| 180 | return nis_error(err); |
Neal Norwitz | efaf629 | 2007-08-26 21:43:00 +0000 | [diff] [blame] | 181 | res = PyUnicode_FromStringAndSize (match, len); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 182 | free (match); |
| 183 | return res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 186 | static PyObject * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 187 | nis_cat (PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 188 | { |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 189 | char *domain = NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 190 | char *map; |
| 191 | struct ypall_callback cb; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 192 | struct ypcallback_data data; |
| 193 | PyObject *dict; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 194 | int err; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 195 | static char *kwlist[] = {"map", "domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 196 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 197 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", |
| 198 | kwlist, &map, &domain)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 199 | return NULL; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 200 | if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 201 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 202 | dict = PyDict_New (); |
| 203 | if (dict == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 204 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | cb.foreach = (foreachfunc)nis_foreach; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 206 | data.dict = dict; |
| 207 | map = nis_mapname (map, &data.fix); |
| 208 | cb.data = (char *)&data; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 209 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 210 | err = yp_all (domain, map, &cb); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 211 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 212 | if (err != 0) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 213 | Py_DECREF(dict); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 214 | return nis_error(err); |
| 215 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 216 | return dict; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | /* These should be u_long on Sun h/w but not on 64-bit h/w. |
| 220 | This is not portable to machines with 16-bit ints and no prototypes */ |
| 221 | #ifndef YPPROC_MAPLIST |
| 222 | #define YPPROC_MAPLIST 11 |
| 223 | #endif |
| 224 | #ifndef YPPROG |
| 225 | #define YPPROG 100004 |
| 226 | #endif |
| 227 | #ifndef YPVERS |
| 228 | #define YPVERS 2 |
| 229 | #endif |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 230 | |
| 231 | typedef char *domainname; |
| 232 | typedef char *mapname; |
| 233 | |
| 234 | enum nisstat { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 235 | NIS_TRUE = 1, |
| 236 | NIS_NOMORE = 2, |
| 237 | NIS_FALSE = 0, |
| 238 | NIS_NOMAP = -1, |
| 239 | NIS_NODOM = -2, |
| 240 | NIS_NOKEY = -3, |
| 241 | NIS_BADOP = -4, |
| 242 | NIS_BADDB = -5, |
| 243 | NIS_YPERR = -6, |
| 244 | NIS_BADARGS = -7, |
| 245 | NIS_VERS = -8 |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 246 | }; |
| 247 | typedef enum nisstat nisstat; |
| 248 | |
| 249 | struct nismaplist { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 250 | mapname map; |
| 251 | struct nismaplist *next; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 252 | }; |
| 253 | typedef struct nismaplist nismaplist; |
| 254 | |
| 255 | struct nisresp_maplist { |
| 256 | nisstat stat; |
| 257 | nismaplist *maps; |
| 258 | }; |
| 259 | typedef struct nisresp_maplist nisresp_maplist; |
| 260 | |
| 261 | static struct timeval TIMEOUT = { 25, 0 }; |
| 262 | |
| 263 | static |
| 264 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 265 | nis_xdr_domainname(XDR *xdrs, domainname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 266 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 267 | if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { |
| 268 | return (FALSE); |
| 269 | } |
| 270 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static |
| 274 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 275 | nis_xdr_mapname(XDR *xdrs, mapname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 276 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 277 | if (!xdr_string(xdrs, objp, YPMAXMAP)) { |
| 278 | return (FALSE); |
| 279 | } |
| 280 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static |
| 284 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 285 | nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) |
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 | if (!nis_xdr_mapname(xdrs, &objp->map)) { |
| 288 | return (FALSE); |
| 289 | } |
| 290 | if (!xdr_pointer(xdrs, (char **)&objp->next, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 291 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 292 | { |
| 293 | return (FALSE); |
| 294 | } |
| 295 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static |
| 299 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 300 | nis_xdr_ypstat(XDR *xdrs, nisstat *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 301 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 302 | if (!xdr_enum(xdrs, (enum_t *)objp)) { |
| 303 | return (FALSE); |
| 304 | } |
| 305 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | static |
| 310 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 311 | nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 312 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 313 | if (!nis_xdr_ypstat(xdrs, &objp->stat)) { |
| 314 | return (FALSE); |
| 315 | } |
| 316 | if (!xdr_pointer(xdrs, (char **)&objp->maps, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 317 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 318 | { |
| 319 | return (FALSE); |
| 320 | } |
| 321 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
| 325 | static |
| 326 | nisresp_maplist * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 327 | nisproc_maplist_2(domainname *argp, CLIENT *clnt) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 328 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 329 | static nisresp_maplist res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 330 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 331 | memset(&res, 0, sizeof(res)); |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 332 | if (clnt_call(clnt, YPPROC_MAPLIST, |
| 333 | (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, |
| 334 | (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, |
| 335 | TIMEOUT) != RPC_SUCCESS) |
| 336 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 337 | return (NULL); |
| 338 | } |
| 339 | return (&res); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static |
| 343 | nismaplist * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 344 | nis_maplist (char *dom) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 345 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 346 | nisresp_maplist *list; |
Thomas Wouters | e75e6d0 | 2001-01-21 23:34:12 +0000 | [diff] [blame] | 347 | CLIENT *cl; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 348 | char *server = NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 349 | int mapi = 0; |
Barry Warsaw | 3696c52 | 1996-12-11 00:29:14 +0000 | [diff] [blame] | 350 | |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 351 | while (!server && aliases[mapi].map != 0L) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 352 | yp_master (dom, aliases[mapi].map, &server); |
| 353 | mapi++; |
| 354 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 355 | if (!server) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 356 | PyErr_SetString(NisError, "No NIS master found for any map"); |
| 357 | return NULL; |
| 358 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 359 | cl = clnt_create(server, YPPROG, YPVERS, "tcp"); |
| 360 | if (cl == NULL) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 361 | PyErr_SetString(NisError, clnt_spcreateerror(server)); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 362 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 363 | } |
| 364 | list = nisproc_maplist_2 (&dom, cl); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 365 | clnt_destroy(cl); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 366 | if (list == NULL) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 367 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 368 | if (list->stat != NIS_TRUE) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 369 | goto finally; |
| 370 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 371 | free(server); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 372 | return list->maps; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 373 | |
| 374 | finally: |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 375 | free(server); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 376 | return NULL; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 379 | static PyObject * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 380 | nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 381 | { |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 382 | char *domain = NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 383 | nismaplist *maps; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 384 | PyObject *list; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 385 | int err; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 386 | static char *kwlist[] = {"domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 387 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 388 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, |
| 389 | "|s:maps", kwlist, &domain)) |
| 390 | return NULL; |
| 391 | if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) { |
| 392 | nis_error(err); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | if ((maps = nis_maplist (domain)) == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 397 | return NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 398 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 399 | return NULL; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 400 | for (maps = maps; maps; maps = maps->next) { |
Neal Norwitz | efaf629 | 2007-08-26 21:43:00 +0000 | [diff] [blame] | 401 | PyObject *str = PyUnicode_FromString(maps->map); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 402 | if (!str || PyList_Append(list, str) < 0) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 403 | { |
| 404 | Py_DECREF(list); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 405 | list = NULL; |
| 406 | break; |
| 407 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 408 | Py_DECREF(str); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 409 | } |
| 410 | /* XXX Shouldn't we free the list of maps now? */ |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 411 | return list; |
| 412 | } |
| 413 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 414 | static PyMethodDef nis_methods[] = { |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 415 | {"match", (PyCFunction)nis_match, |
| 416 | METH_VARARGS | METH_KEYWORDS, |
| 417 | match__doc__}, |
| 418 | {"cat", (PyCFunction)nis_cat, |
| 419 | METH_VARARGS | METH_KEYWORDS, |
| 420 | cat__doc__}, |
| 421 | {"maps", (PyCFunction)nis_maps, |
| 422 | METH_VARARGS | METH_KEYWORDS, |
| 423 | maps__doc__}, |
| 424 | {"get_default_domain", (PyCFunction)nis_get_default_domain, |
| 425 | METH_NOARGS, |
| 426 | get_default_domain__doc__}, |
| 427 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 428 | }; |
| 429 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 430 | PyDoc_STRVAR(nis__doc__, |
| 431 | "This module contains functions for accessing NIS maps.\n"); |
| 432 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 433 | static struct PyModuleDef nismodule = { |
| 434 | PyModuleDef_HEAD_INIT, |
| 435 | "nis", |
| 436 | nis__doc__, |
| 437 | -1, |
| 438 | nis_methods, |
| 439 | NULL, |
| 440 | NULL, |
| 441 | NULL, |
| 442 | NULL |
| 443 | }; |
| 444 | |
| 445 | PyObject* |
| 446 | PyInit_nis (void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 447 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 448 | PyObject *m, *d; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 449 | m = PyModule_Create(&nismodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 450 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 451 | return NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 452 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 453 | NisError = PyErr_NewException("nis.error", NULL, NULL); |
| 454 | if (NisError != NULL) |
| 455 | PyDict_SetItemString(d, "error", NisError); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 456 | return m; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 457 | } |