1/* $OpenBSD: yp_all.c,v 1.16 2022/08/02 16:59:29 deraadt Exp $ */ 2/* 3 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/types.h> 29#include <sys/socket.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <limits.h> 34#include <unistd.h> 35#include <rpc/rpc.h> 36#include <rpc/xdr.h> 37#include <rpcsvc/yp.h> 38#include <rpcsvc/ypclnt.h> 39#include "ypinternal.h" 40 41static int (*ypresp_allfn)(u_long, char *, int, char *, int, void *); 42static void *ypresp_data; 43 44static bool_t 45_xdr_ypresp_all_seq(XDR *xdrs, u_long *objp) 46{ 47 struct ypresp_all out; 48 u_long status; 49 char *key, *val; 50 int size; 51 int done = 0; /* set to 1 when the user does not want more data */ 52 bool_t rc = TRUE; /* FALSE at the end of loop signals failure */ 53 54 memset(&out, 0, sizeof out); 55 while (rc && !done) { 56 rc = FALSE; 57 if (!xdr_ypresp_all(xdrs, &out)) { 58 *objp = (u_long)YP_YPERR; 59 goto fail; 60 } 61 if (out.more == 0) 62 goto fail; 63 status = out.ypresp_all_u.val.stat; 64 if (status == YP_TRUE) { 65 size = out.ypresp_all_u.val.key.keydat_len; 66 if ((key = malloc(size + 1)) == NULL) { 67 *objp = (u_long)YP_YPERR; 68 goto fail; 69 } 70 (void)memcpy(key, out.ypresp_all_u.val.key.keydat_val, 71 size); 72 key[size] = '\0'; 73 74 size = out.ypresp_all_u.val.val.valdat_len; 75 if ((val = malloc(size + 1)) == NULL) { 76 free(key); 77 *objp = (u_long)YP_YPERR; 78 goto fail; 79 } 80 (void)memcpy(val, out.ypresp_all_u.val.val.valdat_val, 81 size); 82 val[size] = '\0'; 83 84 done = (*ypresp_allfn)(status, key, 85 out.ypresp_all_u.val.key.keydat_len, val, 86 out.ypresp_all_u.val.val.valdat_len, ypresp_data); 87 free(key); 88 free(val); 89 } else 90 done = 1; 91 if (status != YP_NOMORE) 92 *objp = status; 93 rc = TRUE; 94fail: 95 xdr_free(xdr_ypresp_all, (char *)&out); 96 } 97 return rc; 98} 99 100int 101yp_all(const char *dom, const char *inmap, struct ypall_callback *incallback) 102{ 103 struct ypreq_nokey yprnk; 104 struct dom_binding ypbinding; 105 struct timeval tv; 106 int connected = 1; 107 u_long status; 108 int r = 0, s; 109 110 if (dom == NULL || strlen(dom) == 0) 111 return YPERR_BADARGS; 112 113 if (strlen(dom) > YPMAXDOMAIN || inmap == NULL || 114 *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL) 115 return YPERR_BADARGS; 116 117again: 118 s = ypconnect(SOCK_STREAM); 119 if (s == -1) 120 return YPERR_DOMAIN; /* YP not running */ 121 ypbinding.dom_socket = s; 122 ypbinding.dom_server_addr.sin_port = -1; /* don't consult portmap */ 123 124 ypbinding.dom_client = clnttcp_create(&ypbinding.dom_server_addr, 125 YPPROG, YPVERS, &ypbinding.dom_socket, 0, 0); 126 if (ypbinding.dom_client == NULL) { 127 close(ypbinding.dom_socket); 128 ypbinding.dom_socket = -1; 129 clnt_pcreateerror("clnttcp_create"); 130 goto again; 131 } 132 clnt_control(ypbinding.dom_client, CLSET_CONNECTED, &connected); 133 134 tv.tv_sec = _yplib_timeout; 135 tv.tv_usec = 0; 136 yprnk.domain = (char *)dom; 137 yprnk.map = (char *)inmap; 138 ypresp_allfn = incallback->foreach; 139 ypresp_data = (void *) incallback->data; 140 (void) clnt_call(ypbinding.dom_client, YPPROC_ALL, 141 xdr_ypreq_nokey, &yprnk, _xdr_ypresp_all_seq, &status, tv); 142 close(ypbinding.dom_socket); 143 clnt_destroy(ypbinding.dom_client); 144 145 if (status != YP_FALSE) 146 r = ypprot_err(status); 147 return r; 148} 149