Merge branch 'vendor/GCC44'
[games.git] / contrib / bind-9.3 / lib / bind / irs / irp.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996, 1998 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: irp.c,v 1.3.2.1.10.4 2006/03/10 00:17:21 marka Exp $";
20 #endif
21
22 /* Imports */
23
24 #include "port_before.h"
25
26 #include <syslog.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <syslog.h>
38 #include <ctype.h>
39 #include <unistd.h>
40
41 #include <isc/memcluster.h>
42
43 #include <irs.h>
44 #include <irp.h>
45
46 #include "irs_p.h"
47 #include "irp_p.h"
48
49 #include "port_after.h"
50
51 /* Forward. */
52
53 static void             irp_close(struct irs_acc *);
54
55 #define LINEINCR 128
56
57 #if !defined(SUN_LEN)
58 #define SUN_LEN(su) \
59         (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
60 #endif
61
62
63 /* Public */
64
65
66 /* send errors to syslog if true. */
67 int irp_log_errors = 1;
68
69 /*
70  * This module handles the irp module connection to irpd.
71  *
72  * The client expects a synchronous interface to functions like
73  * getpwnam(3), so we can't use the ctl_* i/o library on this end of
74  * the wire (it's used in the server).
75  */
76
77 /*
78  * irs_acc *irs_irp_acc(const char *options);
79  *
80  *      Initialize the irp module.
81  */
82 struct irs_acc *
83 irs_irp_acc(const char *options) {
84         struct irs_acc *acc;
85         struct irp_p *irp;
86
87         UNUSED(options);
88
89         if (!(acc = memget(sizeof *acc))) {
90                 errno = ENOMEM;
91                 return (NULL);
92         }
93         memset(acc, 0x5e, sizeof *acc);
94         if (!(irp = memget(sizeof *irp))) {
95                 errno = ENOMEM;
96                 free(acc);
97                 return (NULL);
98         }
99         irp->inlast = 0;
100         irp->incurr = 0;
101         irp->fdCxn = -1;
102         acc->private = irp;
103
104 #ifdef WANT_IRS_GR
105         acc->gr_map = irs_irp_gr;
106 #else
107         acc->gr_map = NULL;
108 #endif
109 #ifdef WANT_IRS_PW
110         acc->pw_map = irs_irp_pw;
111 #else
112         acc->pw_map = NULL;
113 #endif
114         acc->sv_map = irs_irp_sv;
115         acc->pr_map = irs_irp_pr;
116         acc->ho_map = irs_irp_ho;
117         acc->nw_map = irs_irp_nw;
118         acc->ng_map = irs_irp_ng;
119         acc->close = irp_close;
120         return (acc);
121 }
122
123
124 int
125 irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
126         if (irs_irp_is_connected(cxndata)) {
127                 return (0);
128         } else if (irs_irp_connect(cxndata) != 0) {
129                 if (warned != NULL && !*warned) {
130                         syslog(LOG_ERR, "irpd connection failed: %m\n");
131                         (*warned)++;
132                 }
133
134                 return (-1);
135         }
136
137         return (0);
138 }
139
140
141 /*
142  * int irs_irp_connect(void);
143  *
144  *      Sets up the connection to the remote irpd server.
145  *
146  * Returns:
147  *
148  *      0 on success, -1 on failure.
149  *
150  */
151 int
152 irs_irp_connect(struct irp_p *pvt) {
153         int flags;
154         struct sockaddr *addr;
155         struct sockaddr_in iaddr;
156 #ifndef NO_SOCKADDR_UN
157         struct sockaddr_un uaddr;
158 #endif
159         long ipaddr;
160         const char *irphost;
161         int code;
162         char text[256];
163         int socklen = 0;
164
165         if (pvt->fdCxn != -1) {
166                 perror("fd != 1");
167                 return (-1);
168         }
169
170 #ifndef NO_SOCKADDR_UN
171         memset(&uaddr, 0, sizeof uaddr);
172 #endif
173         memset(&iaddr, 0, sizeof iaddr);
174
175         irphost = getenv(IRPD_HOST_ENV);
176         if (irphost == NULL) {
177                 irphost = "127.0.0.1";
178         }
179
180 #ifndef NO_SOCKADDR_UN
181         if (irphost[0] == '/') {
182                 addr = (struct sockaddr *)&uaddr;
183                 strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
184                 uaddr.sun_family = AF_UNIX;
185                 socklen = SUN_LEN(&uaddr);
186 #ifdef HAVE_SA_LEN
187                 uaddr.sun_len = socklen;
188 #endif
189         } else
190 #endif
191         {
192                 if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
193                         errno = EADDRNOTAVAIL;
194                         perror("inet_pton");
195                         return (-1);
196                 }
197
198                 addr = (struct sockaddr *)&iaddr;
199                 socklen = sizeof iaddr;
200 #ifdef HAVE_SA_LEN
201                 iaddr.sin_len = socklen;
202 #endif
203                 iaddr.sin_family = AF_INET;
204                 iaddr.sin_port = htons(IRPD_PORT);
205                 iaddr.sin_addr.s_addr = ipaddr;
206         }
207
208
209         pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
210         if (pvt->fdCxn < 0) {
211                 perror("socket");
212                 return (-1);
213         }
214
215         if (connect(pvt->fdCxn, addr, socklen) != 0) {
216                 perror("connect");
217                 return (-1);
218         }
219
220         flags = fcntl(pvt->fdCxn, F_GETFL, 0);
221         if (flags < 0) {
222                 close(pvt->fdCxn);
223                 perror("close");
224                 return (-1);
225         }
226
227 #if 0
228         flags |= O_NONBLOCK;
229         if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
230                 close(pvt->fdCxn);
231                 perror("fcntl");
232                 return (-1);
233         }
234 #endif
235
236         code = irs_irp_read_response(pvt, text, sizeof text);
237         if (code != IRPD_WELCOME_CODE) {
238                 if (irp_log_errors) {
239                         syslog(LOG_WARNING, "Connection failed: %s", text);
240                 }
241                 irs_irp_disconnect(pvt);
242                 return (-1);
243         }
244
245         return (0);
246 }
247
248
249
250 /*
251  * int  irs_irp_is_connected(struct irp_p *pvt);
252  *
253  * Returns:
254  *
255  *      Non-zero if streams are setup to remote.
256  *
257  */
258
259 int
260 irs_irp_is_connected(struct irp_p *pvt) {
261         return (pvt->fdCxn >= 0);
262 }
263
264
265
266 /*
267  * void
268  * irs_irp_disconnect(struct irp_p *pvt);
269  *
270  *      Closes streams to remote.
271  */
272
273 void
274 irs_irp_disconnect(struct irp_p *pvt) {
275         if (pvt->fdCxn != -1) {
276                 close(pvt->fdCxn);
277                 pvt->fdCxn = -1;
278         }
279 }
280
281
282
283 int
284 irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
285         char *realstart = &pvt->inbuffer[0];
286         char *p, *start, *end;
287         int spare;
288         int i;
289         int buffpos = 0;
290         int left = len - 1;
291
292         while (left > 0) {
293                 start = p = &pvt->inbuffer[pvt->incurr];
294                 end = &pvt->inbuffer[pvt->inlast];
295
296                 while (p != end && *p != '\n')
297                         p++;
298
299                 if (p == end) {
300                         /* Found no newline so shift data down if necessary
301                          * and append new data to buffer
302                          */
303                         if (start > realstart) {
304                                 memmove(realstart, start, end - start);
305                                 pvt->inlast = end - start;
306                                 start = realstart;
307                                 pvt->incurr = 0;
308                                 end = &pvt->inbuffer[pvt->inlast];
309                         }
310
311                         spare = sizeof (pvt->inbuffer) - pvt->inlast;
312
313                         p = end;
314                         i = read(pvt->fdCxn, end, spare);
315                         if (i < 0) {
316                                 close(pvt->fdCxn);
317                                 pvt->fdCxn = -1;
318                                 return (buffpos > 0 ? buffpos : -1);
319                         } else if (i == 0) {
320                                 return (buffpos);
321                         }
322
323                         end += i;
324                         pvt->inlast += i;
325
326                         while (p != end && *p != '\n')
327                                 p++;
328                 }
329
330                 if (p == end) {
331                         /* full buffer and still no newline */
332                         i = sizeof pvt->inbuffer;
333                 } else {
334                         /* include newline */
335                         i = p - start + 1;
336                 }
337
338                 if (i > left)
339                         i = left;
340                 memcpy(buffer + buffpos, start, i);
341                 pvt->incurr += i;
342                 buffpos += i;
343                 buffer[buffpos] = '\0';
344
345                 if (p != end) {
346                         left = 0;
347                 } else {
348                         left -= i;
349                 }
350         }
351
352 #if 0
353         fprintf(stderr, "read line: %s\n", buffer);
354 #endif
355         return (buffpos);
356 }
357
358
359
360
361
362 /*
363  * int irp_read_response(struct irp_p *pvt);
364  *
365  * Returns:
366  *
367  *      The number found at the beginning of the line read from
368  *      FP. 0 on failure(0 is not a legal response code). The
369  *      rest of the line is discarded.
370  *
371  */
372
373 int
374 irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
375         char line[1024];
376         int code;
377         char *p;
378
379         if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
380                 return (0);
381         }
382
383         p = strchr(line, '\n');
384         if (p == NULL) {
385                 return (0);
386         }
387
388         if (sscanf(line, "%d", &code) != 1) {
389                 code = 0;
390         } else if (text != NULL && textlen > 0U) {
391                 p = line;
392                 while (isspace((unsigned char)*p)) p++;
393                 while (isdigit((unsigned char)*p)) p++;
394                 while (isspace((unsigned char)*p)) p++;
395                 strncpy(text, p, textlen - 1);
396                 p[textlen - 1] = '\0';
397         }
398
399         return (code);
400 }
401
402
403
404 /*
405  * char *irp_read_body(struct irp_p *pvt, size_t *size);
406  *
407  *      Read in the body of a response. Terminated by a line with
408  *      just a dot on it. Lines should be terminated with a CR-LF
409  *      sequence, but we're nt piccky if the CR is missing.
410  *      No leading dot escaping is done as the protcol doesn't
411  *      use leading dots anywhere.
412  *
413  * Returns:
414  *
415  *      Pointer to null-terminated buffer allocated by memget.
416  *      *SIZE is set to the length of the buffer.
417  *
418  */
419
420 char *
421 irs_irp_read_body(struct irp_p *pvt, size_t *size) {
422         char line[1024];
423         u_int linelen;
424         size_t len = LINEINCR;
425         char *buffer = memget(len);
426         int idx = 0;
427
428         if (buffer == NULL)
429                 return (NULL);
430
431         for (;;) {
432                 if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
433                     strchr(line, '\n') == NULL)
434                         goto death;
435
436                 linelen = strlen(line);
437
438                 if (line[linelen - 1] != '\n')
439                         goto death;
440
441                 /* We're not strict about missing \r. Should we be??  */
442                 if (linelen > 2 && line[linelen - 2] == '\r') {
443                         line[linelen - 2] = '\n';
444                         line[linelen - 1] = '\0';
445                         linelen--;
446                 }
447
448                 if (linelen == 2 && line[0] == '.') {
449                         *size = len;
450                         buffer[idx] = '\0';
451
452                         return (buffer);
453                 }
454
455                 if (linelen > (len - (idx + 1))) {
456                         char *p = memget(len + LINEINCR);
457
458                         if (p == NULL)
459                                 goto death;
460                         memcpy(p, buffer, len);
461                         memput(buffer, len);
462                         buffer = p;
463                         len += LINEINCR;
464                 }
465
466                 memcpy(buffer + idx, line, linelen);
467                 idx += linelen;
468         }
469  death:
470         memput(buffer, len);
471         return (NULL);
472 }
473
474
475 /*
476  * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
477  *                      char **body, size_t *bodylen);
478  *
479  *      Gets the response to a command. If the response indicates
480  *      there's a body to follow(code % 10 == 1), then the
481  *      body buffer is allcoated with memget and stored in
482  *      *BODY. The length of the allocated body buffer is stored
483  *      in *BODY. The caller must give the body buffer back to
484  *      memput when done. The results code is stored in *CODE.
485  *
486  * Returns:
487  *
488  *      0 if a result was read. -1 on some sort of failure.
489  *
490  */
491
492 int
493 irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
494                           size_t textlen, char **body, size_t *bodylen) {
495         int result = irs_irp_read_response(pvt, text, textlen);
496
497         *body = NULL;
498
499         if (result == 0) {
500                 return (-1);
501         }
502
503         *code = result;
504
505         /* Code that matches 2xx is a good result code.
506          * Code that matches xx1 means there's a response body coming.
507          */
508         if ((result / 100) == 2 && (result % 10) == 1) {
509                 *body = irs_irp_read_body(pvt, bodylen);
510                 if (*body == NULL) {
511                         return (-1);
512                 }
513         }
514
515         return (0);
516 }
517
518
519 /*
520  * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
521  *
522  *      Sends command to remote connected via the PVT
523  *      structure. FMT and args after it are fprintf-like
524  *      arguments for formatting.
525  *
526  * Returns:
527  *
528  *      0 on success, -1 on failure.
529  */
530
531 int
532 irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
533         va_list ap;
534         char buffer[1024];
535         int pos = 0;
536         int i, todo;
537
538
539         if (pvt->fdCxn < 0) {
540                 return (-1);
541         }
542
543         va_start(ap, fmt);
544         todo = vsprintf(buffer, fmt, ap);
545         va_end(ap);
546         if (todo > (int)sizeof(buffer) - 3) {
547                 syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
548                 exit(1);
549         }
550         strcat(buffer, "\r\n");
551         todo = strlen(buffer);
552
553         while (todo > 0) {
554                 i = write(pvt->fdCxn, buffer + pos, todo);
555 #if 0
556                 /* XXX brister */
557                 fprintf(stderr, "Wrote: \"");
558                 fwrite(buffer + pos, sizeof (char), todo, stderr);
559                 fprintf(stderr, "\"\n");
560 #endif
561                 if (i < 0) {
562                         close(pvt->fdCxn);
563                         pvt->fdCxn = -1;
564                         return (-1);
565                 }
566                 todo -= i;
567         }
568
569         return (0);
570 }
571
572
573 /* Methods */
574
575
576
577 /*
578  * void irp_close(struct irs_acc *this)
579  *
580  */
581
582 static void
583 irp_close(struct irs_acc *this) {
584         struct irp_p *irp = (struct irp_p *)this->private;
585
586         if (irp != NULL) {
587                 irs_irp_disconnect(irp);
588                 memput(irp, sizeof *irp);
589         }
590
591         memput(this, sizeof *this);
592 }
593
594
595