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 };
57 /*
58 * private helper functions
59 */
61 static int
62 connect_unixsock(sdb_client_t *client, const char *address)
63 {
64 struct sockaddr_un sa;
66 client->fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0);
67 if (client->fd < 0) {
68 char errbuf[1024];
69 sdb_log(SDB_LOG_ERR, "Failed to open socket: %s",
70 sdb_strerror(errno, errbuf, sizeof(errbuf)));
71 return -1;
72 }
74 sa.sun_family = AF_UNIX;
75 strncpy(sa.sun_path, address, sizeof(sa.sun_path));
76 sa.sun_path[sizeof(sa.sun_path) - 1] = '\0';
78 if (connect(client->fd, (struct sockaddr *)&sa, sizeof(sa))) {
79 char errbuf[1024];
80 sdb_client_close(client);
81 sdb_log(SDB_LOG_ERR, "Failed to connect to '%s': %s",
82 sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf)));
83 return -1;
84 }
85 return client->fd;
86 } /* connect_unixsock */
88 /*
89 * public API
90 */
92 sdb_client_t *
93 sdb_client_create(const char *address)
94 {
95 sdb_client_t *client;
97 if (! address)
98 return NULL;
100 client = malloc(sizeof(*client));
101 if (! client) {
102 sdb_log(SDB_LOG_ERR, "Out of memory");
103 return NULL;
104 }
105 memset(client, 0, sizeof(*client));
106 client->fd = -1;
108 client->address = strdup(address);
109 if (! client->address) {
110 sdb_client_destroy(client);
111 sdb_log(SDB_LOG_ERR, "Out of memory");
112 return NULL;
113 }
115 return client;
116 } /* sdb_client_create */
118 void
119 sdb_client_destroy(sdb_client_t *client)
120 {
121 if (! client)
122 return;
124 sdb_client_close(client);
126 if (client->address)
127 free(client->address);
128 client->address = NULL;
130 free(client);
131 } /* sdb_client_destroy */
133 int
134 sdb_client_connect(sdb_client_t *client, const char *username)
135 {
136 sdb_strbuf_t *buf;
137 ssize_t status;
138 uint32_t rstatus;
140 if ((! client) || (! client->address))
141 return -1;
143 if (client->fd >= 0)
144 return -1;
146 if (!strncasecmp(client->address, "unix:", strlen("unix:")))
147 connect_unixsock(client, client->address + strlen("unix:"));
148 else if (*client->address == '/')
149 connect_unixsock(client, client->address);
150 else {
151 sdb_log(SDB_LOG_ERR, "Unknown address type: %s", client->address);
152 return -1;
153 }
155 if (client->fd < 0)
156 return -1;
158 /* XXX */
159 if (! username)
160 username = "";
162 status = sdb_client_send(client, CONNECTION_STARTUP,
163 (uint32_t)strlen(username), username);
164 if (status < 0) {
165 char errbuf[1024];
166 sdb_client_close(client);
167 sdb_log(SDB_LOG_ERR, "Failed to send STARTUP message to server: %s",
168 sdb_strerror(errno, errbuf, sizeof(errbuf)));
169 return (int)status;
170 }
172 buf = sdb_strbuf_create(64);
173 rstatus = 0;
174 status = sdb_client_recv(client, &rstatus, buf);
175 if (status < 0) {
176 char errbuf[1024];
177 sdb_client_close(client);
178 sdb_strbuf_destroy(buf);
179 sdb_log(SDB_LOG_ERR, "Failed to receive server response: %s",
180 sdb_strerror(errno, errbuf, sizeof(errbuf)));
181 return (int)status;
182 }
184 if (rstatus != CONNECTION_OK) {
185 sdb_client_close(client);
186 sdb_strbuf_destroy(buf);
187 sdb_log(SDB_LOG_ERR, "Access denied for user '%s'", username);
188 return -((int)rstatus);
189 }
190 return 0;
191 } /* sdb_client_connect */
193 void
194 sdb_client_close(sdb_client_t *client)
195 {
196 if (! client)
197 return;
199 close(client->fd);
200 client->fd = -1;
201 } /* sdb_client_close */
203 ssize_t
204 sdb_client_send(sdb_client_t *client,
205 uint32_t cmd, uint32_t msg_len, const char *msg)
206 {
207 if ((! client) || (! client->fd))
208 return -1;
210 return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
211 } /* sdb_client_send */
213 ssize_t
214 sdb_client_recv(sdb_client_t *client,
215 uint32_t *code, sdb_strbuf_t *buf)
216 {
217 uint32_t rstatus = UINT32_MAX;
218 uint32_t rlen = UINT32_MAX;
220 size_t total = 0;
221 size_t req = 2 * sizeof(uint32_t);
223 size_t data_offset = sdb_strbuf_len(buf);
225 if (code)
226 *code = UINT32_MAX;
228 if ((! client) || (! client->fd) || (! buf)) {
229 errno = EBADF;
230 return -1;
231 }
233 while (42) {
234 ssize_t status;
236 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
237 return -1;
239 errno = 0;
240 status = sdb_strbuf_read(buf, client->fd, req);
241 if (status < 0) {
242 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
243 continue;
244 return status;
245 }
246 else if (! status) /* EOF */
247 break;
249 total += (size_t)status;
251 if (total != req)
252 continue;
254 if (rstatus == UINT32_MAX) {
255 /* retrieve status and data len */
256 rstatus = sdb_proto_get_int(buf, data_offset);
257 rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
259 if (! rlen)
260 break;
262 req = (size_t)rlen;
263 total = 0;
264 }
265 else /* finished reading data */
266 break;
267 }
269 if (total != req) {
270 /* unexpected EOF; clear partially read data */
271 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
272 return 0;
273 }
275 if (rstatus != UINT32_MAX)
276 /* remove status,len */
277 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
279 if (code)
280 *code = rstatus;
282 return (ssize_t)total;
283 } /* sdb_client_recv */
285 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */