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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
/*
* Copyright (C) 1996, 1999 Olaf Kirch
* Modified by Jeffrey A. Uphoff, 1997-1999.
* Modified by H.J. Lu, 1998.
* Modified by Lon Hohberger, Oct. 2000
* - Bugfix handling client responses.
* - Paranoia on NOTIFY_CALLBACK case
*
* NSM for Linux.
*/
/*
* After reboot, notify all hosts on our notify list. In order not to
* hang statd with delivery to dead hosts, we perform all RPC calls in
* parallel.
*
* It would have been nice to use the portmapper's rmtcall feature,
* but that's not possible for security reasons (the portmapper would
* have to forward the call with root privs for most statd's, which
* it won't if it's worth its money).
*/
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <rpc/pmap_rmt.h>
#include <time.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include "sm_inter.h"
#include "statd.h"
#include "notlist.h"
#include "log.h"
#include "ha-callout.h"
#define MAXMSGSIZE (2048 / sizeof(unsigned int))
static unsigned long xid = 0; /* RPC XID counter */
static int sockfd = -1; /* notify socket */
/*
* Initialize callback socket
*/
int
statd_get_socket(int port)
{
struct sockaddr_in sin;
if (sockfd >= 0)
return sockfd;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
note(N_CRIT, "Can't create socket: %m");
return -1;
}
FD_SET(sockfd, &SVC_FDSET);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
/*
* If a local hostname is given (-n option to statd), bind to the address
* specified. This is required to support clients that ignore the mon_name in
* the statd protocol but use the source address from the request packet.
*/
if (MY_NAME) {
struct hostent *hp = gethostbyname(MY_NAME);
if (hp)
sin.sin_addr = *(struct in_addr *) hp->h_addr;
}
if (port != 0) {
sin.sin_port = htons(port);
if (bind(sockfd, &sin, sizeof(sin)) == 0)
goto out_success;
note(N_CRIT, "statd: failed to bind to outgoing port, %d\n"
" falling back on randomly chosen port\n", port);
}
if (bindresvport(sockfd, &sin) < 0) {
dprintf(N_WARNING,
"process_hosts: can't bind to reserved port\n");
}
out_success:
return sockfd;
}
/*
* Try to resolve host name for notify/callback request
*
* When compiled with RESTRICTED_STATD defined, we expect all
* host names to be dotted quads. See monitor.c for details. --okir
*/
#ifdef RESTRICTED_STATD
static int
try_to_resolve(notify_list *lp)
{
char *hname;
if (NL_TYPE(lp) == NOTIFY_REBOOT)
hname = NL_MON_NAME(lp);
else
hname = NL_MY_NAME(lp);
if (!inet_aton(hname, &(NL_ADDR(lp)))) {
note(N_ERROR, "%s is not an dotted-quad address", hname);
NL_TIMES(lp) = 0;
return 0;
}
/* XXX: In order to handle multi-homed hosts, we could do
* a reverse lookup, a forward lookup, and cycle through
* all the addresses.
*/
return 1;
}
#else
static int
try_to_resolve(notify_list *lp)
{
struct hostent *hp;
char *hname;
if (NL_TYPE(lp) == NOTIFY_REBOOT)
hname = NL_MON_NAME(lp);
else
hname = NL_MY_NAME(lp);
dprintf(N_DEBUG, "Trying to resolve %s.", hname);
if (!(hp = gethostbyname(hname))) {
herror("gethostbyname");
NL_TIMES(lp) -= 1;
return 0;
}
if (hp->h_addrtype != AF_INET) {
note(N_ERROR, "%s is not an AF_INET address", hname);
NL_TIMES(lp) = 0;
return 0;
}
/* FIXME: should try all addresses for multi-homed hosts in
* alternation because one interface might be down/unreachable. */
NL_ADDR(lp) = *(struct in_addr *) hp->h_addr;
dprintf(N_DEBUG, "address of %s is %s", hname, inet_ntoa(NL_ADDR(lp)));
return 1;
}
#endif
static unsigned long
xmit_call(int sockfd, struct sockaddr_in *sin,
u_int32_t prog, u_int32_t vers, u_int32_t proc,
xdrproc_t func, void *obj)
/* __u32 prog, __u32 vers, __u32 proc, xdrproc_t func, void *obj) */
{
unsigned int msgbuf[MAXMSGSIZE], msglen;
struct rpc_msg mesg;
struct pmap pmap;
XDR xdr, *xdrs = &xdr;
int err;
if (!xid)
xid = getpid() + time(NULL);
mesg.rm_xid = ++xid;
mesg.rm_direction = CALL;
mesg.rm_call.cb_rpcvers = 2;
if (sin->sin_port == 0) {
sin->sin_port = htons(PMAPPORT);
mesg.rm_call.cb_prog = PMAPPROG;
mesg.rm_call.cb_vers = PMAPVERS;
mesg.rm_call.cb_proc = PMAPPROC_GETPORT;
pmap.pm_prog = prog;
pmap.pm_vers = vers;
pmap.pm_prot = IPPROTO_UDP;
pmap.pm_port = 0;
func = (xdrproc_t) xdr_pmap;
obj = &pmap;
} else {
mesg.rm_call.cb_prog = prog;
mesg.rm_call.cb_vers = vers;
mesg.rm_call.cb_proc = proc;
}
mesg.rm_call.cb_cred.oa_flavor = AUTH_NULL;
mesg.rm_call.cb_cred.oa_base = (caddr_t) NULL;
mesg.rm_call.cb_cred.oa_length = 0;
mesg.rm_call.cb_verf.oa_flavor = AUTH_NULL;
mesg.rm_call.cb_verf.oa_base = (caddr_t) NULL;
mesg.rm_call.cb_verf.oa_length = 0;
/* Create XDR memory object for encoding */
xdrmem_create(xdrs, (caddr_t) msgbuf, sizeof(msgbuf), XDR_ENCODE);
/* Encode the RPC header part and payload */
if (!xdr_callmsg(xdrs, &mesg) || !func(xdrs, obj)) {
dprintf(N_WARNING, "xmit_mesg: can't encode RPC message!\n");
xdr_destroy(xdrs);
return 0;
}
/* Get overall length of datagram */
msglen = xdr_getpos(xdrs);
if ((err = sendto(sockfd, msgbuf, msglen, 0,
(struct sockaddr *) sin, sizeof(*sin))) < 0) {
dprintf(N_WARNING, "xmit_mesg: sendto failed: %m");
} else if (err != msglen) {
dprintf(N_WARNING, "xmit_mesg: short write: %m\n");
}
xdr_destroy(xdrs);
return err == msglen? xid : 0;
}
static notify_list *
recv_rply(int sockfd, struct sockaddr_in *sin, u_long *portp)
{
unsigned int msgbuf[MAXMSGSIZE], msglen;
struct rpc_msg mesg;
notify_list *lp = NULL;
XDR xdr, *xdrs = &xdr;
int alen = sizeof(*sin);
/* Receive message */
if ((msglen = recvfrom(sockfd, msgbuf, sizeof(msgbuf), 0,
(struct sockaddr *) sin, &alen)) < 0) {
dprintf(N_WARNING, "recv_rply: recvfrom failed: %m");
return NULL;
}
/* Create XDR object for decoding buffer */
xdrmem_create(xdrs, (caddr_t) msgbuf, msglen, XDR_DECODE);
memset(&mesg, 0, sizeof(mesg));
mesg.rm_reply.rp_acpt.ar_results.where = NULL;
mesg.rm_reply.rp_acpt.ar_results.proc = (xdrproc_t) xdr_void;
if (!xdr_replymsg(xdrs, &mesg)) {
note(N_WARNING, "recv_rply: can't decode RPC message!\n");
goto done;
}
if (mesg.rm_reply.rp_stat != 0) {
note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
inet_ntoa(sin->sin_addr),
mesg.rm_reply.rp_stat);
goto done;
}
if (mesg.rm_reply.rp_acpt.ar_stat != 0) {
note(N_WARNING, "recv_rply: [%s] RPC status %d\n",
inet_ntoa(sin->sin_addr),
mesg.rm_reply.rp_acpt.ar_stat);
goto done;
}
for (lp = notify; lp != NULL; lp = lp->next) {
/* LH - this was a bug... it should have been checking
* the xid from the response message from the client,
* not the static, internal xid */
if (lp->xid != mesg.rm_xid)
continue;
if (lp->addr.s_addr != sin->sin_addr.s_addr) {
char addr [18];
strncpy (addr, inet_ntoa(lp->addr),
sizeof (addr) - 1);
addr [sizeof (addr) - 1] = '\0';
dprintf(N_WARNING, "address mismatch: "
"expected %s, got %s\n",
addr, inet_ntoa(sin->sin_addr));
}
if (lp->port == 0) {
if (!xdr_u_long(xdrs, portp)) {
note(N_WARNING, "recv_rply: [%s] "
"can't decode reply body!\n",
inet_ntoa(sin->sin_addr));
lp = NULL;
goto done;
}
}
break;
}
done:
xdr_destroy(xdrs);
return lp;
}
/*
* Notify operation for a single list entry
*/
static int
process_entry(int sockfd, notify_list *lp)
{
struct sockaddr_in sin;
struct status new_status;
xdrproc_t func;
void *objp;
u_int32_t proc, vers, prog;
/* __u32 proc, vers, prog; */
if (lp->addr.s_addr == INADDR_ANY && !try_to_resolve(lp))
return NL_TIMES(lp);
if (NL_TIMES(lp) == 0) {
note(N_DEBUG, "Cannot notify %s, giving up.\n",
inet_ntoa(NL_ADDR(lp)));
return 0;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = lp->port;
/* LH - moved address into switch */
switch (NL_TYPE(lp)) {
case NOTIFY_REBOOT:
prog = SM_PROG;
vers = SM_VERS;
proc = SM_NOTIFY;
/* Use source address for notify replies */
sin.sin_addr = lp->addr;
func = (xdrproc_t) xdr_stat_chge;
objp = &SM_stat_chge;
break;
case NOTIFY_CALLBACK:
prog = NL_MY_PROG(lp);
vers = NL_MY_VERS(lp);
proc = NL_MY_PROC(lp);
/* __FORCE__ loopback for callbacks to lockd ... */
/* Just in case we somehow ignored it thus far */
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
func = (xdrproc_t) xdr_status;
objp = &new_status;
new_status.mon_name = NL_MON_NAME(lp);
new_status.state = NL_STATE(lp);
memcpy(new_status.priv, NL_PRIV(lp), SM_PRIV_SIZE);
break;
default:
note(N_ERROR, "notify_host: unknown notify type %d",
NL_TYPE(lp));
return 0;
}
lp->xid = xmit_call(sockfd, &sin, prog, vers, proc, func, objp);
if (!lp->xid) {
note(N_WARNING, "notify_host: failed to notify %s\n",
inet_ntoa(lp->addr));
}
NL_TIMES(lp) -= 1;
return 1;
}
/*
* Process a datagram received on the notify socket
*/
int
process_reply(FD_SET_TYPE *rfds)
{
struct sockaddr_in sin;
notify_list *lp;
u_long port;
if (sockfd == -1 || !FD_ISSET(sockfd, rfds))
return 0;
if (!(lp = recv_rply(sockfd, &sin, &port)))
return 1;
if (lp->port == 0) {
if (port != 0) {
lp->port = htons((unsigned short) port);
process_entry(sockfd, lp);
NL_WHEN(lp) = time(NULL) + NOTIFY_TIMEOUT;
nlist_remove(¬ify, lp);
nlist_insert_timer(¬ify, lp);
return 1;
}
note(N_WARNING, "recv_rply: [%s] service %d not registered",
inet_ntoa(lp->addr),
NL_TYPE(lp) == NOTIFY_REBOOT? SM_PROG : NL_MY_PROG(lp));
} else if (NL_TYPE(lp) == NOTIFY_REBOOT) {
dprintf(N_DEBUG, "Notification of %s succeeded.",
NL_MON_NAME(lp));
xunlink(SM_BAK_DIR, NL_MON_NAME(lp), 0);
} else {
dprintf(N_DEBUG, "Callback to %s (for %d) succeeded.",
NL_MY_NAME(lp), NL_MON_NAME(lp));
}
nlist_free(¬ify, lp);
return 1;
}
/*
* Process a notify list, either for notifying remote hosts after reboot
* or for calling back (local) statd clients when the remote has notified
* us of a crash.
*/
int
process_notify_list(void)
{
notify_list *entry;
time_t now;
int fd;
if ((fd = statd_get_socket(0)) < 0)
return 0;
while ((entry = notify) != NULL && NL_WHEN(entry) < time(&now)) {
if (process_entry(fd, entry)) {
NL_WHEN(entry) = time(NULL) + NOTIFY_TIMEOUT;
nlist_remove(¬ify, entry);
nlist_insert_timer(¬ify, entry);
} else if (NL_TYPE(entry) == NOTIFY_CALLBACK) {
note(N_ERROR,
"Can't callback %s (%d,%d), giving up.",
NL_MY_NAME(entry),
NL_MY_PROG(entry),
NL_MY_VERS(entry));
nlist_free(¬ify, entry);
} else {
note(N_ERROR,
"Can't notify %s, giving up.",
NL_MON_NAME(entry));
/* PRC: do the HA callout */
ha_callout("del-client", NL_MON_NAME(entry), NL_MY_NAME(entry), -1);
xunlink(SM_BAK_DIR, NL_MON_NAME(entry), 0);
nlist_free(¬ify, entry);
}
}
return 1;
}
|