1 @node Name Service Switch, Users and Groups, Job Control, Top
2 @chapter System Databases and Name Service Switch
3 @c %MENU% Accessing system databases
4 @cindex Name Service Switch
8 Various functions in the C Library need to be configured to work
9 correctly in the local environment. Traditionally, this was done by
10 using files (e.g., @file{/etc/passwd}), but other nameservices (like the
11 Network Information Service (NIS) and the Domain Name Service (DNS))
12 became popular, and were hacked into the C library, usually with a fixed
13 search order (@pxref{frobnicate, , ,jargon, The Jargon File}).
15 The GNU C Library contains a cleaner solution of this problem. It is
16 designed after a method used by Sun Microsystems in the C library of
17 @w{Solaris 2}. GNU C Library follows their name and calls this
18 scheme @dfn{Name Service Switch} (NSS).
20 Though the interface might be similar to Sun's version there is no
21 common code. We never saw any source code of Sun's implementation and
22 so the internal interface is incompatible. This also manifests in the
23 file names we use as we will see later.
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.
33 @node NSS Basics, NSS Configuration File, Name Service Switch, Name Service Switch
36 The basic idea is to put the implementation of the different services
37 offered to access the databases in separate modules. This has some
42 Contributors can add new services without adding them to GNU C Library.
44 The modules can be updated separately.
46 The C library image is smaller.
49 To fulfill the first goal above the ABI of the modules will be described
50 below. For getting the implementation of a new service right it is
51 important to understand how the functions in the modules get called.
52 They are in no way designed to be used by the programmer directly.
53 Instead the programmer should only use the documented and standardized
54 functions to access the databases.
57 The databases available in the NSS are
72 @comment @pxref{Mail Aliases}.
75 @comment @pxref{Ethernet Numbers}.
77 Groups of users, @pxref{Group Database}.
79 Host names and numbers, @pxref{Host Names}.
81 Network wide list of host and users, @pxref{Netgroup Database}.
83 Network names and numbers, @pxref{Networks Database}.
85 Network protocols, @pxref{Protocols Database}.
87 User passwords, @pxref{User Database}.
89 Remote procedure call names and numbers,
90 @comment @pxref{RPC Database}.
92 Network services, @pxref{Services Database}.
94 Shadow user passwords,
95 @comment @pxref{Shadow Password Database}.
99 There will be some more added later (@code{automount}, @code{bootparams},
100 @code{netmasks}, and @code{publickey}).
102 @node NSS Configuration File, NSS Module Internals, NSS Basics, Name Service Switch
103 @section The NSS Configuration File
105 @cindex @file{/etc/nsswitch.conf}
106 @cindex @file{nsswitch.conf}
107 Somehow the NSS code must be told about the wishes of the user. For
108 this reason there is the file @file{/etc/nsswitch.conf}. For each
109 database this file contain a specification how the lookup process should
110 work. The file could look like this:
113 @include nsswitch.texi
116 The first column is the database as you can guess from the table above.
117 The rest of the line specifies how the lookup process works. Please
118 note that you specify the way it works for each database individually.
119 This cannot be done with the old way of a monolithic implementation.
121 The configuration specification for each database can contain two
126 the service specification like @code{files}, @code{db}, or @code{nis}.
128 the reaction on lookup result like @code{[NOTFOUND=return]}.
132 * Services in the NSS configuration:: Service names in the NSS configuration.
133 * Actions in the NSS configuration:: React appropriately to the lookup result.
134 * Notes on NSS Configuration File:: Things to take care about while
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
141 The above example file mentions four different services: @code{files},
142 @code{db}, @code{nis}, and @code{nisplus}. This does not mean these
143 services are available on all sites and it does also not mean these are
144 all the services which will ever be available.
146 In fact, these names are simply strings which the NSS code uses to find
147 the implicitly addressed functions. The internal interface will be
148 described later. Visible to the user are the modules which implement an
151 Assume the service @var{name} shall be used for a lookup. The code for
152 this service is implemented in a module called @file{libnss_@var{name}}.
153 On a system supporting shared libraries this is in fact a shared library
154 with the name (for example) @file{libnss_@var{name}.so.2}. The number
155 at the end is the currently used version of the interface which will not
156 change frequently. Normally the user should not have to be cognizant of
157 these files since they should be placed in a directory where they are
158 found automatically. Only the names of all available services are
161 @node Actions in the NSS configuration, Notes on NSS Configuration File, Services in the NSS configuration, NSS Configuration File
162 @subsection Actions in the NSS configuration
164 The second item in the specification gives the user much finer control
165 on the lookup process. Action items are placed between two service
166 names and are written within brackets. The general form is
169 @code{[} ( @code{!}? @var{status} @code{=} @var{action} )+ @code{]}
176 @var{status} @result{} success | notfound | unavail | tryagain
177 @var{action} @result{} return | continue
180 The case of the keywords is insignificant. The @var{status}
181 values are the results of a call to a lookup function of a specific
186 No error occurred and the wanted entry is returned. The default action
187 for this is @code{return}.
190 The lookup process works ok but the needed value was not found. The
191 default action is @code{continue}.
194 @cindex DNS server unavailable
195 The service is permanently unavailable. This can either mean the needed
196 file is not available, or, for DNS, the server is not available or does
197 not allow queries. The default action is @code{continue}.
200 The service is temporarily unavailable. This could mean a file is
201 locked or a server currently cannot accept more connections. The
202 default action is @code{continue}.
206 If we have a line like
209 ethers: nisplus [NOTFOUND=return] db files
213 this is equivalent to
216 ethers: nisplus [SUCCESS=return NOTFOUND=return UNAVAIL=continue
218 db [SUCCESS=return NOTFOUND=continue UNAVAIL=continue
224 (except that it would have to be written on one line). The default
225 value for the actions are normally what you want, and only need to be
226 changed in exceptional cases.
228 If the optional @code{!} is placed before the @var{status} this means
229 the following action is used for all statii but @var{status} itself.
230 I.e., @code{!} is negation as in the C language (and others).
232 Before we explain the exception which makes this action item necessary
233 one more remark: obviously it makes no sense to add another action
234 item after the @code{files} service. Since there is no other service
235 following the action @emph{always} is @code{return}.
237 @cindex nisplus, and completeness
238 Now, why is this @code{[NOTFOUND=return]} action useful? To understand
239 this we should know that the @code{nisplus} service is often
240 complete; i.e., if an entry is not available in the NIS+ tables it is
241 not available anywhere else. This is what is expressed by this action
242 item: it is useless to examine further services since they will not give
245 @cindex nisplus, and booting
246 @cindex bootstrapping, and services
247 The situation would be different if the NIS+ service is not available
248 because the machine is booting. In this case the return value of the
249 lookup function is not @code{notfound} but instead @code{unavail}. And
250 as you can see in the complete form above: in this situation the
251 @code{db} and @code{files} services are used. Neat, isn't it? The
252 system administrator need not pay special care for the time the system
253 is not completely ready to work (while booting or shutdown or
257 @node Notes on NSS Configuration File, , Actions in the NSS configuration, NSS Configuration File
258 @subsection Notes on the NSS Configuration File
260 Finally a few more hints. The NSS implementation is not completely
261 helpless if @file{/etc/nsswitch.conf} does not exist. For
262 all supported databases there is a default value so it should normally
263 be possible to get the system running even if the file is corrupted or
266 @cindex default value, and NSS
267 For the @code{hosts} and @code{networks} databases the default value is
268 @code{dns [!UNAVAIL=return] files}. I.e., the system is prepared for
269 the DNS service not to be available but if it is available the answer it
270 returns is ultimative.
272 The @code{passwd}, @code{group}, and @code{shadow} databases are
273 traditionally handled in a special way. The appropriate files in the
274 @file{/etc} directory are read but if an entry with a name starting
275 with a @code{+} character is found NIS is used. This kind of lookup
276 remains possible by using the special lookup service @code{compat}
277 and the default value for the three databases above is
278 @code{compat [NOTFOUND=return] files}.
280 For all other databases the default value is
281 @code{nis [NOTFOUND=return] files}. This solution give the best
282 chance to be correct since NIS and file based lookup is used.
284 @cindex optimizing NSS
285 A second point is that the user should try to optimize the lookup
286 process. The different service have different response times.
287 A simple file look up on a local file could be fast, but if the file
288 is long and the needed entry is near the end of the file this may take
289 quite some time. In this case it might be better to use the @code{db}
290 service which allows fast local access to large data sets.
292 Often the situation is that some global information like NIS must be
293 used. So it is unavoidable to use service entries like @code{nis} etc.
294 But one should avoid slow services like this if possible.
297 @node NSS Module Internals, Extending NSS, NSS Configuration File, Name Service Switch
298 @section NSS Module Internals
300 Now it is time to described how the modules look like. The functions
301 contained in a module are identified by their names. I.e., there is no
302 jump table or the like. How this is done is of no interest here; those
303 interested in this topic should read about Dynamic Linking.
304 @comment @ref{Dynamic Linking}.
308 * NSS Module Names:: Construction of the interface function of
310 * NSS Modules Interface:: Programming interface in the NSS module
314 @node NSS Module Names, NSS Modules Interface, NSS Module Internals, NSS Module Internals
315 @subsection The Naming Scheme of the NSS Modules
318 The name of each function consist of various parts:
321 _nss_@var{service}_@var{function}
324 @var{service} of course corresponds to the name of the module this
325 function is found in.@footnote{Now you might ask why to duplicate this
326 information. The answer is that we want to keep the possibility to link
327 directly with these shared objects.} The @var{function} part is derived
328 from the interface function in the C library itself. If the user calls
329 the function @code{gethostbyname} and the service used is @code{files}
333 _nss_files_gethostbyname_r
344 @cindex reentrant NSS functions
345 is used. You see, what is explained above in not the whole truth. In
346 fact the NSS modules only contain reentrant versions of the lookup
347 functions. I.e., if the user would call the @code{gethostbyname_r}
348 function this also would end in the above function. For all user
349 interface functions the C library maps this call to a call to the
350 reentrant function. For reentrant functions this is trivial since the
351 interface is (nearly) the same. For the non-reentrant version The
352 library keeps internal buffers which are used to replace the user
355 I.e., the reentrant functions @emph{can} have counterparts. No service
356 module is forced to have functions for all databases and all kinds to
357 access them. If a function is not available it is simply treated as if
358 the function would return @code{unavail}
359 (@pxref{Actions in the NSS configuration}).
361 The file name @file{libnss_files.so.2} would be on a @w{Solaris 2}
362 system @file{nss_files.so.2}. This is the difference mentioned above.
363 Sun's NSS modules are usable as modules which get indirectly loaded
366 The NSS modules in the GNU C Library are prepared to be used as normal
368 @comment Fix me if necessary.
369 This is @emph{not} true in the moment, though. But the different
370 organization of the name space in the modules does not make it
371 impossible like it is for Solaris. Now you can see why the modules are
372 still libraries.@footnote{There is a second explanation: we were too
373 lazy to change the Makefiles to allow the generation of shared objects
374 not starting with @file{lib} but do not tell this anybody.}
377 @node NSS Modules Interface, , NSS Module Names, NSS Module Internals
378 @subsection The Interface of the Function in NSS Modules
380 Now we know about the functions contained in the modules. It is now
381 time to describe the types. When we mentioned the reentrant versions of
382 the functions above, this means there are some additional arguments
383 (compared with the standard, non-reentrant version). The prototypes for
384 the non-reentrant and reentrant versions of our function above are:
387 struct hostent *gethostbyname (const char *name)
389 int gethostbyname_r (const char *name, struct hostent *result_buf,
390 char *buf, size_t buflen, struct hostent **result,
395 The actual prototype of the function in the NSS modules in this case is
398 enum nss_status _nss_files_gethostbyname_r (const char *name,
399 struct hostent *result_buf,
400 char *buf, size_t buflen,
401 int *errnop, int *h_errnop)
404 I.e., the interface function is in fact the reentrant function with the
405 change of the return value and the omission of the @var{result}
406 parameter. While the user-level function returns a pointer to the
407 result the reentrant function return an @code{enum nss_status} value:
409 @vindex NSS_STATUS_TRYAGAIN
410 @vindex NSS_STATUS_UNAVAIL
411 @vindex NSS_STATUS_NOTFOUND
412 @vindex NSS_STATUS_SUCCESS
414 @item NSS_STATUS_TRYAGAIN
415 numeric value @code{-2}
417 @item NSS_STATUS_UNAVAIL
418 numeric value @code{-1}
420 @item NSS_STATUS_NOTFOUND
421 numeric value @code{0}
423 @item NSS_STATUS_SUCCESS
424 numeric value @code{1}
428 Now you see where the action items of the @file{/etc/nsswitch.conf} file
431 If you study the source code you will find there is a fifth value:
432 @code{NSS_STATUS_RETURN}. This is an internal use only value, used by a
433 few functions in places where none of the above value can be used. If
434 necessary the source code should be examined to learn about the details.
436 The above function has something special which is missing for almost all
437 the other module functions. There is an argument @var{h_errnop}. This
438 points to a variable which will be filled with the error code in case
439 the execution of the function fails for some reason. The reentrant
440 function cannot use the global variable @var{h_errno};
441 @code{gethostbyname} calls @code{gethostbyname_r} with the
442 last argument set to @code{&h_errno}.
444 The @code{get@var{XXX}by@var{YYY}} functions are the most important
445 functions in the NSS modules. But there are others which implement
446 the other ways to access system databases (say for the
447 password database, there are @code{setpwent}, @code{getpwent}, and
448 @code{endpwent}). These will be described in more detail later.
449 Here we give a general way to determine the
450 signature of the module function:
454 the return value is @code{int};
456 the name is as explain in @pxref{NSS Module Names};
458 the first arguments are identical to the arguments of the non-reentrant
461 the next three arguments are:
464 @item STRUCT_TYPE *result_buf
465 pointer to buffer where the result is stored. @code{STRUCT_TYPE} is
466 normally a struct which corresponds to the database.
468 pointer to a buffer where the function can store additional adata for
471 length of the buffer pointed to by @var{buffer}.
475 possibly a last argument @var{h_errnop}, for the host name and network
476 name lookup functions.
480 This table is correct for all functions but the @code{set@dots{}ent}
481 and @code{end@dots{}ent} functions.
484 @node Extending NSS, , NSS Module Internals, Name Service Switch
485 @section Extending NSS
487 One of the advantages of NSS mentioned above is that it can be extended
488 quite easily. There are two ways in which the extension can happen:
489 adding another database or adding another service. The former is
490 normally done only by the C library developers. It is
491 here only important to remember that adding another database is
492 independent from adding another service because a service need not
493 support all databases or lookup functions.
495 A designer/implementor of a new service is therefore free to choose the
496 databases s/he is interested in and leave the rest for later (or
500 * Adding another Service to NSS:: What is to do to add a new service.
501 * NSS Module Function Internals:: Guidelines for writing new NSS
505 @node Adding another Service to NSS, NSS Module Function Internals, Extending NSS, Extending NSS
506 @subsection Adding another Service to NSS
508 The sources for a new service need not (and should not) be part of the
509 GNU C Library itself. The developer retains complete control over the
510 sources and its development. The links between the C library and the
511 new service module consists solely of the interface functions.
513 Each module is designed following a specific interface specification.
514 For now the version is 2 (the interface in version 1 was not adequate)
515 and this manifests in the version number of the shared library object of
516 the NSS modules: they have the extension @code{.2}. If the interface
517 changes again in an incompatible way, this number will be increased.
518 Modules using the old interface will still be usable.
520 Developers of a new service will have to make sure that their module is
521 created using the correct interface number. This means the file itself
522 must have the correct name and on ElF systems the @dfn{soname} (Shared
523 Object Name) must also have this number. Building a module from a bunch
524 of object files on an ELF system using GNU CC could be done like this:
527 gcc -shared -o libnss_NAME.so.2 -Wl,-soname,libnss_NAME.so.2 OBJECTS
531 @ref{Link Options, Options for Linking, , gcc, GNU CC}, to learn
532 more about this command line.
534 To use the new module the library must be able to find it. This can be
535 achieved by using options for the dynamic linker so that it will search
536 directory where the binary is placed. For an ELF system this could be
537 done by adding the wanted directory to the value of
538 @code{LD_LIBRARY_PATH}.
540 But this is not always possible since some program (those which run
541 under IDs which do not belong to the user) ignore this variable.
542 Therefore the stable version of the module should be placed into a
543 directory which is searched by the dynamic linker. Normally this should
544 be the directory @file{$prefix/lib}, where @file{$prefix} corresponds to
545 the value given to configure using the @code{--prefix} option. But be
546 careful: this should only be done if it is clear the module does not
547 cause any harm. System administrators should be careful.
550 @node NSS Module Function Internals, , Adding another Service to NSS, Extending NSS
551 @subsection Internals of the NSS Module Functions
553 Until now we only provided the syntactic interface for the functions in
554 the NSS module. In fact there is not more much we can tell since the
555 implementation obviously is different for each function. But a few
556 general rules must be followed by all functions.
558 In fact there are four kinds of different functions which may appear in
559 the interface. All derive from the traditional ones for system databases.
560 @var{db} in the following table is normally an abbreviation for the
561 database (e.g., it is @code{pw} for the password database).
564 @item enum nss_status _nss_@var{database}_set@var{db}ent (void)
565 This function prepares the service for following operations. For a
566 simple file based lookup this means files could be opened, for other
567 services this function simply is a noop.
569 One special case for this function is that it takes an additional
570 argument for some @var{database}s (i.e., the interface is
571 @code{int set@var{db}ent (int)}). @ref{Host Names}, which describes the
572 @code{sethostent} function.
574 The return value should be @var{NSS_STATUS_SUCCESS} or according to the
575 table above in case of an error (@pxref{NSS Modules Interface}).
577 @item enum nss_status _nss_@var{database}_end@var{db}ent (void)
578 This function simply closes all files which are still open or removes
579 buffer caches. If there are no files or buffers to remove this is again
582 There normally is no return value different to @var{NSS_STATUS_SUCCESS}.
584 @item enum nss_status _nss_@var{database}_get@var{db}ent_r (@var{STRUCTURE} *result, char *buffer, size_t buflen, int *errnop)
585 Since this function will be called several times in a row to retrieve
586 one entry after the other it must keep some kind of state. But this
587 also means the functions are not really reentrant. They are reentrant
588 only in that simultaneous calls to this function will not try to
589 write the retrieved data in the same place (as it would be the case for
590 the non-reentrant functions); instead, it writes to the structure
591 pointed to by the @var{result} parameter. But the calls share a common
592 state and in the case of a file access this means they return neighboring
595 The buffer of length @var{buflen} pointed to by @var{buffer} can be used
596 for storing some additional data for the result. It is @emph{not}
597 guaranteed that the same buffer will be passed for the next call of this
598 function. Therefore one must not misuse this buffer to save some state
599 information from one call to another.
601 Before the function returns the implementation should store the value of
602 the local @var{errno} variable in the variable pointed to be
603 @var{errnop}. This is important to guarantee the module working in
604 statically linked programs.
606 As explained above this function could also have an additional last
607 argument. This depends on the database used; it happens only for
608 @code{host} and @code{networks}.
610 The function shall return @code{NSS_STATUS_SUCCESS} as long as their are
611 more entries. When the last entry was read it should return
612 @code{NSS_STATUS_NOTFOUND}. When the buffer given as an argument is too
613 small for the data to be returned @code{NSS_STATUS_TRYAGAIN} should be
614 returned. When the service was not formerly initialized by a call to
615 @code{_nss_@var{DATABASE}_set@var{db}ent} all return value allowed for
616 this function can also be returned here.
618 @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)
619 This function shall return the entry from the database which is
620 addressed by the @var{PARAMS}. The type and number of these arguments
621 vary. It must be individually determined by looking to the user-level
622 interface functions. All arguments given to the non-reentrant version
623 are here described by @var{PARAMS}.
625 The result must be stored in the structure pointed to by @var{result}.
626 If there is additional data to return (say strings, where the
627 @var{result} structure only contains pointers) the function must use the
628 @var{buffer} or length @var{buflen}. There must not be any references
629 to non-constant global data.
631 The implementation of this function should honour the @var{stayopen}
632 flag set by the @code{set@var{DB}ent} function whenever this makes sense.
634 Before the function returns the implementation should store the value of
635 the local @var{errno} variable in the variable pointed to be
636 @var{errnop}. This is important to guarantee the module working in
637 statically linked programs.
639 Again, this function takes an additional last argument for the
640 @code{host} and @code{networks} database.
642 The return value should as always follow the rules given above
643 (@pxref{NSS Modules Interface}).