-
Notifications
You must be signed in to change notification settings - Fork 0
/
vzsock.c
360 lines (311 loc) · 8.72 KB
/
vzsock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright (c) 2006-2017, Parallels International GmbH
* Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved.
*
* This file is part of OpenVZ libraries. OpenVZ is free software; you can
* redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/> or write to Free Software Foundation,
* 51 Franklin Street, Fifth Floor Boston, MA 02110, USA.
*
* Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200
* Schaffhausen, Switzerland.
*
*/
#include <limits.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "libvzsock.h"
#include "util.h"
#include "ssh.h"
#include "sock.h"
#include "fd.h"
#ifdef WITH_SSL
#include "ssl.h"
#endif
/* context operations */
int vzsock_init(int type, struct vzsock_ctx *ctx)
{
int rc;
char path[PATH_MAX];
struct vzs_handlers *handlers;
struct vzs_void_list *clist;
// openlog("VZM", LOG_PERROR, LOG_USER);
/* init context */
if ((handlers = (struct vzs_handlers *)
malloc(sizeof(struct vzs_handlers))) == NULL)
return _vz_error(ctx, VZS_ERR_SYSTEM, "malloc() : %m");
if ((clist = (struct vzs_void_list *)
malloc(sizeof(struct vzs_void_list))) == NULL) {
rc = _vz_error(ctx, VZS_ERR_SYSTEM, "malloc() : %m");
goto cleanup_0;
}
_vzs_void_list_init(clist);
ctx->type = type;
ctx->handlers = (void *)handlers;
ctx->clist = (void *)clist;
ctx->debug = 0;
ctx->errcode = 0;
ctx->errmsg[0] = '\0';
ctx->logger = NULL;
ctx->filter = NULL;
ctx->password[0] = '\0';
ctx->lpassword = 0;
ctx->tmo = VZSOCK_DEF_TMO;
/* create temporary directory (mkdtemp() will set perms 0700) */
_vzs_get_tmp_dir(path, sizeof(path));
snprintf(ctx->tmpdir, sizeof(ctx->tmpdir), "%s/vzsock.XXXXXX", path);
if (mkdtemp(ctx->tmpdir) == NULL) {
rc = _vz_error(ctx, VZS_ERR_SYSTEM,
"mkdtemp(%s) : %m", ctx->tmpdir);
goto cleanup_1;
}
switch (type) {
case VZSOCK_SOCK:
if ((rc = _vzs_sock_init(ctx, handlers)))
goto cleanup_2;
break;
case VZSOCK_SSH:
if ((rc = _vzs_ssh_init(ctx, handlers)))
goto cleanup_2;
break;
case VZSOCK_FD:
if ((rc = _vzs_fd_init(ctx, handlers)))
goto cleanup_2;
break;
#ifdef WITH_SSL
case VZSOCK_SSL:
if ((rc = _vzs_ssl_init(ctx, handlers)))
goto cleanup_2;
break;
#endif
default:
rc = _vz_error(ctx, VZS_ERR_BAD_PARAM,
"undefined vzsock type: %d", type);
goto cleanup_2;
}
return 0;
cleanup_2:
_vzs_rmdir(ctx, ctx->tmpdir);
cleanup_1:
free((void *)clist);
cleanup_0:
free((void *)handlers);
return rc;
}
int vzsock_open(struct vzsock_ctx *ctx)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->open(ctx);
}
void vzsock_close(struct vzsock_ctx *ctx)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
struct vzs_void_list *clist = (struct vzs_void_list *)ctx->clist;
struct vzs_void_list_el *cn;
/* close all connections */
_vzs_void_list_for_each(clist, cn)
handlers->close_conn(ctx, cn->p);
_vzs_void_list_clean(clist);
free(ctx->clist);
ctx->clist = NULL;
handlers->close(ctx);
free(ctx->handlers);
ctx->handlers = NULL;
_vzs_rmdir(ctx, ctx->tmpdir);
// closelog();
}
int vzsock_set(struct vzsock_ctx *ctx, int type, void *data, size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
switch (type) {
case VZSOCK_DATA_TMO:
ctx->tmo = *((long *)data);
break;
case VZSOCK_DATA_DEBUG:
ctx->debug = *((int *)data);
break;
case VZSOCK_DATA_LOGGER:
ctx->logger = (int (*)(int, const char *, va_list))data;
break;
case VZSOCK_DATA_FILTER:
ctx->filter = (int (*)(const char *, int *, char *, size_t *))data;
break;
case VZSOCK_DATA_PASSWORD:
memcpy(ctx->password, data, size);
ctx->lpassword = 1;
break;
default:
return handlers->set(ctx, type, data, size);
}
return 0;
}
int vzsock_get(struct vzsock_ctx *ctx, int type, void *data, size_t *size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
switch (type) {
case VZSOCK_DATA_PASSWORD:
if (*size <= strlen(ctx->password))
return _vz_error(ctx, VZS_ERR_BAD_PARAM,
"It is't enough buffer size (%d) "\
"for data type : %d", *size, type);
memcpy(data, ctx->password, strlen(ctx->password)+1);
break;
default:
return handlers->get(ctx, type, data, size);
}
return 0;
}
/* per-connection functions */
int vzsock_open_conn(struct vzsock_ctx *ctx, void *data, void **conn)
{
int rc;
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
struct vzs_void_list *clist = (struct vzs_void_list *)ctx->clist;
if ((rc = handlers->open_conn(ctx, data, conn)))
return rc;
/* and add into list */
if (_vzs_void_list_add(clist, *conn))
return _vz_error(ctx, VZS_ERR_SYSTEM, "memory alloc : %m");
return 0;
}
/*
int vzsock_wait_conn(struct vzsock_ctx *ctx, void **conn)
{
int rc;
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
struct vzs_void_list *clist = (struct vzs_void_list *)ctx->clist;
if ((rc = handlers->wait_conn(ctx, conn)))
return rc;
if (_vzs_void_list_add(clist, *conn))
return _vz_error(ctx, VZS_ERR_SYSTEM, "memory alloc : %m");
return 0;
}
*/
int vzsock_accept_conn(struct vzsock_ctx *ctx, void *srv_conn, void **conn)
{
int rc;
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
struct vzs_void_list *clist = (struct vzs_void_list *)ctx->clist;
if ((rc = handlers->accept_conn(ctx, srv_conn, conn)))
return rc;
/* and add into list */
if (_vzs_void_list_add(clist, *conn))
return _vz_error(ctx, VZS_ERR_SYSTEM, "memory alloc : %m");
return 0;
}
int vzsock_is_open_conn(struct vzsock_ctx *ctx, void *conn)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->is_open_conn(conn);
}
int vzsock_close_conn(struct vzsock_ctx *ctx, void *conn)
{
int rc;
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
struct vzs_void_list *clist = (struct vzs_void_list *)ctx->clist;
struct vzs_void_list_el *cn;
if ((rc = handlers->close_conn(ctx, conn)))
return rc;
/* and remove from list */
_vzs_void_list_for_each(clist, cn) {
if (cn->p == conn)
_vzs_void_list_remove(clist, cn);
}
return 0;
}
int vzsock_set_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->set_conn(ctx, conn, type, data, size);
}
int vzsock_get_conn(struct vzsock_ctx *ctx, void *conn,
int type, void *data, size_t *size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->get_conn(ctx, conn, type, data, size);
}
int vzsock_send(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->send(ctx, conn, data, size);
}
int vzsock_send_err_msg(
struct vzsock_ctx *ctx,
void *conn,
const char * data,
size_t size)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->send_err_msg(ctx, conn, data, size);
}
int vzsock_recv(
struct vzsock_ctx *ctx,
void *conn,
char separator,
char *data,
size_t *size)
{
char buffer[BUFSIZ];
int rc, ret;
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
size_t sz;
while (1) {
sz = sizeof(buffer);
if ((rc = handlers->recv_str(ctx, conn, separator, buffer, &sz)))
return rc;
if (ctx->filter == NULL) {
if (sz > *size)
return _vz_error(ctx, VZS_ERR_TOOLONG,
"vzsock_recv : too long data (%s bytes)",
sz);
*size = sz;
memcpy(data, buffer, *size);
return 0;
}
ret = ctx->filter(buffer, &ctx->code, data, size);
if (ret == 0) {
return 0;
} else if (ret < 0) {
return VZS_ERR_FILTER;
}
}
return 0;
}
int vzsock_send_data(
struct vzsock_ctx *ctx,
void *conn,
char * const *argv)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->send_data(ctx, conn, argv);
}
int vzsock_recv_data(
struct vzsock_ctx *ctx,
void *conn,
char * const *argv)
{
struct vzs_handlers *handlers = (struct vzs_handlers *)ctx->handlers;
return handlers->recv_data(ctx, conn, argv);
}