This source file includes following definitions.
- debug_log
- remote_connection_destroy
- init_remote_listener
- check_group_membership
- cib_remote_auth
- remote_auth_timeout_cb
- cib_remote_listen
- cib_remote_connection_destroy
- cib_handle_remote_msg
- cib_remote_msg
- construct_pam_passwd
- authenticate_user
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11 #include <crm/crm.h>
12
13 #include <sys/param.h>
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <inttypes.h>
19 #include <sys/socket.h>
20 #include <arpa/inet.h>
21
22 #include <netinet/ip.h>
23
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <glib.h>
27
28 #include <crm/msg_xml.h>
29 #include <crm/common/ipc.h>
30 #include <crm/common/ipc_internal.h>
31 #include <crm/common/xml.h>
32 #include <crm/common/remote_internal.h>
33 #include <crm/cib/internal.h>
34
35 #include "pacemaker-based.h"
36
37
38
39
40 #ifdef HAVE_GNUTLS_GNUTLS_H
41 # undef KEYFILE
42 # include <gnutls/gnutls.h>
43 #endif
44
45 #include <pwd.h>
46 #include <grp.h>
47 #if HAVE_SECURITY_PAM_APPL_H
48 # include <security/pam_appl.h>
49 # define HAVE_PAM 1
50 #else
51 # if HAVE_PAM_PAM_APPL_H
52 # include <pam/pam_appl.h>
53 # define HAVE_PAM 1
54 # endif
55 #endif
56
57 extern int remote_tls_fd;
58 extern gboolean cib_shutdown_flag;
59
60 int init_remote_listener(int port, gboolean encrypted);
61 void cib_remote_connection_destroy(gpointer user_data);
62
63 #ifdef HAVE_GNUTLS_GNUTLS_H
64 gnutls_dh_params_t dh_params;
65 gnutls_anon_server_credentials_t anon_cred_s;
66 static void
67 debug_log(int level, const char *str)
68 {
69 fputs(str, stderr);
70 }
71 #endif
72
73 #define REMOTE_AUTH_TIMEOUT 10000
74
75 int num_clients;
76 int authenticate_user(const char *user, const char *passwd);
77 static int cib_remote_listen(gpointer data);
78 static int cib_remote_msg(gpointer data);
79
80 static void
81 remote_connection_destroy(gpointer user_data)
82 {
83 crm_info("No longer listening for remote connections");
84 return;
85 }
86
87 int
88 init_remote_listener(int port, gboolean encrypted)
89 {
90 int rc;
91 int *ssock = NULL;
92 struct sockaddr_in saddr;
93 int optval;
94
95 static struct mainloop_fd_callbacks remote_listen_fd_callbacks = {
96 .dispatch = cib_remote_listen,
97 .destroy = remote_connection_destroy,
98 };
99
100 if (port <= 0) {
101
102 return 0;
103 }
104
105 if (encrypted) {
106 #ifndef HAVE_GNUTLS_GNUTLS_H
107 crm_warn("TLS support is not available");
108 return 0;
109 #else
110 crm_notice("Starting TLS listener on port %d", port);
111 crm_gnutls_global_init();
112
113 gnutls_global_set_log_function(debug_log);
114 if (pcmk__init_tls_dh(&dh_params) != pcmk_rc_ok) {
115 return -1;
116 }
117 gnutls_anon_allocate_server_credentials(&anon_cred_s);
118 gnutls_anon_set_server_dh_params(anon_cred_s, dh_params);
119 #endif
120 } else {
121 crm_warn("Starting plain-text listener on port %d", port);
122 }
123 #ifndef HAVE_PAM
124 crm_warn("PAM is _not_ enabled!");
125 #endif
126
127
128 ssock = malloc(sizeof(int));
129 if(ssock == NULL) {
130 crm_perror(LOG_ERR, "Listener socket allocation failed");
131 return -1;
132 }
133
134 *ssock = socket(AF_INET, SOCK_STREAM, 0);
135 if (*ssock == -1) {
136 crm_perror(LOG_ERR, "Listener socket creation failed");
137 free(ssock);
138 return -1;
139 }
140
141
142 optval = 1;
143 rc = setsockopt(*ssock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
144 if (rc < 0) {
145 crm_perror(LOG_WARNING,
146 "Local address reuse not allowed on listener socket");
147 }
148
149
150 memset(&saddr, '\0', sizeof(saddr));
151 saddr.sin_family = AF_INET;
152 saddr.sin_addr.s_addr = INADDR_ANY;
153 saddr.sin_port = htons(port);
154 if (bind(*ssock, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
155 crm_perror(LOG_ERR, "Cannot bind to listener socket");
156 close(*ssock);
157 free(ssock);
158 return -2;
159 }
160 if (listen(*ssock, 10) == -1) {
161 crm_perror(LOG_ERR, "Cannot listen on socket");
162 close(*ssock);
163 free(ssock);
164 return -3;
165 }
166
167 mainloop_add_fd("cib-remote", G_PRIORITY_DEFAULT, *ssock, ssock, &remote_listen_fd_callbacks);
168 crm_debug("Started listener on port %d", port);
169
170 return *ssock;
171 }
172
173 static int
174 check_group_membership(const char *usr, const char *grp)
175 {
176 int index = 0;
177 struct passwd *pwd = NULL;
178 struct group *group = NULL;
179
180 CRM_CHECK(usr != NULL, return FALSE);
181 CRM_CHECK(grp != NULL, return FALSE);
182
183 pwd = getpwnam(usr);
184 if (pwd == NULL) {
185 crm_err("No user named '%s' exists!", usr);
186 return FALSE;
187 }
188
189 group = getgrgid(pwd->pw_gid);
190 if (group != NULL && pcmk__str_eq(grp, group->gr_name, pcmk__str_none)) {
191 return TRUE;
192 }
193
194 group = getgrnam(grp);
195 if (group == NULL) {
196 crm_err("No group named '%s' exists!", grp);
197 return FALSE;
198 }
199
200 while (TRUE) {
201 char *member = group->gr_mem[index++];
202
203 if (member == NULL) {
204 break;
205
206 } else if (pcmk__str_eq(usr, member, pcmk__str_none)) {
207 return TRUE;
208 }
209 };
210
211 return FALSE;
212 }
213
214 static gboolean
215 cib_remote_auth(xmlNode * login)
216 {
217 const char *user = NULL;
218 const char *pass = NULL;
219 const char *tmp = NULL;
220
221 crm_log_xml_info(login, "Login: ");
222 if (login == NULL) {
223 return FALSE;
224 }
225
226 tmp = crm_element_name(login);
227 if (!pcmk__str_eq(tmp, "cib_command", pcmk__str_casei)) {
228 crm_err("Wrong tag: %s", tmp);
229 return FALSE;
230 }
231
232 tmp = crm_element_value(login, "op");
233 if (!pcmk__str_eq(tmp, "authenticate", pcmk__str_casei)) {
234 crm_err("Wrong operation: %s", tmp);
235 return FALSE;
236 }
237
238 user = crm_element_value(login, "user");
239 pass = crm_element_value(login, "password");
240
241 if (!user || !pass) {
242 crm_err("missing auth credentials");
243 return FALSE;
244 }
245
246
247
248
249 if (check_group_membership(user, CRM_DAEMON_GROUP) == FALSE) {
250 crm_err("User is not a member of the required group");
251 return FALSE;
252
253 } else if (authenticate_user(user, pass) == FALSE) {
254 crm_err("PAM auth failed");
255 return FALSE;
256 }
257
258 return TRUE;
259 }
260
261 static gboolean
262 remote_auth_timeout_cb(gpointer data)
263 {
264 pcmk__client_t *client = data;
265
266 client->remote->auth_timeout = 0;
267
268 if (client->remote->authenticated == TRUE) {
269 return FALSE;
270 }
271
272 mainloop_del_fd(client->remote->source);
273 crm_err("Remote client authentication timed out");
274
275 return FALSE;
276 }
277
278 static int
279 cib_remote_listen(gpointer data)
280 {
281 int csock = 0;
282 unsigned laddr;
283 struct sockaddr_storage addr;
284 char ipstr[INET6_ADDRSTRLEN];
285 int ssock = *(int *)data;
286 int rc;
287
288 pcmk__client_t *new_client = NULL;
289
290 static struct mainloop_fd_callbacks remote_client_fd_callbacks = {
291 .dispatch = cib_remote_msg,
292 .destroy = cib_remote_connection_destroy,
293 };
294
295
296 laddr = sizeof(addr);
297 memset(&addr, 0, sizeof(addr));
298 csock = accept(ssock, (struct sockaddr *)&addr, &laddr);
299 if (csock == -1) {
300 crm_perror(LOG_ERR, "Could not accept socket connection");
301 return TRUE;
302 }
303
304 pcmk__sockaddr2str(&addr, ipstr);
305 crm_debug("New %s connection from %s",
306 ((ssock == remote_tls_fd)? "secure" : "clear-text"), ipstr);
307
308 rc = pcmk__set_nonblocking(csock);
309 if (rc != pcmk_rc_ok) {
310 crm_err("Could not set socket non-blocking: %s " CRM_XS " rc=%d",
311 pcmk_rc_str(rc), rc);
312 close(csock);
313 return TRUE;
314 }
315
316 num_clients++;
317
318 new_client = pcmk__new_unauth_client(NULL);
319 new_client->remote = calloc(1, sizeof(pcmk__remote_t));
320
321 if (ssock == remote_tls_fd) {
322 #ifdef HAVE_GNUTLS_GNUTLS_H
323 pcmk__set_client_flags(new_client, pcmk__client_tls);
324
325
326 new_client->remote->tls_session = pcmk__new_tls_session(csock,
327 GNUTLS_SERVER,
328 GNUTLS_CRD_ANON,
329 anon_cred_s);
330 if (new_client->remote->tls_session == NULL) {
331 close(csock);
332 return TRUE;
333 }
334 #endif
335 } else {
336 pcmk__set_client_flags(new_client, pcmk__client_tcp);
337 new_client->remote->tcp_socket = csock;
338 }
339
340
341 new_client->remote->auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT,
342 remote_auth_timeout_cb,
343 new_client);
344 crm_info("Remote CIB client pending authentication "
345 CRM_XS " %p id: %s", new_client, new_client->id);
346
347 new_client->remote->source =
348 mainloop_add_fd("cib-remote-client", G_PRIORITY_DEFAULT, csock, new_client,
349 &remote_client_fd_callbacks);
350
351 return TRUE;
352 }
353
354 void
355 cib_remote_connection_destroy(gpointer user_data)
356 {
357 pcmk__client_t *client = user_data;
358 int csock = 0;
359
360 if (client == NULL) {
361 return;
362 }
363
364 crm_trace("Cleaning up after client %s disconnect",
365 pcmk__client_name(client));
366
367 num_clients--;
368 crm_trace("Num unfree'd clients: %d", num_clients);
369
370 switch (PCMK__CLIENT_TYPE(client)) {
371 case pcmk__client_tcp:
372 csock = client->remote->tcp_socket;
373 break;
374 #ifdef HAVE_GNUTLS_GNUTLS_H
375 case pcmk__client_tls:
376 if (client->remote->tls_session) {
377 void *sock_ptr = gnutls_transport_get_ptr(*client->remote->tls_session);
378
379 csock = GPOINTER_TO_INT(sock_ptr);
380 if (client->remote->tls_handshake_complete) {
381 gnutls_bye(*client->remote->tls_session, GNUTLS_SHUT_WR);
382 }
383 gnutls_deinit(*client->remote->tls_session);
384 gnutls_free(client->remote->tls_session);
385 client->remote->tls_session = NULL;
386 }
387 break;
388 #endif
389 default:
390 crm_warn("Unknown transport for client %s "
391 CRM_XS " flags=0x%016" PRIx64,
392 pcmk__client_name(client), client->flags);
393 }
394
395 if (csock > 0) {
396 close(csock);
397 }
398
399 pcmk__free_client(client);
400
401 crm_trace("Freed the cib client");
402
403 if (cib_shutdown_flag) {
404 cib_shutdown(0);
405 }
406 return;
407 }
408
409 static void
410 cib_handle_remote_msg(pcmk__client_t *client, xmlNode *command)
411 {
412 const char *value = NULL;
413
414 value = crm_element_name(command);
415 if (!pcmk__str_eq(value, "cib_command", pcmk__str_casei)) {
416 crm_log_xml_trace(command, "Bad command: ");
417 return;
418 }
419
420 if (client->name == NULL) {
421 value = crm_element_value(command, F_CLIENTNAME);
422 if (value == NULL) {
423 client->name = strdup(client->id);
424 } else {
425 client->name = strdup(value);
426 }
427 }
428
429 if (client->userdata == NULL) {
430 value = crm_element_value(command, F_CIB_CALLBACK_TOKEN);
431 if (value != NULL) {
432 client->userdata = strdup(value);
433 crm_trace("Callback channel for %s is %s", client->id, (char*)client->userdata);
434
435 } else {
436 client->userdata = strdup(client->id);
437 }
438 }
439
440
441 xml_remove_prop(command, F_ORIG);
442 xml_remove_prop(command, F_CIB_HOST);
443 xml_remove_prop(command, F_CIB_GLOBAL_UPDATE);
444
445 crm_xml_add(command, F_TYPE, T_CIB);
446 crm_xml_add(command, F_CIB_CLIENTID, client->id);
447 crm_xml_add(command, F_CIB_CLIENTNAME, client->name);
448 crm_xml_add(command, F_CIB_USER, client->user);
449
450 if (crm_element_value(command, F_CIB_CALLID) == NULL) {
451 char *call_uuid = crm_generate_uuid();
452
453
454 crm_xml_add(command, F_CIB_CALLID, call_uuid);
455 free(call_uuid);
456 }
457
458 if (crm_element_value(command, F_CIB_CALLOPTS) == NULL) {
459 crm_xml_add_int(command, F_CIB_CALLOPTS, 0);
460 }
461
462 crm_log_xml_trace(command, "Remote command: ");
463 cib_common_callback_worker(0, 0, command, client, TRUE);
464 }
465
466 static int
467 cib_remote_msg(gpointer data)
468 {
469 xmlNode *command = NULL;
470 pcmk__client_t *client = data;
471 int rc;
472 int timeout = client->remote->authenticated ? -1 : 1000;
473
474 crm_trace("Remote %s message received for client %s",
475 pcmk__client_type_str(PCMK__CLIENT_TYPE(client)),
476 pcmk__client_name(client));
477
478 #ifdef HAVE_GNUTLS_GNUTLS_H
479 if ((PCMK__CLIENT_TYPE(client) == pcmk__client_tls)
480 && !(client->remote->tls_handshake_complete)) {
481
482 int rc = pcmk__read_handshake_data(client);
483
484 if (rc == EAGAIN) {
485
486
487
488 return 0;
489 } else if (rc != pcmk_rc_ok) {
490 return -1;
491 }
492
493 crm_debug("TLS handshake with remote CIB client completed");
494 client->remote->tls_handshake_complete = TRUE;
495 if (client->remote->auth_timeout) {
496 g_source_remove(client->remote->auth_timeout);
497 }
498
499
500 client->remote->auth_timeout = g_timeout_add(REMOTE_AUTH_TIMEOUT,
501 remote_auth_timeout_cb,
502 client);
503 return 0;
504 }
505 #endif
506
507 rc = pcmk__read_remote_message(client->remote, timeout);
508
509
510 if (client->remote->authenticated == FALSE) {
511 xmlNode *reg;
512 const char *user = NULL;
513
514 command = pcmk__remote_message_xml(client->remote);
515 if (cib_remote_auth(command) == FALSE) {
516 free_xml(command);
517 return -1;
518 }
519
520 crm_notice("Remote CIB client connection accepted");
521 client->remote->authenticated = TRUE;
522 g_source_remove(client->remote->auth_timeout);
523 client->remote->auth_timeout = 0;
524 client->name = crm_element_value_copy(command, "name");
525
526 user = crm_element_value(command, "user");
527 if (user) {
528 client->user = strdup(user);
529 }
530
531
532 reg = create_xml_node(NULL, "cib_result");
533 crm_xml_add(reg, F_CIB_OPERATION, CRM_OP_REGISTER);
534 crm_xml_add(reg, F_CIB_CLIENTID, client->id);
535 pcmk__remote_send_xml(client->remote, reg);
536 free_xml(reg);
537 free_xml(command);
538 }
539
540 command = pcmk__remote_message_xml(client->remote);
541 while (command) {
542 crm_trace("Remote client message received");
543 cib_handle_remote_msg(client, command);
544 free_xml(command);
545 command = pcmk__remote_message_xml(client->remote);
546 }
547
548 if (rc == ENOTCONN) {
549 crm_trace("Remote CIB client disconnected while reading from it");
550 return -1;
551 }
552
553 return 0;
554 }
555
556 #ifdef HAVE_PAM
557 static int
558 construct_pam_passwd(int num_msg, const struct pam_message **msg,
559 struct pam_response **response, void *data)
560 {
561 int count = 0;
562 struct pam_response *reply;
563 char *string = (char *)data;
564
565 CRM_CHECK(data, return PAM_CONV_ERR);
566 CRM_CHECK(num_msg == 1, return PAM_CONV_ERR);
567
568 reply = calloc(1, sizeof(struct pam_response));
569 CRM_ASSERT(reply != NULL);
570
571 for (count = 0; count < num_msg; ++count) {
572 switch (msg[count]->msg_style) {
573 case PAM_TEXT_INFO:
574 crm_info("PAM: %s", msg[count]->msg);
575 break;
576 case PAM_PROMPT_ECHO_OFF:
577 case PAM_PROMPT_ECHO_ON:
578 reply[count].resp_retcode = 0;
579 reply[count].resp = string;
580 case PAM_ERROR_MSG:
581
582
583
584
585 break;
586 default:
587 crm_err("Unhandled conversation type: %d", msg[count]->msg_style);
588 goto bail;
589 }
590 }
591
592 *response = reply;
593 reply = NULL;
594
595 return PAM_SUCCESS;
596
597 bail:
598 for (count = 0; count < num_msg; ++count) {
599 if (reply[count].resp != NULL) {
600 switch (msg[count]->msg_style) {
601 case PAM_PROMPT_ECHO_ON:
602 case PAM_PROMPT_ECHO_OFF:
603
604 while (*(reply[count].resp)) {
605 *(reply[count].resp)++ = '\0';
606 }
607 free(reply[count].resp);
608 break;
609 }
610 reply[count].resp = NULL;
611 }
612 }
613 free(reply);
614 reply = NULL;
615
616 return PAM_CONV_ERR;
617 }
618 #endif
619
620 int
621 authenticate_user(const char *user, const char *passwd)
622 {
623 #ifndef HAVE_PAM
624 gboolean pass = TRUE;
625 #else
626 int rc = 0;
627 gboolean pass = FALSE;
628 const void *p_user = NULL;
629
630 struct pam_conv p_conv;
631 struct pam_handle *pam_h = NULL;
632 static const char *pam_name = NULL;
633
634 if (pam_name == NULL) {
635 pam_name = getenv("CIB_pam_service");
636 }
637 if (pam_name == NULL) {
638 pam_name = "login";
639 }
640
641 p_conv.conv = construct_pam_passwd;
642 p_conv.appdata_ptr = strdup(passwd);
643
644 rc = pam_start(pam_name, user, &p_conv, &pam_h);
645 if (rc != PAM_SUCCESS) {
646 crm_err("Could not initialize PAM: %s (%d)", pam_strerror(pam_h, rc), rc);
647 goto bail;
648 }
649
650 rc = pam_authenticate(pam_h, 0);
651 if (rc != PAM_SUCCESS) {
652 crm_err("Authentication failed for %s: %s (%d)", user, pam_strerror(pam_h, rc), rc);
653 goto bail;
654 }
655
656
657
658
659
660
661 rc = pam_get_item(pam_h, PAM_USER, &p_user);
662 if (rc != PAM_SUCCESS) {
663 crm_err("Internal PAM error: %s (%d)", pam_strerror(pam_h, rc), rc);
664 goto bail;
665
666 } else if (p_user == NULL) {
667 crm_err("Unknown user authenticated.");
668 goto bail;
669
670 } else if (!pcmk__str_eq(p_user, user, pcmk__str_casei)) {
671 crm_err("User mismatch: %s vs. %s.", (const char *)p_user, (const char *)user);
672 goto bail;
673 }
674
675 rc = pam_acct_mgmt(pam_h, 0);
676 if (rc != PAM_SUCCESS) {
677 crm_err("Access denied: %s (%d)", pam_strerror(pam_h, rc), rc);
678 goto bail;
679 }
680 pass = TRUE;
681
682 bail:
683 pam_end(pam_h, rc);
684 #endif
685 return pass;
686 }