AddressList.__str__(): Get rid of useless, and broken method. Closes
[python/dscho.git] / Modules / nismodule.c
blob0a38ee85c5a34c70c0be5969e4cb58c9a6fcdbc0
1 /***********************************************************
2 Written by:
3 Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
4 B&O group,
5 Faculteit der Informatica,
6 Universiteit Twente,
7 Enschede,
8 the Netherlands.
9 ******************************************************************/
11 /* NIS module implementation */
13 #include "Python.h"
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <rpc/rpc.h>
18 #include <rpcsvc/yp_prot.h>
19 #include <rpcsvc/ypclnt.h>
21 #ifdef __sgi
22 /* This is missing from rpcsvc/ypclnt.h */
23 extern int yp_get_default_domain(char **);
24 #endif
26 static PyObject *NisError;
28 static PyObject *
29 nis_error (int err)
31 PyErr_SetString(NisError, yperr_string(err));
32 return NULL;
35 static struct nis_map {
36 char *alias;
37 char *map;
38 int fix;
39 } aliases [] = {
40 {"passwd", "passwd.byname", 0},
41 {"group", "group.byname", 0},
42 {"networks", "networks.byaddr", 0},
43 {"hosts", "hosts.byname", 0},
44 {"protocols", "protocols.bynumber", 0},
45 {"services", "services.byname", 0},
46 {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */
47 {"ethers", "ethers.byname", 0},
48 {0L, 0L, 0}
51 static char *
52 nis_mapname (char *map, int *pfix)
54 int i;
56 *pfix = 0;
57 for (i=0; aliases[i].alias != 0L; i++) {
58 if (!strcmp (aliases[i].alias, map)) {
59 *pfix = aliases[i].fix;
60 return aliases[i].map;
62 if (!strcmp (aliases[i].map, map)) {
63 *pfix = aliases[i].fix;
64 return aliases[i].map;
68 return map;
71 typedef int (*foreachfunc)(int, char *, int, char *, int, char *);
73 struct ypcallback_data {
74 PyObject *dict;
75 int fix;
78 static int
79 nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
80 int invallen, struct ypcallback_data *indata)
82 if (instatus == YP_TRUE) {
83 PyObject *key;
84 PyObject *val;
85 int err;
87 if (indata->fix) {
88 if (inkeylen > 0 && inkey[inkeylen-1] == '\0')
89 inkeylen--;
90 if (invallen > 0 && inval[invallen-1] == '\0')
91 invallen--;
93 key = PyString_FromStringAndSize(inkey, inkeylen);
94 val = PyString_FromStringAndSize(inval, invallen);
95 if (key == NULL || val == NULL) {
96 /* XXX error -- don't know how to handle */
97 PyErr_Clear();
98 Py_XDECREF(key);
99 Py_XDECREF(val);
100 return 1;
102 err = PyDict_SetItem(indata->dict, key, val);
103 Py_DECREF(key);
104 Py_DECREF(val);
105 if (err != 0) {
106 PyErr_Clear();
107 return 1;
109 return 0;
111 return 1;
114 static PyObject *
115 nis_match (PyObject *self, PyObject *args)
117 char *match;
118 char *domain;
119 int keylen, len;
120 char *key, *map;
121 int err;
122 PyObject *res;
123 int fix;
125 if (!PyArg_ParseTuple(args, "t#s:match", &key, &keylen, &map))
126 return NULL;
127 if ((err = yp_get_default_domain(&domain)) != 0)
128 return nis_error(err);
129 map = nis_mapname (map, &fix);
130 if (fix)
131 keylen++;
132 Py_BEGIN_ALLOW_THREADS
133 err = yp_match (domain, map, key, keylen, &match, &len);
134 Py_END_ALLOW_THREADS
135 if (fix)
136 len--;
137 if (err != 0)
138 return nis_error(err);
139 res = PyString_FromStringAndSize (match, len);
140 free (match);
141 return res;
144 static PyObject *
145 nis_cat (PyObject *self, PyObject *args)
147 char *domain;
148 char *map;
149 struct ypall_callback cb;
150 struct ypcallback_data data;
151 PyObject *dict;
152 int err;
154 if (!PyArg_ParseTuple(args, "s:cat", &map))
155 return NULL;
156 if ((err = yp_get_default_domain(&domain)) != 0)
157 return nis_error(err);
158 dict = PyDict_New ();
159 if (dict == NULL)
160 return NULL;
161 cb.foreach = (foreachfunc)nis_foreach;
162 data.dict = dict;
163 map = nis_mapname (map, &data.fix);
164 cb.data = (char *)&data;
165 Py_BEGIN_ALLOW_THREADS
166 err = yp_all (domain, map, &cb);
167 Py_END_ALLOW_THREADS
168 if (err != 0) {
169 Py_DECREF(dict);
170 return nis_error(err);
172 return dict;
175 /* These should be u_long on Sun h/w but not on 64-bit h/w.
176 This is not portable to machines with 16-bit ints and no prototypes */
177 #ifndef YPPROC_MAPLIST
178 #define YPPROC_MAPLIST 11
179 #endif
180 #ifndef YPPROG
181 #define YPPROG 100004
182 #endif
183 #ifndef YPVERS
184 #define YPVERS 2
185 #endif
187 typedef char *domainname;
188 typedef char *mapname;
190 enum nisstat {
191 NIS_TRUE = 1,
192 NIS_NOMORE = 2,
193 NIS_FALSE = 0,
194 NIS_NOMAP = -1,
195 NIS_NODOM = -2,
196 NIS_NOKEY = -3,
197 NIS_BADOP = -4,
198 NIS_BADDB = -5,
199 NIS_YPERR = -6,
200 NIS_BADARGS = -7,
201 NIS_VERS = -8
203 typedef enum nisstat nisstat;
205 struct nismaplist {
206 mapname map;
207 struct nismaplist *next;
209 typedef struct nismaplist nismaplist;
211 struct nisresp_maplist {
212 nisstat stat;
213 nismaplist *maps;
215 typedef struct nisresp_maplist nisresp_maplist;
217 static struct timeval TIMEOUT = { 25, 0 };
219 static
220 bool_t
221 nis_xdr_domainname(XDR *xdrs, domainname *objp)
223 if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
224 return (FALSE);
226 return (TRUE);
229 static
230 bool_t
231 nis_xdr_mapname(XDR *xdrs, mapname *objp)
233 if (!xdr_string(xdrs, objp, YPMAXMAP)) {
234 return (FALSE);
236 return (TRUE);
239 static
240 bool_t
241 nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp)
243 if (!nis_xdr_mapname(xdrs, &objp->map)) {
244 return (FALSE);
246 if (!xdr_pointer(xdrs, (char **)&objp->next,
247 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
249 return (FALSE);
251 return (TRUE);
254 static
255 bool_t
256 nis_xdr_ypstat(XDR *xdrs, nisstat *objp)
258 if (!xdr_enum(xdrs, (enum_t *)objp)) {
259 return (FALSE);
261 return (TRUE);
265 static
266 bool_t
267 nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp)
269 if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
270 return (FALSE);
272 if (!xdr_pointer(xdrs, (char **)&objp->maps,
273 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
275 return (FALSE);
277 return (TRUE);
281 static
282 nisresp_maplist *
283 nisproc_maplist_2(domainname *argp, CLIENT *clnt)
285 static nisresp_maplist res;
287 memset(&res, 0, sizeof(res));
288 if (clnt_call(clnt, YPPROC_MAPLIST,
289 (xdrproc_t)nis_xdr_domainname, (caddr_t)argp,
290 (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res,
291 TIMEOUT) != RPC_SUCCESS)
293 return (NULL);
295 return (&res);
298 static
299 nismaplist *
300 nis_maplist (void)
302 nisresp_maplist *list;
303 char *dom;
304 CLIENT *cl;
305 char *server = NULL;
306 int mapi = 0;
307 int err;
309 if ((err = yp_get_default_domain (&dom)) != 0) {
310 nis_error(err);
311 return NULL;
314 while (!server && aliases[mapi].map != 0L) {
315 yp_master (dom, aliases[mapi].map, &server);
316 mapi++;
318 if (!server) {
319 PyErr_SetString(NisError, "No NIS master found for any map");
320 return NULL;
322 cl = clnt_create(server, YPPROG, YPVERS, "tcp");
323 if (cl == NULL) {
324 PyErr_SetString(NisError, clnt_spcreateerror(server));
325 goto finally;
327 list = nisproc_maplist_2 (&dom, cl);
328 clnt_destroy(cl);
329 if (list == NULL)
330 goto finally;
331 if (list->stat != NIS_TRUE)
332 goto finally;
334 free(server);
335 return list->maps;
337 finally:
338 free(server);
339 return NULL;
342 static PyObject *
343 nis_maps (PyObject *self)
345 nismaplist *maps;
346 PyObject *list;
348 if ((maps = nis_maplist ()) == NULL)
349 return NULL;
350 if ((list = PyList_New(0)) == NULL)
351 return NULL;
352 for (maps = maps; maps; maps = maps->next) {
353 PyObject *str = PyString_FromString(maps->map);
354 if (!str || PyList_Append(list, str) < 0)
356 Py_DECREF(list);
357 list = NULL;
358 break;
360 Py_DECREF(str);
362 /* XXX Shouldn't we free the list of maps now? */
363 return list;
366 static PyMethodDef nis_methods[] = {
367 {"match", nis_match, METH_VARARGS},
368 {"cat", nis_cat, METH_VARARGS},
369 {"maps", (PyCFunction)nis_maps, METH_NOARGS},
370 {NULL, NULL} /* Sentinel */
373 void
374 initnis (void)
376 PyObject *m, *d;
377 m = Py_InitModule("nis", nis_methods);
378 d = PyModule_GetDict(m);
379 NisError = PyErr_NewException("nis.error", NULL, NULL);
380 if (NisError != NULL)
381 PyDict_SetItemString(d, "error", NisError);