1 /* $OpenBSD: yp.c,v 1.14 2015/02/11 01:26:00 pelikan Exp $ */
3 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/select.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
40 #include <rpc/pmap_clnt.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_rmt.h>
43 #include <rpcsvc/yp.h>
44 #include <rpcsvc/ypclnt.h>
48 void yp_dispatch(struct svc_req *, SVCXPRT *);
49 void yp_disable_events(void);
50 void yp_fd_event(int, short, void *);
51 int yp_check(struct svc_req *);
52 int yp_valid_domain(char *, struct ypresp_val *);
53 void yp_make_val(struct ypresp_val *, char *, int);
54 void yp_make_keyval(struct ypresp_key_val *, char *, char *);
56 static struct env *env;
59 TAILQ_ENTRY(yp_event) ye_entry;
60 struct event ye_event;
64 SVCXPRT *yp_trans_udp;
65 SVCXPRT *yp_trans_tcp;
66 TAILQ_HEAD(, yp_event) yd_events;
70 yp_disable_events(void)
74 while ((ye = TAILQ_FIRST(&env->sc_yp->yd_events)) != NULL) {
75 TAILQ_REMOVE(&env->sc_yp->yd_events, ye, ye_entry);
76 event_del(&ye->ye_event);
82 yp_enable_events(void)
87 for (i = 0; i < getdtablesize(); i++) {
88 if ((ye = calloc(1, sizeof(*ye))) == NULL)
90 event_set(&ye->ye_event, i, EV_READ, yp_fd_event, NULL);
91 event_add(&ye->ye_event, NULL);
92 TAILQ_INSERT_TAIL(&env->sc_yp->yd_events, ye, ye_entry);
97 yp_fd_event(int fd, short event, void *p)
99 svc_getreq_common(fd);
105 yp_init(struct env *x_env)
109 if ((yp = calloc(1, sizeof(*yp))) == NULL)
111 TAILQ_INIT(&yp->yd_events);
116 (void)pmap_unset(YPPROG, YPVERS);
118 if ((yp->yp_trans_udp = svcudp_create(RPC_ANYSOCK)) == NULL)
119 fatal("cannot create udp service");
120 if ((yp->yp_trans_tcp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL)
121 fatal("cannot create tcp service");
123 if (!svc_register(yp->yp_trans_udp, YPPROG, YPVERS,
124 yp_dispatch, IPPROTO_UDP)) {
125 fatal("unable to register (YPPROG, YPVERS, udp)");
127 if (!svc_register(yp->yp_trans_tcp, YPPROG, YPVERS,
128 yp_dispatch, IPPROTO_TCP)) {
129 fatal("unable to register (YPPROG, YPVERS, tcp)");
134 * lots of inspiration from ypserv by Mats O Jansson
137 yp_dispatch(struct svc_req *req, SVCXPRT *trans)
139 xdrproc_t xdr_argument;
140 xdrproc_t xdr_result;
142 char *(*cb)(char *, struct svc_req *);
144 domainname ypproc_domain_2_arg;
145 domainname ypproc_domain_nonack_2_arg;
146 ypreq_key ypproc_match_2_arg;
147 ypreq_nokey ypproc_first_2_arg;
148 ypreq_key ypproc_next_2_arg;
149 ypreq_xfr ypproc_xfr_2_arg;
150 ypreq_nokey ypproc_all_2_arg;
151 ypreq_nokey ypproc_master_2_arg;
152 ypreq_nokey ypproc_order_2_arg;
153 domainname ypproc_maplist_2_arg;
156 xdr_argument = (xdrproc_t) xdr_void;
157 xdr_result = (xdrproc_t) xdr_void;
159 switch (req->rq_proc) {
161 xdr_argument = (xdrproc_t) xdr_void;
162 xdr_result = (xdrproc_t) xdr_void;
163 if (yp_check(req) == -1)
166 if (!svc_sendreply(trans, (xdrproc_t) xdr_void,
168 svcerr_systemerr(trans);
171 xdr_argument = (xdrproc_t) xdr_domainname;
172 xdr_result = (xdrproc_t) xdr_bool;
173 if (yp_check(req) == -1)
175 cb = (void *)ypproc_domain_2_svc;
177 case YPPROC_DOMAIN_NONACK:
178 xdr_argument = (xdrproc_t) xdr_domainname;
179 xdr_result = (xdrproc_t) xdr_bool;
180 if (yp_check(req) == -1)
182 cb = (void *)ypproc_domain_nonack_2_svc;
185 xdr_argument = (xdrproc_t) xdr_ypreq_key;
186 xdr_result = (xdrproc_t) xdr_ypresp_val;
187 if (yp_check(req) == -1)
189 cb = (void *)ypproc_match_2_svc;
192 xdr_argument = (xdrproc_t) xdr_ypreq_nokey;
193 xdr_result = (xdrproc_t) xdr_ypresp_key_val;
194 if (yp_check(req) == -1)
196 cb = (void *)ypproc_first_2_svc;
199 xdr_argument = (xdrproc_t) xdr_ypreq_key;
200 xdr_result = (xdrproc_t) xdr_ypresp_key_val;
201 if (yp_check(req) == -1)
203 cb = (void *)ypproc_next_2_svc;
206 if (yp_check(req) == -1)
208 svcerr_noproc(trans);
211 log_debug("ypproc_clear");
212 if (yp_check(req) == -1)
214 svcerr_noproc(trans);
217 log_debug("ypproc_all");
218 xdr_argument = (xdrproc_t) xdr_ypreq_nokey;
219 xdr_result = (xdrproc_t) xdr_ypresp_all;
220 if (yp_check(req) == -1)
222 cb = (void *)ypproc_all_2_svc;
225 log_debug("ypproc_master");
226 xdr_argument = (xdrproc_t) xdr_ypreq_nokey;
227 xdr_result = (xdrproc_t) xdr_ypresp_master;
228 if (yp_check(req) == -1)
230 cb = (void *)ypproc_master_2_svc;
233 log_debug("ypproc_order");
234 if (yp_check(req) == -1)
236 svcerr_noproc(trans);
239 log_debug("ypproc_maplist");
240 xdr_argument = (xdrproc_t) xdr_domainname;
241 xdr_result = (xdrproc_t) xdr_ypresp_maplist;
242 if (yp_check(req) == -1)
244 cb = (void *)ypproc_maplist_2_svc;
247 svcerr_noproc(trans);
250 (void)memset(&argument, 0, sizeof(argument));
252 if (!svc_getargs(trans, xdr_argument, (caddr_t)&argument)) {
253 svcerr_decode(trans);
256 result = (*cb)((char *)&argument, req);
257 if (result != NULL && !svc_sendreply(trans, xdr_result, result))
258 svcerr_systemerr(trans);
259 if (!svc_freeargs(trans, xdr_argument, (caddr_t)&argument)) {
261 * ypserv does it too.
263 fatal("unable to free arguments");
268 yp_check(struct svc_req *req)
271 * We might want to know who we allow here.
277 yp_valid_domain(char *domain, struct ypresp_val *res)
279 if (domain == NULL) {
280 log_debug("NULL domain !");
283 if (strcmp(domain, env->sc_domainname) != 0) {
284 res->stat = YP_NODOM;
291 ypproc_domain_2_svc(domainname *arg, struct svc_req *req)
296 if (strcmp(*arg, env->sc_domainname) != 0)
302 ypproc_domain_nonack_2_svc(domainname *arg, struct svc_req *req)
306 if (strcmp(*arg, env->sc_domainname) != 0)
313 ypproc_match_2_svc(ypreq_key *arg, struct svc_req *req)
317 struct groupent gkey;
319 static struct ypresp_val res;
324 log_debug("matching '%.*s' in map %s", arg->key.keydat_len,
325 arg->key.keydat_val, arg->map);
327 if (yp_valid_domain(arg->domain, (struct ypresp_val *)&res) == -1)
330 if (env->sc_user_names == NULL) {
337 if (arg->key.keydat_len > YPMAXRECORD) {
338 log_debug("argument too long");
341 key = calloc(arg->key.keydat_len + 1, 1);
344 (void)strncpy(key, arg->key.keydat_val, arg->key.keydat_len);
346 if (strcmp(arg->map, "passwd.byname") == 0 ||
347 strcmp(arg->map, "master.passwd.byname") == 0) {
349 if ((ue = RB_FIND(user_name_tree, env->sc_user_names,
355 yp_make_val(&res, ue->ue_line, 1);
357 } else if (strcmp(arg->map, "passwd.byuid") == 0 ||
358 strcmp(arg->map, "master.passwd.byuid") == 0) {
359 ukey.ue_uid = strtonum(key, 0, UID_MAX, &estr);
361 res.stat = YP_BADARGS;
365 if ((ue = RB_FIND(user_uid_tree, &env->sc_user_uids,
371 yp_make_val(&res, ue->ue_line, 1);
373 } else if (strcmp(arg->map, "group.bygid") == 0) {
374 gkey.ge_gid = strtonum(key, 0, GID_MAX, &estr);
376 res.stat = YP_BADARGS;
379 if ((ge = RB_FIND(group_gid_tree, &env->sc_group_gids,
385 yp_make_val(&res, ge->ge_line, 1);
387 } else if (strcmp(arg->map, "group.byname") == 0) {
389 if ((ge = RB_FIND(group_name_tree, env->sc_group_names,
395 yp_make_val(&res, ge->ge_line, 1);
397 } else if (strcmp(arg->map, "netid.byname") == 0) {
400 if (strncmp(bp, "unix.", strlen("unix.")) != 0) {
401 res.stat = YP_BADARGS;
405 bp += strlen("unix.");
408 res.stat = YP_BADARGS;
412 if (!(cp = strsep(&bp, "@"))) {
413 res.stat = YP_BADARGS;
417 if (strcmp(bp, arg->domain) != 0) {
418 res.stat = YP_BADARGS;
422 ukey.ue_uid = strtonum(cp, 0, UID_MAX, &estr);
424 res.stat = YP_BADARGS;
428 if ((ue = RB_FIND(user_uid_tree, &env->sc_user_uids,
434 yp_make_val(&res, ue->ue_netid_line, 0);
438 log_debug("unknown map %s", arg->map);
448 ypproc_first_2_svc(ypreq_nokey *arg, struct svc_req *req)
450 static struct ypresp_key_val res;
452 if (yp_valid_domain(arg->domain, (struct ypresp_val *)&res) == -1)
455 if (strcmp(arg->map, "passwd.byname") == 0 ||
456 strcmp(arg->map, "master.passwd.byname") == 0) {
457 if (env->sc_user_lines == NULL)
460 yp_make_keyval(&res, env->sc_user_lines, env->sc_user_lines);
461 } else if (strcmp(arg->map, "group.byname") == 0) {
462 if (env->sc_group_lines == NULL)
465 yp_make_keyval(&res, env->sc_group_lines, env->sc_group_lines);
467 log_debug("unknown map %s", arg->map);
475 ypproc_next_2_svc(ypreq_key *arg, struct svc_req *req)
479 struct groupent gkey;
482 static struct ypresp_key_val res;
485 if (yp_valid_domain(arg->domain, (struct ypresp_val *)&res) == -1)
489 if (strcmp(arg->map, "passwd.byname") == 0 ||
490 strcmp(arg->map, "master.passwd.byname") == 0) {
491 key = calloc(arg->key.keydat_len + 1, 1);
496 (void)strncpy(key, arg->key.keydat_val,
497 arg->key.keydat_len);
499 if ((ue = RB_FIND(user_name_tree, env->sc_user_names,
503 * the user might have been deleted in between calls
504 * to next since the tree may be modified by a reload.
505 * next should still return the next user in
506 * lexicographical order, hence insert the search key
507 * and look up the next field, then remove it again.
509 RB_INSERT(user_name_tree, env->sc_user_names, &ukey);
510 if ((ue = RB_NEXT(user_name_tree, &env->sc_user_names,
512 RB_REMOVE(user_name_tree, env->sc_user_names,
518 RB_REMOVE(user_name_tree, env->sc_user_names, &ukey);
520 line = ue->ue_line + (strlen(ue->ue_line) + 1);
521 line = line + (strlen(line) + 1);
522 yp_make_keyval(&res, line, line);
527 } else if (strcmp(arg->map, "group.byname") == 0) {
528 key = calloc(arg->key.keydat_len + 1, 1);
533 (void)strncpy(key, arg->key.keydat_val,
534 arg->key.keydat_len);
537 if ((ge = RB_FIND(group_name_tree, env->sc_group_names,
540 * canacar's trick reloaded.
542 RB_INSERT(group_name_tree, env->sc_group_names, &gkey);
543 if ((ge = RB_NEXT(group_name_tree, &env->sc_group_names,
545 RB_REMOVE(group_name_tree, env->sc_group_names,
551 RB_REMOVE(group_name_tree, env->sc_group_names, &gkey);
554 line = ge->ge_line + (strlen(ge->ge_line) + 1);
555 line = line + (strlen(line) + 1);
556 yp_make_keyval(&res, line, line);
560 log_debug("unknown map %s", arg->map);
567 ypproc_all_2_svc(ypreq_nokey *arg, struct svc_req *req)
569 static struct ypresp_all res;
571 if (yp_valid_domain(arg->domain, (struct ypresp_val *)&res) == -1)
574 svcerr_auth(req->rq_xprt, AUTH_FAILED);
579 ypproc_master_2_svc(ypreq_nokey *arg, struct svc_req *req)
581 static struct ypresp_master res;
582 static char master[YPMAXPEER + 1];
584 memset(&res, 0, sizeof(res));
585 if (yp_valid_domain(arg->domain, (struct ypresp_val *)&res) == -1)
588 if (gethostname(master, sizeof(master)) == 0) {
589 res.peer = (peername)master;
598 ypproc_maplist_2_svc(domainname *arg, struct svc_req *req)
605 { "passwd.byname", YPMAP_PASSWD_BYNAME },
606 { "passwd.byuid", YPMAP_PASSWD_BYUID },
607 { "master.passwd.byname", YPMAP_MASTER_PASSWD_BYNAME },
608 { "master.passwd.byuid", YPMAP_MASTER_PASSWD_BYUID },
609 { "group.byname", YPMAP_GROUP_BYNAME },
610 { "group.bygid", YPMAP_GROUP_BYGID },
611 { "netid.byname", YPMAP_NETID_BYNAME },
613 static ypresp_maplist res;
614 static struct ypmaplist maps[nitems(mapnames)];
616 if (yp_valid_domain(*arg, (struct ypresp_val *)&res) == -1)
621 for (i = 0; i < nitems(mapnames); i++) {
622 if (!(env->sc_flags & mapnames[i].cond))
624 maps[i].map = mapnames[i].name;
625 maps[i].next = res.maps;
633 yp_make_val(struct ypresp_val *res, char *line, int replacecolon)
635 static char buf[LINE_WIDTH];
637 memset(buf, 0, sizeof(buf));
640 line[strlen(line)] = ':';
641 (void)strlcpy(buf, line, sizeof(buf));
643 line[strcspn(line, ":")] = '\0';
644 log_debug("sending out %s", buf);
647 res->val.valdat_len = strlen(buf);
648 res->val.valdat_val = buf;
652 yp_make_keyval(struct ypresp_key_val *res, char *key, char *line)
654 static char keybuf[YPMAXRECORD+1];
655 static char buf[LINE_WIDTH];
657 memset(keybuf, 0, sizeof(keybuf));
658 memset(buf, 0, sizeof(buf));
660 (void)strlcpy(keybuf, key, sizeof(keybuf));
661 res->key.keydat_len = strlen(keybuf);
662 res->key.keydat_val = keybuf;
665 res->stat = YP_NOMORE;
669 line[strlen(line)] = ':';
670 (void)strlcpy(buf, line, sizeof(buf));
671 line[strcspn(line, ":")] = '\0';
672 log_debug("sending out %s => %s", keybuf, buf);
674 res->val.valdat_len = strlen(buf);
675 res->val.valdat_val = buf;