xref: /netbsd-src/lib/libc/yp/ypclnt.3 (revision 5742f479cd9d5f3313287cfb42ac1a34d471014e)
1.\"	$NetBSD: ypclnt.3,v 1.30 2018/02/11 13:30:55 wiz 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 FOUNDATION OR CONTRIBUTORS
22.\" BE 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 February 7, 2018
31.Dt YPCLNT 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_master ,
39.Nm yp_match ,
40.Nm yp_next ,
41.Nm yp_order ,
42.Nm yp_unbind ,
43.Nm yperr_string ,
44.Nm ypprot_err ,
45.Nm yp_setbindtries
46.Nd Interface to the YP subsystem
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In sys/types.h
51.In rpc/rpc.h
52.In rpcsvc/ypclnt.h
53.In rpcsvc/yp_prot.h
54.Ft int
55.Fn yp_all "const char *indomain" "const char *inmap" "struct ypall_callback *incallback"
56.Ft int
57.Fn yp_bind "const char *dom"
58.Ft int
59.Fn yp_first "const char *indomain" "const char *inmap" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen"
60.Ft int
61.Fn yp_get_default_domain "char **outdomain"
62.Ft int
63.Fn yp_master "const char *indomain" "const char *inmap" "char **outname"
64.Ft int
65.Fn yp_match "const char *indomain" "const char *inmap" "const char *inkey" "int inkeylen" "char **outval" "int *outvallen"
66.Ft int
67.Fn yp_next "const char *indomain" "const char *inmap" "const char *inkey" "int inkeylen" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen"
68.Ft int
69.Fn yp_order "const char *indomain" "const char *inmap" "int *outorder"
70.Ft void
71.Fn yp_unbind "const char *dom"
72.Ft char *
73.Fn yperr_string "int incode"
74.Ft int
75.Fn ypprot_err "unsigned int incode"
76.Ft int
77.Fn yp_setbindtries "int ntries"
78.Sh DESCRIPTION
79The
80.Nm ypclnt
81suite provides an interface to the YP subsystem.
82For a general description of the YP subsystem, see
83.Xr yp 8 .
84.Pp
85For all functions, input values begin with
86.Pa in
87and output values begin with
88.Pa out .
89.Pp
90Any output values of type
91.Em char **
92should be the addresses of uninitialized character pointers.
93These values will be reset to the null pointer (unless the address
94itself is the null pointer, in which case the error
95.Er YPERR_BADARGS
96will be returned).
97If necessary,
98memory will be allocated by the YP client routines using
99.Fn malloc ,
100and the result will be stored in the appropriate output value.
101If the invocation of a YP client routine doesn't return an error,
102and an output value is not the null pointer, then this memory
103should be freed by the user when there is no additional need for
104the data stored there.
105For
106.Pa outkey
107and
108.Pa outval ,
109two extra bytes of memory are allocated for a
110.Ql \en
111and
112.Ql \e0 ,
113which are not
114reflected in the values of
115.Pa outkeylen
116or
117.Pa outvallen .
118.Pp
119All occurrences of
120.Pa indomain
121and
122.Pa inmap
123must be non-null, NUL-terminated strings.
124All input strings which also have
125a corresponding length parameter cannot be the null pointer unless the
126corresponding length value is zero.
127Such strings need not be NUL-terminated.
128.Pp
129All YP lookup calls (the functions
130.Fn yp_all ,
131.Fn yp_first ,
132.Fn yp_master ,
133.Fn yp_match ,
134.Fn yp_next ,
135.Fn yp_order )
136require a YP domain name and a YP map name.
137The default domain name may be obtained by calling
138.Fn yp_get_default_domain ,
139and should thus be used before all other YP calls in a client program.
140The value it places
141.Pa outdomain
142is suitable for use as the
143.Pa indomain
144parameter to all subsequent YP calls.
145.Pp
146In order for YP lookup calls to succeed, the client process must be bound
147to a YP server process.
148The client process need not explicitly bind to
149the server, as it happens automatically whenever a lookup occurs.
150The function
151.Fn yp_bind
152is provided for a backup strategy, e.g. a local file, when a YP
153server process is not available.
154Each binding uses one socket descriptor on the client
155process, which may be explicitly freed using
156.Fn yp_unbind ,
157which frees all per-process and per-node resources to bind the domain and
158marks the domain unbound.
159.Pp
160If, during a YP lookup, an RPC failure occurs, the domain used in the lookup
161is automatically marked unbound and the
162.Nm ypclnt
163layer retries the lookup as long as
164.Xr ypbind 8
165is running and either the client process cannot bind to a server for the domain
166specified in the lookup, or RPC requests to the YP server process fail.
167If an error is not RPC-related, one of the YP error codes described below
168is returned and control given back to the user code.
169.Pp
170The
171.Nm ypclnt
172suite provides the following functionality:
173.Bl -tag -width yp_matchxxxx
174.It Fn yp_match
175Provides the value associated with the given key.
176.It Fn yp_first
177Provides the first key-value pair from the given map in the named domain.
178.It Fn yp_next
179Provides the next key-value pair in the given map.
180To obtain the second pair,
181the
182.Pa inkey
183value should be the
184.Pa outkey
185value provided by the initial call to
186.Fn yp_first .
187In the general case, the next key-value pair may be obtained by using the
188.Pa outkey
189value from the previous call to
190.Fn yp_next
191as the value for
192.Pa inkey .
193.Pp
194Of course, the notions of
195.Dq first
196and
197.Dq next
198are particular to the
199type of YP map being accessed, and thus there is no guarantee of lexical order.
200The only guarantees provided with
201.Fn yp_first
202and
203.Fn yp_next ,
204providing that the same map on the same server is polled repeatedly until
205.Fn yp_next
206returns YPERR_NOMORE, are that all key-value pairs in that map will be accessed
207exactly once, and if the entire procedure is repeated, the order will be
208the same.
209.Pp
210If the server is heavily loaded or the server fails for some reason, the
211domain being used may become unbound.
212If this happens, and the client process re-binds, the retrieval rules
213will break: some entries may be seen twice, and others not at all.
214For this reason, the function
215.Fn yp_all
216provides a better solution for reading all of the entries in a particular map.
217.It Fn yp_all
218This function provides a way to transfer an entire map from
219the server to the client process with a single request.
220This transfer uses TCP, unlike all other functions in the
221.Nm ypclnt
222suite, which use UDP.
223The entire transaction occurs in a single RPC request-response.
224The third argument to this function provides a way to supply
225the name of a function to process each key-value pair in the map.
226.Fn yp_all
227returns after the entire transaction is complete, or the
228.Pa foreach
229function decides that it does not want any more key-value pairs.
230The third argument to
231.Fn yp_all
232is:
233.Bd -literal -offset indent
234struct ypall_callback *incallback {
235	int (*foreach)();
236	char *data;
237};
238.Ed
239.Pp
240The
241.Em char *data
242argument is an opaque pointer for use by the callback function.
243The
244.Pa foreach
245function should return non-zero when it no longer wishes to process
246key-value pairs, at which time
247.Fn yp_all
248returns a value of 0, and is called with the following arguments:
249.Bd -literal -offset indent
250int foreach (
251	int instatus,
252	char *inkey,
253	int inkeylen,
254	char *inval,
255	int invallen,
256	char *indata
257);
258.Ed
259.Pp
260Where:
261.Bl -tag -width "inkey, inval"
262.It Fa instatus
263Holds one of the return status values described in
264.In rpcsvc/yp_prot.h :
265see
266.Fn ypprot_err
267below for a function that will translate YP protocol errors into a
268.Nm ypclnt
269layer error code as described in
270.In rpcsvc/ypclnt.h .
271.It Fa inkey , inval
272The key and value arguments are somewhat different here than described
273above.
274In this case, the memory pointed to by
275.Fa inkey
276and
277.Fa inval
278is private to
279.Fn yp_all ,
280and is overwritten with each subsequent key-value pair, thus the
281.Pa foreach
282function should do something useful with the contents of that memory during
283each iteration.
284If the key-value pairs are not terminated with either
285.Ql \en
286or
287.Ql \e0
288in the map, then they will not be terminated as such when given to the
289.Pa foreach
290function, either.
291.It Fa indata
292This is the contents of the
293.Pa incallback->data
294element of the callback structure.
295It is provided as a means to share state between the
296.Pa foreach
297function and the user code.
298Its use is completely optional: cast it to
299something useful or simply ignore it.
300.El
301.It Fn yp_order
302Returns the order number for a map.
303.It Fn yp_master
304Returns the hostname for the machine on which the master YP server process for
305a map is running.
306.It Fn yperr_string
307Returns a pointer to a NUL-terminated error string that does not contain a
308.Ql \&.
309or
310.Ql \en .
311.It Fn ypprot_err
312Converts a YP protocol error code to a
313.Nm ypclnt
314error code suitable for
315.Fn yperr_string .
316.It Fn yp_setbindtries
317Set the number of tries to attempt to bind to the domain before returning
318an error.
319The default is
320.Dv 0
321which means wait forever if no ypserver is not found of if the RPC
322communication with the yp server fails.
323If the number passed is negative, the current number of tries is not modified.
324.Pp
325This function is an extension to the client library that allows an application
326to catch communication problems with the ypserver without blocking forever.
327.El
328.Sh RETURN VALUES
329All functions in the
330.Nm ypclnt
331suite which are of type
332.Em int
333return 0 upon success or one of the following error codes upon failure:
334.Bl -tag -width "YPERR_BADARGS   "
335.It Bq Er YPERR_BADARGS
336The passed arguments to the function are invalid.
337.It Bq Er YPERR_BADDB
338The YP map that was polled is defective.
339.It Bq Er YPERR_BIND
340Cannot communicate with
341.Xr ypbind 8 .
342.It Bq Er YPERR_DOM
343The local YP domain is not set.
344.It Bq Er YPERR_DOMAIN
345Client process cannot bind to server on this YP domain.
346.It Bq Er YPERR_KEY
347The key passed does not exist.
348.It Bq Er YPERR_MAP
349There is no such map in the server's domain.
350.It Bq Er YPERR_NOMORE
351There are no more records in the queried map.
352.It Bq Er YPERR_PMAP
353Cannot communicate with portmapper (see
354.Xr rpcbind 8 ) .
355.It Bq Er YPERR_RESRC
356A resource allocation failure occurred.
357.It Bq Er YPERR_RPC
358An RPC failure has occurred.
359The domain has been marked unbound.
360.It Bq Er YPERR_VERS
361Client/server version mismatch.
362If the server is running version 1 of the YP protocol,
363.Fn yp_all
364functionality does not exist.
365.It Bq Er YPERR_YPERR
366An internal server or client error has occurred.
367.It Bq Er YPERR_YPSERV
368The client cannot communicate with the YP server process.
369.El
370.Sh SEE ALSO
371.Xr malloc 3 ,
372.Xr yp 8 ,
373.Xr ypbind 8 ,
374.Xr ypserv 8
375.Sh AUTHORS
376.An Theo De Raadt
377