1992-08-12 14:57:12 +00:00
|
|
|
/***********************************************************
|
|
|
|
Written by:
|
|
|
|
Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
|
2000-02-29 15:52:40 +00:00
|
|
|
B&O group,
|
1992-08-12 14:57:12 +00:00
|
|
|
Faculteit der Informatica,
|
|
|
|
Universiteit Twente,
|
|
|
|
Enschede,
|
|
|
|
the Netherlands.
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* NIS module implementation */
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
#include "Python.h"
|
1992-08-12 14:57:12 +00:00
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
1996-08-08 19:11:41 +00:00
|
|
|
#include <rpcsvc/ypclnt.h>
|
1992-08-12 14:57:12 +00:00
|
|
|
|
1996-12-09 18:46:28 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
/* This is missing from rpcsvc/ypclnt.h */
|
2000-07-22 23:57:55 +00:00
|
|
|
extern int yp_get_default_domain(char **);
|
1996-12-09 18:46:28 +00:00
|
|
|
#endif
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyObject *NisError;
|
1992-08-12 15:26:16 +00:00
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyObject *
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_error (int err)
|
1992-08-12 15:26:16 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
PyErr_SetString(NisError, yperr_string(err));
|
1992-08-12 15:26:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1992-08-12 14:57:12 +00:00
|
|
|
static struct nis_map {
|
|
|
|
char *alias;
|
|
|
|
char *map;
|
2000-02-29 15:52:40 +00:00
|
|
|
int fix;
|
1992-08-12 14:57:12 +00:00
|
|
|
} aliases [] = {
|
2000-02-29 15:52:40 +00:00
|
|
|
{"passwd", "passwd.byname", 0},
|
|
|
|
{"group", "group.byname", 0},
|
|
|
|
{"networks", "networks.byaddr", 0},
|
|
|
|
{"hosts", "hosts.byname", 0},
|
|
|
|
{"protocols", "protocols.bynumber", 0},
|
|
|
|
{"services", "services.byname", 0},
|
|
|
|
{"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */
|
|
|
|
{"ethers", "ethers.byname", 0},
|
|
|
|
{0L, 0L, 0}
|
1992-08-12 14:57:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static char *
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_mapname (char *map, int *pfix)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1992-08-12 15:26:16 +00:00
|
|
|
int i;
|
1992-08-12 14:57:12 +00:00
|
|
|
|
2000-02-29 15:52:40 +00:00
|
|
|
*pfix = 0;
|
|
|
|
for (i=0; aliases[i].alias != 0L; i++) {
|
|
|
|
if (!strcmp (aliases[i].alias, map)) {
|
|
|
|
*pfix = aliases[i].fix;
|
|
|
|
return aliases[i].map;
|
|
|
|
}
|
|
|
|
if (!strcmp (aliases[i].map, map)) {
|
|
|
|
*pfix = aliases[i].fix;
|
|
|
|
return aliases[i].map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1992-08-12 14:57:12 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2000-07-09 03:09:57 +00:00
|
|
|
typedef int (*foreachfunc)(int, char *, int, char *, int, char *);
|
1994-08-01 11:34:53 +00:00
|
|
|
|
2000-02-29 15:52:40 +00:00
|
|
|
struct ypcallback_data {
|
|
|
|
PyObject *dict;
|
|
|
|
int fix;
|
|
|
|
};
|
|
|
|
|
1992-08-12 14:57:12 +00:00
|
|
|
static int
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_foreach (int instatus, char *inkey, int inkeylen, char *inval,
|
|
|
|
int invallen, struct ypcallback_data *indata)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
|
|
|
if (instatus == YP_TRUE) {
|
2000-02-29 15:52:40 +00:00
|
|
|
PyObject *key;
|
|
|
|
PyObject *val;
|
1993-11-03 15:01:26 +00:00
|
|
|
int err;
|
2000-02-29 15:52:40 +00:00
|
|
|
|
|
|
|
if (indata->fix) {
|
|
|
|
inkeylen--;
|
|
|
|
invallen--;
|
|
|
|
}
|
|
|
|
key = PyString_FromStringAndSize(inkey, inkeylen);
|
|
|
|
val = PyString_FromStringAndSize(inval, invallen);
|
1993-11-03 15:01:26 +00:00
|
|
|
if (key == NULL || val == NULL) {
|
|
|
|
/* XXX error -- don't know how to handle */
|
1996-12-11 00:15:58 +00:00
|
|
|
PyErr_Clear();
|
|
|
|
Py_XDECREF(key);
|
|
|
|
Py_XDECREF(val);
|
1993-11-03 15:01:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2000-02-29 15:52:40 +00:00
|
|
|
err = PyDict_SetItem(indata->dict, key, val);
|
1996-12-11 00:15:58 +00:00
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(val);
|
1993-11-03 15:01:26 +00:00
|
|
|
if (err != 0) {
|
1996-12-11 00:15:58 +00:00
|
|
|
PyErr_Clear();
|
1993-11-03 15:01:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1992-08-12 14:57:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyObject *
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_match (PyObject *self, PyObject *args)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1992-08-12 15:26:16 +00:00
|
|
|
char *match;
|
|
|
|
char *domain;
|
1993-11-03 15:01:26 +00:00
|
|
|
int keylen, len;
|
1992-08-12 15:26:16 +00:00
|
|
|
char *key, *map;
|
|
|
|
int err;
|
1996-12-11 00:15:58 +00:00
|
|
|
PyObject *res;
|
2000-02-29 15:52:40 +00:00
|
|
|
int fix;
|
1992-08-12 14:57:12 +00:00
|
|
|
|
1998-10-08 02:25:24 +00:00
|
|
|
if (!PyArg_Parse(args, "(t#s)", &key, &keylen, &map))
|
1992-08-12 14:57:12 +00:00
|
|
|
return NULL;
|
1992-08-12 15:26:16 +00:00
|
|
|
if ((err = yp_get_default_domain(&domain)) != 0)
|
|
|
|
return nis_error(err);
|
2000-02-29 15:52:40 +00:00
|
|
|
map = nis_mapname (map, &fix);
|
|
|
|
if (fix)
|
|
|
|
keylen++;
|
1996-12-11 00:15:58 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1993-11-03 15:01:26 +00:00
|
|
|
err = yp_match (domain, map, key, keylen, &match, &len);
|
1996-12-11 00:15:58 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
2000-02-29 15:52:40 +00:00
|
|
|
if (fix)
|
|
|
|
len--;
|
1992-08-12 15:26:16 +00:00
|
|
|
if (err != 0)
|
|
|
|
return nis_error(err);
|
1996-12-11 00:15:58 +00:00
|
|
|
res = PyString_FromStringAndSize (match, len);
|
1992-08-12 15:26:16 +00:00
|
|
|
free (match);
|
|
|
|
return res;
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyObject *
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_cat (PyObject *self, PyObject *args)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1992-08-12 15:26:16 +00:00
|
|
|
char *domain;
|
|
|
|
char *map;
|
|
|
|
struct ypall_callback cb;
|
2000-02-29 15:52:40 +00:00
|
|
|
struct ypcallback_data data;
|
|
|
|
PyObject *dict;
|
1992-08-12 15:26:16 +00:00
|
|
|
int err;
|
1992-08-12 14:57:12 +00:00
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!PyArg_Parse(args, "s", &map))
|
1992-08-12 14:57:12 +00:00
|
|
|
return NULL;
|
1992-08-12 15:26:16 +00:00
|
|
|
if ((err = yp_get_default_domain(&domain)) != 0)
|
|
|
|
return nis_error(err);
|
2000-02-29 15:52:40 +00:00
|
|
|
dict = PyDict_New ();
|
|
|
|
if (dict == NULL)
|
1992-08-12 14:57:12 +00:00
|
|
|
return NULL;
|
1994-08-01 11:34:53 +00:00
|
|
|
cb.foreach = (foreachfunc)nis_foreach;
|
2000-02-29 15:52:40 +00:00
|
|
|
data.dict = dict;
|
|
|
|
map = nis_mapname (map, &data.fix);
|
|
|
|
cb.data = (char *)&data;
|
1996-12-11 00:15:58 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1992-08-12 15:26:16 +00:00
|
|
|
err = yp_all (domain, map, &cb);
|
1996-12-11 00:15:58 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1992-08-12 15:26:16 +00:00
|
|
|
if (err != 0) {
|
2000-02-29 15:52:40 +00:00
|
|
|
Py_DECREF(dict);
|
1992-08-12 15:26:16 +00:00
|
|
|
return nis_error(err);
|
|
|
|
}
|
2000-02-29 15:52:40 +00:00
|
|
|
return dict;
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
1994-08-01 11:34:53 +00: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 14:57:12 +00:00
|
|
|
|
|
|
|
typedef char *domainname;
|
|
|
|
typedef char *mapname;
|
|
|
|
|
|
|
|
enum nisstat {
|
1996-12-11 00:15:58 +00: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 14:57:12 +00:00
|
|
|
};
|
|
|
|
typedef enum nisstat nisstat;
|
|
|
|
|
|
|
|
struct nismaplist {
|
1996-12-11 00:15:58 +00:00
|
|
|
mapname map;
|
|
|
|
struct nismaplist *next;
|
1992-08-12 14:57:12 +00: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
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_xdr_domainname(XDR *xdrs, domainname *objp)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_xdr_mapname(XDR *xdrs, mapname *objp)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!xdr_string(xdrs, objp, YPMAXMAP)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!nis_xdr_mapname(xdrs, &objp->map)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!xdr_pointer(xdrs, (char **)&objp->next,
|
1998-10-07 16:36:14 +00:00
|
|
|
sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
|
1996-12-11 00:15:58 +00:00
|
|
|
{
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_xdr_ypstat(XDR *xdrs, nisstat *objp)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!xdr_enum(xdrs, (enum_t *)objp)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
bool_t
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
if (!nis_xdr_ypstat(xdrs, &objp->stat)) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
if (!xdr_pointer(xdrs, (char **)&objp->maps,
|
1998-10-07 16:36:14 +00:00
|
|
|
sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist))
|
1996-12-11 00:15:58 +00:00
|
|
|
{
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
return (TRUE);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
nisresp_maplist *
|
2000-07-10 13:12:27 +00:00
|
|
|
nisproc_maplist_2(domainname *argp, CLIENT *clnt)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
static nisresp_maplist res;
|
|
|
|
|
|
|
|
memset(&res, 0, sizeof(res));
|
1998-10-07 16:36:14 +00:00
|
|
|
if (clnt_call(clnt, YPPROC_MAPLIST,
|
|
|
|
(xdrproc_t)nis_xdr_domainname, (caddr_t)argp,
|
|
|
|
(xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res,
|
|
|
|
TIMEOUT) != RPC_SUCCESS)
|
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (&res);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
nismaplist *
|
2000-07-21 06:00:07 +00:00
|
|
|
nis_maplist (void)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1992-08-12 15:26:16 +00:00
|
|
|
nisresp_maplist *list;
|
|
|
|
char *dom;
|
|
|
|
CLIENT *cl, *clnt_create();
|
1997-01-09 22:22:05 +00:00
|
|
|
char *server = NULL;
|
1996-12-11 00:15:58 +00:00
|
|
|
int mapi = 0;
|
1996-12-11 01:00:46 +00:00
|
|
|
int err;
|
1992-08-12 14:57:12 +00:00
|
|
|
|
1996-12-11 01:00:46 +00:00
|
|
|
if ((err = yp_get_default_domain (&dom)) != 0) {
|
|
|
|
nis_error(err);
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-12-11 00:29:14 +00:00
|
|
|
|
1997-01-09 22:22:05 +00:00
|
|
|
while (!server && aliases[mapi].map != 0L) {
|
1996-12-11 00:15:58 +00:00
|
|
|
yp_master (dom, aliases[mapi].map, &server);
|
|
|
|
mapi++;
|
|
|
|
}
|
1997-01-09 22:22:05 +00:00
|
|
|
if (!server) {
|
1996-12-11 00:15:58 +00:00
|
|
|
PyErr_SetString(NisError, "No NIS master found for any map");
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-12 14:57:12 +00:00
|
|
|
cl = clnt_create(server, YPPROG, YPVERS, "tcp");
|
|
|
|
if (cl == NULL) {
|
1996-12-11 00:15:58 +00:00
|
|
|
PyErr_SetString(NisError, clnt_spcreateerror(server));
|
1997-01-09 22:22:05 +00:00
|
|
|
goto finally;
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
list = nisproc_maplist_2 (&dom, cl);
|
1997-01-09 22:22:05 +00:00
|
|
|
clnt_destroy(cl);
|
1992-08-12 14:57:12 +00:00
|
|
|
if (list == NULL)
|
1997-01-09 22:22:05 +00:00
|
|
|
goto finally;
|
1992-08-12 14:57:12 +00:00
|
|
|
if (list->stat != NIS_TRUE)
|
1997-01-09 22:22:05 +00:00
|
|
|
goto finally;
|
|
|
|
|
2000-05-03 23:44:39 +00:00
|
|
|
free(server);
|
1992-08-12 14:57:12 +00:00
|
|
|
return list->maps;
|
1997-01-09 22:22:05 +00:00
|
|
|
|
|
|
|
finally:
|
2000-05-03 23:44:39 +00:00
|
|
|
free(server);
|
1997-01-09 22:22:05 +00:00
|
|
|
return NULL;
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyObject *
|
2000-07-10 13:12:27 +00:00
|
|
|
nis_maps (PyObject *self, PyObject *args)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1992-08-12 15:26:16 +00:00
|
|
|
nismaplist *maps;
|
1996-12-11 00:15:58 +00:00
|
|
|
PyObject *list;
|
1992-08-12 14:57:12 +00:00
|
|
|
|
1996-12-11 00:29:14 +00:00
|
|
|
if (!PyArg_NoArgs(args))
|
|
|
|
return NULL;
|
1992-08-12 14:57:12 +00:00
|
|
|
if ((maps = nis_maplist ()) == NULL)
|
|
|
|
return NULL;
|
1996-12-11 00:15:58 +00:00
|
|
|
if ((list = PyList_New(0)) == NULL)
|
1992-08-12 14:57:12 +00:00
|
|
|
return NULL;
|
2000-02-29 15:52:40 +00:00
|
|
|
for (maps = maps; maps; maps = maps->next) {
|
1997-01-09 22:22:05 +00:00
|
|
|
PyObject *str = PyString_FromString(maps->map);
|
|
|
|
if (!str || PyList_Append(list, str) < 0)
|
1996-12-11 00:15:58 +00:00
|
|
|
{
|
|
|
|
Py_DECREF(list);
|
1992-08-12 15:26:16 +00:00
|
|
|
list = NULL;
|
|
|
|
break;
|
|
|
|
}
|
1997-01-09 22:22:05 +00:00
|
|
|
Py_DECREF(str);
|
1992-08-12 15:26:16 +00:00
|
|
|
}
|
|
|
|
/* XXX Shouldn't we free the list of maps now? */
|
1992-08-12 14:57:12 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1996-12-11 00:15:58 +00:00
|
|
|
static PyMethodDef nis_methods[] = {
|
1992-08-12 14:57:12 +00:00
|
|
|
{"match", nis_match},
|
|
|
|
{"cat", nis_cat},
|
|
|
|
{"maps", nis_maps},
|
|
|
|
{NULL, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2000-07-21 06:00:07 +00:00
|
|
|
initnis (void)
|
1992-08-12 14:57:12 +00:00
|
|
|
{
|
1996-12-11 00:15:58 +00:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = Py_InitModule("nis", nis_methods);
|
|
|
|
d = PyModule_GetDict(m);
|
1997-10-01 04:29:29 +00:00
|
|
|
NisError = PyErr_NewException("nis.error", NULL, NULL);
|
|
|
|
if (NisError != NULL)
|
|
|
|
PyDict_SetItemString(d, "error", NisError);
|
1992-08-12 14:57:12 +00:00
|
|
|
}
|