Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/nss/common.h
blob: 7873b93de86f38519eb95be08000fe43019a419f (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
   common.h - common functions for NSS lookups

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Arthur de Jong

   This 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.

   This 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 this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#ifndef NSS__COMMON_H
#define NSS__COMMON_H 1

#include <stdio.h>
#include <stdlib.h>

#include "nslcd.h"
#include "common/nslcd-prot.h"
#include "compat/attrs.h"
#include "compat/nss_compat.h"

/* These are macros for handling read and write problems, they are
   NSS specific due to the return code so are defined here. They
   genrally close the open file, set an error code and return with
   an error status. */

/* Macro is called to handle errors in opening a client connection. */
#define ERROR_OUT_OPENERROR \
  *errnop=ENOENT; \
  return (errno==EAGAIN)?NSS_STATUS_TRYAGAIN:NSS_STATUS_UNAVAIL;

/* Macro is called to handle errors on read operations. */
#define ERROR_OUT_READERROR(fp) \
  (void)tio_close(fp); \
  fp=NULL; \
  *errnop=ENOENT; \
  return NSS_STATUS_UNAVAIL;

/* Macro is called to handle problems with too small a buffer.
   This triggers the caller to call the function with a larger
   buffer (see NSS_GETENT below). */
#define ERROR_OUT_BUFERROR(fp) \
  *errnop=ERANGE; \
  return NSS_STATUS_TRYAGAIN;

/* This macro is called if there was a problem with a write
   operation. */
#define ERROR_OUT_WRITEERROR(fp) \
  ERROR_OUT_READERROR(fp)

/* This macro is called if the read status code is not
   NSLCD_RESULT_BEGIN. */
#define ERROR_OUT_NOSUCCESS(fp) \
  (void)tio_close(fp); \
  fp=NULL; \
  return NSS_STATUS_NOTFOUND;

/* These are some general macros that are used to build parts of the
   genral macros below. */

/* check to see if we should answer NSS requests */
#define NSS_AVAILCHECK \
  if (!_nss_ldap_enablelookups) \
    return NSS_STATUS_UNAVAIL;

#ifdef NSS_FLAVOUR_GLIBC

/* extra definitions we need (nothing for Glibc) */
#define NSS_EXTRA_DEFS ;

/* check validity of passed buffer (Glibc flavour) */
#define NSS_BUFCHECK \
  if ((buffer==NULL)||(buflen==0)) \
  { \
      *errnop=EINVAL; \
      return NSS_STATUS_UNAVAIL; \
  }

#endif /* NSS_FLAVOUR_GLIBC */

#ifdef NSS_FLAVOUR_SOLARIS

/* extra definitions we need (Solaris NSS functions don't pass errno) */
#define NSS_EXTRA_DEFS \
  int *errnop=&(errno);

/* check validity of passed buffer (Solaris flavour) */
#define NSS_BUFCHECK \
  if ((NSS_ARGS(args)->buf.buffer==NULL)||(NSS_ARGS(args)->buf.buflen<=0)) \
  { \
      NSS_ARGS(args)->erange=1; \
      return NSS_STATUS_TRYAGAIN; \
  }

/* this is the backend structure for Solaris */
struct nss_ldap_backend
{
  nss_backend_op_t *ops; /* function-pointer table */
  int n_ops; /* number of function pointers */
  TFILE *fp; /* the file pointer for {set,get,end}ent() functions */
};

/* constructor for LDAP backends */
nss_backend_t *nss_ldap_constructor(nss_backend_op_t *ops,size_t sizeofops);

/* destructor for LDAP backends */
nss_status_t nss_ldap_destructor(nss_backend_t *be,void UNUSED(*args));

#endif /* NSS_FLAVOUR_SOLARIS */

/* The following macros to automatically generate get..byname(),
   get..bynumber(), setent(), getent() and endent() function
   bodies. These functions have very common code so this can
   easily be reused. */

/* This is a generic get..by..() generation macro. The action
   parameter is the NSLCD_ACTION_.. action, the writefn is the
   operation for writing the parameters and readfn is the function
   name for reading a single result entry. The function is assumed
   to have result, buffer, buflen and errnop parameters that define
   the result structure, the user buffer with length and the
   errno to return. This macro should be called through some of
   the customized ones below. */
#define NSS_BYGEN(action,writefn,readfn) \
  TFILE *fp; \
  int32_t tmpint32; \
  nss_status_t retv; \
  NSS_EXTRA_DEFS; \
  NSS_AVAILCHECK; \
  NSS_BUFCHECK; \
  /* open socket and write request */ \
  NSLCD_REQUEST(fp,action,writefn); \
  /* read response */ \
  READ_RESPONSE_CODE(fp); \
  retv=readfn; \
  /* close socket and we're done */ \
  if ((retv==NSS_STATUS_SUCCESS)||(retv==NSS_STATUS_TRYAGAIN)) \
  { \
    (void)tio_skipall(fp); \
    (void)tio_close(fp); \
  } \
  return retv;

/* This macro can be used to generate a get..byname() function
   body. */
#define NSS_BYNAME(action,name,readfn) \
  NSS_BYGEN(action,WRITE_STRING(fp,name),readfn)

/* This macro can be used to generate a get..by..() function
   body where the value that is the key has the specified type. */
#define NSS_BYTYPE(action,val,type,readfn) \
  NSS_BYGEN(action,WRITE_TYPE(fp,val,type),readfn)

/* This macro can be used to generate a get..by..() function
   body where the value should be passed as an int32_t. */
#define NSS_BYINT32(action,val,readfn) \
  NSS_BYGEN(action,WRITE_INT32(fp,val),readfn)

/* This macro generates a simple setent() function body. This closes any
   open streams so that NSS_GETENT() can open a new file. */
#define NSS_SETENT(fp) \
  NSS_AVAILCHECK; \
  if (fp!=NULL) \
  { \
    (void)tio_close(fp); \
    fp=NULL; \
  } \
  return NSS_STATUS_SUCCESS;

/* This macro generates a getent() function body. If the stream is not yet
   open, a new one is opened, a request is written and a check is done for
   a response header. A single entry is read with the readfn() function. */
#define NSS_GETENT(fp,action,readfn) \
  int32_t tmpint32; \
  nss_status_t retv; \
  NSS_EXTRA_DEFS; \
  NSS_AVAILCHECK; \
  NSS_BUFCHECK; \
  /* check that we have a valid file descriptor */ \
  if (fp==NULL) \
  { \
    /* open a new stream and write the request */ \
    NSLCD_REQUEST(fp,action,/* no writefn */;); \
  } \
  /* prepare for buffer errors */ \
  tio_mark(fp); \
  /* read a response */ \
  READ_RESPONSE_CODE(fp); \
  retv=readfn; \
  /* check read result */ \
  if (retv==NSS_STATUS_TRYAGAIN) \
  { \
    /* if we have a full buffer try to reset the stream */ \
    if (tio_reset(fp)) \
    { \
      /* reset failed, we close and give up with a permanent error \
         because we cannot retry just the getent() call because it \
         may not be only the first entry that failed */ \
      tio_close(fp); \
      fp=NULL; \
      *errnop=EINVAL; \
      return NSS_STATUS_UNAVAIL; \
    } \
  } \
  else if (retv!=NSS_STATUS_SUCCESS) \
    fp=NULL; /* file should be closed by now */ \
  return retv;

/* This macro generates an endent() function body. This just closes
   the stream. */
#define NSS_ENDENT(fp) \
  NSS_AVAILCHECK; \
  if (fp!=NULL) \
  { \
    (void)tio_skipall(fp); \
    (void)tio_close(fp); \
    fp=NULL; \
  } \
  return NSS_STATUS_SUCCESS;

#endif /* not NSS__COMMON_H */