| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | /***********************************************************
 | 
					
						
							|  |  |  |     Written by: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Fred Gansevles <Fred.Gansevles@cs.utwente.nl> | 
					
						
							|  |  |  |     B&O group, | 
					
						
							|  |  |  |     Faculteit der Informatica, | 
					
						
							|  |  |  |     Universiteit Twente, | 
					
						
							|  |  |  |     Enschede, | 
					
						
							|  |  |  |     the Netherlands. | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | ******************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* NIS module implementation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <rpc/rpc.h>
 | 
					
						
							|  |  |  | #include <rpcsvc/yp_prot.h>
 | 
					
						
							| 
									
										
										
										
											1996-08-08 19:11:41 +00:00
										 |  |  | #include <rpcsvc/ypclnt.h>
 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-09 18:46:28 +00:00
										 |  |  | #ifdef __sgi
 | 
					
						
							|  |  |  | /* This is missing from rpcsvc/ypclnt.h */ | 
					
						
							| 
									
										
										
										
											2000-07-22 23:57:55 +00:00
										 |  |  | extern int yp_get_default_domain(char **); | 
					
						
							| 
									
										
										
										
											1996-12-09 18:46:28 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | PyDoc_STRVAR(get_default_domain__doc__, | 
					
						
							| 
									
										
										
										
											2006-02-04 19:12:37 +00:00
										 |  |  | "get_default_domain() -> str\n\
 | 
					
						
							|  |  |  | Corresponds to the C library yp_get_default_domain() call, returning\n\ | 
					
						
							|  |  |  | the default NIS domain.\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(match__doc__, | 
					
						
							|  |  |  | "match(key, map, domain = defaultdomain)\n\
 | 
					
						
							|  |  |  | Corresponds to the C library yp_match() call, returning the value of\n\ | 
					
						
							|  |  |  | key in the given map. Optionally domain can be specified but it\n\ | 
					
						
							|  |  |  | defaults to the system default domain.\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(cat__doc__, | 
					
						
							|  |  |  | "cat(map, domain = defaultdomain)\n\
 | 
					
						
							|  |  |  | Returns the entire map as a dictionary. Optionally domain can be\n\ | 
					
						
							|  |  |  | specified but it defaults to the system default domain.\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(maps__doc__, | 
					
						
							|  |  |  | "maps(domain = defaultdomain)\n\
 | 
					
						
							|  |  |  | Returns an array of all available NIS maps within a domain. If domain\n\ | 
					
						
							|  |  |  | is not specified it defaults to the system default domain.\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject *nis_error; | 
					
						
							|  |  |  | } nis_state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline nis_state* | 
					
						
							|  |  |  | get_nis_state(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *state = PyModule_GetState(module); | 
					
						
							|  |  |  |     assert(state != NULL); | 
					
						
							|  |  |  |     return (nis_state *)state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | nis_clear(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_CLEAR(get_nis_state(m)->nis_error); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | nis_traverse(PyObject *m, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(get_nis_state(m)->nis_error); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | nis_free(void *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     nis_clear((PyObject *) m); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1992-08-12 15:26:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_error(nis_state *state, int err) | 
					
						
							| 
									
										
										
										
											1992-08-12 15:26:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     PyErr_SetString(state->nis_error, yperr_string(err)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1992-08-12 15:26:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | static struct nis_map { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *alias; | 
					
						
							|  |  |  |     char *map; | 
					
						
							|  |  |  |     int  fix; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } aliases [] = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {"passwd",          "passwd.byname",        0}, | 
					
						
							|  |  |  |     {"group",           "group.byname",         0}, | 
					
						
							|  |  |  |     {"networks",        "networks.byaddr",      0}, | 
					
						
							|  |  |  |     {"hosts",           "hosts.byname",         0}, | 
					
						
							|  |  |  |     {"protocols",       "protocols.bynumber",   0}, | 
					
						
							|  |  |  |     {"services",        "services.byname",      0}, | 
					
						
							|  |  |  |     {"aliases",         "mail.aliases",         1}, /* created with 'makedbm -a' */ | 
					
						
							|  |  |  |     {"ethers",          "ethers.byname",        0}, | 
					
						
							|  |  |  |     {0L,                0L,                     0} | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_mapname(char *map, int *pfix) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *pfix = 0; | 
					
						
							|  |  |  |     for (i=0; aliases[i].alias != 0L; i++) { | 
					
						
							| 
									
										
										
										
											2015-07-24 00:43:44 -04:00
										 |  |  |         if (!strcmp (aliases[i].alias, map) || !strcmp (aliases[i].map, map)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             *pfix = aliases[i].fix; | 
					
						
							|  |  |  |             return aliases[i].map; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return map; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-07 06:51:26 +00:00
										 |  |  | #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
 | 
					
						
							| 
									
										
										
										
											2004-07-10 00:57:37 +00:00
										 |  |  | typedef int (*foreachfunc)(unsigned long, char *, int, char *, int, void *); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2000-07-09 03:09:57 +00:00
										 |  |  | typedef int (*foreachfunc)(int, char *, int, char *, int, char *); | 
					
						
							| 
									
										
										
										
											2004-07-10 00:57:37 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-29 15:52:40 +00:00
										 |  |  | struct ypcallback_data { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject            *dict; | 
					
						
							|  |  |  |     int                         fix; | 
					
						
							|  |  |  |     PyThreadState *state; | 
					
						
							| 
									
										
										
										
											2000-02-29 15:52:40 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_foreach(int instatus, char *inkey, int inkeylen, char *inval, | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  |              int invallen, struct ypcallback_data *indata) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (instatus == YP_TRUE) { | 
					
						
							|  |  |  |         PyObject *key; | 
					
						
							|  |  |  |         PyObject *val; | 
					
						
							|  |  |  |         int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         PyEval_RestoreThread(indata->state); | 
					
						
							|  |  |  |         if (indata->fix) { | 
					
						
							|  |  |  |             if (inkeylen > 0 && inkey[inkeylen-1] == '\0') | 
					
						
							|  |  |  |             inkeylen--; | 
					
						
							|  |  |  |             if (invallen > 0 && inval[invallen-1] == '\0') | 
					
						
							|  |  |  |             invallen--; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |         key = PyUnicode_DecodeFSDefaultAndSize(inkey, inkeylen); | 
					
						
							|  |  |  |         val = PyUnicode_DecodeFSDefaultAndSize(inval, invallen); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (key == NULL || val == NULL) { | 
					
						
							|  |  |  |             /* XXX error -- don't know how to handle */ | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |             Py_XDECREF(key); | 
					
						
							|  |  |  |             Py_XDECREF(val); | 
					
						
							| 
									
										
										
										
											2010-08-18 16:12:23 +00:00
										 |  |  |             indata->state = PyEval_SaveThread(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         err = PyDict_SetItem(indata->dict, key, val); | 
					
						
							|  |  |  |         Py_DECREF(key); | 
					
						
							|  |  |  |         Py_DECREF(val); | 
					
						
							|  |  |  |         if (err != 0) | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |         indata->state = PyEval_SaveThread(); | 
					
						
							|  |  |  |         if (err != 0) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_get_default_domain(PyObject *module, PyObject *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *domain; | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  |     PyObject *res; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     nis_state *state = get_nis_state(module); | 
					
						
							|  |  |  |     if ((err = yp_get_default_domain(&domain)) != 0) { | 
					
						
							|  |  |  |         return nis_error(state, err); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-02-04 19:12:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     res = PyUnicode_FromStringAndSize (domain, strlen(domain)); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2006-02-04 19:12:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_match(PyObject *module, PyObject *args, PyObject *kwdict) | 
					
						
							| 
									
										
										
										
											2006-02-04 19:12:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *match; | 
					
						
							|  |  |  |     char *domain = NULL; | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     Py_ssize_t keylen; | 
					
						
							|  |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *key, *map; | 
					
						
							|  |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     PyObject *ukey, *bkey, *res; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int fix; | 
					
						
							|  |  |  |     static char *kwlist[] = {"key", "map", "domain", NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_ParseTupleAndKeywords(args, kwdict, | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |                                      "Us|s:match", kwlist, | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |                                      &ukey, &map, &domain)) { | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     if ((bkey = PyUnicode_EncodeFSDefault(ukey)) == NULL) { | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-06-28 08:30:06 +03:00
										 |  |  |     /* check for embedded null bytes */ | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     if (PyBytes_AsStringAndSize(bkey, &key, &keylen) == -1) { | 
					
						
							|  |  |  |         Py_DECREF(bkey); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     nis_state *state = get_nis_state(module); | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) { | 
					
						
							|  |  |  |         Py_DECREF(bkey); | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |         return nis_error(state, err); | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     map = nis_mapname (map, &fix); | 
					
						
							|  |  |  |     if (fix) | 
					
						
							|  |  |  |         keylen++; | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     err = yp_match (domain, map, key, keylen, &match, &len); | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     Py_DECREF(bkey); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (fix) | 
					
						
							|  |  |  |         len--; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     if (err != 0) { | 
					
						
							|  |  |  |         return nis_error(state, err); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-19 09:11:51 +00:00
										 |  |  |     res = PyUnicode_DecodeFSDefaultAndSize(match, len); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     free (match); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_cat(PyObject *module, PyObject *args, PyObject *kwdict) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *domain = NULL; | 
					
						
							|  |  |  |     char *map; | 
					
						
							|  |  |  |     struct ypall_callback cb; | 
					
						
							|  |  |  |     struct ypcallback_data data; | 
					
						
							|  |  |  |     PyObject *dict; | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  |     static char *kwlist[] = {"map", "domain", NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |                                      kwlist, &map, &domain)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     nis_state *state = get_nis_state(module); | 
					
						
							|  |  |  |     if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) { | 
					
						
							|  |  |  |         return nis_error(state, err); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     dict = PyDict_New (); | 
					
						
							|  |  |  |     if (dict == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     cb.foreach = (foreachfunc)nis_foreach; | 
					
						
							|  |  |  |     data.dict = dict; | 
					
						
							|  |  |  |     map = nis_mapname (map, &data.fix); | 
					
						
							|  |  |  |     cb.data = (char *)&data; | 
					
						
							|  |  |  |     data.state = PyEval_SaveThread(); | 
					
						
							|  |  |  |     err = yp_all (domain, map, &cb); | 
					
						
							|  |  |  |     PyEval_RestoreThread(data.state); | 
					
						
							|  |  |  |     if (err != 0) { | 
					
						
							|  |  |  |         Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |         return nis_error(state, err); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return dict; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | /* These should be u_long on Sun h/w but not on 64-bit h/w.
 | 
					
						
							|  |  |  |    This is not portable to machines with 16-bit ints and no prototypes */ | 
					
						
							|  |  |  | #ifndef YPPROC_MAPLIST
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define YPPROC_MAPLIST  11
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifndef YPPROG
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define YPPROG          100004
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifndef YPVERS
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #define YPVERS          2
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef char *domainname; | 
					
						
							|  |  |  | typedef char *mapname; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum nisstat { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     NIS_TRUE = 1, | 
					
						
							|  |  |  |     NIS_NOMORE = 2, | 
					
						
							|  |  |  |     NIS_FALSE = 0, | 
					
						
							|  |  |  |     NIS_NOMAP = -1, | 
					
						
							|  |  |  |     NIS_NODOM = -2, | 
					
						
							|  |  |  |     NIS_NOKEY = -3, | 
					
						
							|  |  |  |     NIS_BADOP = -4, | 
					
						
							|  |  |  |     NIS_BADDB = -5, | 
					
						
							|  |  |  |     NIS_YPERR = -6, | 
					
						
							|  |  |  |     NIS_BADARGS = -7, | 
					
						
							|  |  |  |     NIS_VERS = -8 | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef enum nisstat nisstat; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct nismaplist { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     mapname map; | 
					
						
							|  |  |  |     struct nismaplist *next; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef struct nismaplist nismaplist; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct nisresp_maplist { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     nisstat stat; | 
					
						
							|  |  |  |     nismaplist *maps; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | typedef struct nisresp_maplist nisresp_maplist; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct timeval TIMEOUT = { 25, 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | bool_t | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nis_xdr_domainname(XDR *xdrs, domainname *objp) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (TRUE); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | bool_t | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nis_xdr_mapname(XDR *xdrs, mapname *objp) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!xdr_string(xdrs, objp, YPMAXMAP)) { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (TRUE); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | bool_t | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!nis_xdr_mapname(xdrs, &objp->map)) { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!xdr_pointer(xdrs, (char **)&objp->next, | 
					
						
							|  |  |  |                      sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (TRUE); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | bool_t | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nis_xdr_ypstat(XDR *xdrs, nisstat *objp) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!xdr_enum(xdrs, (enum_t *)objp)) { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (TRUE); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | bool_t | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!nis_xdr_ypstat(xdrs, &objp->stat)) { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!xdr_pointer(xdrs, (char **)&objp->maps, | 
					
						
							|  |  |  |                      sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return (FALSE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (TRUE); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | nisresp_maplist * | 
					
						
							| 
									
										
										
										
											2000-07-10 13:12:27 +00:00
										 |  |  | nisproc_maplist_2(domainname *argp, CLIENT *clnt) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     static nisresp_maplist res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memset(&res, 0, sizeof(res)); | 
					
						
							|  |  |  |     if (clnt_call(clnt, YPPROC_MAPLIST, | 
					
						
							|  |  |  |                   (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, | 
					
						
							|  |  |  |                   (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, | 
					
						
							|  |  |  |                   TIMEOUT) != RPC_SUCCESS) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return (NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (&res); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static | 
					
						
							|  |  |  | nismaplist * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_maplist(nis_state *state, char *dom) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     nisresp_maplist *list; | 
					
						
							|  |  |  |     CLIENT *cl; | 
					
						
							|  |  |  |     char *server = NULL; | 
					
						
							|  |  |  |     int mapi = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (!server && aliases[mapi].map != 0L) { | 
					
						
							|  |  |  |         yp_master (dom, aliases[mapi].map, &server); | 
					
						
							|  |  |  |         mapi++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!server) { | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |         PyErr_SetString(state->nis_error, "No NIS master found for any map"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     cl = clnt_create(server, YPPROG, YPVERS, "tcp"); | 
					
						
							|  |  |  |     if (cl == NULL) { | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |         PyErr_SetString(state->nis_error, clnt_spcreateerror(server)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto finally; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     list = nisproc_maplist_2 (&dom, cl); | 
					
						
							|  |  |  |     clnt_destroy(cl); | 
					
						
							|  |  |  |     if (list == NULL) | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  |     if (list->stat != NIS_TRUE) | 
					
						
							|  |  |  |         goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     free(server); | 
					
						
							|  |  |  |     return list->maps; | 
					
						
							| 
									
										
										
										
											1997-01-09 22:22:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   finally: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     free(server); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | nis_maps (PyObject *module, PyObject *args, PyObject *kwdict) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     char *domain = NULL; | 
					
						
							|  |  |  |     nismaplist *maps; | 
					
						
							|  |  |  |     PyObject *list; | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  |     static char *kwlist[] = {"domain", NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_ParseTupleAndKeywords(args, kwdict, | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |                                      "|s:maps", kwlist, &domain)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     nis_state *state = get_nis_state(module); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) { | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |         nis_error(state, err); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     if ((maps = nis_maplist(state, domain)) == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     if ((list = PyList_New(0)) == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-06-06 20:24:11 -07:00
										 |  |  |     for (; maps; maps = maps->next) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         PyObject *str = PyUnicode_FromString(maps->map); | 
					
						
							|  |  |  |         if (!str || PyList_Append(list, str) < 0) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-11-30 00:40:16 -07:00
										 |  |  |             Py_XDECREF(str); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             Py_DECREF(list); | 
					
						
							|  |  |  |             list = NULL; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(str); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* XXX Shouldn't we free the list of maps now? */ | 
					
						
							|  |  |  |     return list; | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-11 00:15:58 +00:00
										 |  |  | static PyMethodDef nis_methods[] = { | 
					
						
							| 
									
										
										
										
											2018-11-27 13:27:31 +02:00
										 |  |  |     {"match",                   (PyCFunction)(void(*)(void))nis_match, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                     METH_VARARGS | METH_KEYWORDS, | 
					
						
							|  |  |  |                                     match__doc__}, | 
					
						
							| 
									
										
										
										
											2018-11-27 13:27:31 +02:00
										 |  |  |     {"cat",                     (PyCFunction)(void(*)(void))nis_cat, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                     METH_VARARGS | METH_KEYWORDS, | 
					
						
							|  |  |  |                                     cat__doc__}, | 
					
						
							| 
									
										
										
										
											2018-11-27 13:27:31 +02:00
										 |  |  |     {"maps",                    (PyCFunction)(void(*)(void))nis_maps, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                     METH_VARARGS | METH_KEYWORDS, | 
					
						
							|  |  |  |                                     maps__doc__}, | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  |     {"get_default_domain",      nis_get_default_domain, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                                     METH_NOARGS, | 
					
						
							|  |  |  |                                     get_default_domain__doc__}, | 
					
						
							|  |  |  |     {NULL,                      NULL}            /* Sentinel */ | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  | static int | 
					
						
							|  |  |  | nis_exec(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     nis_state* state = get_nis_state(module); | 
					
						
							|  |  |  |     state->nis_error = PyErr_NewException("nis.error", NULL, NULL); | 
					
						
							|  |  |  |     if (state->nis_error == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_INCREF(state->nis_error); | 
					
						
							|  |  |  |     if (PyModule_AddObject(module, "error", state->nis_error) < 0) { | 
					
						
							|  |  |  |         Py_DECREF(state->nis_error); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyModuleDef_Slot nis_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, nis_exec}, | 
					
						
							|  |  |  |     {0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-04 19:12:37 +00:00
										 |  |  | PyDoc_STRVAR(nis__doc__, | 
					
						
							|  |  |  | "This module contains functions for accessing NIS maps.\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | static struct PyModuleDef nismodule = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     .m_name = "nis", | 
					
						
							|  |  |  |     .m_doc = nis__doc__, | 
					
						
							|  |  |  |     .m_size = sizeof(nis_state), | 
					
						
							|  |  |  |     .m_methods = nis_methods, | 
					
						
							|  |  |  |     .m_traverse = nis_traverse, | 
					
						
							|  |  |  |     .m_clear = nis_clear, | 
					
						
							|  |  |  |     .m_free = nis_free, | 
					
						
							|  |  |  |     .m_slots = nis_slots, | 
					
						
							| 
									
										
										
										
											2008-06-11 05:26:20 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 17:48:27 +01:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							|  |  |  | PyInit_nis(void) | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-12 11:26:00 +09:00
										 |  |  |     return PyModuleDef_Init(&nismodule); | 
					
						
							| 
									
										
										
										
											1992-08-12 14:57:12 +00:00
										 |  |  | } |