1 /*
2 * SysDB - src/client/sock.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "client/sock.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
35 #include "utils/proto.h"
37 #include <arpa/inet.h>
39 #include <errno.h>
40 #include <limits.h>
42 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
47 #include <unistd.h>
49 #include <sys/socket.h>
50 #include <sys/un.h>
52 /*
53 * private data types
54 */
56 struct sdb_client {
57 char *address;
58 int fd;
59 bool eof;
60 };
62 /*
63 * private helper functions
64 */
66 static int
67 connect_unixsock(sdb_client_t *client, const char *address)
68 {
69 struct sockaddr_un sa;
71 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
72 if (client->fd < 0) {
73 char errbuf[1024];
74 sdb_log(SDB_LOG_ERR, "Failed to open socket: %s",
75 sdb_strerror(errno, errbuf, sizeof(errbuf)));
76 return -1;
77 }
79 sa.sun_family = AF_UNIX;
80 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
81 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
83 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
84 char errbuf[1024];
85 sdb_client_close(client);
86 sdb_log(SDB_LOG_ERR, "Failed to connect to '%s': %s",
87 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
88 return -1;
89 }
90 return client->fd;
91 } /* connect_unixsock */
93 /*
94 * public API
95 */
97 sdb_client_t *
98 sdb_client_create(const char *address)
99 {
100 sdb_client_t *client;
102 if (! address)
103 return NULL;
105 client = malloc(sizeof(*client));
106 if (! client) {
107 sdb_log(SDB_LOG_ERR, "Out of memory");
108 return NULL;
109 }
110 memset(client, 0, sizeof(*client));
111 client->fd = -1;
112 client->eof = 1;
114 client->address = strdup(address);
115 if (! client->address) {
116 sdb_client_destroy(client);
117 sdb_log(SDB_LOG_ERR, "Out of memory");
118 return NULL;
119 }
121 return client;
122 } /* sdb_client_create */
124 void
125 sdb_client_destroy(sdb_client_t *client)
126 {
127 if (! client)
128 return;
130 sdb_client_close(client);
132 if (client->address)
133 free(client->address);
134 client->address = NULL;
136 free(client);
137 } /* sdb_client_destroy */
139 int
140 sdb_client_connect(sdb_client_t *client, const char *username)
141 {
142 sdb_strbuf_t *buf;
143 ssize_t status;
144 uint32_t rstatus;
146 if ((! client) || (! client->address))
147 return -1;
149 if (client->fd >= 0)
150 return -1;
152 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
153 connect_unixsock(client, client->address + strlen("unix:"));
154 else if (*client->address == '/')
155 connect_unixsock(client, client->address);
156 else {
157 sdb_log(SDB_LOG_ERR, "Unknown address type: %s", client->address);
158 return -1;
159 }
161 if (client->fd < 0)
162 return -1;
163 client->eof = 0;
165 /* XXX */
166 if (! username)
167 username = "";
169 status = sdb_client_send(client, SDB_CONNECTION_STARTUP,
170 (uint32_t)strlen(username), username);
171 if (status < 0) {
172 char errbuf[1024];
173 sdb_client_close(client);
174 sdb_log(SDB_LOG_ERR, "Failed to send STARTUP message to server: %s",
175 sdb_strerror(errno, errbuf, sizeof(errbuf)));
176 return (int)status;
177 }
179 buf = sdb_strbuf_create(64);
180 rstatus = 0;
181 status = sdb_client_recv(client, &rstatus, buf);
182 if ((status > 0) && (rstatus == SDB_CONNECTION_OK)) {
183 sdb_strbuf_destroy(buf);
184 return 0;
185 }
187 if (status < 0) {
188 char errbuf[1024];
189 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
190 sdb_strerror(errno, errbuf, sizeof(errbuf)));
191 }
192 else if (client->eof)
193 sdb_log(SDB_LOG_ERR, "Encountered end-of-file while waiting "
194 "for server response");
196 if (rstatus == SDB_CONNECTION_ERROR) {
197 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
198 status = -((int)rstatus);
199 }
200 else if (rstatus != SDB_CONNECTION_OK) {
201 sdb_log(SDB_LOG_ERR, "Received unsupported authentication request "
202 "(status %d) during startup", (int)rstatus);
203 status = -((int)rstatus);
204 }
206 sdb_client_close(client);
207 sdb_strbuf_destroy(buf);
208 return (int)status;
209 } /* sdb_client_connect */
211 int
212 sdb_client_sockfd(sdb_client_t *client)
213 {
214 if (! client)
215 return -1;
216 return client->fd;
217 } /* sdb_client_sockfd */
219 int
220 sdb_client_shutdown(sdb_client_t *client, int how)
221 {
222 if (! client) {
223 errno = ENOTSOCK;
224 return -1;
225 }
227 if (client->fd < 0) {
228 errno = EBADF;
229 return -1;
230 }
232 return shutdown(client->fd, how);
233 } /* sdb_client_shutdown */
235 void
236 sdb_client_close(sdb_client_t *client)
237 {
238 if (! client)
239 return;
241 close(client->fd);
242 client->fd = -1;
243 client->eof = 1;
244 } /* sdb_client_close */
246 ssize_t
247 sdb_client_send(sdb_client_t *client,
248 uint32_t cmd, uint32_t msg_len, const char *msg)
249 {
250 if ((! client) || (! client->fd))
251 return -1;
253 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
254 } /* sdb_client_send */
256 ssize_t
257 sdb_client_recv(sdb_client_t *client,
258 uint32_t *code, sdb_strbuf_t *buf)
259 {
260 uint32_t rstatus = UINT32_MAX;
261 uint32_t rlen = UINT32_MAX;
263 size_t total = 0;
264 size_t req = 2 * sizeof(uint32_t);
266 size_t data_offset = sdb_strbuf_len(buf);
268 if (code)
269 *code = UINT32_MAX;
271 if ((! client) || (! client->fd) || (! buf)) {
272 errno = EBADF;
273 return -1;
274 }
276 while (42) {
277 ssize_t status;
279 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
280 return -1;
282 errno = 0;
283 status = sdb_strbuf_read(buf, client->fd, req);
284 if (status < 0) {
285 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
286 continue;
287 return status;
288 }
289 else if (! status) {
290 client->eof = 1;
291 break;
292 }
294 total += (size_t)status;
296 if (total != req)
297 continue;
299 if (rstatus == UINT32_MAX) {
300 /* retrieve status and data len */
301 rstatus = sdb_proto_get_int(buf, data_offset);
302 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
304 if (! rlen)
305 break;
307 req = (size_t)rlen;
308 total = 0;
309 }
310 else /* finished reading data */
311 break;
312 }
314 if (total != req) {
315 /* unexpected EOF; clear partially read data */
316 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
317 return 0;
318 }
320 if (rstatus != UINT32_MAX)
321 /* remove status,len */
322 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
324 if (code)
325 *code = rstatus;
327 return (ssize_t)total;
328 } /* sdb_client_recv */
330 bool
331 sdb_client_eof(sdb_client_t *client)
332 {
333 if ((! client) || (client->fd < 0))
334 return 1;
335 return client->eof;
336 } /* sdb_client_eof */
338 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */