1992-08-12 22:57:12 +08:00
|
|
|
/***********************************************************
|
|
|
|
Written by:
|
|
|
|
Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
|
|
|
|
Vakgroep Spa,
|
|
|
|
Faculteit der Informatica,
|
|
|
|
Universiteit Twente,
|
|
|
|
Enschede,
|
|
|
|
the Netherlands.
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* NIS module implementation */
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
#include "Python.h"
|
1992-08-12 22:57:12 +08:00
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
1996-08-09 03:11:41 +08:00
|
|
|
#include <rpcsvc/ypclnt.h>
|
1992-08-12 22:57:12 +08:00
|
|
|
|
1996-12-10 02:46:28 +08:00
|
|
|
#ifdef __sgi
|
|
|
|
/* This is missing from rpcsvc/ypclnt.h */
|
|
|
|
extern int yp_get_default_domain();
|
|
|
|
#endif
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyObject *NisError;
|
1992-08-12 23:26:16 +08:00
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyObject *
|
1992-08-12 23:26:16 +08:00
|
|
|
nis_error (err)
|
|
|
|
int err;
|
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
PyErr_SetString(NisError, yperr_string(err));
|
1992-08-12 23:26:16 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1992-08-12 22:57:12 +08:00
|
|
|
static struct nis_map {
|
|
|
|
char *alias;
|
|
|
|
char *map;
|
|
|
|
} aliases [] = {
|
1992-08-12 23:26:16 +08:00
|
|
|
{"passwd", "passwd.byname"},
|
|
|
|
{"group", "group.byname"},
|
|
|
|
{"networks", "networks.byaddr"},
|
|
|
|
{"hosts", "hosts.byname"},
|
|
|
|
{"protocols", "protocols.bynumber"},
|
|
|
|
{"services", "services.byname"},
|
|
|
|
{"aliases", "mail.aliases"},
|
|
|
|
{"ethers", "ethers.byname"},
|
|
|
|
{0L, 0L}
|
1992-08-12 22:57:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static char *
|
|
|
|
nis_mapname (map)
|
1992-08-12 23:26:16 +08:00
|
|
|
char *map;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1992-08-12 23:26:16 +08:00
|
|
|
int i;
|
1992-08-12 22:57:12 +08:00
|
|
|
|
|
|
|
for (i=0; aliases[i].alias != 0L; i++)
|
|
|
|
if (!strcmp (aliases[i].alias, map))
|
|
|
|
map = aliases[i].map;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
typedef int (*foreachfunc) Py_PROTO((int, char *, int, char *, int, char *));
|
1994-08-01 19:34:53 +08:00
|
|
|
|
1992-08-12 22:57:12 +08:00
|
|
|
static int
|
1992-08-12 23:26:16 +08:00
|
|
|
nis_foreach (instatus, inkey, inkeylen, inval, invallen, indata)
|
|
|
|
int instatus;
|
|
|
|
char *inkey;
|
|
|
|
int inkeylen;
|
|
|
|
char *inval;
|
|
|
|
int invallen;
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *indata;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
|
|
|
if (instatus == YP_TRUE) {
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *key = PyString_FromStringAndSize(inkey, inkeylen);
|
|
|
|
PyObject *val = PyString_FromStringAndSize(inval, invallen);
|
1993-11-03 23:01:26 +08:00
|
|
|
int err;
|
|
|
|
if (key == NULL || val == NULL) {
|
|
|
|
/* XXX error -- don't know how to handle */
|
1996-12-11 08:15:58 +08:00
|
|
|
PyErr_Clear();
|
|
|
|
Py_XDECREF(key);
|
|
|
|
Py_XDECREF(val);
|
1993-11-03 23:01:26 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1996-12-11 08:15:58 +08:00
|
|
|
err = PyDict_SetItem(indata, key, val);
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(val);
|
1993-11-03 23:01:26 +08:00
|
|
|
if (err != 0) {
|
1996-12-11 08:15:58 +08:00
|
|
|
PyErr_Clear();
|
1993-11-03 23:01:26 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1992-08-12 22:57:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyObject *
|
1992-08-12 22:57:12 +08:00
|
|
|
nis_match (self, args)
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1992-08-12 23:26:16 +08:00
|
|
|
char *match;
|
|
|
|
char *domain;
|
1993-11-03 23:01:26 +08:00
|
|
|
int keylen, len;
|
1992-08-12 23:26:16 +08:00
|
|
|
char *key, *map;
|
|
|
|
int err;
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *res;
|
1992-08-12 22:57:12 +08:00
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!PyArg_Parse(args, "(s#s)", &key, &keylen, &map))
|
1992-08-12 22:57:12 +08:00
|
|
|
return NULL;
|
1992-08-12 23:26:16 +08:00
|
|
|
if ((err = yp_get_default_domain(&domain)) != 0)
|
|
|
|
return nis_error(err);
|
1996-12-11 08:15:58 +08:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-12 23:26:16 +08:00
|
|
|
map = nis_mapname (map);
|
1993-11-03 23:01:26 +08:00
|
|
|
err = yp_match (domain, map, key, keylen, &match, &len);
|
1996-12-11 08:15:58 +08:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-12 23:26:16 +08:00
|
|
|
if (err != 0)
|
|
|
|
return nis_error(err);
|
1996-12-11 08:15:58 +08:00
|
|
|
res = PyString_FromStringAndSize (match, len);
|
1992-08-12 23:26:16 +08:00
|
|
|
free (match);
|
|
|
|
return res;
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyObject *
|
1992-08-12 22:57:12 +08:00
|
|
|
nis_cat (self, args)
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1992-08-12 23:26:16 +08:00
|
|
|
char *domain;
|
|
|
|
char *map;
|
|
|
|
struct ypall_callback cb;
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *cat;
|
1992-08-12 23:26:16 +08:00
|
|
|
int err;
|
1992-08-12 22:57:12 +08:00
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!PyArg_Parse(args, "s", &map))
|
1992-08-12 22:57:12 +08:00
|
|
|
return NULL;
|
1992-08-12 23:26:16 +08:00
|
|
|
if ((err = yp_get_default_domain(&domain)) != 0)
|
|
|
|
return nis_error(err);
|
1996-12-11 08:15:58 +08:00
|
|
|
cat = PyDict_New ();
|
1992-08-12 22:57:12 +08:00
|
|
|
if (cat == NULL)
|
|
|
|
return NULL;
|
1994-08-01 19:34:53 +08:00
|
|
|
cb.foreach = (foreachfunc)nis_foreach;
|
1992-08-12 22:57:12 +08:00
|
|
|
cb.data = (char *)cat;
|
1996-12-11 08:15:58 +08:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-12 23:26:16 +08:00
|
|
|
map = nis_mapname (map);
|
|
|
|
err = yp_all (domain, map, &cb);
|
1996-12-11 08:15:58 +08:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-12 23:26:16 +08:00
|
|
|
if (err != 0) {
|
1996-12-11 08:15:58 +08:00
|
|
|
Py_DECREF(cat);
|
1992-08-12 23:26:16 +08:00
|
|
|
return nis_error(err);
|
|
|
|
}
|
1992-08-12 22:57:12 +08:00
|
|
|
return cat;
|
|
|
|
}
|
|
|
|
|
1994-08-01 19:34:53 +08:00
|
|
|
/* These should be u_long on Sun h/w but not on 64-bit h/w.
|
|
|
|
This is not portable to machines with 16-bit ints and no prototypes */
|
|
|
|
#ifndef YPPROC_MAPLIST
|
|
|
|
#define YPPROC_MAPLIST 11
|
|
|
|
#endif
|
|
|
|
#ifndef YPPROG
|
|
|
|
#define YPPROG 100004
|
|
|
|
#endif
|
|
|
|
#ifndef YPVERS
|
|
|
|
#define YPVERS 2
|
|
|
|
#endif
|
1992-08-12 22:57:12 +08:00
|
|
|
|
|
|
|
typedef char *domainname;
|
|
|
|
typedef char *mapname;
|
|
|
|
|
|
|
|
enum nisstat {
|
1996-12-11 08:15:58 +08:00
|
|
|
NIS_TRUE = 1,
|
|
|
|
NIS_NOMORE = 2,
|
|
|
|
NIS_FALSE = 0,
|
|
|
|
NIS_NOMAP = -1,
|
|
|
|
NIS_NODOM = -2,
|
|
|
|
NIS_NOKEY = -3,
|
|
|
|
NIS_BADOP = -4,
|
|
|
|
NIS_BADDB = -5,
|
|
|
|
NIS_YPERR = -6,
|
|
|
|
NIS_BADARGS = -7,
|
|
|
|
NIS_VERS = -8
|
1992-08-12 22:57:12 +08:00
|
|
|
};
|
|
|
|
typedef enum nisstat nisstat;
|
|
|
|
|
|
|
|
struct nismaplist {
|
1996-12-11 08:15:58 +08:00
|
|
|
mapname map;
|
|
|
|
struct nismaplist *next;
|
1992-08-12 22:57:12 +08:00
|
|
|
};
|
|
|
|
typedef struct nismaplist nismaplist;
|
|
|
|
|
|
|
|
struct nisresp_maplist {
|
|
|
|
nisstat stat;
|
|
|
|
nismaplist *maps;
|
|
|
|
};
|
|
|
|
typedef struct nisresp_maplist nisresp_maplist;
|
|
|
|
|
|
|
|
static struct timeval TIMEOUT = { 25, 0 };
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
|
|
|
nis_xdr_domainname(xdrs, objp)
|
1996-12-11 08:15:58 +08:00
|
|
|
XDR *xdrs;
|
|
|
|
domainname *objp;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
|
|
|
nis_xdr_mapname(xdrs, objp)
|
1996-12-11 08:15:58 +08:00
|
|
|
XDR *xdrs;
|
|
|
|
mapname *objp;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!xdr_string(xdrs, objp, YPMAXMAP)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
|
|
|
nis_xdr_ypmaplist(xdrs, objp)
|
1996-12-11 08:15:58 +08:00
|
|
|
XDR *xdrs;
|
|
|
|
nismaplist *objp;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!nis_xdr_mapname(xdrs, &objp->map)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!xdr_pointer(xdrs, (char **)&objp->next,
|
|
|
|
sizeof(nismaplist), nis_xdr_ypmaplist))
|
|
|
|
{
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
|
|
|
nis_xdr_ypstat(xdrs, objp)
|
1996-12-11 08:15:58 +08:00
|
|
|
XDR *xdrs;
|
|
|
|
nisstat *objp;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!xdr_enum(xdrs, (enum_t *)objp)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
|
|
|
nis_xdr_ypresp_maplist(xdrs, objp)
|
1996-12-11 08:15:58 +08:00
|
|
|
XDR *xdrs;
|
|
|
|
nisresp_maplist *objp;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!xdr_pointer(xdrs, (char **)&objp->maps,
|
|
|
|
sizeof(nismaplist), nis_xdr_ypmaplist))
|
|
|
|
{
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
nisresp_maplist *
|
|
|
|
nisproc_maplist_2(argp, clnt)
|
1996-12-11 08:15:58 +08:00
|
|
|
domainname *argp;
|
|
|
|
CLIENT *clnt;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
static nisresp_maplist res;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
|
|
|
if (clnt_call(clnt, YPPROC_MAPLIST, nis_xdr_domainname, (caddr_t)argp,
|
|
|
|
nis_xdr_ypresp_maplist, (caddr_t)&res, TIMEOUT)
|
|
|
|
!= RPC_SUCCESS) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (&res);
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
nismaplist *
|
|
|
|
nis_maplist ()
|
|
|
|
{
|
1992-08-12 23:26:16 +08:00
|
|
|
nisresp_maplist *list;
|
|
|
|
char *dom;
|
|
|
|
CLIENT *cl, *clnt_create();
|
1996-12-11 08:15:58 +08:00
|
|
|
char *server = "";
|
|
|
|
int mapi = 0;
|
1992-08-12 22:57:12 +08:00
|
|
|
|
|
|
|
yp_get_default_domain (&dom);
|
1996-12-11 08:15:58 +08:00
|
|
|
while (!strcmp("", server) && aliases[mapi].map != 0L) {
|
|
|
|
yp_master (dom, aliases[mapi].map, &server);
|
|
|
|
mapi++;
|
|
|
|
}
|
|
|
|
if (!strcmp("", server)) {
|
|
|
|
PyErr_SetString(NisError, "No NIS master found for any map");
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-12 22:57:12 +08:00
|
|
|
cl = clnt_create(server, YPPROG, YPVERS, "tcp");
|
|
|
|
if (cl == NULL) {
|
1996-12-11 08:15:58 +08:00
|
|
|
PyErr_SetString(NisError, clnt_spcreateerror(server));
|
1992-08-12 22:57:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list = nisproc_maplist_2 (&dom, cl);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (list->stat != NIS_TRUE)
|
|
|
|
return NULL;
|
|
|
|
return list->maps;
|
|
|
|
}
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyObject *
|
1992-08-12 22:57:12 +08:00
|
|
|
nis_maps (self, args)
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-08-12 22:57:12 +08:00
|
|
|
{
|
1992-08-12 23:26:16 +08:00
|
|
|
nismaplist *maps;
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *list;
|
1992-08-12 22:57:12 +08:00
|
|
|
|
|
|
|
if ((maps = nis_maplist ()) == NULL)
|
|
|
|
return NULL;
|
1996-12-11 08:15:58 +08:00
|
|
|
if ((list = PyList_New(0)) == NULL)
|
1992-08-12 22:57:12 +08:00
|
|
|
return NULL;
|
1992-08-12 23:26:16 +08:00
|
|
|
for (maps = maps->next; maps; maps = maps->next) {
|
1996-12-11 08:15:58 +08:00
|
|
|
if (PyList_Append (list, PyString_FromString (maps->map)) < 0)
|
|
|
|
{
|
|
|
|
Py_DECREF(list);
|
1992-08-12 23:26:16 +08:00
|
|
|
list = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXX Shouldn't we free the list of maps now? */
|
1992-08-12 22:57:12 +08:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1996-12-11 08:15:58 +08:00
|
|
|
static PyMethodDef nis_methods[] = {
|
1992-08-12 22:57:12 +08:00
|
|
|
{"match", nis_match},
|
|
|
|
{"cat", nis_cat},
|
|
|
|
{"maps", nis_maps},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
initnis ()
|
|
|
|
{
|
1996-12-11 08:15:58 +08:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = Py_InitModule("nis", nis_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
NisError = PyString_FromString("nis.error");
|
|
|
|
if (NisError == NULL ||
|
|
|
|
PyDict_SetItemString(d, "error", NisError) != 0)
|
|
|
|
Py_FatalError("Cannot define nis.error");
|
1992-08-12 22:57:12 +08:00
|
|
|
}
|