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, 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 == 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 != CONNECTION_OK) {
197 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
198 status = -((int)rstatus);
199 }
201 sdb_client_close(client);
202 sdb_strbuf_destroy(buf);
203 return (int)status;
204 } /* sdb_client_connect */
206 int
207 sdb_client_sockfd(sdb_client_t *client)
208 {
209 if (! client)
210 return -1;
211 return client->fd;
212 } /* sdb_client_sockfd */
214 void
215 sdb_client_close(sdb_client_t *client)
216 {
217 if (! client)
218 return;
220 close(client->fd);
221 client->fd = -1;
222 client->eof = 1;
223 } /* sdb_client_close */
225 ssize_t
226 sdb_client_send(sdb_client_t *client,
227 uint32_t cmd, uint32_t msg_len, const char *msg)
228 {
229 if ((! client) || (! client->fd))
230 return -1;
232 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
233 } /* sdb_client_send */
235 ssize_t
236 sdb_client_recv(sdb_client_t *client,
237 uint32_t *code, sdb_strbuf_t *buf)
238 {
239 uint32_t rstatus = UINT32_MAX;
240 uint32_t rlen = UINT32_MAX;
242 size_t total = 0;
243 size_t req = 2 * sizeof(uint32_t);
245 size_t data_offset = sdb_strbuf_len(buf);
247 if (code)
248 *code = UINT32_MAX;
250 if ((! client) || (! client->fd) || (! buf)) {
251 errno = EBADF;
252 return -1;
253 }
255 while (42) {
256 ssize_t status;
258 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
259 return -1;
261 errno = 0;
262 status = sdb_strbuf_read(buf, client->fd, req);
263 if (status < 0) {
264 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
265 continue;
266 return status;
267 }
268 else if (! status) {
269 client->eof = 1;
270 break;
271 }
273 total += (size_t)status;
275 if (total != req)
276 continue;
278 if (rstatus == UINT32_MAX) {
279 /* retrieve status and data len */
280 rstatus = sdb_proto_get_int(buf, data_offset);
281 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
283 if (! rlen)
284 break;
286 req = (size_t)rlen;
287 total = 0;
288 }
289 else /* finished reading data */
290 break;
291 }
293 if (total != req) {
294 /* unexpected EOF; clear partially read data */
295 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
296 return 0;
297 }
299 if (rstatus != UINT32_MAX)
300 /* remove status,len */
301 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
303 if (code)
304 *code = rstatus;
306 return (ssize_t)total;
307 } /* sdb_client_recv */
309 _Bool
310 sdb_client_eof(sdb_client_t *client)
311 {
312 if ((! client) || (client->fd < 0))
313 return 1;
314 return client->eof;
315 } /* sdb_client_eof */
317 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */