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 return 0;
185 if (status < 0) {
186 char errbuf[1024];
187 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
188 sdb_strerror(errno, errbuf, sizeof(errbuf)));
189 }
190 else if (client->eof)
191 sdb_log(SDB_LOG_ERR, "Encountered end-of-file while waiting "
192 "for server response");
194 if (rstatus != CONNECTION_OK) {
195 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
196 status = -((int)rstatus);
197 }
199 sdb_client_close(client);
200 sdb_strbuf_destroy(buf);
201 return (int)status;
202 } /* sdb_client_connect */
204 int
205 sdb_client_sockfd(sdb_client_t *client)
206 {
207 if (! client)
208 return -1;
209 return client->fd;
210 } /* sdb_client_sockfd */
212 void
213 sdb_client_close(sdb_client_t *client)
214 {
215 if (! client)
216 return;
218 close(client->fd);
219 client->fd = -1;
220 client->eof = 1;
221 } /* sdb_client_close */
223 ssize_t
224 sdb_client_send(sdb_client_t *client,
225 uint32_t cmd, uint32_t msg_len, const char *msg)
226 {
227 if ((! client) || (! client->fd))
228 return -1;
230 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
231 } /* sdb_client_send */
233 ssize_t
234 sdb_client_recv(sdb_client_t *client,
235 uint32_t *code, sdb_strbuf_t *buf)
236 {
237 uint32_t rstatus = UINT32_MAX;
238 uint32_t rlen = UINT32_MAX;
240 size_t total = 0;
241 size_t req = 2 * sizeof(uint32_t);
243 size_t data_offset = sdb_strbuf_len(buf);
245 if (code)
246 *code = UINT32_MAX;
248 if ((! client) || (! client->fd) || (! buf)) {
249 errno = EBADF;
250 return -1;
251 }
253 while (42) {
254 ssize_t status;
256 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
257 return -1;
259 errno = 0;
260 status = sdb_strbuf_read(buf, client->fd, req);
261 if (status < 0) {
262 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
263 continue;
264 return status;
265 }
266 else if (! status) {
267 client->eof = 1;
268 break;
269 }
271 total += (size_t)status;
273 if (total != req)
274 continue;
276 if (rstatus == UINT32_MAX) {
277 /* retrieve status and data len */
278 rstatus = sdb_proto_get_int(buf, data_offset);
279 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
281 if (! rlen)
282 break;
284 req = (size_t)rlen;
285 total = 0;
286 }
287 else /* finished reading data */
288 break;
289 }
291 if (total != req) {
292 /* unexpected EOF; clear partially read data */
293 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
294 return 0;
295 }
297 if (rstatus != UINT32_MAX)
298 /* remove status,len */
299 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
301 if (code)
302 *code = rstatus;
304 return (ssize_t)total;
305 } /* sdb_client_recv */
307 _Bool
308 sdb_client_eof(sdb_client_t *client)
309 {
310 if ((! client) || (client->fd < 0))
311 return 1;
312 return client->eof;
313 } /* sdb_client_eof */
315 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */