1 /***********************************************************
3 Fred Gansevles <Fred.Gansevles@cs.utwente.nl>
5 Faculteit der Informatica,
9 ******************************************************************/
11 /* NIS module implementation */
13 #include "allobjects.h"
14 #include "modsupport.h"
17 #include <rpcsvc/ypclnt.h>
19 #include <sys/types.h>
21 #include <rpcsvc/yp_prot.h>
23 static object
*NisError
;
29 err_setstr(NisError
, yperr_string(err
));
33 static struct nis_map
{
37 {"passwd", "passwd.byname"},
38 {"group", "group.byname"},
39 {"networks", "networks.byaddr"},
40 {"hosts", "hosts.byname"},
41 {"protocols", "protocols.bynumber"},
42 {"services", "services.byname"},
43 {"aliases", "mail.aliases"},
44 {"ethers", "ethers.byname"},
54 for (i
=0; aliases
[i
].alias
!= 0L; i
++)
55 if (!strcmp (aliases
[i
].alias
, map
))
60 typedef int (*foreachfunc
) PROTO((int, char *, int, char *, int, char *));
63 nis_foreach (instatus
, inkey
, inkeylen
, inval
, invallen
, indata
)
71 if (instatus
== YP_TRUE
) {
72 object
*key
= newsizedstringobject(inkey
, inkeylen
);
73 object
*val
= newsizedstringobject(inval
, invallen
);
75 if (key
== NULL
|| val
== NULL
) {
76 /* XXX error -- don't know how to handle */
82 err
= mappinginsert(indata
, key
, val
);
95 nis_match (self
, args
)
106 if (!getargs(args
, "(s#s)", &key
, &keylen
, &map
))
108 if ((err
= yp_get_default_domain(&domain
)) != 0)
109 return nis_error(err
);
111 map
= nis_mapname (map
);
112 err
= yp_match (domain
, map
, key
, keylen
, &match
, &len
);
115 return nis_error(err
);
116 res
= newsizedstringobject (match
, len
);
128 struct ypall_callback cb
;
132 if (!getstrarg(args
, &map
))
134 if ((err
= yp_get_default_domain(&domain
)) != 0)
135 return nis_error(err
);
136 cat
= newdictobject ();
139 cb
.foreach
= (foreachfunc
)nis_foreach
;
140 cb
.data
= (char *)cat
;
142 map
= nis_mapname (map
);
143 err
= yp_all (domain
, map
, &cb
);
147 return nis_error(err
);
152 /* These should be u_long on Sun h/w but not on 64-bit h/w.
153 This is not portable to machines with 16-bit ints and no prototypes */
154 #ifndef YPPROC_MAPLIST
155 #define YPPROC_MAPLIST 11
158 #define YPPROG 100004
164 typedef char *domainname
;
165 typedef char *mapname
;
180 typedef enum nisstat nisstat
;
184 struct nismaplist
*next
;
186 typedef struct nismaplist nismaplist
;
188 struct nisresp_maplist
{
192 typedef struct nisresp_maplist nisresp_maplist
;
194 static struct timeval TIMEOUT
= { 25, 0 };
198 nis_xdr_domainname(xdrs
, objp
)
202 if (!xdr_string(xdrs
, objp
, YPMAXDOMAIN
)) {
210 nis_xdr_mapname(xdrs
, objp
)
214 if (!xdr_string(xdrs
, objp
, YPMAXMAP
)) {
222 nis_xdr_ypmaplist(xdrs
, objp
)
226 if (!nis_xdr_mapname(xdrs
, &objp
->map
)) {
229 if (!xdr_pointer(xdrs
, (char **)&objp
->next
, sizeof(nismaplist
), nis_xdr_ypmaplist
)) {
237 nis_xdr_ypstat(xdrs
, objp
)
241 if (!xdr_enum(xdrs
, (enum_t
*)objp
)) {
250 nis_xdr_ypresp_maplist(xdrs
, objp
)
252 nisresp_maplist
*objp
;
254 if (!nis_xdr_ypstat(xdrs
, &objp
->stat
)) {
257 if (!xdr_pointer(xdrs
, (char **)&objp
->maps
, sizeof(nismaplist
), nis_xdr_ypmaplist
)) {
266 nisproc_maplist_2(argp
, clnt
)
270 static nisresp_maplist res
;
272 memset(&res
, 0, sizeof(res
));
273 if (clnt_call(clnt
, YPPROC_MAPLIST
, nis_xdr_domainname
, (caddr_t
)argp
,
274 nis_xdr_ypresp_maplist
, (caddr_t
)&res
, TIMEOUT
)
285 nisresp_maplist
*list
;
287 CLIENT
*cl
, *clnt_create();
290 yp_get_default_domain (&dom
);
291 yp_master (dom
, aliases
[0].map
, &server
);
292 cl
= clnt_create(server
, YPPROG
, YPVERS
, "tcp");
294 clnt_pcreateerror(server
);
297 list
= nisproc_maplist_2 (&dom
, cl
);
300 if (list
->stat
!= NIS_TRUE
)
306 nis_maps (self
, args
)
313 if ((maps
= nis_maplist ()) == NULL
)
315 if ((list
= newlistobject(0)) == NULL
)
317 for (maps
= maps
->next
; maps
; maps
= maps
->next
) {
318 if (addlistitem (list
, newstringobject (maps
->map
)) < 0) {
324 /* XXX Shouldn't we free the list of maps now? */
328 static struct methodlist nis_methods
[] = {
329 {"match", nis_match
},
332 {NULL
, NULL
} /* Sentinel */
339 m
= initmodule("nis", nis_methods
);
340 d
= getmoduledict(m
);
341 NisError
= newstringobject("nis.error");
342 if (NisError
== NULL
|| dictinsert(d
, "error", NisError
) != 0)
343 fatal("Cannot define nis.error");