Skip to contentSkip to conversation

PAM plugin doesn't daemonize correctly

Openvpn-devel Simon Perreault 2 participants 3 messages May 18, 2005 – May 19, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

The close_fds_except() function in the auth-pam plugin contains a bug, but it seems to be by design. It doesn't close standard fds (stdin, stdout, stderr). This means that a program that starts openvpn and reads its stdout from a pipe will never receive EOF and will idle forever.

To reproduce: with a configuration using the auth-pam plugin, this command never returns:

/etc/init.d/openvpn start | cat

ps alx shows:

1 0 4764 1 16 0 5984 1564 - S+ pts/19
0:00 /usr/sbin/openvpn
5 99 4770 1 15 0 5988 1964 - Ss ?
0:00 /usr/sbin/openvpn

The process with PID 4764 is the pam server because it has UID 0. Sending it a kill -9 makes the command return. Sending the real openvpn process (PID 4770) a kill -9 does not make the command return, as expected. This confirms that it's the pam server process that is at fault.

Solution: either
1) reopen stdin, stdout and stderr to /dev/null when --daemon option is specified (need a way to access options structure in the plugin)
2) start plugins after becoming a daemon in init_instance()

What do you think?

#1 lore raw

[Openvpn-devel] Re: PAM plugin doesn't daemonize correctly [PATCH]

Quoted 2 lines · Simon Perreault

1) reopen stdin, stdout and stderr to /dev/null when --daemon option is specified (need a way to access options structure in the plugin)

Here's a patch using the first solution. Please consider applying.

Index: options.c
===================================================================
RCS file: /cvsroot/openvpn/openvpn/options.c,v
retrieving revision 1.62

options.c +1 -0
diff -u -p -r1.62 options.c
@@ -622,6 +622,7 @@ setenv_settings (struct env_set *es, con
   setenv_str (es, "local", o->local);
   setenv_int (es, "local_port", o->local_port);
   setenv_int (es, "verb", o->verbosity);
+  setenv_int (es, "daemon", o->daemon);
 
   if (o->remote_list)
     {

Index: plugin/auth-pam/auth-pam.c
===================================================================
RCS file: /cvsroot/openvpn/openvpn/plugin/auth-pam/auth-pam.c,v retrieving revision 1.5

auth-pam/auth-pam.c +10 -0
diff -u -p -r1.5 auth-pam.c
@@ -373,6 +373,16 @@ openvpn_plugin_open_v1 (unsigned int *ty
       /* Ignore most signals (the parent will receive them) */
       set_signals ();
 
+      /*
+       * Daemonize if --daemon option is set.
+       */
+      {
+        const char *daemon_string = get_env ("daemon", envp);
+        if (daemon_string && daemon_string[0] == '1')
+          if (daemon (0, 0) < 0)
+            perror ("AUTH-PAM: daemonization failed");
+      }
+
       /* execute the event loop */
       pam_server (fd[1], argv[1], context->verb, &name_value_list);
 
#2 lore raw

Re: [Openvpn-devel] Re: PAM plugin doesn't daemonize correctly [PATCH]

Quoted 5 lines · Simon Perreault
Quoted 2 lines · Simon Perreault

1) reopen stdin, stdout and stderr to /dev/null when --daemon option is specified (need a way to access options structure in the plugin)

Here's a patch using the first solution. Please consider applying.

Simon,

Thanks for the problem analysis and patch. I agree that this is a problem that should be fixed, and I think your patch does a good job of fixing it with a minimal footprint. I plan to merge for 2.0.1-rc3.

James