blob: 4be465744e7503fd6af8aacca6c0371af0abe888 [file] [log] [blame]
Guido van Rossum9de7a011992-08-12 14:57:12 +00001/***********************************************************
2 Written by:
3 Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
Guido van Rossum61b705a2000-02-29 15:52:40 +00004 B&O group,
Guido van Rossum9de7a011992-08-12 14:57:12 +00005 Faculteit der Informatica,
6 Universiteit Twente,
7 Enschede,
8 the Netherlands.
9******************************************************************/
10
11/* NIS module implementation */
12
Barry Warsawadbf4e61996-12-11 00:15:58 +000013#include "Python.h"
Guido van Rossum9de7a011992-08-12 14:57:12 +000014
Guido van Rossum9de7a011992-08-12 14:57:12 +000015#include <sys/time.h>
16#include <sys/types.h>
17#include <rpc/rpc.h>
18#include <rpcsvc/yp_prot.h>
Guido van Rossum8a170cb1996-08-08 19:11:41 +000019#include <rpcsvc/ypclnt.h>
Guido van Rossum9de7a011992-08-12 14:57:12 +000020
Guido van Rossum259552d1996-12-09 18:46:28 +000021#ifdef __sgi
22/* This is missing from rpcsvc/ypclnt.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000023extern int yp_get_default_domain(char **);
Guido van Rossum259552d1996-12-09 18:46:28 +000024#endif
25
Martin v. Löwis57a34e82006-02-04 19:12:37 +000026PyDoc_STRVAR(get_default_domain__doc__,
27"get_default_domain() -> str\n\
28Corresponds to the C library yp_get_default_domain() call, returning\n\
29the default NIS domain.\n");
30
31PyDoc_STRVAR(match__doc__,
32"match(key, map, domain = defaultdomain)\n\
33Corresponds to the C library yp_match() call, returning the value of\n\
34key in the given map. Optionally domain can be specified but it\n\
35defaults to the system default domain.\n");
36
37PyDoc_STRVAR(cat__doc__,
38"cat(map, domain = defaultdomain)\n\
39Returns the entire map as a dictionary. Optionally domain can be\n\
40specified but it defaults to the system default domain.\n");
41
42PyDoc_STRVAR(maps__doc__,
43"maps(domain = defaultdomain)\n\
44Returns an array of all available NIS maps within a domain. If domain\n\
45is not specified it defaults to the system default domain.\n");
46
Barry Warsawadbf4e61996-12-11 00:15:58 +000047static PyObject *NisError;
Guido van Rossum3562d521992-08-12 15:26:16 +000048
Barry Warsawadbf4e61996-12-11 00:15:58 +000049static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000050nis_error (int err)
Guido van Rossum3562d521992-08-12 15:26:16 +000051{
Barry Warsawadbf4e61996-12-11 00:15:58 +000052 PyErr_SetString(NisError, yperr_string(err));
Guido van Rossum3562d521992-08-12 15:26:16 +000053 return NULL;
54}
55
Guido van Rossum9de7a011992-08-12 14:57:12 +000056static struct nis_map {
57 char *alias;
58 char *map;
Guido van Rossum61b705a2000-02-29 15:52:40 +000059 int fix;
Guido van Rossum9de7a011992-08-12 14:57:12 +000060} aliases [] = {
Guido van Rossum61b705a2000-02-29 15:52:40 +000061 {"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 Rossum9de7a011992-08-12 14:57:12 +000070};
71
72static char *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000073nis_mapname (char *map, int *pfix)
Guido van Rossum9de7a011992-08-12 14:57:12 +000074{
Guido van Rossum3562d521992-08-12 15:26:16 +000075 int i;
Guido van Rossum9de7a011992-08-12 14:57:12 +000076
Guido van Rossum61b705a2000-02-29 15:52:40 +000077 *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 Rossum9de7a011992-08-12 14:57:12 +000089 return map;
90}
91
Amaury Forgeot d'Arc783f86e2009-07-07 06:51:26 +000092#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
Brett Cannonf6067ec2004-07-10 00:57:37 +000093typedef int (*foreachfunc)(unsigned long, char *, int, char *, int, void *);
94#else
Tim Petersdbd9ba62000-07-09 03:09:57 +000095typedef int (*foreachfunc)(int, char *, int, char *, int, char *);
Brett Cannonf6067ec2004-07-10 00:57:37 +000096#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000097
Guido van Rossum61b705a2000-02-29 15:52:40 +000098struct ypcallback_data {
99 PyObject *dict;
100 int fix;
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000101 PyThreadState *state;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000102};
103
Guido van Rossum9de7a011992-08-12 14:57:12 +0000104static int
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000105nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
106 int invallen, struct ypcallback_data *indata)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000107{
108 if (instatus == YP_TRUE) {
Guido van Rossum61b705a2000-02-29 15:52:40 +0000109 PyObject *key;
110 PyObject *val;
Guido van Rossume77a7571993-11-03 15:01:26 +0000111 int err;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000112
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000113 PyEval_RestoreThread(indata->state);
Guido van Rossum61b705a2000-02-29 15:52:40 +0000114 if (indata->fix) {
Neal Norwitz66239062002-11-04 23:21:09 +0000115 if (inkeylen > 0 && inkey[inkeylen-1] == '\0')
116 inkeylen--;
117 if (invallen > 0 && inval[invallen-1] == '\0')
118 invallen--;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000119 }
Neal Norwitzefaf6292007-08-26 21:43:00 +0000120 key = PyUnicode_FromStringAndSize(inkey, inkeylen);
121 val = PyUnicode_FromStringAndSize(inval, invallen);
Guido van Rossume77a7571993-11-03 15:01:26 +0000122 if (key == NULL || val == NULL) {
123 /* XXX error -- don't know how to handle */
Barry Warsawadbf4e61996-12-11 00:15:58 +0000124 PyErr_Clear();
125 Py_XDECREF(key);
126 Py_XDECREF(val);
Guido van Rossume77a7571993-11-03 15:01:26 +0000127 return 1;
128 }
Guido van Rossum61b705a2000-02-29 15:52:40 +0000129 err = PyDict_SetItem(indata->dict, key, val);
Barry Warsawadbf4e61996-12-11 00:15:58 +0000130 Py_DECREF(key);
131 Py_DECREF(val);
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000132 if (err != 0)
Barry Warsawadbf4e61996-12-11 00:15:58 +0000133 PyErr_Clear();
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000134 indata->state = PyEval_SaveThread();
135 if (err != 0)
136 return 1;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000137 return 0;
138 }
139 return 1;
140}
141
Barry Warsawadbf4e61996-12-11 00:15:58 +0000142static PyObject *
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000143nis_get_default_domain (PyObject *self)
144{
145 char *domain;
146 int err;
147 PyObject *res;
148
149 if ((err = yp_get_default_domain(&domain)) != 0)
150 return nis_error(err);
151
Neal Norwitzefaf6292007-08-26 21:43:00 +0000152 res = PyUnicode_FromStringAndSize (domain, strlen(domain));
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000153 return res;
154}
155
156static PyObject *
157nis_match (PyObject *self, PyObject *args, PyObject *kwdict)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000158{
Guido van Rossum3562d521992-08-12 15:26:16 +0000159 char *match;
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000160 char *domain = NULL;
Guido van Rossume77a7571993-11-03 15:01:26 +0000161 int keylen, len;
Guido van Rossum3562d521992-08-12 15:26:16 +0000162 char *key, *map;
163 int err;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000164 PyObject *res;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000165 int fix;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000166 static char *kwlist[] = {"key", "map", "domain", NULL};
Guido van Rossum9de7a011992-08-12 14:57:12 +0000167
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000168 if (!PyArg_ParseTupleAndKeywords(args, kwdict,
Martin v. Löwisd24fdbc2007-08-30 10:21:51 +0000169 "s#s|s:match", kwlist,
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000170 &key, &keylen, &map, &domain))
Guido van Rossum9de7a011992-08-12 14:57:12 +0000171 return NULL;
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000172 if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
Guido van Rossum3562d521992-08-12 15:26:16 +0000173 return nis_error(err);
Guido van Rossum61b705a2000-02-29 15:52:40 +0000174 map = nis_mapname (map, &fix);
175 if (fix)
176 keylen++;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000177 Py_BEGIN_ALLOW_THREADS
Guido van Rossume77a7571993-11-03 15:01:26 +0000178 err = yp_match (domain, map, key, keylen, &match, &len);
Barry Warsawadbf4e61996-12-11 00:15:58 +0000179 Py_END_ALLOW_THREADS
Guido van Rossum61b705a2000-02-29 15:52:40 +0000180 if (fix)
181 len--;
Guido van Rossum3562d521992-08-12 15:26:16 +0000182 if (err != 0)
183 return nis_error(err);
Neal Norwitzefaf6292007-08-26 21:43:00 +0000184 res = PyUnicode_FromStringAndSize (match, len);
Guido van Rossum3562d521992-08-12 15:26:16 +0000185 free (match);
186 return res;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000187}
188
Barry Warsawadbf4e61996-12-11 00:15:58 +0000189static PyObject *
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000190nis_cat (PyObject *self, PyObject *args, PyObject *kwdict)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000191{
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000192 char *domain = NULL;
Guido van Rossum3562d521992-08-12 15:26:16 +0000193 char *map;
194 struct ypall_callback cb;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000195 struct ypcallback_data data;
196 PyObject *dict;
Guido van Rossum3562d521992-08-12 15:26:16 +0000197 int err;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000198 static char *kwlist[] = {"map", "domain", NULL};
Guido van Rossum9de7a011992-08-12 14:57:12 +0000199
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000200 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat",
201 kwlist, &map, &domain))
Guido van Rossum9de7a011992-08-12 14:57:12 +0000202 return NULL;
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000203 if (!domain && ((err = yp_get_default_domain(&domain)) != 0))
Guido van Rossum3562d521992-08-12 15:26:16 +0000204 return nis_error(err);
Guido van Rossum61b705a2000-02-29 15:52:40 +0000205 dict = PyDict_New ();
206 if (dict == NULL)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000207 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208 cb.foreach = (foreachfunc)nis_foreach;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000209 data.dict = dict;
210 map = nis_mapname (map, &data.fix);
211 cb.data = (char *)&data;
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000212 data.state = PyEval_SaveThread();
Guido van Rossum3562d521992-08-12 15:26:16 +0000213 err = yp_all (domain, map, &cb);
Amaury Forgeot d'Arc451a3562008-07-02 23:44:19 +0000214 PyEval_RestoreThread(data.state);
Guido van Rossum3562d521992-08-12 15:26:16 +0000215 if (err != 0) {
Guido van Rossum61b705a2000-02-29 15:52:40 +0000216 Py_DECREF(dict);
Guido van Rossum3562d521992-08-12 15:26:16 +0000217 return nis_error(err);
218 }
Guido van Rossum61b705a2000-02-29 15:52:40 +0000219 return dict;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000220}
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222/* These should be u_long on Sun h/w but not on 64-bit h/w.
223 This is not portable to machines with 16-bit ints and no prototypes */
224#ifndef YPPROC_MAPLIST
225#define YPPROC_MAPLIST 11
226#endif
227#ifndef YPPROG
228#define YPPROG 100004
229#endif
230#ifndef YPVERS
231#define YPVERS 2
232#endif
Guido van Rossum9de7a011992-08-12 14:57:12 +0000233
234typedef char *domainname;
235typedef char *mapname;
236
237enum nisstat {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000238 NIS_TRUE = 1,
239 NIS_NOMORE = 2,
240 NIS_FALSE = 0,
241 NIS_NOMAP = -1,
242 NIS_NODOM = -2,
243 NIS_NOKEY = -3,
244 NIS_BADOP = -4,
245 NIS_BADDB = -5,
246 NIS_YPERR = -6,
247 NIS_BADARGS = -7,
248 NIS_VERS = -8
Guido van Rossum9de7a011992-08-12 14:57:12 +0000249};
250typedef enum nisstat nisstat;
251
252struct nismaplist {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000253 mapname map;
254 struct nismaplist *next;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000255};
256typedef struct nismaplist nismaplist;
257
258struct nisresp_maplist {
259 nisstat stat;
260 nismaplist *maps;
261};
262typedef struct nisresp_maplist nisresp_maplist;
263
264static struct timeval TIMEOUT = { 25, 0 };
265
266static
267bool_t
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000268nis_xdr_domainname(XDR *xdrs, domainname *objp)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000269{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000270 if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
271 return (FALSE);
272 }
273 return (TRUE);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000274}
275
276static
277bool_t
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000278nis_xdr_mapname(XDR *xdrs, mapname *objp)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000279{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000280 if (!xdr_string(xdrs, objp, YPMAXMAP)) {
281 return (FALSE);
282 }
283 return (TRUE);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000284}
285
286static
287bool_t
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000288nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000289{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000290 if (!nis_xdr_mapname(xdrs, &objp->map)) {
291 return (FALSE);
292 }
293 if (!xdr_pointer(xdrs, (char **)&objp->next,
Guido van Rossumc5015831998-10-07 16:36:14 +0000294 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
Barry Warsawadbf4e61996-12-11 00:15:58 +0000295 {
296 return (FALSE);
297 }
298 return (TRUE);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000299}
300
301static
302bool_t
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000303nis_xdr_ypstat(XDR *xdrs, nisstat *objp)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000304{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000305 if (!xdr_enum(xdrs, (enum_t *)objp)) {
306 return (FALSE);
307 }
308 return (TRUE);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000309}
310
311
312static
313bool_t
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000314nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000315{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000316 if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
317 return (FALSE);
318 }
319 if (!xdr_pointer(xdrs, (char **)&objp->maps,
Guido van Rossumc5015831998-10-07 16:36:14 +0000320 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
Barry Warsawadbf4e61996-12-11 00:15:58 +0000321 {
322 return (FALSE);
323 }
324 return (TRUE);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000325}
326
327
328static
329nisresp_maplist *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000330nisproc_maplist_2(domainname *argp, CLIENT *clnt)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000331{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000332 static nisresp_maplist res;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000333
Barry Warsawadbf4e61996-12-11 00:15:58 +0000334 memset(&res, 0, sizeof(res));
Guido van Rossumc5015831998-10-07 16:36:14 +0000335 if (clnt_call(clnt, YPPROC_MAPLIST,
336 (xdrproc_t)nis_xdr_domainname, (caddr_t)argp,
337 (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res,
338 TIMEOUT) != RPC_SUCCESS)
339 {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000340 return (NULL);
341 }
342 return (&res);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000343}
344
345static
346nismaplist *
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000347nis_maplist (char *dom)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000348{
Guido van Rossum3562d521992-08-12 15:26:16 +0000349 nisresp_maplist *list;
Thomas Wouterse75e6d02001-01-21 23:34:12 +0000350 CLIENT *cl;
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000351 char *server = NULL;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000352 int mapi = 0;
Barry Warsaw3696c521996-12-11 00:29:14 +0000353
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000354 while (!server && aliases[mapi].map != 0L) {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000355 yp_master (dom, aliases[mapi].map, &server);
356 mapi++;
357 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000358 if (!server) {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000359 PyErr_SetString(NisError, "No NIS master found for any map");
360 return NULL;
361 }
Guido van Rossum9de7a011992-08-12 14:57:12 +0000362 cl = clnt_create(server, YPPROG, YPVERS, "tcp");
363 if (cl == NULL) {
Barry Warsawadbf4e61996-12-11 00:15:58 +0000364 PyErr_SetString(NisError, clnt_spcreateerror(server));
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000365 goto finally;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000366 }
367 list = nisproc_maplist_2 (&dom, cl);
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000368 clnt_destroy(cl);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000369 if (list == NULL)
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000370 goto finally;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000371 if (list->stat != NIS_TRUE)
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000372 goto finally;
373
Guido van Rossumb18618d2000-05-03 23:44:39 +0000374 free(server);
Guido van Rossum9de7a011992-08-12 14:57:12 +0000375 return list->maps;
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000376
377 finally:
Guido van Rossumb18618d2000-05-03 23:44:39 +0000378 free(server);
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000379 return NULL;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000380}
381
Barry Warsawadbf4e61996-12-11 00:15:58 +0000382static PyObject *
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000383nis_maps (PyObject *self, PyObject *args, PyObject *kwdict)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000384{
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000385 char *domain = NULL;
Guido van Rossum3562d521992-08-12 15:26:16 +0000386 nismaplist *maps;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000387 PyObject *list;
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000388 int err;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000389 static char *kwlist[] = {"domain", NULL};
Guido van Rossum9de7a011992-08-12 14:57:12 +0000390
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000391 if (!PyArg_ParseTupleAndKeywords(args, kwdict,
392 "|s:maps", kwlist, &domain))
393 return NULL;
394 if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) {
395 nis_error(err);
396 return NULL;
397 }
398
399 if ((maps = nis_maplist (domain)) == NULL)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000400 return NULL;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000401 if ((list = PyList_New(0)) == NULL)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000402 return NULL;
Guido van Rossum61b705a2000-02-29 15:52:40 +0000403 for (maps = maps; maps; maps = maps->next) {
Neal Norwitzefaf6292007-08-26 21:43:00 +0000404 PyObject *str = PyUnicode_FromString(maps->map);
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000405 if (!str || PyList_Append(list, str) < 0)
Barry Warsawadbf4e61996-12-11 00:15:58 +0000406 {
407 Py_DECREF(list);
Guido van Rossum3562d521992-08-12 15:26:16 +0000408 list = NULL;
409 break;
410 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000411 Py_DECREF(str);
Guido van Rossum3562d521992-08-12 15:26:16 +0000412 }
413 /* XXX Shouldn't we free the list of maps now? */
Guido van Rossum9de7a011992-08-12 14:57:12 +0000414 return list;
415}
416
Barry Warsawadbf4e61996-12-11 00:15:58 +0000417static PyMethodDef nis_methods[] = {
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000418 {"match", (PyCFunction)nis_match,
419 METH_VARARGS | METH_KEYWORDS,
420 match__doc__},
421 {"cat", (PyCFunction)nis_cat,
422 METH_VARARGS | METH_KEYWORDS,
423 cat__doc__},
424 {"maps", (PyCFunction)nis_maps,
425 METH_VARARGS | METH_KEYWORDS,
426 maps__doc__},
427 {"get_default_domain", (PyCFunction)nis_get_default_domain,
428 METH_NOARGS,
429 get_default_domain__doc__},
430 {NULL, NULL} /* Sentinel */
Guido van Rossum9de7a011992-08-12 14:57:12 +0000431};
432
Martin v. Löwis57a34e82006-02-04 19:12:37 +0000433PyDoc_STRVAR(nis__doc__,
434"This module contains functions for accessing NIS maps.\n");
435
Martin v. Löwis1a214512008-06-11 05:26:20 +0000436static struct PyModuleDef nismodule = {
437 PyModuleDef_HEAD_INIT,
438 "nis",
439 nis__doc__,
440 -1,
441 nis_methods,
442 NULL,
443 NULL,
444 NULL,
445 NULL
446};
447
448PyObject*
449PyInit_nis (void)
Guido van Rossum9de7a011992-08-12 14:57:12 +0000450{
Barry Warsawadbf4e61996-12-11 00:15:58 +0000451 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000452 m = PyModule_Create(&nismodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000453 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000454 return NULL;
Barry Warsawadbf4e61996-12-11 00:15:58 +0000455 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000456 NisError = PyErr_NewException("nis.error", NULL, NULL);
457 if (NisError != NULL)
458 PyDict_SetItemString(d, "error", NisError);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000459 return m;
Guido van Rossum9de7a011992-08-12 14:57:12 +0000460}