1 /***********************************************************
3 Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
5 Faculteit der Informatica,
9 ******************************************************************/
11 /* NIS module implementation */
16 #include <sys/types.h>
18 #include <rpcsvc/yp_prot.h>
19 #include <rpcsvc/ypclnt.h>
22 /* This is missing from rpcsvc/ypclnt.h */
23 extern int yp_get_default_domain();
26 static PyObject
*NisError
;
32 PyErr_SetString(NisError
, yperr_string(err
));
36 static struct nis_map
{
40 {"passwd", "passwd.byname"},
41 {"group", "group.byname"},
42 {"networks", "networks.byaddr"},
43 {"hosts", "hosts.byname"},
44 {"protocols", "protocols.bynumber"},
45 {"services", "services.byname"},
46 {"aliases", "mail.aliases"},
47 {"ethers", "ethers.byname"},
57 for (i
=0; aliases
[i
].alias
!= 0L; i
++)
58 if (!strcmp (aliases
[i
].alias
, map
))
63 typedef int (*foreachfunc
) Py_PROTO((int, char *, int, char *, int, char *));
66 nis_foreach (instatus
, inkey
, inkeylen
, inval
, invallen
, indata
)
74 if (instatus
== YP_TRUE
) {
75 PyObject
*key
= PyString_FromStringAndSize(inkey
, inkeylen
);
76 PyObject
*val
= PyString_FromStringAndSize(inval
, invallen
);
78 if (key
== NULL
|| val
== NULL
) {
79 /* XXX error -- don't know how to handle */
85 err
= PyDict_SetItem(indata
, key
, val
);
98 nis_match (self
, args
)
109 if (!PyArg_Parse(args
, "(t#s)", &key
, &keylen
, &map
))
111 if ((err
= yp_get_default_domain(&domain
)) != 0)
112 return nis_error(err
);
113 Py_BEGIN_ALLOW_THREADS
114 map
= nis_mapname (map
);
115 err
= yp_match (domain
, map
, key
, keylen
, &match
, &len
);
118 return nis_error(err
);
119 res
= PyString_FromStringAndSize (match
, len
);
131 struct ypall_callback cb
;
135 if (!PyArg_Parse(args
, "s", &map
))
137 if ((err
= yp_get_default_domain(&domain
)) != 0)
138 return nis_error(err
);
142 cb
.foreach
= (foreachfunc
)nis_foreach
;
143 cb
.data
= (char *)cat
;
144 Py_BEGIN_ALLOW_THREADS
145 map
= nis_mapname (map
);
146 err
= yp_all (domain
, map
, &cb
);
150 return nis_error(err
);
155 /* These should be u_long on Sun h/w but not on 64-bit h/w.
156 This is not portable to machines with 16-bit ints and no prototypes */
157 #ifndef YPPROC_MAPLIST
158 #define YPPROC_MAPLIST 11
161 #define YPPROG 100004
167 typedef char *domainname
;
168 typedef char *mapname
;
183 typedef enum nisstat nisstat
;
187 struct nismaplist
*next
;
189 typedef struct nismaplist nismaplist
;
191 struct nisresp_maplist
{
195 typedef struct nisresp_maplist nisresp_maplist
;
197 static struct timeval TIMEOUT
= { 25, 0 };
201 nis_xdr_domainname(xdrs
, objp
)
205 if (!xdr_string(xdrs
, objp
, YPMAXDOMAIN
)) {
213 nis_xdr_mapname(xdrs
, objp
)
217 if (!xdr_string(xdrs
, objp
, YPMAXMAP
)) {
225 nis_xdr_ypmaplist(xdrs
, objp
)
229 if (!nis_xdr_mapname(xdrs
, &objp
->map
)) {
232 if (!xdr_pointer(xdrs
, (char **)&objp
->next
,
233 sizeof(nismaplist
), (xdrproc_t
)nis_xdr_ypmaplist
))
242 nis_xdr_ypstat(xdrs
, objp
)
246 if (!xdr_enum(xdrs
, (enum_t
*)objp
)) {
255 nis_xdr_ypresp_maplist(xdrs
, objp
)
257 nisresp_maplist
*objp
;
259 if (!nis_xdr_ypstat(xdrs
, &objp
->stat
)) {
262 if (!xdr_pointer(xdrs
, (char **)&objp
->maps
,
263 sizeof(nismaplist
), (xdrproc_t
)nis_xdr_ypmaplist
))
273 nisproc_maplist_2(argp
, clnt
)
277 static nisresp_maplist res
;
279 memset(&res
, 0, sizeof(res
));
280 if (clnt_call(clnt
, YPPROC_MAPLIST
,
281 (xdrproc_t
)nis_xdr_domainname
, (caddr_t
)argp
,
282 (xdrproc_t
)nis_xdr_ypresp_maplist
, (caddr_t
)&res
,
283 TIMEOUT
) != RPC_SUCCESS
)
294 nisresp_maplist
*list
;
296 CLIENT
*cl
, *clnt_create();
301 if ((err
= yp_get_default_domain (&dom
)) != 0) {
306 while (!server
&& aliases
[mapi
].map
!= 0L) {
307 yp_master (dom
, aliases
[mapi
].map
, &server
);
311 PyErr_SetString(NisError
, "No NIS master found for any map");
314 cl
= clnt_create(server
, YPPROG
, YPVERS
, "tcp");
316 PyErr_SetString(NisError
, clnt_spcreateerror(server
));
319 list
= nisproc_maplist_2 (&dom
, cl
);
323 if (list
->stat
!= NIS_TRUE
)
335 nis_maps (self
, args
)
342 if (!PyArg_NoArgs(args
))
344 if ((maps
= nis_maplist ()) == NULL
)
346 if ((list
= PyList_New(0)) == NULL
)
348 for (maps
= maps
->next
; maps
; maps
= maps
->next
) {
349 PyObject
*str
= PyString_FromString(maps
->map
);
350 if (!str
|| PyList_Append(list
, str
) < 0)
358 /* XXX Shouldn't we free the list of maps now? */
362 static PyMethodDef nis_methods
[] = {
363 {"match", nis_match
},
366 {NULL
, NULL
} /* Sentinel */
373 m
= Py_InitModule("nis", nis_methods
);
374 d
= PyModule_GetDict(m
);
375 NisError
= PyErr_NewException("nis.error", NULL
, NULL
);
376 if (NisError
!= NULL
)
377 PyDict_SetItemString(d
, "error", NisError
);