-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.c
266 lines (238 loc) · 7.33 KB
/
notify.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
#include <signal.h>
#include "common.h"
int sfd; /* socket descriptor */
unsigned int sa_len;
struct sockaddr_in sa; /* Internet address struct */
struct sockaddr_in csa; /* Client's Internet address struct */
int fflag=0;
unsigned short port=0;
struct hostent* h;
void recvTimer() {
sendto(sfd, "timeout", strlen("timeout"), 0, (struct sockaddr *)&csa, sa_len);
if (fflag) { // send timeout to admin dashboard
memcpy(&csa.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
csa.sin_port = port;
sendto(sfd, "timeout", strlen("timeout"), 0, (struct sockaddr *)&csa, sa_len);
}
exit(112);
}
void usage() {
puts ("Usage: notify -u <domain or userID> [-f] [-t <timeout>] [-m <message to send>] [-d]\n");
puts ("-u - domain/user to send message to\n");
puts ("-m - message to send to dashboard client\n");
puts ("-f - admin mode - send to default admin dashboard\n");
puts (" AND specified user with 10 minute timeout\n");
puts ("-t - specify notification timeout\n");
puts ("-d - debug mode, message output to syslog\n");
exit(1);
}
int main(int argc, char *argv[]) {
extern int optind,opterr,optopt;
extern char* optarg;
int ch, uflag=0, mflag=0, debug=0;
int rc; /* system calls return value storage */
char buf[BUFLEN+1]; /* buffer for incoming data */
char* ip;
char user[50], clientuser[50];
char *tport, *tclientport;
unsigned short clientport=0;
struct hostent* client;
DEPOT* regDB;
int timeout=-1; /* default timeout is 60 seconds unless we read differently from conf file */
FILE* conf_file;
char ticks[BUFLEN+1]; /* timeout value read from config file */
struct sigaction action;
void (*func)() = recvTimer;
char response[20];
/* parse option parameters */
while ((ch = getopt(argc, argv, "dfm:t:u:")) != EOF) {
switch ((char)ch) {
case 'u': /* get user/domain */
strcpy(user,optarg);
uflag = 1;
break;
case 'm': /* get message */
strcpy(buf,optarg);
mflag = 1;
break;
case 'f': /* admin mode - send to first address found */
fflag = 1;
break;
case 't': /* specify timeout value */
timeout = atoi(optarg);
break;
case 'd': /* turn on debug */
debug = 1;
break;
default:
usage();
break;
}
}
if (!uflag || !mflag) usage();
if (fflag) {
strcpy(clientuser, user);
strcpy(user, "admin");
}
if (timeout == -1) {
if ((conf_file = fopen (DASHBOARD_CONF, "r")) != NULL) {
fgets(ticks, BUFLEN, conf_file);
timeout = atoi(ticks);
} else {
timeout = 60;
}
}
action.sa_handler = func;
action.sa_flags = 0;
sigemptyset(&(action.sa_mask)); /* ignore all known signals */
sigaction(SIGALRM,&action,NULL); /* ensures that SA_RESTART is NOT set */
/* initiate Internet address structure */
sa_len = sizeof(sa);
if (port == 0) port=S_PORT;
memset(&sa, 0, sa_len);
memset(&csa, 0, sa_len);
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
csa.sin_family = AF_INET;
/* Local machine on IP. */
sa.sin_addr.s_addr = htonl(INADDR_ANY);
// look up client IP address
/* open database */
if(!(regDB = dpopen(IP_DBNAME, DP_OREADER | DP_ONOLCK, -1))) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen ipDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* find the user record */
if(!(ip = dpget(regDB, user, -1, 0, -1, NULL))) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpget ipDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* close the database */
if(!dpclose(regDB)) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose ipDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
// look up client port number
/* open database */
if(!(regDB = dpopen(PORT_DBNAME, DP_OREADER | DP_ONOLCK, -1))) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpopen portDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* find the record */
if(!(tport = dpget(regDB, user, -1, 0, -1, NULL))) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpget portDB: %s", dperrmsg(dpecode));
closelog();
if (!fflag) exit(1);
} else {
port = htons(atoi(tport));
}
/* find client user port record if in -f mode*/
if (fflag) {
if(!(tclientport = dpget(regDB, clientuser, -1, 0, -1, NULL))) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpget portDB 'clientuser': %s", dperrmsg(dpecode));
closelog();
} else {
clientport = htons(atoi(tclientport));
}
}
/* close the database */
if(!dpclose(regDB)) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "dpclose portDB: %s", dperrmsg(dpecode));
closelog();
exit(1);
}
/* Get IP address (no check if input is IP address or DNS name). */
h = gethostbyname(ip);
if (h == NULL) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "Unknown host '%s'", ip);
closelog();
if (!fflag) exit(1);
}
memcpy(&csa.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
csa.sin_port = port;
/* allocate a socket */
if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "socket: failed!");
closelog();
exit(1);
}
rc = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&csa, sa_len);
if (rc < 0) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "sendto error");
closelog();
if (!fflag) exit(1);
} else {
if (debug) {
openlog("notify", LOG_PID, LOG_DAEMON);
syslog(LOG_INFO, "Sent to %s - %s", ip, buf);
closelog();
}
if (fflag) {
client = gethostbyname(clientuser);
if (client == NULL) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "Unknown host '%s'", clientuser);
closelog();
} else {
memcpy(&csa.sin_addr.s_addr, client->h_addr_list[0], client->h_length);
csa.sin_port = clientport;
rc = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&csa, sa_len);
if (rc < 0) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "sendto client error");
closelog();
}
}
}
strcpy(buf,"");
alarm(timeout);
rc = recvfrom(sfd, buf, BUFLEN, 0, (struct sockaddr *)&csa, &sa_len);
alarm(0); /* we got a message - disable alarm */
if (rc < 0) {
openlog("notify", 0, LOG_DAEMON);
syslog(LOG_ERR, "recvfrom error");
closelog();
exit(1);
} else {
buf[rc] = '\0';
if (debug) {
openlog("notify", LOG_PID, LOG_DAEMON);
syslog(LOG_INFO, "Received response: %s from IP: %s Port: %u", buf, inet_ntoa(csa.sin_addr), ntohs(csa.sin_port));
closelog();
}
if (fflag) {
if (!strcmp(buf,"0")) {
strcpy(response,"accepted");
} else {
strcpy(response,"rejected");
}
if ((csa.sin_port == clientport) && (!strcmp(inet_ntoa(csa.sin_addr), clientuser))) {
memcpy(&csa.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
csa.sin_port = port;
sendto(sfd, response, strlen(response), 0, (struct sockaddr *)&csa, sa_len);
} else {
memcpy(&csa.sin_addr.s_addr, client->h_addr_list[0], client->h_length);
csa.sin_port = clientport;
sendto(sfd, response, strlen(response), 0, (struct sockaddr *)&csa, sa_len);
}
}
close(sfd);
return(atoi(buf));
}
}
return(1);
}