OpenVPN
auth-pam.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2016-2026 Selva Nair <selva.nair@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
24/*
25 * OpenVPN plugin module to do PAM authentication using a split
26 * privilege model.
27 */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#include <security/pam_appl.h>
33
34#ifdef USE_PAM_DLOPEN
35#include <dlfcn.h>
36#include "pamdl.h"
37#endif
38
39#include <stdio.h>
40#include <string.h>
41#include <ctype.h>
42#include <unistd.h>
43#include <stdlib.h>
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <sys/wait.h>
47#include <fcntl.h>
48#include <signal.h>
49#include <syslog.h>
50#include <limits.h>
51#include "utils.h"
52#include <arpa/inet.h>
53#include <openvpn-plugin.h>
54
55#define DEBUG(verb) ((verb) >= 4)
56
57/* Command codes for foreground -> background communication */
58#define COMMAND_VERIFY 0
59#define COMMAND_EXIT 1
60
61/* Response codes for background -> foreground communication */
62#define RESPONSE_INIT_SUCCEEDED 10
63#define RESPONSE_INIT_FAILED 11
64#define RESPONSE_VERIFY_SUCCEEDED 12
65#define RESPONSE_VERIFY_FAILED 13
66#define RESPONSE_DEFER 14
67
68/* Pointers to functions exported from openvpn */
72
73/* module name for plugin_log() */
74static char *MODULE = "AUTH-PAM";
75
76/*
77 * Plugin state, used by foreground
78 */
80{
81 /* Foreground's socket to background process */
83
84 /* Process ID of background process */
86
87 /* Verbosity level of OpenVPN */
88 int verb;
89};
90
91/*
92 * Name/Value pairs for conversation function.
93 * Special Values:
94 *
95 * "USERNAME" -- substitute client-supplied username
96 * "PASSWORD" -- substitute client-specified password
97 * "COMMONNAME" -- substitute client certificate common name
98 * "OTP" -- substitute static challenge response if available
99 */
100
101#define N_NAME_VALUE 16
102
104{
105 const char *name;
106 const char *value;
107};
108
114
115/*
116 * Used to pass the username/password
117 * to the PAM conversation function.
118 */
119struct user_pass
120{
121 int verb;
122
123 char username[128];
124 char password[128];
125 char common_name[128];
126 char response[128];
127 char remote[INET6_ADDRSTRLEN];
128
130};
131
132/* Background process function */
133static void pam_server(int fd, const char *service, int verb,
134 const struct name_value_list *name_value_list);
135
136
137/*
138 * Socket read/write functions.
139 */
140
141static int
143{
144 unsigned char c;
145 const ssize_t size = read(fd, &c, sizeof(c));
146 if (size == sizeof(c))
147 {
148 return c;
149 }
150 else
151 {
152 /*fprintf (stderr, "AUTH-PAM: DEBUG recv_control.read=%d\n", (int)size);*/
153 return -1;
154 }
155}
156
157static int
158send_control(int fd, int code)
159{
160 unsigned char c = (unsigned char)code;
161 const ssize_t size = write(fd, &c, sizeof(c));
162 if (size == sizeof(c))
163 {
164 return (int)size;
165 }
166 else
167 {
168 return -1;
169 }
170}
171
172static ssize_t
173recv_string(int fd, char *buffer, size_t len)
174{
175 if (len > 0)
176 {
177 memset(buffer, 0, len);
178 ssize_t size = read(fd, buffer, len);
179 buffer[len - 1] = 0;
180 if (size >= 1)
181 {
182 return size;
183 }
184 }
185 return -1;
186}
187
188#if defined(__GNUC__) || defined(__clang__)
189#pragma GCC diagnostic push
190#pragma GCC diagnostic ignored "-Wsign-conversion"
191#endif
192
193static ssize_t
194send_string(int fd, const char *string)
195{
196 const ssize_t len = strlen(string) + 1;
197 const ssize_t size = write(fd, string, len);
198 if (size == len)
199 {
200 return size;
201 }
202 else
203 {
204 return -1;
205 }
206}
207
208#if defined(__GNUC__) || defined(__clang__)
209#pragma GCC diagnostic pop
210#endif
211
212#ifdef DO_DAEMONIZE
213
214/*
215 * Daemonize if "daemon" env var is true.
216 * Preserve stderr across daemonization if
217 * "daemon_log_redirect" env var is true.
218 */
219static void
220daemonize(const char *envp[])
221{
222 const char *daemon_string = get_env("daemon", envp);
223 if (daemon_string && daemon_string[0] == '1')
224 {
225 const char *log_redirect = get_env("daemon_log_redirect", envp);
226 int fd = -1;
227 if (log_redirect && log_redirect[0] == '1')
228 {
229 fd = dup(2);
230 }
231#if defined(__APPLE__) && defined(__clang__)
232#pragma clang diagnostic push
233#pragma clang diagnostic ignored "-Wdeprecated-declarations"
234#endif
235 if (daemon(0, 0) < 0)
236 {
237 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "daemonization failed");
238 }
239#if defined(__APPLE__) && defined(__clang__)
240#pragma clang diagnostic pop
241#endif
242 else if (fd >= 3)
243 {
244 dup2(fd, 2);
245 close(fd);
246 }
247 }
248}
249
250#endif /* ifdef DO_DAEMONIZE */
251
252/*
253 * Close most of parent's fds.
254 * Keep stdin/stdout/stderr, plus one
255 * other fd which is presumed to be
256 * our pipe back to parent.
257 * Admittedly, a bit of a kludge,
258 * but posix doesn't give us a kind
259 * of FD_CLOEXEC which will stop
260 * fds from crossing a fork().
261 */
262static void
264{
265 int i;
266 closelog();
267 for (i = 3; i <= 100; ++i)
268 {
269 if (i != keep)
270 {
271 close(i);
272 }
273 }
274}
275
276/*
277 * Usually we ignore signals, because our parent will
278 * deal with them.
279 */
280static void
291
292/*
293 * Return 1 if query matches match.
294 */
295static int
296name_value_match(const char *query, const char *match)
297{
298 while (!isalnum(*query))
299 {
300 if (*query == '\0')
301 {
302 return 0;
303 }
304 ++query;
305 }
306 return strncasecmp(match, query, strlen(match)) == 0;
307}
308
309/*
310 * Split and decode up->password in the form SCRV1:base64_pass:base64_response
311 * into pass and response and save in up->password and up->response.
312 * If the password is not in the expected format, input is not changed.
313 */
314static void
316{
317 const int skip = strlen("SCRV1:");
318 if (strncmp(up->password, "SCRV1:", skip) != 0)
319 {
320 return;
321 }
322
323 char *tmp = strdup(up->password);
324 if (!tmp)
325 {
326 plugin_log(PLOG_ERR, MODULE, "out of memory parsing static challenge password");
327 goto out;
328 }
329
330 char *pass = tmp + skip;
331 char *resp = strchr(pass, ':');
332 if (!resp) /* string not in SCRV1:xx:yy format */
333 {
334 goto out;
335 }
336 *resp++ = '\0';
337
338 int n = plugin_base64_decode(pass, up->password, sizeof(up->password) - 1);
339 if (n >= 0)
340 {
341 up->password[n] = '\0';
342 n = plugin_base64_decode(resp, up->response, sizeof(up->response) - 1);
343 if (n >= 0)
344 {
345 up->response[n] = '\0';
346 if (DEBUG(up->verb))
347 {
348 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: parsed static challenge password");
349 }
350 goto out;
351 }
352 }
353
354 /* decode error: reinstate original value of up->password and return */
355 plugin_secure_memzero(up->password, sizeof(up->password));
356 plugin_secure_memzero(up->response, sizeof(up->response));
357 strcpy(up->password, tmp); /* tmp is guaranteed to fit in up->password */
358
359 plugin_log(PLOG_ERR, MODULE, "base64 decode error while parsing static challenge password");
360
361out:
362 if (tmp)
363 {
365 free(tmp);
366 }
367}
368
372{
373 pid_t pid;
374 int fd[2];
375
378
379 const int base_parms = 2;
380
381 const char **argv = args->argv;
382 const char **envp = args->envp;
383
384 /* Check API compatibility -- struct version 5 or higher needed */
385 if (v3structver < 5)
386 {
388 "AUTH-PAM: This plugin is incompatible with the running version of OpenVPN\n");
390 }
391
392 /*
393 * Allocate our context
394 */
395 context = (struct auth_pam_context *)calloc(1, sizeof(struct auth_pam_context));
396 if (!context)
397 {
398 goto error;
399 }
400 context->foreground_fd = -1;
401
402 /*
403 * Intercept the --auth-user-pass-verify callback.
404 */
406
407 /* Save global pointers to functions exported from openvpn */
411
412 /*
413 * Make sure we have two string arguments: the first is the .so name,
414 * the second is the PAM service type.
415 */
416 if (string_array_len(argv) < base_parms)
417 {
418 plugin_log(PLOG_ERR, MODULE, "need PAM service parameter");
419 goto error;
420 }
421
422 /*
423 * See if we have optional name/value pairs to match against
424 * PAM module queried fields in the conversation function.
425 */
427 if (string_array_len(argv) > base_parms)
428 {
429 const int nv_len = string_array_len(argv) - base_parms;
430 int i;
431
432 if ((nv_len & 1) == 1 || (nv_len / 2) > N_NAME_VALUE)
433 {
434 plugin_log(PLOG_ERR, MODULE, "bad name/value list length");
435 goto error;
436 }
437
438 name_value_list.len = nv_len / 2;
439 for (i = 0; i < name_value_list.len; ++i)
440 {
441 const int base = base_parms + i * 2;
442 name_value_list.data[i].name = argv[base];
443 name_value_list.data[i].value = argv[base + 1];
444 }
445 }
446
447 /*
448 * Get verbosity level from environment
449 */
450 {
451 const char *verb_string = get_env("verb", envp);
452 if (verb_string)
453 {
454 context->verb = atoi(verb_string);
455 }
456 }
457
458 /*
459 * Make a socket for foreground and background processes
460 * to communicate.
461 */
462 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) == -1)
463 {
464 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "socketpair call failed");
465 goto error;
466 }
467
468 /*
469 * Fork off the privileged process. It will remain privileged
470 * even after the foreground process drops its privileges.
471 */
472 pid = fork();
473
474 if (pid)
475 {
476 int status;
477
478 /*
479 * Foreground Process
480 */
481
482 context->background_pid = pid;
483
484 /* close our copy of child's socket */
485 close(fd[1]);
486
487 /* don't let future subprocesses inherit child socket */
488 if (fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0)
489 {
491 "Set FD_CLOEXEC flag on socket file descriptor failed");
492 }
493
494 /* wait for background child process to initialize */
495 status = recv_control(fd[0]);
497 {
498 context->foreground_fd = fd[0];
500 plugin_log(PLOG_NOTE, MODULE, "initialization succeeded (fg)");
502 }
503 }
504 else
505 {
506 /*
507 * Background Process
508 */
509
510 /* close all parent fds except our socket back to parent */
511 close_fds_except(fd[1]);
512
513 /* Ignore most signals (the parent will receive them) */
514 set_signals();
515
516#ifdef DO_DAEMONIZE
517 /* Daemonize if --daemon option is set. */
518 daemonize(envp);
519#endif
520
521 /* execute the event loop */
522 pam_server(fd[1], argv[1], context->verb, &name_value_list);
523
524 close(fd[1]);
525
526 exit(0);
527 return 0; /* NOTREACHED */
528 }
529
530error:
531 free(context);
533}
534
536openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[],
537 const char *envp[])
538{
539 const struct auth_pam_context *context = (struct auth_pam_context *)handle;
540
541 if (type == OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY && context->foreground_fd >= 0)
542 {
543 /* get username/password from envp string array */
544 const char *username = get_env("username", envp);
545 const char *password = get_env("password", envp);
546 const char *common_name = get_env("common_name", envp) ? get_env("common_name", envp) : "";
547 const char *remote = get_env("untrusted_ip6", envp);
548
549 if (remote == NULL)
550 {
551 remote = get_env("untrusted_ip", envp);
552 }
553
554 if (remote == NULL)
555 {
556 remote = "";
557 }
558
559 /* should we do deferred auth?
560 * yes, if there is "auth_control_file" and "deferred_auth_pam" env
561 */
562 const char *auth_control_file = get_env("auth_control_file", envp);
563 const char *deferred_auth_pam = get_env("deferred_auth_pam", envp);
564 if (auth_control_file != NULL && deferred_auth_pam != NULL)
565 {
566 if (DEBUG(context->verb))
567 {
568 plugin_log(PLOG_NOTE, MODULE, "do deferred auth '%s'", auth_control_file);
569 }
570 }
571 else
572 {
573 auth_control_file = "";
574 }
575
576 if (username && strlen(username) > 0 && password)
577 {
578 if (send_control(context->foreground_fd, COMMAND_VERIFY) == -1
579 || send_string(context->foreground_fd, username) == -1
580 || send_string(context->foreground_fd, password) == -1
581 || send_string(context->foreground_fd, common_name) == -1
582 || send_string(context->foreground_fd, auth_control_file) == -1
583 || send_string(context->foreground_fd, remote) == -1)
584 {
586 "Error sending auth info to background process");
587 }
588 else
589 {
590 const int status = recv_control(context->foreground_fd);
592 {
594 }
595 if (status == RESPONSE_DEFER)
596 {
597 if (DEBUG(context->verb))
598 {
599 plugin_log(PLOG_NOTE, MODULE, "deferred authentication");
600 }
602 }
603 if (status == -1)
604 {
606 "Error receiving auth confirmation from background process");
607 }
608 }
609 }
610 }
612}
613
616{
617 struct auth_pam_context *context = (struct auth_pam_context *)handle;
618
619 if (DEBUG(context->verb))
620 {
621 plugin_log(PLOG_NOTE, MODULE, "close");
622 }
623
624 if (context->foreground_fd >= 0)
625 {
626 /* tell background process to exit */
627 if (send_control(context->foreground_fd, COMMAND_EXIT) == -1)
628 {
629 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "Error signaling background process to exit");
630 }
631
632 /* wait for background process to exit */
633 if (context->background_pid > 0)
634 {
635 waitpid(context->background_pid, NULL, 0);
636 }
637
638 close(context->foreground_fd);
639 context->foreground_fd = -1;
640 }
641
642 free(context);
643}
644
647{
648 struct auth_pam_context *context = (struct auth_pam_context *)handle;
649
650 /* tell background process to exit */
651 if (context && context->foreground_fd >= 0)
652 {
653 send_control(context->foreground_fd, COMMAND_EXIT);
654 close(context->foreground_fd);
655 context->foreground_fd = -1;
656 }
657}
658
659/*
660 * PAM conversation function
661 */
662static int
663my_conv(int num_msg, const struct pam_message **msg_array, struct pam_response **response_array,
664 void *appdata_ptr)
665{
666 const struct user_pass *up = (const struct user_pass *)appdata_ptr;
667 struct pam_response *aresp;
668 int ret = PAM_SUCCESS;
669
670 *response_array = NULL;
671
672 if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
673 {
674 return (PAM_CONV_ERR);
675 }
676 if ((aresp = calloc((size_t)num_msg, sizeof *aresp)) == NULL)
677 {
678 return (PAM_BUF_ERR);
679 }
680
681 /* loop through each PAM-module query */
682 for (int i = 0; i < num_msg; ++i)
683 {
684 const struct pam_message *msg = msg_array[i];
685 aresp[i].resp_retcode = 0;
686 aresp[i].resp = NULL;
687
688 if (DEBUG(up->verb))
689 {
690 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: my_conv[%d] query='%s' style=%d", i,
691 msg->msg ? msg->msg : "NULL", msg->msg_style);
692 }
693
694 if (up->name_value_list && up->name_value_list->len > 0)
695 {
696 /* use name/value list match method */
697 const struct name_value_list *list = up->name_value_list;
698
699 /* loop through name/value pairs */
700 int j; /* checked after loop */
701 for (j = 0; j < list->len; ++j)
702 {
703 const char *match_name = list->data[j].name;
704 const char *match_value = list->data[j].value;
705
707 {
708 /* found name/value match */
709 aresp[i].resp = NULL;
710
711 if (DEBUG(up->verb))
712 {
715 "BACKGROUND: name match found, query/match-string ['%s', '%s'] = '%s'",
716 msg->msg, match_name, match_value);
717 }
718
719 if (strstr(match_value, "USERNAME"))
720 {
721 aresp[i].resp = searchandreplace(match_value, "USERNAME", up->username);
722 }
723 else if (strstr(match_value, "PASSWORD"))
724 {
725 aresp[i].resp = searchandreplace(match_value, "PASSWORD", up->password);
726 }
727 else if (strstr(match_value, "COMMONNAME"))
728 {
729 aresp[i].resp =
730 searchandreplace(match_value, "COMMONNAME", up->common_name);
731 }
732 else if (strstr(match_value, "OTP"))
733 {
734 aresp[i].resp = searchandreplace(match_value, "OTP", up->response);
735 }
736 else
737 {
738 aresp[i].resp = strdup(match_value);
739 }
740
741 if (aresp[i].resp == NULL)
742 {
744 }
745 break;
746 }
747 }
748
749 if (j == list->len)
750 {
752 }
753 }
754 else
755 {
756 /* use PAM_PROMPT_ECHO_x hints */
757 switch (msg->msg_style)
758 {
760 aresp[i].resp = strdup(up->password);
761 if (aresp[i].resp == NULL)
762 {
764 }
765 break;
766
768 aresp[i].resp = strdup(up->username);
769 if (aresp[i].resp == NULL)
770 {
772 }
773 break;
774
775 case PAM_ERROR_MSG:
776 case PAM_TEXT_INFO:
777 break;
778
779 default:
781 break;
782 }
783 }
784 }
785
786 if (ret == PAM_SUCCESS)
787 {
789 }
790 else
791 {
792 for (int i = 0; i < num_msg; ++i)
793 {
794 if (aresp[i].resp)
795 {
797 free(aresp[i].resp);
798 }
799 }
800 free(aresp);
801 }
802
803 return ret;
804}
805
806/*
807 * Return 1 if authenticated and 0 if failed.
808 * Called once for every username/password
809 * to be authenticated.
810 */
811static int
812pam_auth(const char *service, const struct user_pass *up)
813{
814 struct pam_conv conv;
815 pam_handle_t *pamh = NULL;
816 int status = PAM_SUCCESS;
817 int ret = 0;
818 const int name_value_list_provided = (up->name_value_list && up->name_value_list->len > 0);
819
820 /* Initialize PAM */
821 conv.conv = my_conv;
822 conv.appdata_ptr = (void *)up;
823 status = pam_start(service, name_value_list_provided ? NULL : up->username, &conv, &pamh);
824 if (status == PAM_SUCCESS)
825 {
826 /* Set PAM_RHOST environment variable */
827 if (*(up->remote))
828 {
829 status = pam_set_item(pamh, PAM_RHOST, up->remote);
830 }
831 /* Call PAM to verify username/password */
832 if (status == PAM_SUCCESS)
833 {
834 status = pam_authenticate(pamh, 0);
835 }
836 if (status == PAM_SUCCESS)
837 {
838 status = pam_acct_mgmt(pamh, 0);
839 }
840 if (status == PAM_SUCCESS)
841 {
842 ret = 1;
843 }
844
845 /* Output error message if failed */
846 if (!ret)
847 {
848 plugin_log(PLOG_ERR, MODULE, "BACKGROUND: user '%s' failed to authenticate: %s",
849 up->username, pam_strerror(pamh, status));
850 }
851
852 /* Close PAM */
853 pam_end(pamh, status);
854 }
855
856 return ret;
857}
858
859/*
860 * deferred auth handler
861 * - fork() (twice, to avoid the need for async wait / SIGCHLD handling)
862 * - query PAM stack via pam_auth()
863 * - send response back to OpenVPN via "ac_file_name"
864 *
865 * parent process returns "0" for "fork() and wait() succeeded",
866 * "-1" for "something went wrong, abort program"
867 */
868
869static void
870do_deferred_pam_auth(int fd, const char *ac_file_name, const char *service,
871 const struct user_pass *up)
872{
873 if (send_control(fd, RESPONSE_DEFER) == -1)
874 {
875 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "BACKGROUND: write error on response socket [4]");
876 return;
877 }
878
879 /* double forking so we do not need to wait() for async auth kids */
880 pid_t p1 = fork();
881
882 if (p1 < 0)
883 {
884 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "BACKGROUND: fork(1) failed");
885 return;
886 }
887 if (p1 != 0) /* parent */
888 {
889 waitpid(p1, NULL, 0);
890 return; /* parent's job succeeded */
891 }
892
893 /* child */
894 close(fd); /* socketpair no longer needed */
895
896 pid_t p2 = fork();
897 if (p2 < 0)
898 {
899 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "BACKGROUND: fork(2) failed");
900 exit(1);
901 }
902
903 if (p2 != 0) /* new parent: exit right away */
904 {
905 exit(0);
906 }
907
908 /* grandchild */
909 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: deferred auth for '%s', pid=%d", up->username,
910 (int)getpid());
911
912 /* the rest is very simple: do PAM, write status byte to file, done */
913 int ac_fd = open(ac_file_name, O_WRONLY);
914 if (ac_fd < 0)
915 {
916 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "cannot open '%s' for writing", ac_file_name);
917 exit(1);
918 }
919 int pam_success = pam_auth(service, up);
920
921 if (write(ac_fd, pam_success ? "1" : "0", 1) != 1)
922 {
923 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "cannot write to '%s'", ac_file_name);
924 }
925 close(ac_fd);
926 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: %s: deferred auth: PAM %s", up->username,
927 pam_success ? "succeeded" : "rejected");
928 exit(0);
929}
930
931/*
932 * Background process -- runs with privilege.
933 */
934static void
935pam_server(int fd, const char *service, int verb, const struct name_value_list *name_value_list)
936{
937 struct user_pass up;
938 char ac_file_name[PATH_MAX];
939 int command;
940#ifdef USE_PAM_DLOPEN
941 static const char pam_so[] = "libpam.so";
942#endif
943
944 /*
945 * Do initialization
946 */
947 if (DEBUG(verb))
948 {
949 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: INIT service='%s'", service);
950 }
951
952#ifdef USE_PAM_DLOPEN
953 /*
954 * Load PAM shared object
955 */
956 if (!dlopen_pam(pam_so))
957 {
958 plugin_log(PLOG_ERR, MODULE, "BACKGROUND: could not load PAM lib %s: %s", pam_so,
959 dlerror());
961 goto done;
962 }
963#endif
964
965 /*
966 * Tell foreground that we initialized successfully
967 */
969 {
970 plugin_log(PLOG_ERR | PLOG_ERRNO, MODULE, "BACKGROUND: write error on response socket [1]");
971 goto done;
972 }
973
974 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: initialization succeeded");
975
976 /*
977 * Event loop
978 */
979 while (1)
980 {
981 memset(&up, 0, sizeof(up));
982 up.verb = verb;
984
985 /* get a command from foreground process */
986 command = recv_control(fd);
987
988 if (DEBUG(verb))
989 {
990 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: received command code: %d", command);
991 }
992
993 switch (command)
994 {
995 case COMMAND_VERIFY:
996 if (recv_string(fd, up.username, sizeof(up.username)) == -1
997 || recv_string(fd, up.password, sizeof(up.password)) == -1
998 || recv_string(fd, up.common_name, sizeof(up.common_name)) == -1
999 || recv_string(fd, ac_file_name, sizeof(ac_file_name)) == -1
1000 || recv_string(fd, up.remote, sizeof(up.remote)) == -1)
1001 {
1003 "BACKGROUND: read error on command channel: code=%d, exiting",
1004 command);
1005 goto done;
1006 }
1007
1008 if (DEBUG(verb))
1009 {
1010#if 0
1011 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: USER/PASS: %s/%s",
1012 up.username, up.password);
1013#else
1014 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: USER: %s", up.username);
1015 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: REMOTE: %s", up.remote);
1016#endif
1017 }
1018
1019 /* If password is of the form SCRV1:base64:base64 split it up */
1021
1022 /* client wants deferred auth
1023 */
1024 if (strlen(ac_file_name) > 0)
1025 {
1026 do_deferred_pam_auth(fd, ac_file_name, service, &up);
1027 break;
1028 }
1029
1030
1031 /* non-deferred auth: wait for pam result and send
1032 * result back via control socketpair
1033 */
1034 if (pam_auth(service, &up)) /* Succeeded */
1035 {
1037 {
1039 "BACKGROUND: write error on response socket [2]");
1040 goto done;
1041 }
1042 }
1043 else /* Failed */
1044 {
1045 if (send_control(fd, RESPONSE_VERIFY_FAILED) == -1)
1046 {
1048 "BACKGROUND: write error on response socket [3]");
1049 goto done;
1050 }
1051 }
1052 plugin_secure_memzero(up.password, sizeof(up.password));
1053 break;
1054
1055 case COMMAND_EXIT:
1056 goto done;
1057
1058 case -1:
1060 "BACKGROUND: read error on command channel");
1061 goto done;
1062
1063 default:
1064 plugin_log(PLOG_ERR, MODULE, "BACKGROUND: unknown command code: code=%d, exiting",
1065 command);
1066 goto done;
1067 }
1068 plugin_secure_memzero(up.response, sizeof(up.response));
1069 }
1070done:
1071 plugin_secure_memzero(up.password, sizeof(up.password));
1072 plugin_secure_memzero(up.response, sizeof(up.response));
1073#ifdef USE_PAM_DLOPEN
1074 dlclose_pam();
1075#endif
1076 if (DEBUG(verb))
1077 {
1078 plugin_log(PLOG_NOTE, MODULE, "BACKGROUND: EXIT");
1079 }
1080
1081 return;
1082}
OPENVPN_EXPORT int openvpn_plugin_func_v1(openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
This function is called by OpenVPN each time the OpenVPN reaches a point where plug-in calls should h...
Definition auth-pam.c:536
static void set_signals(void)
Definition auth-pam.c:281
#define RESPONSE_DEFER
Definition auth-pam.c:66
OPENVPN_EXPORT void openvpn_plugin_close_v1(openvpn_plugin_handle_t handle)
This cleans up the last part of the plug-in, allows it to shut down cleanly and release the plug-in g...
Definition auth-pam.c:615
static plugin_log_t plugin_log
Definition auth-pam.c:69
static int pam_auth(const char *service, const struct user_pass *up)
Definition auth-pam.c:812
#define N_NAME_VALUE
Definition auth-pam.c:101
static int name_value_match(const char *query, const char *match)
Definition auth-pam.c:296
static char * MODULE
Definition auth-pam.c:74
#define COMMAND_VERIFY
Definition auth-pam.c:58
#define RESPONSE_INIT_SUCCEEDED
Definition auth-pam.c:62
static plugin_secure_memzero_t plugin_secure_memzero
Definition auth-pam.c:70
static void do_deferred_pam_auth(int fd, const char *ac_file_name, const char *service, const struct user_pass *up)
Definition auth-pam.c:870
#define RESPONSE_INIT_FAILED
Definition auth-pam.c:63
static ssize_t send_string(int fd, const char *string)
Definition auth-pam.c:194
static plugin_base64_decode_t plugin_base64_decode
Definition auth-pam.c:71
static void pam_server(int fd, const char *service, int verb, const struct name_value_list *name_value_list)
Definition auth-pam.c:935
#define RESPONSE_VERIFY_FAILED
Definition auth-pam.c:65
OPENVPN_EXPORT void openvpn_plugin_abort_v1(openvpn_plugin_handle_t handle)
Definition auth-pam.c:646
static int recv_control(int fd)
Definition auth-pam.c:142
#define COMMAND_EXIT
Definition auth-pam.c:59
static int send_control(int fd, int code)
Definition auth-pam.c:158
static void close_fds_except(int keep)
Definition auth-pam.c:263
OPENVPN_EXPORT int openvpn_plugin_open_v3(const int v3structver, struct openvpn_plugin_args_open_in const *args, struct openvpn_plugin_args_open_return *ret)
This function is called when OpenVPN loads the plug-in.
Definition auth-pam.c:370
#define RESPONSE_VERIFY_SUCCEEDED
Definition auth-pam.c:64
#define DEBUG(verb)
Definition auth-pam.c:55
static int my_conv(int num_msg, const struct pam_message **msg_array, struct pam_response **response_array, void *appdata_ptr)
Definition auth-pam.c:663
static ssize_t recv_string(int fd, char *buffer, size_t len)
Definition auth-pam.c:173
static void split_scrv1_password(struct user_pass *up)
Definition auth-pam.c:315
int string_array_len(const char **array)
Return the number of elements in a NULL-terminated array of strings.
Definition buffer.c:663
int daemon(int nochdir, int noclose)
static void daemonize(const char *envp[])
Definition down-root.c:163
static SERVICE_STATUS status
Definition interactive.c:52
@ write
@ read
static SERVICE_STATUS_HANDLE service
Definition interactive.c:51
@ PLOG_NOTE
@ PLOG_ERRNO
@ PLOG_ERR
#define OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY
void * openvpn_plugin_handle_t
#define OPENVPN_PLUGIN_MASK(x)
void(* plugin_log_t)(openvpn_plugin_log_flags_t flags, const char *plugin_name, const char *format,...) _ovpn_chk_fmt(3
#define OPENVPN_PLUGIN_FUNC_DEFERRED
#define OPENVPN_PLUGIN_FUNC_SUCCESS
void(*) typedef void(*) typedef void(* plugin_secure_memzero_t)(void *data, size_t len)
Export of secure_memzero() to be used inside plug-ins.
#define OPENVPN_EXPORT
#define OPENVPN_PLUGIN_FUNC_ERROR
int(* plugin_base64_decode_t)(const char *str, void *data, int size)
Export of openvpn_base64_decode() to be used inside plug-ins.
#define msg(flags,...)
Definition error.h:152
static const char * get_env(const char *name, const char *envp[])
Definition argv.h:35
char ** argv
Definition argv.h:39
pid_t background_pid
Definition auth-pam.c:85
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Contains all state information for one tunnel.
Definition openvpn.h:445
struct name_value data[N_NAME_VALUE]
Definition auth-pam.c:112
const char * name
Definition auth-pam.c:105
const char * value
Definition auth-pam.c:106
Arguments used to transport variables to the plug-in.
struct openvpn_plugin_callbacks * callbacks
Arguments used to transport variables from the plug-in back to the OpenVPN process.
openvpn_plugin_handle_t handle
plugin_base64_decode_t plugin_base64_decode
plugin_secure_memzero_t plugin_secure_memzero
char common_name[128]
Definition auth-pam.c:125
char response[128]
Definition auth-pam.c:126
const struct name_value_list * name_value_list
Definition auth-pam.c:129
char password[USER_PASS_LEN]
Definition misc.h:71
char remote[INET6_ADDRSTRLEN]
Definition auth-pam.c:127
char username[USER_PASS_LEN]
Definition misc.h:70
#define SIGHUP
Definition syshead.h:55
#define SIGINT
Definition syshead.h:56
#define SIGTERM
Definition syshead.h:59
#define SIGUSR1
Definition syshead.h:57
#define SIGUSR2
Definition syshead.h:58
char * searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
Read 'tosearch', replace all occurrences of 'searchfor' with 'replacewith' and return a pointer to th...
Definition utils.c:42