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
|
/*
* Copyright (C) 2011 Martin Willi
* Copyright (C) 2011 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include "whitelist_msg.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
/**
* Connect to the daemon, return FD
*/
static int make_connection()
{
struct sockaddr_un addr;
int fd;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, WHITELIST_SOCKET);
fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (fd < 0)
{
fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
return -1;
}
if (connect(fd, (struct sockaddr *)&addr,
offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path)) < 0)
{
fprintf(stderr, "connecting to %s failed: %s\n",
WHITELIST_SOCKET, strerror(errno));
close(fd);
return -1;
}
return fd;
}
/**
* Send a single message
*/
static int send_msg(int type, char *id)
{
whitelist_msg_t msg = {
.type = type,
};
int fd;
fd = make_connection();
if (fd == -1)
{
return 2;
}
snprintf(msg.id, sizeof(msg.id), "%s", id);
if (send(fd, &msg, sizeof(msg), 0) != sizeof(msg))
{
fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
close(fd);
return 2;
}
if (type == WHITELIST_LIST)
{
while (recv(fd, &msg, sizeof(msg), 0) == sizeof(msg))
{
if (msg.type != WHITELIST_LIST)
{
break;
}
printf("%s\n", msg.id);
}
}
close(fd);
return 0;
}
/**
* Send a batch of messages, reading identities from a file
*/
static int send_batch(int type, char *file)
{
whitelist_msg_t msg = {
.type = type,
};
FILE *f = stdin;
int fd, len;
fd = make_connection();
if (fd == -1)
{
return 2;
}
if (file)
{
f = fopen(file, "r");
if (f == NULL)
{
fprintf(stderr, "opening %s failed: %s\n", file, strerror(errno));
close(fd);
return 3;
}
}
while (fgets(msg.id, sizeof(msg.id), f))
{
len = strlen(msg.id);
if (len == 0)
{
continue;
}
if (msg.id[len-1] == '\n')
{
msg.id[len-1] = '\0';
}
if (send(fd, &msg, sizeof(msg), 0) != sizeof(msg))
{
fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
if (f != stdin)
{
fclose(f);
}
close(fd);
return 2;
}
}
if (f != stdin)
{
fclose(f);
}
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
if (argc == 3 && strcmp(argv[1], "add") == 0)
{
return send_msg(WHITELIST_ADD, argv[2]);
}
if (argc == 3 && strcmp(argv[1], "remove") == 0)
{
return send_msg(WHITELIST_REMOVE, argv[2]);
}
if ((argc == 2 || argc == 3) && strcmp(argv[1], "add-from") == 0)
{
return send_batch(WHITELIST_ADD, argc == 3 ? argv[2] : NULL);
}
if ((argc == 2 || argc == 3) && strcmp(argv[1], "remove-from") == 0)
{
return send_batch(WHITELIST_REMOVE, argc == 3 ? argv[2] : NULL);
}
if ((argc == 2 || argc == 3) && strcmp(argv[1], "flush") == 0)
{
return send_msg(WHITELIST_FLUSH, argc == 3 ? argv[2] : "%any");
}
if ((argc == 2 || argc == 3) && strcmp(argv[1], "list") == 0)
{
return send_msg(WHITELIST_LIST, argc == 3 ? argv[2] : "%any");
}
if (argc == 2 && strcmp(argv[1], "enable") == 0)
{
return send_msg(WHITELIST_ENABLE, "");
}
if (argc == 2 && strcmp(argv[1], "disable") == 0)
{
return send_msg(WHITELIST_DISABLE, "");
}
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s add <identity>\n", argv[0]);
fprintf(stderr, " %s remove <identity>\n", argv[0]);
fprintf(stderr, " %s add-from <file>\n", argv[0]);
fprintf(stderr, " %s remove-from <file>\n", argv[0]);
fprintf(stderr, " %s flush [<pattern>]\n", argv[0]);
fprintf(stderr, " %s list [<pattern>]\n", argv[0]);
fprintf(stderr, " %s enable\n", argv[0]);
fprintf(stderr, " %s disable\n", argv[0]);
return 1;
}
|