about summary refs log tree commit diff
path: root/nss/nss_action.h
blob: 7ab24154175f3d5dae7bcb256ccb9178d661998e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* NSS actions, elements in a nsswitch.conf configuration line.
   Copyright (c) 2020-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#ifndef _NSS_ACTION_H
#define _NSS_ACTION_H

#include <stddef.h>

/* See nss_database.h for a summary of how this relates.  */

#include "nsswitch.h" /* For lookup_actions.  */

struct nss_module;

/* A NSS action pairs a service module with the action for each result
   state.  */
struct nss_action
{
  /* The service module that provides the functionality (potentially
     not yet loaded).  */
  struct nss_module *module;

  /* Action according to result.  Two bits for each lookup_actions
     value (from nsswitch.h), indexed by enum nss_status (from nss.h).  */
  unsigned int action_bits;
};

/* Value to add to first nss_status value to get zero.  */
#define NSS_STATUS_BIAS 2
/* Number of bits per lookup action.  */
#define NSS_BPL 2
#define NSS_BPL_MASK ((1 << NSS_BPL) - 1)

/* Index in actions of an NSS status.  Note that in nss/nss.h the
   status starts at -2, and we shift that up to zero by adding 2.
   Thus for example NSS_STATUS_TRYAGAIN, which is -2, would index into
   the 0th bit place as expected.  */
static inline int
nss_actions_bits_index (enum nss_status status)
{
  return NSS_BPL * (NSS_STATUS_BIAS + status);
}

/* Returns the lookup_action value for STATUS in ACTION.  */
static inline lookup_actions
nss_action_get (const struct nss_action *action, enum nss_status status)
{
  return ((action->action_bits >> nss_actions_bits_index (status))
	  & NSS_BPL_MASK);
}

/* Sets the lookup_action value for STATUS in ACTION.  */
static inline void
nss_action_set (struct nss_action *action,
                enum nss_status status, lookup_actions actions)
{
  int offset = nss_actions_bits_index (status);
  unsigned int mask = NSS_BPL_MASK << offset;
  action->action_bits = ((action->action_bits & ~mask)
                         | ((unsigned int) actions << offset));
}

static inline void
nss_action_set_all (struct nss_action *action, lookup_actions actions)
{
  unsigned int bits = actions & NSS_BPL_MASK;
  action->action_bits = (   bits
			 | (bits << (NSS_BPL * 1))
			 | (bits << (NSS_BPL * 2))
			 | (bits << (NSS_BPL * 3))
			 | (bits << (NSS_BPL * 4))
			 );
}

/* A list of struct nss_action objects in array terminated by an
   action with a NULL module.  */
typedef struct nss_action *nss_action_list;

/* Returns a pointer to an allocated NSS action list that has COUNT
   actions that matches the array at ACTIONS.  */
nss_action_list __nss_action_allocate (struct nss_action *actions,
                                       size_t count) attribute_hidden;

/* Returns a pointer to a list allocated by __nss_action_allocate, or
   NULL on error.  ENOMEM means a (temporary) memory allocation error,
   EINVAL means that LINE is syntactically invalid.  */
nss_action_list __nss_action_parse (const char *line);


#endif /* _NSS_ACTION_H */