Code

.gitignore: Ignore sysdb / sysdbd only as src/sysdb / src/sysdbd.
[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 void
194 sdb_client_close(sdb_client_t *client)
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)
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)
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 ((! client) || (! client->fd) || (! buf)) {
226                 errno = EBADF;
227                 return -1;
228         }
230         if (code)
231                 *code = UINT32_MAX;
233         while (42) {
234                 ssize_t status;
236                 /* XXX: use select */
238                 errno = 0;
239                 status = sdb_strbuf_read(buf, client->fd, req);
240                 if (status < 0) {
241                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
242                                 continue;
243                         return status;
244                 }
245                 else if (! status) /* EOF */
246                         break;
248                 total += (size_t)status;
250                 if (total != req)
251                         continue;
253                 if (rstatus == UINT32_MAX) {
254                         /* retrieve status and data len */
255                         rstatus = sdb_proto_get_int(buf, data_offset);
256                         rlen = sdb_proto_get_int(buf, data_offset + sizeof(rstatus));
258                         if (! rlen)
259                                 break;
261                         req = (size_t)rlen;
262                         total = 0;
263                 }
264                 else /* finished reading data */
265                         break;
266         }
268         if (total != req) {
269                 /* unexpected EOF; clear partially read data */
270                 sdb_strbuf_skip(buf, data_offset, sdb_strbuf_len(buf));
271                 return 0;
272         }
274         if (rstatus != UINT32_MAX)
275                 /* remove status,len */
276                 sdb_strbuf_skip(buf, data_offset, 2 * sizeof(rstatus));
278         if (code)
279                 *code = rstatus;
281         return (ssize_t)total;
282 } /* sdb_client_recv */
284 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */