042dd63bd99a353764b8176289de65f5276730f8
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 #include "client/sock.h"
29 #include "utils/error.h"
30 #include "utils/strbuf.h"
31 #include "utils/proto.h"
33 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <limits.h>
38 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
43 #include <unistd.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
48 /*
49 * private data types
50 */
52 struct sdb_client {
53 char *address;
54 int fd;
55 _Bool eof;
56 };
58 /*
59 * private helper functions
60 */
62 static int
63 connect_unixsock(sdb_client_t *client, const char *address)
64 {
65 struct sockaddr_un sa;
67 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
68 if (client->fd < 0) {
69 char errbuf[1024];
70 sdb_log(SDB_LOG_ERR, "Failed to open socket: %s",
71 sdb_strerror(errno, errbuf, sizeof(errbuf)));
72 return -1;
73 }
75 sa.sun_family = AF_UNIX;
76 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
77 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
79 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
80 char errbuf[1024];
81 sdb_client_close(client);
82 sdb_log(SDB_LOG_ERR, "Failed to connect to '%s': %s",
83 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
84 return -1;
85 }
86 return client->fd;
87 } /* connect_unixsock */
89 /*
90 * public API
91 */
93 sdb_client_t *
94 sdb_client_create(const char *address)
95 {
96 sdb_client_t *client;
98 if (! address)
99 return NULL;
101 client = malloc(sizeof(*client));
102 if (! client) {
103 sdb_log(SDB_LOG_ERR, "Out of memory");
104 return NULL;
105 }
106 memset(client, 0, sizeof(*client));
107 client->fd = -1;
108 client->eof = 1;
110 client->address = strdup(address);
111 if (! client->address) {
112 sdb_client_destroy(client);
113 sdb_log(SDB_LOG_ERR, "Out of memory");
114 return NULL;
115 }
117 return client;
118 } /* sdb_client_create */
120 void
121 sdb_client_destroy(sdb_client_t *client)
122 {
123 if (! client)
124 return;
126 sdb_client_close(client);
128 if (client->address)
129 free(client->address);
130 client->address = NULL;
132 free(client);
133 } /* sdb_client_destroy */
135 int
136 sdb_client_connect(sdb_client_t *client, const char *username)
137 {
138 sdb_strbuf_t *buf;
139 ssize_t status;
140 uint32_t rstatus;
142 if ((! client) || (! client->address))
143 return -1;
145 if (client->fd >= 0)
146 return -1;
148 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
149 connect_unixsock(client, client->address + strlen("unix:"));
150 else if (*client->address == '/')
151 connect_unixsock(client, client->address);
152 else {
153 sdb_log(SDB_LOG_ERR, "Unknown address type: %s", client->address);
154 return -1;
155 }
157 if (client->fd < 0)
158 return -1;
159 client->eof = 0;
161 /* XXX */
162 if (! username)
163 username = "";
165 status = sdb_client_send(client, CONNECTION_STARTUP,
166 (uint32_t)strlen(username), username);
167 if (status < 0) {
168 char errbuf[1024];
169 sdb_client_close(client);
170 sdb_log(SDB_LOG_ERR, "Failed to send STARTUP message to server: %s",
171 sdb_strerror(errno, errbuf, sizeof(errbuf)));
172 return (int)status;
173 }
175 buf = sdb_strbuf_create(64);
176 rstatus = 0;
177 status = sdb_client_recv(client, &rstatus, buf);
178 if (status < 0) {
179 char errbuf[1024];
180 sdb_client_close(client);
181 sdb_strbuf_destroy(buf);
182 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
183 sdb_strerror(errno, errbuf, sizeof(errbuf)));
184 return (int)status;
185 }
187 if (rstatus != CONNECTION_OK) {
188 sdb_client_close(client);
189 sdb_strbuf_destroy(buf);
190 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
191 return -((int)rstatus);
192 }
193 return 0;
194 } /* sdb_client_connect */
196 int
197 sdb_client_sockfd(sdb_client_t *client)
198 {
199 if (! client)
200 return -1;
201 return client->fd;
202 } /* sdb_client_sockfd */
204 void
205 sdb_client_close(sdb_client_t *client)
206 {
207 if (! client)
208 return;
210 close(client->fd);
211 client->fd = -1;
212 client->eof = 1;
213 } /* sdb_client_close */
215 ssize_t
216 sdb_client_send(sdb_client_t *client,
217 uint32_t cmd, uint32_t msg_len, const char *msg)
218 {
219 if ((! client) || (! client->fd))
220 return -1;
222 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
223 } /* sdb_client_send */
225 ssize_t
226 sdb_client_recv(sdb_client_t *client,
227 uint32_t *code, sdb_strbuf_t *buf)
228 {
229 uint32_t rstatus = UINT32_MAX;
230 uint32_t rlen = UINT32_MAX;
232 size_t total = 0;
233 size_t req = 2 * sizeof(uint32_t);
235 size_t data_offset = sdb_strbuf_len(buf);
237 if (code)
238 *code = UINT32_MAX;
240 if ((! client) || (! client->fd) || (! buf)) {
241 errno = EBADF;
242 return -1;
243 }
245 while (42) {
246 ssize_t status;
248 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
249 return -1;
251 errno = 0;
252 status = sdb_strbuf_read(buf, client->fd, req);
253 if (status < 0) {
254 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
255 continue;
256 return status;
257 }
258 else if (! status) {
259 client->eof = 1;
260 break;
261 }
263 total += (size_t)status;
265 if (total != req)
266 continue;
268 if (rstatus == UINT32_MAX) {
269 /* retrieve status and data len */
270 rstatus = sdb_proto_get_int(buf, data_offset);
271 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
273 if (! rlen)
274 break;
276 req = (size_t)rlen;
277 total = 0;
278 }
279 else /* finished reading data */
280 break;
281 }
283 if (total != req) {
284 /* unexpected EOF; clear partially read data */
285 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
286 return 0;
287 }
289 if (rstatus != UINT32_MAX)
290 /* remove status,len */
291 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
293 if (code)
294 *code = rstatus;
296 return (ssize_t)total;
297 } /* sdb_client_recv */
299 _Bool
300 sdb_client_eof(sdb_client_t *client)
301 {
302 if ((! client) || (client->fd < 0))
303 return 1;
304 return client->eof;
305 } /* sdb_client_eof */
307 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */