]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/nss.texi
powerpc: Fix build of wcscpy with --disable-multi-arch
[thirdparty/glibc.git] / manual / nss.texi
CommitLineData
7a68c94a 1@node Name Service Switch, Users and Groups, Job Control, Top
706074a5 2@chapter System Databases and Name Service Switch
7a68c94a 3@c %MENU% Accessing system databases
2c6fe0bd
UD
4@cindex Name Service Switch
5@cindex NSS
6d52618b 6@cindex databases
7a68c94a 7
706074a5
UD
8Various functions in the C Library need to be configured to work
9correctly in the local environment. Traditionally, this was done by
6952e59e 10using files (e.g., @file{/etc/passwd}), but other nameservices (like the
706074a5
UD
11Network Information Service (NIS) and the Domain Name Service (DNS))
12became popular, and were hacked into the C library, usually with a fixed
41f49342 13search order.
706074a5 14
5455692a 15@Theglibc{} contains a cleaner solution to this problem. It is
706074a5 16designed after a method used by Sun Microsystems in the C library of
1f77f049 17@w{Solaris 2}. @Theglibc{} follows their name and calls this
706074a5
UD
18scheme @dfn{Name Service Switch} (NSS).
19
20Though the interface might be similar to Sun's version there is no
21common code. We never saw any source code of Sun's implementation and
6952e59e 22so the internal interface is incompatible. This also manifests in the
706074a5
UD
23file names we use as we will see later.
24
25
26@menu
27* NSS Basics:: What is this NSS good for.
28* NSS Configuration File:: Configuring NSS.
29* NSS Module Internals:: How does it work internally.
30* Extending NSS:: What to do to add services or databases.
31@end menu
32
33@node NSS Basics, NSS Configuration File, Name Service Switch, Name Service Switch
34@section NSS Basics
35
36The basic idea is to put the implementation of the different services
37offered to access the databases in separate modules. This has some
38advantages:
39
40@enumerate
41@item
1f77f049 42Contributors can add new services without adding them to @theglibc{}.
706074a5
UD
43@item
44The modules can be updated separately.
45@item
46The C library image is smaller.
47@end enumerate
48
5455692a 49To fulfill the first goal above, the ABI of the modules will be described
706074a5
UD
50below. For getting the implementation of a new service right it is
51important to understand how the functions in the modules get called.
52They are in no way designed to be used by the programmer directly.
53Instead the programmer should only use the documented and standardized
54functions to access the databases.
55
56@noindent
57The databases available in the NSS are
58
59@cindex ethers
60@cindex group
61@cindex hosts
ba1ffaa1 62@cindex netgroup
b3a86ae1 63@cindex networks
706074a5
UD
64@cindex protocols
65@cindex passwd
66@cindex rpc
67@cindex services
68@cindex shadow
2fe82ca6 69@table @code
26dee9c4
UD
70@item aliases
71Mail aliases
72@comment @pxref{Mail Aliases}.
706074a5
UD
73@item ethers
74Ethernet numbers,
75@comment @pxref{Ethernet Numbers}.
76@item group
77Groups of users, @pxref{Group Database}.
78@item hosts
79Host names and numbers, @pxref{Host Names}.
ba1ffaa1
UD
80@item netgroup
81Network wide list of host and users, @pxref{Netgroup Database}.
b3a86ae1 82@item networks
706074a5
UD
83Network names and numbers, @pxref{Networks Database}.
84@item protocols
85Network protocols, @pxref{Protocols Database}.
86@item passwd
841785ba 87User identities, @pxref{User Database}.
706074a5 88@item rpc
841785ba 89Remote procedure call names and numbers.
706074a5
UD
90@comment @pxref{RPC Database}.
91@item services
92Network services, @pxref{Services Database}.
93@item shadow
841785ba
ZW
94User passphrase hashes and related information.
95@comment @pxref{Shadow Passphrase Database}.
2fe82ca6 96@end table
706074a5
UD
97
98@noindent
26dee9c4
UD
99There will be some more added later (@code{automount}, @code{bootparams},
100@code{netmasks}, and @code{publickey}).
706074a5
UD
101
102@node NSS Configuration File, NSS Module Internals, NSS Basics, Name Service Switch
103@section The NSS Configuration File
104
105@cindex @file{/etc/nsswitch.conf}
106@cindex @file{nsswitch.conf}
107Somehow the NSS code must be told about the wishes of the user. For
108this reason there is the file @file{/etc/nsswitch.conf}. For each
5455692a 109database, this file contains a specification of how the lookup process should
706074a5
UD
110work. The file could look like this:
111
112@example
113@include nsswitch.texi
114@end example
115
116The first column is the database as you can guess from the table above.
117The rest of the line specifies how the lookup process works. Please
118note that you specify the way it works for each database individually.
119This cannot be done with the old way of a monolithic implementation.
120
121The configuration specification for each database can contain two
122different items:
123
124@itemize @bullet
125@item
126the service specification like @code{files}, @code{db}, or @code{nis}.
127@item
26dee9c4 128the reaction on lookup result like @code{[NOTFOUND=return]}.
706074a5
UD
129@end itemize
130
131@menu
ba1ffaa1 132* Services in the NSS configuration:: Service names in the NSS configuration.
6d52618b 133* Actions in the NSS configuration:: React appropriately to the lookup result.
706074a5
UD
134* Notes on NSS Configuration File:: Things to take care about while
135 configuring NSS.
136@end menu
137
138@node Services in the NSS configuration, Actions in the NSS configuration, NSS Configuration File, NSS Configuration File
139@subsection Services in the NSS configuration File
140
16d2c631
JM
141The above example file mentions five different services: @code{files},
142@code{db}, @code{dns}, @code{nis}, and @code{nisplus}. This does not
143mean these
5455692a 144services are available on all sites and neither does it mean these are
706074a5
UD
145all the services which will ever be available.
146
147In fact, these names are simply strings which the NSS code uses to find
148the implicitly addressed functions. The internal interface will be
149described later. Visible to the user are the modules which implement an
150individual service.
151
152Assume the service @var{name} shall be used for a lookup. The code for
153this service is implemented in a module called @file{libnss_@var{name}}.
154On a system supporting shared libraries this is in fact a shared library
c66dbe00 155with the name (for example) @file{libnss_@var{name}.so.2}. The number
706074a5
UD
156at the end is the currently used version of the interface which will not
157change frequently. Normally the user should not have to be cognizant of
158these files since they should be placed in a directory where they are
159found automatically. Only the names of all available services are
160important.
161
162@node Actions in the NSS configuration, Notes on NSS Configuration File, Services in the NSS configuration, NSS Configuration File
163@subsection Actions in the NSS configuration
164
165The second item in the specification gives the user much finer control
166on the lookup process. Action items are placed between two service
167names and are written within brackets. The general form is
168
2de99474 169@display
57ba7bb4 170@code{[} ( @code{!}? @var{status} @code{=} @var{action} )+ @code{]}
2de99474 171@end display
706074a5
UD
172
173@noindent
174where
175
176@smallexample
177@var{status} @result{} success | notfound | unavail | tryagain
178@var{action} @result{} return | continue
179@end smallexample
180
181The case of the keywords is insignificant. The @var{status}
182values are the results of a call to a lookup function of a specific
ced8f893 183service. They mean:
706074a5
UD
184
185@ftable @samp
186@item success
6d52618b 187No error occurred and the wanted entry is returned. The default action
706074a5
UD
188for this is @code{return}.
189
190@item notfound
191The lookup process works ok but the needed value was not found. The
192default action is @code{continue}.
193
194@item unavail
195@cindex DNS server unavailable
196The service is permanently unavailable. This can either mean the needed
197file is not available, or, for DNS, the server is not available or does
198not allow queries. The default action is @code{continue}.
199
200@item tryagain
201The service is temporarily unavailable. This could mean a file is
202locked or a server currently cannot accept more connections. The
203default action is @code{continue}.
204@end ftable
205
ced8f893
SG
206@noindent
207The @var{action} values mean:
208
209@ftable @samp
210@item return
211
212If the status matches, stop the lookup process at this service
213specification. If an entry is available, provide it to the application.
214If an error occurred, report it to the application. In case of a prior
215@samp{merge} action, the data is combined with previous lookup results,
216as explained below.
217
218@item continue
219
220If the status matches, proceed with the lookup process at the next
221entry, discarding the result of the current lookup (and any merged
222data). An exception is the @samp{initgroups} database and the
223@samp{success} status, where @samp{continue} acts like @code{merge}
224below.
225
226@item merge
227
228Proceed with the lookup process, retaining the current lookup result.
229This action is useful only with the @samp{success} status. If a
230subsequent service lookup succeeds and has a matching @samp{return}
231specification, the results are merged, the lookup process ends, and the
232merged results are returned to the application. If the following service
233has a matching @samp{merge} action, the lookup process continues,
234retaining the combined data from this and any previous lookups.
235
236After a @code{merge} action, errors from subsequent lookups are ignored,
237and the data gathered so far will be returned.
238
239The @samp{merge} only applies to the @samp{success} status. It is
240currently implemented for the @samp{group} database and its group
241members field, @samp{gr_mem}. If specified for other databases, it
242causes the lookup to fail (if the @var{status} matches).
243
244When processing @samp{merge} for @samp{group} membership, the group GID
245and name must be identical for both entries. If only one or the other is
246a match, the behavior is undefined.
247
248@end ftable
249
706074a5
UD
250@noindent
251If we have a line like
252
253@smallexample
254ethers: nisplus [NOTFOUND=return] db files
255@end smallexample
256
257@noindent
258this is equivalent to
259
260@smallexample
261ethers: nisplus [SUCCESS=return NOTFOUND=return UNAVAIL=continue
262 TRYAGAIN=continue]
263 db [SUCCESS=return NOTFOUND=continue UNAVAIL=continue
264 TRYAGAIN=continue]
265 files
266@end smallexample
267
268@noindent
269(except that it would have to be written on one line). The default
270value for the actions are normally what you want, and only need to be
271changed in exceptional cases.
272
273If the optional @code{!} is placed before the @var{status} this means
4f8dbcb1 274the following action is used for all statuses but @var{status} itself.
706074a5
UD
275I.e., @code{!} is negation as in the C language (and others).
276
277Before we explain the exception which makes this action item necessary
278one more remark: obviously it makes no sense to add another action
279item after the @code{files} service. Since there is no other service
280following the action @emph{always} is @code{return}.
281
282@cindex nisplus, and completeness
283Now, why is this @code{[NOTFOUND=return]} action useful? To understand
284this we should know that the @code{nisplus} service is often
285complete; i.e., if an entry is not available in the NIS+ tables it is
286not available anywhere else. This is what is expressed by this action
287item: it is useless to examine further services since they will not give
288us a result.
289
290@cindex nisplus, and booting
291@cindex bootstrapping, and services
292The situation would be different if the NIS+ service is not available
293because the machine is booting. In this case the return value of the
294lookup function is not @code{notfound} but instead @code{unavail}. And
295as you can see in the complete form above: in this situation the
296@code{db} and @code{files} services are used. Neat, isn't it? The
297system administrator need not pay special care for the time the system
298is not completely ready to work (while booting or shutdown or
299network problems).
300
301
302@node Notes on NSS Configuration File, , Actions in the NSS configuration, NSS Configuration File
303@subsection Notes on the NSS Configuration File
304
305Finally a few more hints. The NSS implementation is not completely
306helpless if @file{/etc/nsswitch.conf} does not exist. For
307all supported databases there is a default value so it should normally
308be possible to get the system running even if the file is corrupted or
309missing.
310
ba1ffaa1 311@cindex default value, and NSS
b3a86ae1 312For the @code{hosts} and @code{networks} databases the default value is
ba1ffaa1
UD
313@code{dns [!UNAVAIL=return] files}. I.e., the system is prepared for
314the DNS service not to be available but if it is available the answer it
04b9968b 315returns is definitive.
ba1ffaa1 316
10dc2a90
UD
317The @code{passwd}, @code{group}, and @code{shadow} databases are
318traditionally handled in a special way. The appropriate files in the
319@file{/etc} directory are read but if an entry with a name starting
320with a @code{+} character is found NIS is used. This kind of lookup
a1a78204
SE
321remains possible if @theglibc{} was configured with the
322@code{--enable-obsolete-nsl} option and the special lookup service
323@code{compat} is used. If @theglibc{} was configured with the
324@code{--enable-obsolete-nsl} option the default value for the three
325databases above is @code{compat [NOTFOUND=return] files}. If the
326@code{--enable-obsolete-nsl} option was not used the default value
327for the services is @code{files}.
328
329For all other databases the default value is @code{files} unless
330@theglibc{} was configured with @code{--enable-obsolete-rpc} option, in
331which case it the default value is @code{nis [NOTFOUND=return] files}.
ba1ffaa1
UD
332
333@cindex optimizing NSS
706074a5 334A second point is that the user should try to optimize the lookup
10dc2a90
UD
335process. The different service have different response times.
336A simple file look up on a local file could be fast, but if the file
337is long and the needed entry is near the end of the file this may take
338quite some time. In this case it might be better to use the @code{db}
339service which allows fast local access to large data sets.
706074a5
UD
340
341Often the situation is that some global information like NIS must be
342used. So it is unavoidable to use service entries like @code{nis} etc.
343But one should avoid slow services like this if possible.
344
345
346@node NSS Module Internals, Extending NSS, NSS Configuration File, Name Service Switch
347@section NSS Module Internals
348
04b9968b 349Now it is time to describe what the modules look like. The functions
706074a5
UD
350contained in a module are identified by their names. I.e., there is no
351jump table or the like. How this is done is of no interest here; those
352interested in this topic should read about Dynamic Linking.
353@comment @ref{Dynamic Linking}.
354
355
356@menu
357* NSS Module Names:: Construction of the interface function of
358 the NSS modules.
359* NSS Modules Interface:: Programming interface in the NSS module
360 functions.
361@end menu
362
363@node NSS Module Names, NSS Modules Interface, NSS Module Internals, NSS Module Internals
364@subsection The Naming Scheme of the NSS Modules
365
366@noindent
5455692a 367The name of each function consists of various parts:
706074a5
UD
368
369@quotation
370 _nss_@var{service}_@var{function}
371@end quotation
372
373@var{service} of course corresponds to the name of the module this
04b9968b
UD
374function is found in.@footnote{Now you might ask why this information is
375duplicated. The answer is that we want to make it possible to link
706074a5
UD
376directly with these shared objects.} The @var{function} part is derived
377from the interface function in the C library itself. If the user calls
378the function @code{gethostbyname} and the service used is @code{files}
379the function
380
381@smallexample
382 _nss_files_gethostbyname_r
383@end smallexample
384
385@noindent
386in the module
387
388@smallexample
c66dbe00 389 libnss_files.so.2
706074a5
UD
390@end smallexample
391
392@noindent
393@cindex reentrant NSS functions
394is used. You see, what is explained above in not the whole truth. In
395fact the NSS modules only contain reentrant versions of the lookup
396functions. I.e., if the user would call the @code{gethostbyname_r}
397function this also would end in the above function. For all user
398interface functions the C library maps this call to a call to the
399reentrant function. For reentrant functions this is trivial since the
5455692a 400interface is (nearly) the same. For the non-reentrant version the
26dee9c4
UD
401library keeps internal buffers which are used to replace the user
402supplied buffer.
706074a5
UD
403
404I.e., the reentrant functions @emph{can} have counterparts. No service
405module is forced to have functions for all databases and all kinds to
406access them. If a function is not available it is simply treated as if
407the function would return @code{unavail}
408(@pxref{Actions in the NSS configuration}).
409
c66dbe00
UD
410The file name @file{libnss_files.so.2} would be on a @w{Solaris 2}
411system @file{nss_files.so.2}. This is the difference mentioned above.
2de99474
UD
412Sun's NSS modules are usable as modules which get indirectly loaded
413only.
414
1f77f049 415The NSS modules in @theglibc{} are prepared to be used as normal
b642f101 416libraries themselves. This is @emph{not} true at the moment, though.
04b9968b 417However, the organization of the name space in the modules does not make it
2de99474
UD
418impossible like it is for Solaris. Now you can see why the modules are
419still libraries.@footnote{There is a second explanation: we were too
420lazy to change the Makefiles to allow the generation of shared objects
04b9968b 421not starting with @file{lib} but don't tell this to anybody.}
2de99474 422
706074a5
UD
423
424@node NSS Modules Interface, , NSS Module Names, NSS Module Internals
425@subsection The Interface of the Function in NSS Modules
426
427Now we know about the functions contained in the modules. It is now
428time to describe the types. When we mentioned the reentrant versions of
429the functions above, this means there are some additional arguments
5455692a 430(compared with the standard, non-reentrant versions). The prototypes for
706074a5
UD
431the non-reentrant and reentrant versions of our function above are:
432
433@smallexample
434struct hostent *gethostbyname (const char *name)
435
a18f587d
UD
436int gethostbyname_r (const char *name, struct hostent *result_buf,
437 char *buf, size_t buflen, struct hostent **result,
438 int *h_errnop)
706074a5
UD
439@end smallexample
440
441@noindent
ba1ffaa1 442The actual prototype of the function in the NSS modules in this case is
706074a5
UD
443
444@smallexample
ba1ffaa1
UD
445enum nss_status _nss_files_gethostbyname_r (const char *name,
446 struct hostent *result_buf,
a18f587d 447 char *buf, size_t buflen,
c66dbe00 448 int *errnop, int *h_errnop)
706074a5
UD
449@end smallexample
450
ba1ffaa1 451I.e., the interface function is in fact the reentrant function with the
3a35923e
FW
452change of the return value, the omission of the @var{result} parameter,
453and the addition of the @var{errnop} parameter. While the user-level
454function returns a pointer to the result the reentrant function return
455an @code{enum nss_status} value:
706074a5 456
b642f101 457@vtable @code
706074a5
UD
458@item NSS_STATUS_TRYAGAIN
459numeric value @code{-2}
460
461@item NSS_STATUS_UNAVAIL
462numeric value @code{-1}
463
464@item NSS_STATUS_NOTFOUND
465numeric value @code{0}
466
467@item NSS_STATUS_SUCCESS
468numeric value @code{1}
b642f101 469@end vtable
706074a5
UD
470
471@noindent
472Now you see where the action items of the @file{/etc/nsswitch.conf} file
473are used.
474
ba1ffaa1
UD
475If you study the source code you will find there is a fifth value:
476@code{NSS_STATUS_RETURN}. This is an internal use only value, used by a
477few functions in places where none of the above value can be used. If
478necessary the source code should be examined to learn about the details.
479
7be8096f
UD
480In case the interface function has to return an error it is important
481that the correct error code is stored in @code{*@var{errnop}}. Some
5455692a 482return status values have only one associated error code, others have
7be8096f
UD
483more.
484
485@multitable @columnfractions .3 .2 .50
486@item
487@code{NSS_STATUS_TRYAGAIN} @tab
04b9968b 488 @code{EAGAIN} @tab One of the functions used ran temporarily out of
7be8096f
UD
489resources or a service is currently not available.
490@item
491@tab
492 @code{ERANGE} @tab The provided buffer is not large enough.
493The function should be called again with a larger buffer.
494@item
495@code{NSS_STATUS_UNAVAIL} @tab
496 @code{ENOENT} @tab A necessary input file cannot be found.
497@item
498@code{NSS_STATUS_NOTFOUND} @tab
499 @code{ENOENT} @tab The requested entry is not available.
d4e301c5
CD
500
501@item
502@code{NSS_STATUS_NOTFOUND} @tab
503 @code{SUCCESS} @tab There are no entries.
504Use this to avoid returning errors for inactive services which may
505be enabled at a later time. This is not the same as the service
506being temporarily unavailable.
7be8096f
UD
507@end multitable
508
509These are proposed values. There can be other error codes and the
510described error codes can have different meaning. @strong{With one
511exception:} when returning @code{NSS_STATUS_TRYAGAIN} the error code
512@code{ERANGE} @emph{must} mean that the user provided buffer is too
5455692a 513small. Everything else is non-critical.
7be8096f 514
3a35923e
FW
515In statically linked programs, the main application and NSS modules do
516not share the same thread-local variable @code{errno}, which is the
517reason why there is an explicit @var{errnop} function argument.
518
ba1ffaa1 519The above function has something special which is missing for almost all
706074a5
UD
520the other module functions. There is an argument @var{h_errnop}. This
521points to a variable which will be filled with the error code in case
3a35923e
FW
522the execution of the function fails for some reason. (In statically
523linked programs, the thread-local variable @code{h_errno} is not shared
524with the main application.)
706074a5
UD
525
526The @code{get@var{XXX}by@var{YYY}} functions are the most important
527functions in the NSS modules. But there are others which implement
528the other ways to access system databases (say for the
841785ba 529user database, there are @code{setpwent}, @code{getpwent}, and
706074a5
UD
530@code{endpwent}). These will be described in more detail later.
531Here we give a general way to determine the
532signature of the module function:
533
534@itemize @bullet
535@item
3a35923e 536the return value is @code{enum nss_status};
706074a5 537@item
3a35923e 538the name (@pxref{NSS Module Names});
706074a5
UD
539@item
540the first arguments are identical to the arguments of the non-reentrant
541function;
542@item
3a35923e 543the next four arguments are:
706074a5
UD
544
545@table @code
26dee9c4 546@item STRUCT_TYPE *result_buf
706074a5
UD
547pointer to buffer where the result is stored. @code{STRUCT_TYPE} is
548normally a struct which corresponds to the database.
549@item char *buffer
0bc93a2f 550pointer to a buffer where the function can store additional data for
706074a5 551the result etc.
26dee9c4 552@item size_t buflen
706074a5 553length of the buffer pointed to by @var{buffer}.
3a35923e
FW
554@item int *errnop
555the low-level error code to return to the application. If the return
556value is not @code{NSS_STATUS_SUCCESS}, @code{*@var{errnop}} needs to be
557set to a non-zero value. An NSS module should never set
558@code{*@var{errnop}} to zero. The value @code{ERANGE} is special, as
559described above.
706074a5
UD
560@end table
561
562@item
563possibly a last argument @var{h_errnop}, for the host name and network
3a35923e
FW
564name lookup functions. If the return value is not
565@code{NSS_STATUS_SUCCESS}, @code{*@var{h_errnop}} needs to be set to a
566non-zero value. A generic error code is @code{NETDB_INTERNAL}, which
567instructs the caller to examine @code{*@var{errnop}} for further
568details. (This includes the @code{ERANGE} special case.)
706074a5
UD
569@end itemize
570
571@noindent
572This table is correct for all functions but the @code{set@dots{}ent}
573and @code{end@dots{}ent} functions.
574
575
576@node Extending NSS, , NSS Module Internals, Name Service Switch
577@section Extending NSS
578
579One of the advantages of NSS mentioned above is that it can be extended
580quite easily. There are two ways in which the extension can happen:
581adding another database or adding another service. The former is
582normally done only by the C library developers. It is
583here only important to remember that adding another database is
584independent from adding another service because a service need not
585support all databases or lookup functions.
586
ef48b196 587A designer/implementer of a new service is therefore free to choose the
706074a5
UD
588databases s/he is interested in and leave the rest for later (or
589completely aside).
590
591@menu
592* Adding another Service to NSS:: What is to do to add a new service.
593* NSS Module Function Internals:: Guidelines for writing new NSS
594 service functions.
595@end menu
596
597@node Adding another Service to NSS, NSS Module Function Internals, Extending NSS, Extending NSS
598@subsection Adding another Service to NSS
599
1f77f049
JM
600The sources for a new service need not (and should not) be part of @theglibc{}
601itself. The developer retains complete control over the
706074a5
UD
602sources and its development. The links between the C library and the
603new service module consists solely of the interface functions.
604
605Each module is designed following a specific interface specification.
c66dbe00
UD
606For now the version is 2 (the interface in version 1 was not adequate)
607and this manifests in the version number of the shared library object of
608the NSS modules: they have the extension @code{.2}. If the interface
609changes again in an incompatible way, this number will be increased.
706074a5
UD
610Modules using the old interface will still be usable.
611
612Developers of a new service will have to make sure that their module is
613created using the correct interface number. This means the file itself
0bc93a2f 614must have the correct name and on ELF systems the @dfn{soname} (Shared
706074a5
UD
615Object Name) must also have this number. Building a module from a bunch
616of object files on an ELF system using GNU CC could be done like this:
617
618@smallexample
c66dbe00 619gcc -shared -o libnss_NAME.so.2 -Wl,-soname,libnss_NAME.so.2 OBJECTS
706074a5
UD
620@end smallexample
621
622@noindent
623@ref{Link Options, Options for Linking, , gcc, GNU CC}, to learn
624more about this command line.
625
626To use the new module the library must be able to find it. This can be
627achieved by using options for the dynamic linker so that it will search
04b9968b 628the directory where the binary is placed. For an ELF system this could be
706074a5
UD
629done by adding the wanted directory to the value of
630@code{LD_LIBRARY_PATH}.
631
04b9968b 632But this is not always possible since some programs (those which run
706074a5
UD
633under IDs which do not belong to the user) ignore this variable.
634Therefore the stable version of the module should be placed into a
635directory which is searched by the dynamic linker. Normally this should
636be the directory @file{$prefix/lib}, where @file{$prefix} corresponds to
637the value given to configure using the @code{--prefix} option. But be
638careful: this should only be done if it is clear the module does not
639cause any harm. System administrators should be careful.
640
641
642@node NSS Module Function Internals, , Adding another Service to NSS, Extending NSS
643@subsection Internals of the NSS Module Functions
644
645Until now we only provided the syntactic interface for the functions in
04b9968b 646the NSS module. In fact there is not much more we can say since the
706074a5
UD
647implementation obviously is different for each function. But a few
648general rules must be followed by all functions.
649
650In fact there are four kinds of different functions which may appear in
651the interface. All derive from the traditional ones for system databases.
652@var{db} in the following table is normally an abbreviation for the
841785ba 653database (e.g., it is @code{pw} for the user database).
706074a5
UD
654
655@table @code
a18f587d 656@item enum nss_status _nss_@var{database}_set@var{db}ent (void)
706074a5
UD
657This function prepares the service for following operations. For a
658simple file based lookup this means files could be opened, for other
659services this function simply is a noop.
660
661One special case for this function is that it takes an additional
662argument for some @var{database}s (i.e., the interface is
663@code{int set@var{db}ent (int)}). @ref{Host Names}, which describes the
664@code{sethostent} function.
665
666The return value should be @var{NSS_STATUS_SUCCESS} or according to the
667table above in case of an error (@pxref{NSS Modules Interface}).
668
a18f587d 669@item enum nss_status _nss_@var{database}_end@var{db}ent (void)
706074a5
UD
670This function simply closes all files which are still open or removes
671buffer caches. If there are no files or buffers to remove this is again
672a simple noop.
673
5455692a 674There normally is no return value other than @var{NSS_STATUS_SUCCESS}.
706074a5 675
c66dbe00 676@item enum nss_status _nss_@var{database}_get@var{db}ent_r (@var{STRUCTURE} *result, char *buffer, size_t buflen, int *errnop)
706074a5
UD
677Since this function will be called several times in a row to retrieve
678one entry after the other it must keep some kind of state. But this
679also means the functions are not really reentrant. They are reentrant
680only in that simultaneous calls to this function will not try to
681write the retrieved data in the same place (as it would be the case for
682the non-reentrant functions); instead, it writes to the structure
683pointed to by the @var{result} parameter. But the calls share a common
684state and in the case of a file access this means they return neighboring
685entries in the file.
686
687The buffer of length @var{buflen} pointed to by @var{buffer} can be used
688for storing some additional data for the result. It is @emph{not}
689guaranteed that the same buffer will be passed for the next call of this
690function. Therefore one must not misuse this buffer to save some state
691information from one call to another.
692
3a35923e 693Before the function returns with a failure code, the implementation
010fe231 694should store the value of the local @code{errno} variable in the variable
3a35923e
FW
695pointed to be @var{errnop}. This is important to guarantee the module
696working in statically linked programs. The stored value must not be
697zero.
c66dbe00 698
706074a5
UD
699As explained above this function could also have an additional last
700argument. This depends on the database used; it happens only for
b3a86ae1 701@code{host} and @code{networks}.
706074a5 702
04b9968b 703The function shall return @code{NSS_STATUS_SUCCESS} as long as there are
706074a5
UD
704more entries. When the last entry was read it should return
705@code{NSS_STATUS_NOTFOUND}. When the buffer given as an argument is too
706small for the data to be returned @code{NSS_STATUS_TRYAGAIN} should be
707returned. When the service was not formerly initialized by a call to
5455692a 708@code{_nss_@var{DATABASE}_set@var{db}ent} all return values allowed for
706074a5
UD
709this function can also be returned here.
710
c66dbe00 711@item enum nss_status _nss_@var{DATABASE}_get@var{db}by@var{XX}_r (@var{PARAMS}, @var{STRUCTURE} *result, char *buffer, size_t buflen, int *errnop)
706074a5
UD
712This function shall return the entry from the database which is
713addressed by the @var{PARAMS}. The type and number of these arguments
714vary. It must be individually determined by looking to the user-level
715interface functions. All arguments given to the non-reentrant version
716are here described by @var{PARAMS}.
717
718The result must be stored in the structure pointed to by @var{result}.
5455692a 719If there are additional data to return (say strings, where the
706074a5 720@var{result} structure only contains pointers) the function must use the
5455692a 721@var{buffer} of length @var{buflen}. There must not be any references
706074a5
UD
722to non-constant global data.
723
0bc93a2f 724The implementation of this function should honor the @var{stayopen}
706074a5
UD
725flag set by the @code{set@var{DB}ent} function whenever this makes sense.
726
5455692a 727Before the function returns, the implementation should store the value of
010fe231 728the local @code{errno} variable in the variable pointed to by
5455692a 729@var{errnop}. This is important to guarantee the module works in
c66dbe00
UD
730statically linked programs.
731
706074a5 732Again, this function takes an additional last argument for the
b3a86ae1 733@code{host} and @code{networks} database.
706074a5
UD
734
735The return value should as always follow the rules given above
736(@pxref{NSS Modules Interface}).
737
738@end table