/*
* Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1996, 1998-2001, 2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* send errors to syslog if true. */
int irp_log_errors = 1;
/*%
* This module handles the irp module connection to irpd.
*
* The client expects a synchronous interface to functions like
* getpwnam(3), so we can't use the ctl_* i/o library on this end of
* the wire (it's used in the server).
*/
int
irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
if (irs_irp_is_connected(cxndata)) {
return (0);
} else if (irs_irp_connect(cxndata) != 0) {
if (warned != NULL && !*warned) {
syslog(LOG_ERR, "irpd connection failed: %m\n");
(*warned)++;
}
return (-1);
}
return (0);
}
/*%
* int irs_irp_connect(void);
*
* Sets up the connection to the remote irpd server.
*
* Returns:
*
* 0 on success, -1 on failure.
*
*/
int
irs_irp_connect(struct irp_p *pvt) {
int flags;
struct sockaddr *addr;
struct sockaddr_in iaddr;
#ifndef NO_SOCKADDR_UN
struct sockaddr_un uaddr;
#endif
long ipaddr;
const char *irphost;
int code;
char text[256];
int socklen = 0;
int
irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
char *realstart = &pvt->inbuffer[0];
char *p, *start, *end;
int spare;
int i;
int buffpos = 0;
int left = len - 1;
while (left > 0) {
start = p = &pvt->inbuffer[pvt->incurr];
end = &pvt->inbuffer[pvt->inlast];
while (p != end && *p != '\n')
p++;
if (p == end) {
/* Found no newline so shift data down if necessary
* and append new data to buffer
*/
if (start > realstart) {
memmove(realstart, start, end - start);
pvt->inlast = end - start;
start = realstart;
pvt->incurr = 0;
end = &pvt->inbuffer[pvt->inlast];
}
spare = sizeof (pvt->inbuffer) - pvt->inlast;
p = end;
i = read(pvt->fdCxn, end, spare);
if (i < 0) {
close(pvt->fdCxn);
pvt->fdCxn = -1;
return (buffpos > 0 ? buffpos : -1);
} else if (i == 0) {
return (buffpos);
}
end += i;
pvt->inlast += i;
while (p != end && *p != '\n')
p++;
}
if (p == end) {
/* full buffer and still no newline */
i = sizeof pvt->inbuffer;
} else {
/* include newline */
i = p - start + 1;
}
if (i > left)
i = left;
memcpy(buffer + buffpos, start, i);
pvt->incurr += i;
buffpos += i;
buffer[buffpos] = '\0';
/*%
* int irp_read_response(struct irp_p *pvt);
*
* Returns:
*
* The number found at the beginning of the line read from
* FP. 0 on failure(0 is not a legal response code). The
* rest of the line is discarded.
*
*/
int
irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
char line[1024];
int code;
char *p;
p = strchr(line, '\n');
if (p == NULL) {
return (0);
}
if (sscanf(line, "%d", &code) != 1) {
code = 0;
} else if (text != NULL && textlen > 0U) {
p = line;
while (isspace((unsigned char)*p)) p++;
while (isdigit((unsigned char)*p)) p++;
while (isspace((unsigned char)*p)) p++;
strncpy(text, p, textlen - 1);
p[textlen - 1] = '\0';
}
return (code);
}
/*%
* char *irp_read_body(struct irp_p *pvt, size_t *size);
*
* Read in the body of a response. Terminated by a line with
* just a dot on it. Lines should be terminated with a CR-LF
* sequence, but we're nt piccky if the CR is missing.
* No leading dot escaping is done as the protcol doesn't
* use leading dots anywhere.
*
* Returns:
*
* Pointer to null-terminated buffer allocated by memget.
* *SIZE is set to the length of the buffer.
*
*/
/*%
* int irs_irp_get_full_response(struct irp_p *pvt, int *code,
* char **body, size_t *bodylen);
*
* Gets the response to a command. If the response indicates
* there's a body to follow(code % 10 == 1), then the
* body buffer is allcoated with memget and stored in
* *BODY. The length of the allocated body buffer is stored
* in *BODY. The caller must give the body buffer back to
* memput when done. The results code is stored in *CODE.
*
* Returns:
*
* 0 if a result was read. -1 on some sort of failure.
*
*/
int
irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
size_t textlen, char **body, size_t *bodylen) {
int result = irs_irp_read_response(pvt, text, textlen);
*body = NULL;
if (result == 0) {
return (-1);
}
*code = result;
/* Code that matches 2xx is a good result code.
* Code that matches xx1 means there's a response body coming.
*/
if ((result / 100) == 2 && (result % 10) == 1) {
*body = irs_irp_read_body(pvt, bodylen);
if (*body == NULL) {
return (-1);
}
}
return (0);
}
/*%
* int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
*
* Sends command to remote connected via the PVT
* structure. FMT and args after it are fprintf-like
* arguments for formatting.
*
* Returns:
*
* 0 on success, -1 on failure.
*/
int
irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
va_list ap;
char buffer[1024];
int pos = 0;
int i, todo;
if (pvt->fdCxn < 0) {
return (-1);
}
va_start(ap, fmt);
(void) vsprintf(buffer, fmt, ap);
todo = strlen(buffer);
va_end(ap);
if (todo > (int)sizeof(buffer) - 3) {
syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
exit(1);
}
strcat(buffer, "\r\n");
todo = strlen(buffer);
while (todo > 0) {
i = write(pvt->fdCxn, buffer + pos, todo);
#if 0
/* XXX brister */
fprintf(stderr, "Wrote: \"");
fwrite(buffer + pos, sizeof (char), todo, stderr);
fprintf(stderr, "\"\n");
#endif
if (i < 0) {
close(pvt->fdCxn);
pvt->fdCxn = -1;
return (-1);
}
todo -= i;
}