Code

client: Added sdb_client_sockfd() function.
[sysdb.git] / src / client / sock.c
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)
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)
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 int
194 sdb_client_sockfd(sdb_client_t *client)
196         if (! client)
197                 return -1;
198         return client->fd;
199 } /* sdb_client_sockfd */
201 void
202 sdb_client_close(sdb_client_t *client)
204         if (! client)
205                 return;
207         close(client->fd);
208         client->fd = -1;
209 } /* sdb_client_close */
211 ssize_t
212 sdb_client_send(sdb_client_t *client,
213                 uint32_t cmd, uint32_t msg_len, const char *msg)
215         if ((! client) || (! client->fd))
216                 return -1;
218         return sdb_proto_send_msg(client->fd, cmd, msg_len, msg);
219 } /* sdb_client_send */
221 ssize_t
222 sdb_client_recv(sdb_client_t *client,
223                 uint32_t *code, sdb_strbuf_t *buf)
225         uint32_t rstatus = UINT32_MAX;
226         uint32_t rlen = UINT32_MAX;
228         size_t total = 0;
229         size_t req = 2 * sizeof(uint32_t);
231         size_t data_offset = sdb_strbuf_len(buf);
233         if ((! client) || (! client->fd) || (! buf)) {
234                 errno = EBADF;
235                 return -1;
236         }
238         if (code)
239                 *code = UINT32_MAX;
241         while (42) {
242                 ssize_t status;
244                 if (sdb_proto_select(client->fd, SDB_PROTO_SELECTIN))
245                         return -1;
247                 errno = 0;
248                 status = sdb_strbuf_read(buf, client->fd, req);
249                 if (status < 0) {
250                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
251                                 continue;
252                         return status;
253                 }
254                 else if (! status) /* EOF */
255                         break;
257                 total += (size_t)status;
259                 if (total != req)
260                         continue;
262                 if (rstatus == UINT32_MAX) {
263                         /* retrieve status and data len */
264                         rstatus = sdb_proto_get_int(buf, data_offset);
265                         rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
267                         if (! rlen)
268                                 break;
270                         req = (size_t)rlen;
271                         total = 0;
272                 }
273                 else /* finished reading data */
274                         break;
275         }
277         if (total != req) {
278                 /* unexpected EOF; clear partially read data */
279                 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
280                 return 0;
281         }
283         if (rstatus != UINT32_MAX)
284                 /* remove status,len */
285                 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
287         if (code)
288                 *code = rstatus;
290         return (ssize_t)total;
291 } /* sdb_client_recv */
293 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */