1 /* NSS actions, elements in a nsswitch.conf configuration line.
2 Copyright (c) 2020-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _NSS_ACTION_H
20 #define _NSS_ACTION_H
21
22 #include <stddef.h>
23
24 /* See nss_database.h for a summary of how this relates. */
25
26 #include "nsswitch.h" /* For lookup_actions. */
27
28 struct nss_module;
29
30 /* A NSS action pairs a service module with the action for each result
31 state. */
32 struct nss_action
33 {
34 /* The service module that provides the functionality (potentially
35 not yet loaded). */
36 struct nss_module *module;
37
38 /* Action according to result. Two bits for each lookup_actions
39 value (from nsswitch.h), indexed by enum nss_status (from nss.h). */
40 unsigned int action_bits;
41 };
42
43 /* Value to add to first nss_status value to get zero. */
44 #define NSS_STATUS_BIAS 2
45 /* Number of bits per lookup action. */
46 #define NSS_BPL 2
47 #define NSS_BPL_MASK ((1 << NSS_BPL) - 1)
48
49 /* Index in actions of an NSS status. Note that in nss/nss.h the
50 status starts at -2, and we shift that up to zero by adding 2.
51 Thus for example NSS_STATUS_TRYAGAIN, which is -2, would index into
52 the 0th bit place as expected. */
53 static inline int
nss_actions_bits_index(enum nss_status status)54 nss_actions_bits_index (enum nss_status status)
55 {
56 return NSS_BPL * (NSS_STATUS_BIAS + status);
57 }
58
59 /* Returns the lookup_action value for STATUS in ACTION. */
60 static inline lookup_actions
nss_action_get(const struct nss_action * action,enum nss_status status)61 nss_action_get (const struct nss_action *action, enum nss_status status)
62 {
63 return ((action->action_bits >> nss_actions_bits_index (status))
64 & NSS_BPL_MASK);
65 }
66
67 /* Sets the lookup_action value for STATUS in ACTION. */
68 static inline void
nss_action_set(struct nss_action * action,enum nss_status status,lookup_actions actions)69 nss_action_set (struct nss_action *action,
70 enum nss_status status, lookup_actions actions)
71 {
72 int offset = nss_actions_bits_index (status);
73 unsigned int mask = NSS_BPL_MASK << offset;
74 action->action_bits = ((action->action_bits & ~mask)
75 | ((unsigned int) actions << offset));
76 }
77
78 static inline void
nss_action_set_all(struct nss_action * action,lookup_actions actions)79 nss_action_set_all (struct nss_action *action, lookup_actions actions)
80 {
81 unsigned int bits = actions & NSS_BPL_MASK;
82 action->action_bits = ( bits
83 | (bits << (NSS_BPL * 1))
84 | (bits << (NSS_BPL * 2))
85 | (bits << (NSS_BPL * 3))
86 | (bits << (NSS_BPL * 4))
87 );
88 }
89
90 /* A list of struct nss_action objects in array terminated by an
91 action with a NULL module. */
92 typedef struct nss_action *nss_action_list;
93
94 /* Returns a pointer to an allocated NSS action list that has COUNT
95 actions that matches the array at ACTIONS. */
96 nss_action_list __nss_action_allocate (struct nss_action *actions,
97 size_t count) attribute_hidden;
98
99 /* Returns a pointer to a list allocated by __nss_action_allocate, or
100 NULL on error. ENOMEM means a (temporary) memory allocation error,
101 EINVAL means that LINE is syntactically invalid. */
102 nss_action_list __nss_action_parse (const char *line);
103
104 /* Called from __libc_freeres. */
105 void __nss_action_freeres (void) attribute_hidden;
106
107
108 #endif /* _NSS_ACTION_H */
109