Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57a34e8

Browse files
committed
Patch #1422385: Changes to nis module to support multiple NIS domains
1 parent faa26df commit 57a34e8

4 files changed

Lines changed: 101 additions & 26 deletions

File tree

Doc/lib/libnis.tex

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ \section{\module{nis} ---
1515

1616
The \module{nis} module defines the following functions:
1717

18-
\begin{funcdesc}{match}{key, mapname}
18+
\begin{funcdesc}{match}{key, mapname[, domain=default_domain]}
1919
Return the match for \var{key} in map \var{mapname}, or raise an
2020
error (\exception{nis.error}) if there is none.
2121
Both should be strings, \var{key} is 8-bit clean.
@@ -24,22 +24,37 @@ \section{\module{nis} ---
2424

2525
Note that \var{mapname} is first checked if it is an alias to another
2626
name.
27+
28+
\versionchanged[The \var{domain} argument allows to override
29+
the NIS domain used for the lookup. If unspecified, lookup is in the
30+
default NIS domain]{2.5}
2731
\end{funcdesc}
2832

29-
\begin{funcdesc}{cat}{mapname}
33+
\begin{funcdesc}{cat}{mapname[, domain=default_domain]}
3034
Return a dictionary mapping \var{key} to \var{value} such that
3135
\code{match(\var{key}, \var{mapname})==\var{value}}.
3236
Note that both keys and values of the dictionary are arbitrary
3337
arrays of bytes.
3438

3539
Note that \var{mapname} is first checked if it is an alias to another
3640
name.
41+
42+
\versionchanged[The \var{domain} argument allows to override
43+
the NIS domain used for the lookup. If unspecified, lookup is in the
44+
default NIS domain]{2.5}
3745
\end{funcdesc}
3846

39-
\begin{funcdesc}{maps}{}
47+
\begin{funcdesc}{maps}{[domain=default_domain]}
4048
Return a list of all valid maps.
49+
50+
\versionchanged[The \var{domain} argument allows to override
51+
the NIS domain used for the lookup. If unspecified, lookup is in the
52+
default NIS domain]{2.5}
4153
\end{funcdesc}
4254

55+
\begin{funcdesc}{get_default_domain}{}
56+
Return the system default NIS domain. \versionadded{2.5}
57+
\end{funcdesc}
4358

4459
The \module{nis} module defines the following exception:
4560

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Neal Becker
4747
Robin Becker
4848
Bill Bedford
4949
Reimer Behrends
50+
Ben Bell
5051
Thomas Bellman
5152
Juan M. Bello Rivas
5253
Alexander Belopolsky

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Core and builtins
216216
Extension Modules
217217
-----------------
218218

219+
- Patch #1422385: The nis module now supports access to domains other
220+
than the system default domain.
221+
219222
- Use Win32 API to implement os.stat/fstat. As a result, subsecond timestamps
220223
are reported, the limit on path name lengths is removed, and stat reports
221224
WindowsError now (instead of OSError).

Modules/nismodule.c

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
extern int yp_get_default_domain(char **);
2424
#endif
2525

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+
2647
static PyObject *NisError;
2748

2849
static PyObject *
@@ -116,19 +137,36 @@ nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
116137
}
117138

118139
static PyObject *
119-
nis_match (PyObject *self, PyObject *args)
140+
nis_get_default_domain (PyObject *self)
120141
{
121-
char *match;
122142
char *domain;
143+
int err;
144+
PyObject *res;
145+
146+
if ((err = yp_get_default_domain(&domain)) != 0)
147+
return nis_error(err);
148+
149+
res = PyString_FromStringAndSize (domain, strlen(domain));
150+
return res;
151+
}
152+
153+
static PyObject *
154+
nis_match (PyObject *self, PyObject *args, PyObject *kwdict)
155+
{
156+
char *match;
157+
char *domain = NULL;
123158
int keylen, len;
124159
char *key, *map;
125160
int err;
126161
PyObject *res;
127162
int fix;
163+
static const char *kwlist[] = {"key", "map", "domain", NULL};
128164

129-
if (!PyArg_ParseTuple(args, "t#s:match", &key, &keylen, &map))
165+
if (!PyArg_ParseTupleAndKeywords(args, kwdict,
166+
"t#s|s:match", kwlist,
167+
&key, &keylen, &map, &domain))
130168
return NULL;
131-
if ((err = yp_get_default_domain(&domain)) != 0)
169+
if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
132170
return nis_error(err);
133171
map = nis_mapname (map, &fix);
134172
if (fix)
@@ -146,18 +184,20 @@ nis_match (PyObject *self, PyObject *args)
146184
}
147185

148186
static PyObject *
149-
nis_cat (PyObject *self, PyObject *args)
187+
nis_cat (PyObject *self, PyObject *args, PyObject *kwdict)
150188
{
151-
char *domain;
189+
char *domain = NULL;
152190
char *map;
153191
struct ypall_callback cb;
154192
struct ypcallback_data data;
155193
PyObject *dict;
156194
int err;
195+
static const char *kwlist[] = {"map", "domain", NULL};
157196

158-
if (!PyArg_ParseTuple(args, "s:cat", &map))
197+
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat",
198+
kwlist, &map, &domain))
159199
return NULL;
160-
if ((err = yp_get_default_domain(&domain)) != 0)
200+
if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
161201
return nis_error(err);
162202
dict = PyDict_New ();
163203
if (dict == NULL)
@@ -301,19 +341,12 @@ nisproc_maplist_2(domainname *argp, CLIENT *clnt)
301341

302342
static
303343
nismaplist *
304-
nis_maplist (void)
344+
nis_maplist (char *dom)
305345
{
306346
nisresp_maplist *list;
307-
char *dom;
308347
CLIENT *cl;
309348
char *server = NULL;
310349
int mapi = 0;
311-
int err;
312-
313-
if ((err = yp_get_default_domain (&dom)) != 0) {
314-
nis_error(err);
315-
return NULL;
316-
}
317350

318351
while (!server && aliases[mapi].map != 0L) {
319352
yp_master (dom, aliases[mapi].map, &server);
@@ -344,12 +377,23 @@ nis_maplist (void)
344377
}
345378

346379
static PyObject *
347-
nis_maps (PyObject *self)
380+
nis_maps (PyObject *self, PyObject *args, PyObject *kwdict)
348381
{
382+
char *domain = NULL;
349383
nismaplist *maps;
350384
PyObject *list;
385+
int err;
386+
static const char *kwlist[] = {"domain", NULL};
351387

352-
if ((maps = nis_maplist ()) == NULL)
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)
353397
return NULL;
354398
if ((list = PyList_New(0)) == NULL)
355399
return NULL;
@@ -368,17 +412,29 @@ nis_maps (PyObject *self)
368412
}
369413

370414
static PyMethodDef nis_methods[] = {
371-
{"match", nis_match, METH_VARARGS},
372-
{"cat", nis_cat, METH_VARARGS},
373-
{"maps", (PyCFunction)nis_maps, METH_NOARGS},
374-
{NULL, NULL} /* Sentinel */
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 */
375428
};
376429

430+
PyDoc_STRVAR(nis__doc__,
431+
"This module contains functions for accessing NIS maps.\n");
432+
377433
void
378434
initnis (void)
379435
{
380436
PyObject *m, *d;
381-
m = Py_InitModule("nis", nis_methods);
437+
m = Py_InitModule3("nis", nis_methods, nis__doc__);
382438
if (m == NULL)
383439
return;
384440
d = PyModule_GetDict(m);

0 commit comments

Comments
 (0)