Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Written by: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3 | Fred Gansevles <Fred.Gansevles@cs.utwente.nl> |
| 4 | B&O group, |
| 5 | Faculteit der Informatica, |
| 6 | Universiteit Twente, |
| 7 | Enschede, |
| 8 | the Netherlands. |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | PyDoc_STRVAR(get_default_domain__doc__, |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | PyErr_SetString(NisError, yperr_string(err)); |
| 53 | return NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 56 | static struct nis_map { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | char *alias; |
| 58 | char *map; |
| 59 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 60 | } aliases [] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | int i; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | *pfix = 0; |
| 78 | for (i=0; aliases[i].alias != 0L; i++) { |
Raymond Hettinger | 9f4b860 | 2015-07-24 00:43:44 -0400 | [diff] [blame] | 79 | if (!strcmp (aliases[i].alias, map) || !strcmp (aliases[i].map, map)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | *pfix = aliases[i].fix; |
| 81 | return aliases[i].map; |
| 82 | } |
| 83 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | return map; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Amaury Forgeot d'Arc | 783f86e | 2009-07-07 06:51:26 +0000 | [diff] [blame] | 88 | #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) |
Brett Cannon | f6067ec | 2004-07-10 00:57:37 +0000 | [diff] [blame] | 89 | typedef int (*foreachfunc)(unsigned long, char *, int, char *, int, void *); |
| 90 | #else |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 91 | typedef int (*foreachfunc)(int, char *, int, char *, int, char *); |
Brett Cannon | f6067ec | 2004-07-10 00:57:37 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 94 | struct ypcallback_data { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | PyObject *dict; |
| 96 | int fix; |
| 97 | PyThreadState *state; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 100 | static int |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 101 | nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, |
| 102 | int invallen, struct ypcallback_data *indata) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | if (instatus == YP_TRUE) { |
| 105 | PyObject *key; |
| 106 | PyObject *val; |
| 107 | int err; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyEval_RestoreThread(indata->state); |
| 110 | if (indata->fix) { |
| 111 | if (inkeylen > 0 && inkey[inkeylen-1] == '\0') |
| 112 | inkeylen--; |
| 113 | if (invallen > 0 && inval[invallen-1] == '\0') |
| 114 | invallen--; |
| 115 | } |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 116 | key = PyUnicode_DecodeFSDefaultAndSize(inkey, inkeylen); |
| 117 | val = PyUnicode_DecodeFSDefaultAndSize(inval, invallen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | if (key == NULL || val == NULL) { |
| 119 | /* XXX error -- don't know how to handle */ |
| 120 | PyErr_Clear(); |
| 121 | Py_XDECREF(key); |
| 122 | Py_XDECREF(val); |
Martin v. Löwis | dfaf9ec | 2010-08-18 16:12:23 +0000 | [diff] [blame] | 123 | indata->state = PyEval_SaveThread(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | return 1; |
| 125 | } |
| 126 | err = PyDict_SetItem(indata->dict, key, val); |
| 127 | Py_DECREF(key); |
| 128 | Py_DECREF(val); |
| 129 | if (err != 0) |
| 130 | PyErr_Clear(); |
| 131 | indata->state = PyEval_SaveThread(); |
| 132 | if (err != 0) |
| 133 | return 1; |
| 134 | return 0; |
| 135 | } |
| 136 | return 1; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | char *domain; |
| 143 | int err; |
| 144 | PyObject *res; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 147 | return nis_error(err); |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | res = PyUnicode_FromStringAndSize (domain, strlen(domain)); |
| 150 | return res; |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | char *match; |
| 157 | char *domain = NULL; |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 158 | Py_ssize_t keylen; |
| 159 | int len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | char *key, *map; |
| 161 | int err; |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 162 | PyObject *ukey, *bkey, *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | int fix; |
| 164 | static char *kwlist[] = {"key", "map", "domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 167 | "Us|s:match", kwlist, |
| 168 | &ukey, &map, &domain)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | return NULL; |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 170 | if ((bkey = PyUnicode_EncodeFSDefault(ukey)) == NULL) |
| 171 | return NULL; |
| 172 | if (PyBytes_AsStringAndSize(bkey, &key, &keylen) == -1) { |
| 173 | Py_DECREF(bkey); |
| 174 | return NULL; |
| 175 | } |
| 176 | if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) { |
| 177 | Py_DECREF(bkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | return nis_error(err); |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 179 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | map = nis_mapname (map, &fix); |
| 181 | if (fix) |
| 182 | keylen++; |
| 183 | Py_BEGIN_ALLOW_THREADS |
| 184 | err = yp_match (domain, map, key, keylen, &match, &len); |
| 185 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 186 | Py_DECREF(bkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | if (fix) |
| 188 | len--; |
| 189 | if (err != 0) |
| 190 | return nis_error(err); |
Martin v. Löwis | 5ea823c | 2010-08-19 09:11:51 +0000 | [diff] [blame] | 191 | res = PyUnicode_DecodeFSDefaultAndSize(match, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | free (match); |
| 193 | return res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 196 | static PyObject * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 197 | nis_cat (PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | char *domain = NULL; |
| 200 | char *map; |
| 201 | struct ypall_callback cb; |
| 202 | struct ypcallback_data data; |
| 203 | PyObject *dict; |
| 204 | int err; |
| 205 | static char *kwlist[] = {"map", "domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", |
| 208 | kwlist, &map, &domain)) |
| 209 | return NULL; |
| 210 | if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) |
| 211 | return nis_error(err); |
| 212 | dict = PyDict_New (); |
| 213 | if (dict == NULL) |
| 214 | return NULL; |
| 215 | cb.foreach = (foreachfunc)nis_foreach; |
| 216 | data.dict = dict; |
| 217 | map = nis_mapname (map, &data.fix); |
| 218 | cb.data = (char *)&data; |
| 219 | data.state = PyEval_SaveThread(); |
| 220 | err = yp_all (domain, map, &cb); |
| 221 | PyEval_RestoreThread(data.state); |
| 222 | if (err != 0) { |
| 223 | Py_DECREF(dict); |
| 224 | return nis_error(err); |
| 225 | } |
| 226 | return dict; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | /* These should be u_long on Sun h/w but not on 64-bit h/w. |
| 230 | This is not portable to machines with 16-bit ints and no prototypes */ |
| 231 | #ifndef YPPROC_MAPLIST |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | #define YPPROC_MAPLIST 11 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | #endif |
| 234 | #ifndef YPPROG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | #define YPPROG 100004 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | #endif |
| 237 | #ifndef YPVERS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | #define YPVERS 2 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 239 | #endif |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 240 | |
| 241 | typedef char *domainname; |
| 242 | typedef char *mapname; |
| 243 | |
| 244 | enum nisstat { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | NIS_TRUE = 1, |
| 246 | NIS_NOMORE = 2, |
| 247 | NIS_FALSE = 0, |
| 248 | NIS_NOMAP = -1, |
| 249 | NIS_NODOM = -2, |
| 250 | NIS_NOKEY = -3, |
| 251 | NIS_BADOP = -4, |
| 252 | NIS_BADDB = -5, |
| 253 | NIS_YPERR = -6, |
| 254 | NIS_BADARGS = -7, |
| 255 | NIS_VERS = -8 |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 256 | }; |
| 257 | typedef enum nisstat nisstat; |
| 258 | |
| 259 | struct nismaplist { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | mapname map; |
| 261 | struct nismaplist *next; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 262 | }; |
| 263 | typedef struct nismaplist nismaplist; |
| 264 | |
| 265 | struct nisresp_maplist { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | nisstat stat; |
| 267 | nismaplist *maps; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 268 | }; |
| 269 | typedef struct nisresp_maplist nisresp_maplist; |
| 270 | |
| 271 | static struct timeval TIMEOUT = { 25, 0 }; |
| 272 | |
| 273 | static |
| 274 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 275 | nis_xdr_domainname(XDR *xdrs, domainname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { |
| 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_mapname(XDR *xdrs, mapname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | if (!xdr_string(xdrs, objp, YPMAXMAP)) { |
| 288 | return (FALSE); |
| 289 | } |
| 290 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static |
| 294 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 295 | nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | if (!nis_xdr_mapname(xdrs, &objp->map)) { |
| 298 | return (FALSE); |
| 299 | } |
| 300 | if (!xdr_pointer(xdrs, (char **)&objp->next, |
| 301 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
| 302 | { |
| 303 | return (FALSE); |
| 304 | } |
| 305 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static |
| 309 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 310 | nis_xdr_ypstat(XDR *xdrs, nisstat *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (!xdr_enum(xdrs, (enum_t *)objp)) { |
| 313 | return (FALSE); |
| 314 | } |
| 315 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | static |
| 320 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 321 | nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | if (!nis_xdr_ypstat(xdrs, &objp->stat)) { |
| 324 | return (FALSE); |
| 325 | } |
| 326 | if (!xdr_pointer(xdrs, (char **)&objp->maps, |
| 327 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
| 328 | { |
| 329 | return (FALSE); |
| 330 | } |
| 331 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | static |
| 336 | nisresp_maplist * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 337 | nisproc_maplist_2(domainname *argp, CLIENT *clnt) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | static nisresp_maplist res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | memset(&res, 0, sizeof(res)); |
| 342 | if (clnt_call(clnt, YPPROC_MAPLIST, |
| 343 | (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, |
| 344 | (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, |
| 345 | TIMEOUT) != RPC_SUCCESS) |
| 346 | { |
| 347 | return (NULL); |
| 348 | } |
| 349 | return (&res); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static |
| 353 | nismaplist * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 354 | nis_maplist (char *dom) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | nisresp_maplist *list; |
| 357 | CLIENT *cl; |
| 358 | char *server = NULL; |
| 359 | int mapi = 0; |
Barry Warsaw | 3696c52 | 1996-12-11 00:29:14 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | while (!server && aliases[mapi].map != 0L) { |
| 362 | yp_master (dom, aliases[mapi].map, &server); |
| 363 | mapi++; |
| 364 | } |
| 365 | if (!server) { |
| 366 | PyErr_SetString(NisError, "No NIS master found for any map"); |
| 367 | return NULL; |
| 368 | } |
| 369 | cl = clnt_create(server, YPPROG, YPVERS, "tcp"); |
| 370 | if (cl == NULL) { |
| 371 | PyErr_SetString(NisError, clnt_spcreateerror(server)); |
| 372 | goto finally; |
| 373 | } |
| 374 | list = nisproc_maplist_2 (&dom, cl); |
| 375 | clnt_destroy(cl); |
| 376 | if (list == NULL) |
| 377 | goto finally; |
| 378 | if (list->stat != NIS_TRUE) |
| 379 | goto finally; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | free(server); |
| 382 | return list->maps; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 383 | |
| 384 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | free(server); |
| 386 | return NULL; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 389 | static PyObject * |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 390 | nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | char *domain = NULL; |
| 393 | nismaplist *maps; |
| 394 | PyObject *list; |
| 395 | int err; |
| 396 | static char *kwlist[] = {"domain", NULL}; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, |
| 399 | "|s:maps", kwlist, &domain)) |
| 400 | return NULL; |
| 401 | if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) { |
| 402 | nis_error(err); |
| 403 | return NULL; |
| 404 | } |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | if ((maps = nis_maplist (domain)) == NULL) |
| 407 | return NULL; |
| 408 | if ((list = PyList_New(0)) == NULL) |
| 409 | return NULL; |
Brett Cannon | eb175c4 | 2011-06-06 20:24:11 -0700 | [diff] [blame] | 410 | for (; maps; maps = maps->next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | PyObject *str = PyUnicode_FromString(maps->map); |
| 412 | if (!str || PyList_Append(list, str) < 0) |
| 413 | { |
| 414 | Py_DECREF(list); |
| 415 | list = NULL; |
| 416 | break; |
| 417 | } |
| 418 | Py_DECREF(str); |
| 419 | } |
| 420 | /* XXX Shouldn't we free the list of maps now? */ |
| 421 | return list; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 424 | static PyMethodDef nis_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | {"match", (PyCFunction)nis_match, |
| 426 | METH_VARARGS | METH_KEYWORDS, |
| 427 | match__doc__}, |
| 428 | {"cat", (PyCFunction)nis_cat, |
| 429 | METH_VARARGS | METH_KEYWORDS, |
| 430 | cat__doc__}, |
| 431 | {"maps", (PyCFunction)nis_maps, |
| 432 | METH_VARARGS | METH_KEYWORDS, |
| 433 | maps__doc__}, |
| 434 | {"get_default_domain", (PyCFunction)nis_get_default_domain, |
| 435 | METH_NOARGS, |
| 436 | get_default_domain__doc__}, |
| 437 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 438 | }; |
| 439 | |
Martin v. Löwis | 57a34e8 | 2006-02-04 19:12:37 +0000 | [diff] [blame] | 440 | PyDoc_STRVAR(nis__doc__, |
| 441 | "This module contains functions for accessing NIS maps.\n"); |
| 442 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 443 | static struct PyModuleDef nismodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | PyModuleDef_HEAD_INIT, |
| 445 | "nis", |
| 446 | nis__doc__, |
| 447 | -1, |
| 448 | nis_methods, |
| 449 | NULL, |
| 450 | NULL, |
| 451 | NULL, |
| 452 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
Victor Stinner | f024d26 | 2015-03-17 17:48:27 +0100 | [diff] [blame] | 455 | PyMODINIT_FUNC |
| 456 | PyInit_nis(void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | PyObject *m, *d; |
| 459 | m = PyModule_Create(&nismodule); |
| 460 | if (m == NULL) |
| 461 | return NULL; |
| 462 | d = PyModule_GetDict(m); |
| 463 | NisError = PyErr_NewException("nis.error", NULL, NULL); |
| 464 | if (NisError != NULL) |
| 465 | PyDict_SetItemString(d, "error", NisError); |
| 466 | return m; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 467 | } |