1 /* $OpenBSD: yp_all.c,v 1.10 2010/04/02 17:35:03 schwarze 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/param.h> 29 #include <sys/types.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <rpc/rpc.h> 34 #include <rpc/xdr.h> 35 #include <rpcsvc/yp.h> 36 #include <rpcsvc/ypclnt.h> 37 #include "ypinternal.h" 38 39 bool_t 40 xdr_ypresp_all_seq(XDR *xdrs, u_long *objp) 41 { 42 struct ypresp_all out; 43 u_long status; 44 char *key, *val; 45 int size; 46 int done = 0; /* set to 1 when the user does not want more data */ 47 bool_t rc = TRUE; /* FALSE at the end of loop signals failure */ 48 49 memset(&out, 0, sizeof out); 50 while (rc && !done) { 51 rc = FALSE; 52 if (!xdr_ypresp_all(xdrs, &out)) { 53 *objp = (u_long)YP_YPERR; 54 goto fail; 55 } 56 if (out.more == 0) 57 goto fail; 58 status = out.ypresp_all_u.val.stat; 59 if (status == YP_TRUE) { 60 size = out.ypresp_all_u.val.key.keydat_len; 61 if ((key = malloc(size + 1)) == NULL) { 62 *objp = (u_long)YP_YPERR; 63 goto fail; 64 } 65 (void)memcpy(key, out.ypresp_all_u.val.key.keydat_val, 66 size); 67 key[size] = '\0'; 68 69 size = out.ypresp_all_u.val.val.valdat_len; 70 if ((val = malloc(size + 1)) == NULL) { 71 free(key); 72 *objp = (u_long)YP_YPERR; 73 goto fail; 74 } 75 (void)memcpy(val, out.ypresp_all_u.val.val.valdat_val, 76 size); 77 val[size] = '\0'; 78 79 done = (*ypresp_allfn)(status, key, 80 out.ypresp_all_u.val.key.keydat_len, val, 81 out.ypresp_all_u.val.val.valdat_len, ypresp_data); 82 free(key); 83 free(val); 84 } else 85 done = 1; 86 if (status != YP_NOMORE) 87 *objp = status; 88 rc = TRUE; 89 fail: 90 xdr_free(xdr_ypresp_all, (char *)&out); 91 } 92 return rc; 93 } 94 95 int 96 yp_all(const char *indomain, const char *inmap, struct ypall_callback *incallback) 97 { 98 struct ypreq_nokey yprnk; 99 struct dom_binding *ysd; 100 struct timeval tv; 101 struct sockaddr_in clnt_sin; 102 CLIENT *clnt; 103 u_long status; 104 int clnt_sock; 105 int r = 0; 106 107 if (indomain == NULL || *indomain == '\0' || 108 strlen(indomain) > YPMAXDOMAIN || inmap == NULL || 109 *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL) 110 return YPERR_BADARGS; 111 112 if (_yp_dobind(indomain, &ysd) != 0) 113 return YPERR_DOMAIN; 114 115 tv.tv_sec = _yplib_timeout; 116 tv.tv_usec = 0; 117 clnt_sock = RPC_ANYSOCK; 118 clnt_sin = ysd->dom_server_addr; 119 clnt_sin.sin_port = 0; 120 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0); 121 if (clnt == NULL) { 122 printf("clnttcp_create failed\n"); 123 r = YPERR_PMAP; 124 goto out; 125 } 126 yprnk.domain = (char *)indomain; 127 yprnk.map = (char *)inmap; 128 ypresp_allfn = incallback->foreach; 129 ypresp_data = (void *) incallback->data; 130 131 (void) clnt_call(clnt, YPPROC_ALL, 132 xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv); 133 clnt_destroy(clnt); 134 if (status != YP_FALSE) 135 r = ypprot_err(status); 136 out: 137 _yp_unbind(ysd); 138 return r; 139 } 140