Debian Codegraph @

contrib/slapd-modules/nssov/nss-pam-ldapd/nslcd-prot.h

raw
1 /*
2 nslcd-prot.h - helper macros for reading and writing in protocol streams
3
4 Copyright (C) 2006 West Consulting
5 Copyright (C) 2006-2014 Arthur de Jong
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA
21 */
22
23 #ifndef COMMON__NSLCD_PROT_H
24 #define COMMON__NSLCD_PROT_H 1
25
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28
29 #include "tio.h"
30
31 /* If you use these macros you should define the following macros to
32 handle error conditions (these marcos should clean up and return from the
33 function):
34 ERROR_OUT_WRITEERROR(fp)
35 ERROR_OUT_READERROR(fp)
36 ERROR_OUT_BUFERROR(fp)
37 ERROR_OUT_NOSUCCESS(fp) */
38
39
40 /* Debugging marcos that can be used to enable detailed protocol logging,
41 pass -DDEBUG_PROT to do overall protocol debugging, and -DDEBUG_PROT_DUMP
42 to dump the actual bytestream. */
43
44 #ifdef DEBUG_PROT
45 /* define a debugging macro to output logging */
46 #include <string.h>
47 #include <errno.h>
48 #define DEBUG_PRINT(fmt, arg) \
49 fprintf(stderr, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, \
50 __PRETTY_FUNCTION__, arg);
51 #else /* DEBUG_PROT */
52 /* define an empty debug macro to disable logging */
53 #define DEBUG_PRINT(fmt, arg)
54 #endif /* not DEBUG_PROT */
55
56 #ifdef DEBUG_PROT_DUMP
57 /* define a debugging macro to output detailed logging */
58 #ifdef HAVE_STDINT_H
59 #include <stdint.h>
60 #endif /* HAVE_STDINT_H */
61 static void debug_dump(const void *ptr, size_t size)
62 {
63 int i;
64 for (i = 0; i < size; i++)
65 fprintf(stderr, " %02x", ((const uint8_t *)ptr)[i]);
66 fprintf(stderr, "\n");
67 }
68 #define DEBUG_DUMP(ptr, size) \
69 fprintf(stderr, "%s:%d:%s:", __FILE__, __LINE__, __PRETTY_FUNCTION__); \
70 debug_dump(ptr, size);
71 #else /* DEBUG_PROT_DUMP */
72 /* define an empty debug macro to disable logging */
73 #define DEBUG_DUMP(ptr, size)
74 #endif /* not DEBUG_PROT_DUMP */
75
76
77 /* WRITE marcos, used for writing data, on write error they will
78 call the ERROR_OUT_WRITEERROR macro
79 these macros may require the availability of the following
80 variables:
81 int32_t tmpint32; - temporary variable
82 */
83
84 #define WRITE(fp, ptr, size) \
85 DEBUG_PRINT("WRITE : var="__STRING(ptr)" size=%d", (int)size); \
86 DEBUG_DUMP(ptr, size); \
87 if (tio_write(fp, ptr, (size_t)size)) \
88 { \
89 char ebuf[128]; \
90 int saved_errno = errno; \
91 DEBUG_PRINT("WRITE : var="__STRING(ptr)" error: %s", \
92 AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \
93 ERROR_OUT_WRITEERROR(fp); \
94 }
95
96 #define WRITE_INT32(fp, i) \
97 DEBUG_PRINT("WRITE_INT32 : var="__STRING(i)" int32=%08x", (int)i); \
98 tmpint32 = htonl((int32_t)(i)); \
99 WRITE(fp, &tmpint32, sizeof(int32_t))
100
101 #define WRITE_STRING(fp, str) \
102 DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"", (str)); \
103 if ((str) == NULL) \
104 { \
105 WRITE_INT32(fp, 0); \
106 } \
107 else \
108 { \
109 WRITE_INT32(fp, strlen(str)); \
110 tmpint32 = ntohl(tmpint32); \
111 if (tmpint32 > 0) \
112 { \
113 WRITE(fp, (str), tmpint32); \
114 } \
115 }
116
117 #define WRITE_STRINGLIST(fp, arr) \
118 if ((arr) == NULL) \
119 { \
120 DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", 0); \
121 WRITE_INT32(fp, 0); \
122 } \
123 else \
124 { \
125 /* first determine length of array */ \
126 for (tmp3int32 = 0; (arr)[tmp3int32] != NULL; tmp3int32++) \
127 /* noting */ ; \
128 /* write number of strings */ \
129 DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
130 WRITE_INT32(fp, tmp3int32); \
131 /* write strings */ \
132 for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
133 { \
134 WRITE_STRING(fp, (arr)[tmp2int32]); \
135 } \
136 }
137
138 #define WRITE_STRINGLIST_EXCEPT(fp, arr, not) \
139 /* first determine length of array */ \
140 tmp3int32 = 0; \
141 for (tmp2int32 = 0; (arr)[tmp2int32] != NULL; tmp2int32++) \
142 if (strcmp((arr)[tmp2int32], (not)) != 0) \
143 tmp3int32++; \
144 /* write number of strings (mius one because we intend to skip one) */ \
145 DEBUG_PRINT("WRITE_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
146 WRITE_INT32(fp, tmp3int32); \
147 /* write strings */ \
148 for (tmp2int32 = 0; (arr)[tmp2int32] != NULL; tmp2int32++) \
149 { \
150 if (strcmp((arr)[tmp2int32], (not)) != 0) \
151 { \
152 WRITE_STRING(fp, (arr)[tmp2int32]); \
153 } \
154 }
155
156 /* READ macros, used for reading data, on read error they will
157 call the ERROR_OUT_READERROR or ERROR_OUT_BUFERROR macro
158 these macros may require the availability of the following
159 variables:
160 int32_t tmpint32; - temporary variable
161 */
162
163 #define READ(fp, ptr, size) \
164 if (tio_read(fp, ptr, (size_t)size)) \
165 { \
166 char ebuf[128]; \
167 int saved_errno = errno; \
168 DEBUG_PRINT("READ : var="__STRING(ptr)" error: %s", \
169 AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \
170 ERROR_OUT_READERROR(fp); \
171 } \
172 DEBUG_PRINT("READ : var="__STRING(ptr)" size=%d", (int)(size)); \
173 DEBUG_DUMP(ptr, size);
174
175 #define READ_INT32(fp, i) \
176 READ(fp, &tmpint32, sizeof(int32_t)); \
177 (i) = (int32_t)ntohl(tmpint32); \
178 DEBUG_PRINT("READ_INT32 : var="__STRING(i)" int32==%08x", (int)(i));
179
180 /* read a string in a fixed-size "normal" buffer */
181 #define READ_STRING(fp, buffer) \
182 /* read the size of the string */ \
183 READ(fp, &tmpint32, sizeof(int32_t)); \
184 tmpint32 = ntohl(tmpint32); \
185 DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" strlen=%d", tmpint32); \
186 /* check if read would fit */ \
187 if (((size_t)tmpint32) >= sizeof(buffer)) \
188 { \
189 /* will not fit */ \
190 tmpint32 = (tmpint32 - sizeof(buffer)) + 1; \
191 DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \
192 ERROR_OUT_BUFERROR(fp); \
193 } \
194 /* read string from the stream */ \
195 if (tmpint32 > 0) \
196 { \
197 READ(fp, buffer, (size_t)tmpint32); \
198 } \
199 /* null-terminate string in buffer */ \
200 buffer[tmpint32] = '\0'; \
201 DEBUG_PRINT("READ_STRING: var="__STRING(buffer)" string=\"%s\"", buffer);
202
203
204 /* READ BUF macros that read data into a pre-allocated buffer.
205 these macros may require the availability of the following
206 variables:
207 int32_t tmpint32; - temporary variable
208 char *buffer; - pointer to a buffer for reading strings
209 size_t buflen; - the size of the buffer
210 size_t bufptr; - the current position in the buffer
211 */
212
213 /* current position in the buffer */
214 #define BUF_CUR \
215 (buffer + bufptr)
216
217 /* check that the buffer has sz bytes left in it */
218 #define BUF_CHECK(fp, sz) \
219 if ((bufptr + (size_t)(sz)) > buflen) \
220 { \
221 /* will not fit */ \
222 tmpint32 = bufptr + (sz) - (buflen); \
223 DEBUG_PRINT("READ : buffer %d bytes too small", tmpint32); \
224 ERROR_OUT_BUFERROR(fp); \
225 }
226
227 /* move the buffer pointer */
228 #define BUF_SKIP(sz) \
229 bufptr += (size_t)(sz);
230
231 /* move BUF_CUR forward so that it is aligned to the specified
232 type width */
233 #define BUF_ALIGN(fp, type) \
234 /* figure out number of bytes to skip forward */ \
235 tmp2int32 = (sizeof(type) - ((BUF_CUR - (char *)NULL) % sizeof(type))) \
236 % sizeof(type); \
237 /* check and skip */ \
238 BUF_CHECK(fp, tmp2int32); \
239 BUF_SKIP(tmp2int32);
240
241 /* allocate a piece of the buffer to store an array in */
242 #define BUF_ALLOC(fp, ptr, type, num) \
243 /* align to the specified type width */ \
244 BUF_ALIGN(fp, type); \
245 /* check that we have enough room */ \
246 BUF_CHECK(fp, (size_t)(num) * sizeof(type)); \
247 /* store the pointer */ \
248 (ptr) = (type *)BUF_CUR; \
249 /* reserve the space */ \
250 BUF_SKIP((size_t)(num) * sizeof(type));
251
252 /* read a binary blob into the buffer */
253 #define READ_BUF(fp, ptr, sz) \
254 /* check that there is enough room and read */ \
255 BUF_CHECK(fp, sz); \
256 READ(fp, BUF_CUR, (size_t)sz); \
257 /* store pointer and skip */ \
258 (ptr) = BUF_CUR; \
259 BUF_SKIP(sz);
260
261 /* read string in the buffer (using buffer, buflen and bufptr)
262 and store the actual location of the string in field */
263 #define READ_BUF_STRING(fp, field) \
264 /* read the size of the string */ \
265 READ(fp, &tmpint32, sizeof(int32_t)); \
266 tmpint32 = ntohl(tmpint32); \
267 DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" strlen=%d", tmpint32); \
268 /* check if read would fit */ \
269 BUF_CHECK(fp, tmpint32 + 1); \
270 /* read string from the stream */ \
271 if (tmpint32 > 0) \
272 { \
273 READ(fp, BUF_CUR, (size_t)tmpint32); \
274 } \
275 /* null-terminate string in buffer */ \
276 BUF_CUR[tmpint32] = '\0'; \
277 DEBUG_PRINT("READ_BUF_STRING: var="__STRING(field)" string=\"%s\"", BUF_CUR); \
278 /* prepare result */ \
279 (field) = BUF_CUR; \
280 BUF_SKIP(tmpint32 + 1);
281
282 /* read an array from a stream and store it as a null-terminated
283 array list (size for the array is allocated) */
284 #define READ_BUF_STRINGLIST(fp, arr) \
285 /* read the number of entries */ \
286 READ(fp, &tmp3int32, sizeof(int32_t)); \
287 tmp3int32 = ntohl(tmp3int32); \
288 DEBUG_PRINT("READ_STRLST: var="__STRING(arr)" num=%d", (int)tmp3int32); \
289 /* allocate room for *char[num + 1] */ \
290 BUF_ALLOC(fp, arr, char *, tmp3int32 + 1); \
291 /* read all entries */ \
292 for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
293 { \
294 READ_BUF_STRING(fp, (arr)[tmp2int32]); \
295 } \
296 /* set last entry to NULL */ \
297 (arr)[tmp2int32] = NULL;
298
299
300 /* SKIP macros for skipping over certain parts of the protocol stream. */
301
302 /* skip a number of bytes forward */
303 #define SKIP(fp, sz) \
304 DEBUG_PRINT("READ : skip %d bytes", (int)(sz)); \
305 /* read (skip) the specified number of bytes */ \
306 if (tio_skip(fp, sz)) \
307 { \
308 char ebuf[128]; \
309 int saved_errno = errno; \
310 DEBUG_PRINT("READ : skip error: %s", \
311 AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \
312 ERROR_OUT_READERROR(fp); \
313 }
314
315 /* read a string from the stream but don't do anything with the result */
316 #define SKIP_STRING(fp) \
317 /* read the size of the string */ \
318 READ(fp, &tmpint32, sizeof(int32_t)); \
319 tmpint32 = ntohl(tmpint32); \
320 DEBUG_PRINT("READ_STRING: skip %d bytes", (int)tmpint32); \
321 /* read (skip) the specified number of bytes */ \
322 SKIP(fp, tmpint32);
323
324 /* skip a list of strings */
325 #define SKIP_STRINGLIST(fp) \
326 /* read the number of entries */ \
327 READ(fp, &tmp3int32, sizeof(int32_t)); \
328 tmp3int32 = ntohl(tmp3int32); \
329 DEBUG_PRINT("READ_STRLST: skip %d strings", (int)tmp3int32); \
330 /* read all entries */ \
331 for (tmp2int32 = 0; tmp2int32 < tmp3int32; tmp2int32++) \
332 { \
333 SKIP_STRING(fp); \
334 }
335
336
337 /* These are functions and macros for performing common operations in
338 the nslcd request/response protocol. */
339
340 /* returns a socket to the server or NULL on error (see errno),
341 socket should be closed with tio_close() */
342 TFILE *nslcd_client_open(void)
343 MUST_USE;
344
345 /* generic request code */
346 #define NSLCD_REQUEST(fp, action, writefn) \
347 /* open a client socket */ \
348 if ((fp = nslcd_client_open()) == NULL) \
349 { \
350 ERROR_OUT_OPENERROR; \
351 } \
352 /* write a request header with a request code */ \
353 WRITE_INT32(fp, (int32_t)NSLCD_VERSION) \
354 WRITE_INT32(fp, (int32_t)action) \
355 /* write the request parameters (if any) */ \
356 writefn; \
357 /* flush the stream */ \
358 if (tio_flush(fp) < 0) \
359 { \
360 char ebuf[128]; \
361 int saved_errno = errno; \
362 DEBUG_PRINT("WRITE_FLUSH : error: %s", \
363 AC_STRERROR_R(saved_errno, ebuf, sizeof(ebuf))); \
364 ERROR_OUT_WRITEERROR(fp); \
365 } \
366 /* read and check response version number */ \
367 READ(fp, &tmpint32, sizeof(int32_t)); \
368 tmpint32 = ntohl(tmpint32); \
369 if (tmpint32 != (int32_t)NSLCD_VERSION) \
370 { \
371 ERROR_OUT_READERROR(fp); \
372 } \
373 /* read and check response request number */ \
374 READ(fp, &tmpint32, sizeof(int32_t)); \
375 tmpint32 = ntohl(tmpint32); \
376 if (tmpint32 != (int32_t)(action)) \
377 { \
378 ERROR_OUT_READERROR(fp); \
379 }
380
381 /* Read the response code (the result code of the query) from
382 the stream. */
383 #define READ_RESPONSE_CODE(fp) \
384 READ(fp, &tmpint32, sizeof(int32_t)); \
385 tmpint32 = ntohl(tmpint32); \
386 if (tmpint32 != (int32_t)NSLCD_RESULT_BEGIN) \
387 { \
388 ERROR_OUT_NOSUCCESS(fp); \
389 }
390
391 #endif /* not COMMON__NSLCD_PROT_H */