From 57a34e802638d51fc4b1dfba5ee9d8231ab52afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sat, 4 Feb 2006 19:12:37 +0000 Subject: Patch #1422385: Changes to nis module to support multiple NIS domains --- Doc/lib/libnis.tex | 21 +++++++++-- Misc/ACKS | 1 + Misc/NEWS | 3 ++ Modules/nismodule.c | 102 ++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 101 insertions(+), 26 deletions(-) diff --git a/Doc/lib/libnis.tex b/Doc/lib/libnis.tex index 1d52868..cc1482d 100644 --- a/Doc/lib/libnis.tex +++ b/Doc/lib/libnis.tex @@ -15,7 +15,7 @@ only available for \UNIX. The \module{nis} module defines the following functions: -\begin{funcdesc}{match}{key, mapname} +\begin{funcdesc}{match}{key, mapname[, domain=default_domain]} Return the match for \var{key} in map \var{mapname}, or raise an error (\exception{nis.error}) if there is none. Both should be strings, \var{key} is 8-bit clean. @@ -24,9 +24,13 @@ and other joys). Note that \var{mapname} is first checked if it is an alias to another name. + +\versionchanged[The \var{domain} argument allows to override +the NIS domain used for the lookup. If unspecified, lookup is in the +default NIS domain]{2.5} \end{funcdesc} -\begin{funcdesc}{cat}{mapname} +\begin{funcdesc}{cat}{mapname[, domain=default_domain]} Return a dictionary mapping \var{key} to \var{value} such that \code{match(\var{key}, \var{mapname})==\var{value}}. Note that both keys and values of the dictionary are arbitrary @@ -34,12 +38,23 @@ arrays of bytes. Note that \var{mapname} is first checked if it is an alias to another name. + +\versionchanged[The \var{domain} argument allows to override +the NIS domain used for the lookup. If unspecified, lookup is in the +default NIS domain]{2.5} \end{funcdesc} -\begin{funcdesc}{maps}{} + \begin{funcdesc}{maps}{[domain=default_domain]} Return a list of all valid maps. + +\versionchanged[The \var{domain} argument allows to override +the NIS domain used for the lookup. If unspecified, lookup is in the +default NIS domain]{2.5} \end{funcdesc} + \begin{funcdesc}{get_default_domain}{} +Return the system default NIS domain. \versionadded{2.5} +\end{funcdesc} The \module{nis} module defines the following exception: diff --git a/Misc/ACKS b/Misc/ACKS index b005b36..8ec8fb4 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -47,6 +47,7 @@ Neal Becker Robin Becker Bill Bedford Reimer Behrends +Ben Bell Thomas Bellman Juan M. Bello Rivas Alexander Belopolsky diff --git a/Misc/NEWS b/Misc/NEWS index 6250a51..7fe0393 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -216,6 +216,9 @@ Core and builtins Extension Modules ----------------- +- Patch #1422385: The nis module now supports access to domains other + than the system default domain. + - Use Win32 API to implement os.stat/fstat. As a result, subsecond timestamps are reported, the limit on path name lengths is removed, and stat reports WindowsError now (instead of OSError). diff --git a/Modules/nismodule.c b/Modules/nismodule.c index 207f7b8..084852f 100644 --- a/Modules/nismodule.c +++ b/Modules/nismodule.c @@ -23,6 +23,27 @@ extern int yp_get_default_domain(char **); #endif +PyDoc_STRVAR(get_default_domain__doc__, +"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"); + static PyObject *NisError; static PyObject * @@ -116,19 +137,36 @@ nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, } static PyObject * -nis_match (PyObject *self, PyObject *args) +nis_get_default_domain (PyObject *self) { - char *match; char *domain; + int err; + PyObject *res; + + if ((err = yp_get_default_domain(&domain)) != 0) + return nis_error(err); + + res = PyString_FromStringAndSize (domain, strlen(domain)); + return res; +} + +static PyObject * +nis_match (PyObject *self, PyObject *args, PyObject *kwdict) +{ + char *match; + char *domain = NULL; int keylen, len; char *key, *map; int err; PyObject *res; int fix; + static const char *kwlist[] = {"key", "map", "domain", NULL}; - if (!PyArg_ParseTuple(args, "t#s:match", &key, &keylen, &map)) + if (!PyArg_ParseTupleAndKeywords(args, kwdict, + "t#s|s:match", kwlist, + &key, &keylen, &map, &domain)) return NULL; - if ((err = yp_get_default_domain(&domain)) != 0) + if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) return nis_error(err); map = nis_mapname (map, &fix); if (fix) @@ -146,18 +184,20 @@ nis_match (PyObject *self, PyObject *args) } static PyObject * -nis_cat (PyObject *self, PyObject *args) +nis_cat (PyObject *self, PyObject *args, PyObject *kwdict) { - char *domain; + char *domain = NULL; char *map; struct ypall_callback cb; struct ypcallback_data data; PyObject *dict; int err; + static const char *kwlist[] = {"map", "domain", NULL}; - if (!PyArg_ParseTuple(args, "s:cat", &map)) + if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", + kwlist, &map, &domain)) return NULL; - if ((err = yp_get_default_domain(&domain)) != 0) + if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) return nis_error(err); dict = PyDict_New (); if (dict == NULL) @@ -301,19 +341,12 @@ nisproc_maplist_2(domainname *argp, CLIENT *clnt) static nismaplist * -nis_maplist (void) +nis_maplist (char *dom) { nisresp_maplist *list; - char *dom; CLIENT *cl; char *server = NULL; int mapi = 0; - int err; - - if ((err = yp_get_default_domain (&dom)) != 0) { - nis_error(err); - return NULL; - } while (!server && aliases[mapi].map != 0L) { yp_master (dom, aliases[mapi].map, &server); @@ -344,12 +377,23 @@ nis_maplist (void) } static PyObject * -nis_maps (PyObject *self) +nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) { + char *domain = NULL; nismaplist *maps; PyObject *list; + int err; + static const char *kwlist[] = {"domain", NULL}; - if ((maps = nis_maplist ()) == NULL) + if (!PyArg_ParseTupleAndKeywords(args, kwdict, + "|s:maps", kwlist, &domain)) + return NULL; + if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) { + nis_error(err); + return NULL; + } + + if ((maps = nis_maplist (domain)) == NULL) return NULL; if ((list = PyList_New(0)) == NULL) return NULL; @@ -368,17 +412,29 @@ nis_maps (PyObject *self) } static PyMethodDef nis_methods[] = { - {"match", nis_match, METH_VARARGS}, - {"cat", nis_cat, METH_VARARGS}, - {"maps", (PyCFunction)nis_maps, METH_NOARGS}, - {NULL, NULL} /* Sentinel */ + {"match", (PyCFunction)nis_match, + METH_VARARGS | METH_KEYWORDS, + match__doc__}, + {"cat", (PyCFunction)nis_cat, + METH_VARARGS | METH_KEYWORDS, + cat__doc__}, + {"maps", (PyCFunction)nis_maps, + METH_VARARGS | METH_KEYWORDS, + maps__doc__}, + {"get_default_domain", (PyCFunction)nis_get_default_domain, + METH_NOARGS, + get_default_domain__doc__}, + {NULL, NULL} /* Sentinel */ }; +PyDoc_STRVAR(nis__doc__, +"This module contains functions for accessing NIS maps.\n"); + void initnis (void) { PyObject *m, *d; - m = Py_InitModule("nis", nis_methods); + m = Py_InitModule3("nis", nis_methods, nis__doc__); if (m == NULL) return; d = PyModule_GetDict(m); -- cgit v0.12