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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* Resource manager daemon
*
* Copyright (C) 2001-2002, Olaf Kirch <okir@lst.de>
*/
#include "resmgrd.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
struct conn *
rsm_connect(const char *path)
{
struct sockaddr_un un;
struct conn *conn;
conn = (struct conn *) calloc(1, sizeof(*conn));
conn->fd = -1;
conn->passfd = -1;
conn->acceptfd = 1;
conn->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (conn->fd < 0)
goto fail;
memset(&un, 0, sizeof(un));
un.sun_family = AF_LOCAL;
strcpy(un.sun_path, path);
if (connect(conn->fd, (struct sockaddr *) &un, SUN_LEN(&un)) < 0)
goto fail;
return conn;
fail: rsm_close(conn);
return NULL;
}
static int
rsm_recv_char(struct conn *conn, char *cp)
{
char control[1024];
struct msghdr msg;
struct iovec iov;
int len;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = cp;
iov.iov_len = 1;
if (conn->acceptfd && conn->passfd < 0) {
msg.msg_controllen = sizeof(control);
msg.msg_control = &control;
}
len = recvmsg(conn->fd, &msg, 0);
if (len < 0)
return -1;
if (msg.msg_controllen) {
struct cmsghdr *cmsg;
unsigned int left, clen;
/* Note: the Linux CMSG_{FIRST,NXT}HDR macros are
* somewhat broken when it comes to receiving more
* than one cmsg */
cmsg = (struct cmsghdr *) msg.msg_control;
left = msg.msg_controllen;
while (cmsg != NULL) {
if (left < sizeof(*cmsg))
break;
clen = CMSG_ALIGN(cmsg->cmsg_len);
if (clen > left)
break;
left -= clen;
#if 0
syslog(LOG_DEBUG, "CMSG lvl=%d type=%d len=%d",
cmsg->cmsg_level, cmsg->cmsg_type,
cmsg->cmsg_len);
#endif
if (cmsg->cmsg_level != SOL_SOCKET)
goto next;
if (cmsg->cmsg_type == SCM_RIGHTS) {
int *p, n, m = 0;
n = cmsg->cmsg_len / CMSG_LEN(sizeof(int));
p = (int *) CMSG_DATA(cmsg);
if (n)
conn->passfd = p[m++];
while (m < n)
close(p[m++]);
}
next: cmsg = (struct cmsghdr *) ((caddr_t) cmsg
+ cmsg->cmsg_len);
}
}
return len;
}
int
rsm_recv(struct conn *conn, char *buffer, unsigned int size)
{
unsigned int have, want;
int len = 0;
char c;
buffer[0] = '\0';
have = strlen(conn->buffer);
want = sizeof(conn->buffer);
if (have >= want - 1)
return -1;
while (have < want - 1) {
len = rsm_recv_char(conn, &c);
if (len == 0 || (len < 0 && errno == EAGAIN))
return 0;
if (len < 0) {
/* log("read error on socket: %m"); */
return -1;
}
conn->buffer[have++] = c;
conn->buffer[have] = '\0';
if (c == '\n')
break;
}
if (have >= want - 1) {
/* log("long line from %s, closing connection", conn->user); */
rsm_printf(conn, "%03d line too long", MSG_ERROR);
return -1;
}
/* Line complete... */
if (conn->debug)
printf("%s> %s", conn->user, conn->buffer);
strncpy(buffer, conn->buffer, size-1);
buffer[size-1] = '\0';
memset(conn->buffer, 0, sizeof(conn->buffer));
return strlen(buffer);
}
/*
* Send a message to the peer.
* We don't care about whether we're able to write the full
* message or not; we don't want to be subject to DoS by
* a rogue user app that never reads its data.
*/
int
rsm_send(struct conn *conn, const char *mesg, size_t len)
{
struct { struct cmsghdr cmsg; int fd; } control;
struct sigaction act;
struct msghdr msg;
struct iovec iov;
int oerrno;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = (char *) mesg;
iov.iov_len = len;
if (conn->passfd >= 0) {
/* Do the file descriptor passing fandango */
msg.msg_controllen = CMSG_SPACE(sizeof(int));
msg.msg_control = &control;
control.cmsg.cmsg_len = CMSG_LEN(sizeof(int));
control.cmsg.cmsg_level = SOL_SOCKET;
control.cmsg.cmsg_type = SCM_RIGHTS;
*(int *) CMSG_DATA(&control.cmsg) = conn->passfd;
}
/* ignore sigpipe */
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, &act);
oerrno = 0;
if (sendmsg(conn->fd, &msg, 0) < 0) {
oerrno = errno;
if (conn->debug)
printf("sendmsg failed: %m\n");
}
if (conn->passfd >= 0) {
close(conn->passfd);
conn->passfd = -1;
}
sigaction(SIGPIPE, &act, NULL);
if (oerrno) {
errno = oerrno;
return -1;
}
return 0;
}
/*
* Enqueue a file descriptor that should be passed to the peer
*/
void
rsm_queue_fd(struct conn *conn, int fd)
{
conn->passfd = fd;
}
/*
* Send a message to the client, printf style
*/
int
rsm_printf(struct conn *conn, const char *fmt, ...)
{
int res;
va_list ap;
va_start(ap, fmt);
res = rsm_vprintf(conn, fmt, ap);
va_end(ap);
return res;
}
/*
* Send a message to the client, vprintf style
*/
int
rsm_vprintf(struct conn *conn, const char *fmt, va_list ap)
{
char buffer[1024];
vsnprintf(buffer, sizeof(buffer)-2, fmt, ap);
strcat(buffer, "\n");
if (conn->debug)
printf("< %s", buffer);
return rsm_send(conn, buffer, strlen(buffer));
}
/*
* Receive response
* This function will deal with a single line response only
*/
int
rsm_recv_response(struct conn *conn)
{
char respbuf[256], *sp;
int code;
do {
if (rsm_recv(conn, respbuf, sizeof(respbuf)) < 0)
return -1;
if (!isdigit(respbuf[0])) {
errno = EINVAL;
return -1;
}
code = strtoul(respbuf, &sp, 10);
} while (*sp == '-');
return code;
}
void
rsm_close_stream(struct conn *conn)
{
if (conn->fd >= 0)
close(conn->fd);
conn->fd = -1;
}
void
rsm_close(struct conn *conn)
{
if (conn) {
rsm_close_stream(conn);
free(conn);
}
}
|