1.\" $OpenBSD: yp_bind.3,v 1.2 2020/12/03 22:47:21 jmc Exp $ 2.\" 3.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Jason R. Thorpe. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 22.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd $Mdocdate: December 3 2020 $ 31.Dt YP_BIND 3 32.Os 33.Sh NAME 34.Nm yp_all , 35.Nm yp_bind , 36.Nm yp_first , 37.Nm yp_get_default_domain , 38.Nm yp_maplist , 39.Nm yp_master , 40.Nm yp_match , 41.Nm yp_next , 42.Nm yp_order , 43.Nm yp_unbind , 44.Nm yperr_string , 45.Nm ypprot_err 46.Nd Interface to the YP subsystem 47.Sh SYNOPSIS 48.In sys/types.h 49.In rpc/rpc.h 50.In rpcsvc/ypclnt.h 51.In rpcsvc/yp_prot.h 52.Ft int 53.Fn yp_all "char *indomain" "char *inmap" "struct ypall_callback *incallback" 54.Ft int 55.Fn yp_bind "char *dom" 56.Ft int 57.Fn yp_first "char *indomain" "char *inmap" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen" 58.Ft int 59.Fn yp_get_default_domain "char **domp" 60.Ft int 61.Fn yp_maplist "char *indomain" "struct ypmaplist **outmaplist" 62.Ft int 63.Fn yp_master "char *indomain" "char *inmap" "char **outname" 64.Ft int 65.Fn yp_match "char *indomain" "char *inmap" "const char *inkey" "int inkeylen" "char **outval" "int *outvallen" 66.Ft int 67.Fn yp_next "char *indomain" "char *inmap" "char *inkey" "int inkeylen" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen" 68.Ft int 69.Fn yp_order "char *indomain" "char *inmap" "char *outorder" 70.Ft void 71.Fn yp_unbind "char *dom" 72.Ft char * 73.Fn yperr_string "int incode" 74.Ft int 75.Fn ypprot_err "unsigned int incode" 76.Sh DESCRIPTION 77This function suite provides an interface to the YP subsystem. 78For a general description of the YP subsystem, see 79.Xr yp 8 . 80.Pp 81For all functions, input values begin with 82.Sq in 83and output values begin with 84.Sq out . 85Any output values of type 86.Em char ** 87should be the addresses of uninitialized character pointers. 88Only if a call succeeds will memory be allocated by the YP client routines 89using 90.Fn malloc . 91This memory can later be freed by the user if there is no additional need for 92the data stored there. 93For 94.Fa outkey 95and 96.Fa outval , 97two extra bytes of memory are allocated for a 98.Ql \en 99and 100.Ql \e0 , 101which are not 102reflected in the values of 103.Fa outkeylen 104or 105.Fa outvallen . 106All occurrences of 107.Fa indomain 108and 109.Fa inmap 110must be non-null, NUL-terminated strings. 111All input strings which also have 112a corresponding length parameter cannot be null unless the corresponding 113length value is zero. 114Such strings need not be NUL-terminated. 115.Pp 116All YP lookup calls (the functions 117.Fn yp_all , 118.Fn yp_first , 119.Fn yp_master , 120.Fn yp_match , 121.Fn yp_next , 122.Fn yp_order ) 123require a YP domain name and a YP map name. 124The default domain name may be obtained by calling 125.Fn yp_get_default_domain , 126and should thus be used before all other YP calls in a client program. 127The value it places 128.Fa outdomain 129is suitable for use as the 130.Fa indomain 131parameter to all subsequent YP calls. 132.Pp 133In order for YP lookup calls to succeed, the client process must be bound 134to a YP server process. 135The client process need not explicitly bind to the server, as it happens 136automatically whenever a lookup occurs. 137The function 138.Fn yp_bind 139is provided for a backup strategy, e.g., a local file, when a YP server process 140is not available. 141Each binding uses one socket descriptor on the client process, which may 142be explicitly freed using 143.Fn yp_unbind , 144which frees all per-process and per-node resources to bind the domain and 145marks the domain unbound. 146.Pp 147If, during a YP lookup, an RPC failure occurs, the domain used in the lookup 148is automatically marked unbound and the layer retries the lookup as long as 149.Xr ypbind 8 150is running and either the client process cannot bind to a server for the domain 151specified in the lookup, or RPC requests to the YP server process fail. 152If an error is not RPC-related, one of the YP error codes described below 153is returned and control given back to the user code. 154.Pp 155The suite provides the following functionality: 156.Bl -tag -width "yperr_string()" 157.It Fn yp_match 158Provides the value associated with the given key. 159.It Fn yp_first 160Provides the first key-value pair from the given map in the named domain. 161.It Fn yp_next 162Provides the next key-value pair in the given map. 163To obtain the second pair, the 164.Fa inkey 165value should be the 166.Fa outkey 167value provided by the initial call to 168.Fn yp_first . 169In the general case, the next key-value pair may be obtained by using the 170.Fa outkey 171value from the previous call to 172.Fn yp_next 173as the value for 174.Fa inkey . 175.Pp 176Of course, the notions of 177.Dq first 178and 179.Dq next 180are particular to the 181type of YP map being accessed, and thus there is no guarantee of lexical 182order. 183The only guarantees provided with 184.Fn yp_first 185and 186.Fn yp_next , 187providing that the same map on the same server is polled repeatedly 188until 189.Fn yp_next 190returns YPERR_NOMORE, are that all key-value pairs in that map will be accessed 191exactly once, and if the entire procedure is repeated, the order will be 192the same. 193.Pp 194If the server is heavily loaded or the server fails for some reason, the 195domain being used may become unbound. 196If this happens, and the client process 197re-binds, the retrieval rules will break: some entries may be seen twice, and 198others not at all. 199For this reason, the function 200.Fn yp_all 201provides a better solution for reading all of the entries in a particular 202map. 203.It Fn yp_all 204This function provides a way to transfer an entire map from 205the server to the client process with a single request. 206This transfer method uses TCP, unlike all other functions which use UDP. 207The entire transaction occurs in a single RPC request-response. 208The third argument to this function provides a way to supply the name 209of a function to process each key-value pair in the map. 210.Fn yp_all 211returns after the entire transaction is complete, or the 212.Fn foreach 213function decides that it does not want any more key-value pairs. 214The third argument to 215.Fn yp_all 216is: 217.Bd -literal -offset indent 218struct ypall_callback *incallback { 219 int (*foreach)(); 220 char *data; 221}; 222.Ed 223.Pp 224The 225.Vt char * Ns Va data 226argument is an opaque pointer for use by the callback function. 227The 228.Fn foreach 229function should return non-zero when it no longer wishes to process 230key-value pairs, at which time 231.Fn yp_all 232returns a value of 0, and is called with the following arguments: 233.Bd -literal -offset indent 234int foreach ( 235 unsigned long instatus, 236 char *inkey, 237 int inkeylen, 238 char *inval, 239 int invallen, 240 char *indata 241); 242.Ed 243.Pp 244Where: 245.Bl -tag -width "inkey, inval" 246.It Fa instatus 247Holds one of the return status values described in 248.In rpcsvc/yp_prot.h : 249see 250.Fn ypprot_err 251below for a function that will translate YP protocol errors into a 252error code as described in 253.In rpcsvc/ypclnt.h . 254.It Fa inkey , inval 255The key and value arguments are somewhat different here than described 256above. 257In this case, the memory pointed to by 258.Fa inkey 259and 260.Fa inval 261is private to 262.Fn yp_all , 263and is overwritten with each subsequent key-value pair; therefore, the 264.Fn foreach 265function should do something useful with the contents of that memory during 266each iteration. 267If the key-value pairs are not terminated with either 268.Ql \en 269or 270.Ql \e0 271in the map, then they will not be terminated as such when given to the 272.Fn foreach 273function, either. 274.It Fa indata 275This is the contents of the 276.Fa incallback Ns -> Ns Va data 277element of the callback structure. 278It is provided as a means to share state between the 279.Fn foreach 280function and the user code. 281Its use is completely optional: cast it to something useful or simply 282ignore it. 283.El 284.It Fn yp_order 285Returns the order number for a map. 286.It Fn yp_master 287Returns the hostname for the machine on which the master YP server process for 288a map is running. 289.It Fn yp_maplist 290Returns a singly-linked list of the names of all the maps available in the 291named domain. 292The second argument to 293.Fn yp_maplist 294is: 295.Bd -literal -offset indent 296struct ypmaplist { 297 char *map; 298 struct ypmaplist *next; 299}; 300.Ed 301.It Fn yperr_string 302Returns a pointer to a NUL-terminated error string that does not contain a 303.Ql \&. 304or 305.Ql \en . 306.It Fn ypprot_err 307Converts a YP protocol error code to an error code suitable for 308.Fn yperr_string . 309.El 310.Sh RETURN VALUES 311All functions which are of type 312.Em int 313return 0 upon success or one of the following error codes upon failure: 314.Bl -tag -width "YPERR_BADARGS " 315.It Bq Er YPERR_BADARGS 316The passed arguments to the function are invalid. 317.It Bq Er YPERR_BADDB 318The YP map that was polled is defective. 319.It Bq Er YPERR_DOMAIN 320Client process cannot bind to server on this YP domain. 321.It Bq Er YPERR_KEY 322The key passed does not exist. 323.It Bq Er YPERR_MAP 324There is no such map in the server's domain. 325.It Bq Er YPERR_NODOM 326The local YP domain is not set. 327.It Bq Er YPERR_NOMORE 328There are no more records in the queried map. 329.It Bq Er YPERR_PMAP 330Cannot communicate with portmap. 331.It Bq Er YPERR_RESRC 332A resource allocation failure occurred. 333.It Bq Er YPERR_RPC 334An RPC failure has occurred. 335The domain has been marked unbound. 336.It Bq Er YPERR_VERS 337Client/server version mismatch. 338If the server is running version 1 of the YP protocol, 339.Fn yp_all 340functionality does not exist. 341.It Bq Er YPERR_YPBIND 342Cannot communicate with 343.Xr ypbind 8 . 344.It Bq Er YPERR_YPERR 345An internal server or client error has occurred. 346.It Bq Er YPERR_YPSERV 347The client cannot communicate with the YP server process. 348.El 349.Sh SEE ALSO 350.Xr malloc 3 , 351.Xr yp 8 , 352.Xr ypbind 8 , 353.Xr ypserv 8 354.Sh AUTHORS 355.An Theo de Raadt 356