Skip to contentSkip to conversation

Why must PAM library be dlopened?

Openvpn-devel Simon Perreault 4 participants 31 messages Jun 14, 2005 – Jun 26, 2005 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi,

I am wondering why the PAM library is dlopened by the auth-pam plugin. Why can't it simply be linked with it?

Thanks for not making fun of me for asking this stupid question. ;)

#1 lore raw
Quoted 2 lines · Simon Perreault

I am wondering why the PAM library is dlopened by the auth-pam plugin. Why can't it simply be linked with it?

More specifically, why can't the attached patch be applied?

Index: Makefile
===================================================================
RCS file: /cvsroot/openvpn/openvpn/plugin/auth-pam/Makefile,v retrieving revision 1.1

Makefile +3 -6
diff -u -p -r1.1 Makefile
@@ -7,14 +7,11 @@ INCLUDE=-I../..
 
 CC_FLAGS=-O2 -Wall
 
-openvpn-auth-pam.so : auth-pam.o pamdl.o
-	gcc ${CC_FLAGS} -fPIC -shared -Wl,-soname,openvpn-auth-pam.so -o openvpn-auth-pam.so auth-pam.o pamdl.o -lc
+openvpn-auth-pam.so : auth-pam.o
+	gcc ${CC_FLAGS} -fPIC -shared -Wl,-soname,openvpn-auth-pam.so -o openvpn-auth-pam.so auth-pam.o -lc -lpam
 
-auth-pam.o : auth-pam.c pamdl.h
+auth-pam.o : auth-pam.c
 	gcc ${CC_FLAGS} -fPIC -c ${INCLUDE} auth-pam.c
 
-pamdl.o : pamdl.c pamdl.h
-	gcc ${CC_FLAGS} -fPIC -c ${INCLUDE} pamdl.c
-
 clean :
 	rm -f *.o *.so

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

auth-pam.c +2 -14
diff -u -p -r1.6 auth-pam.c
@@ -27,6 +27,8 @@
  * privilege model.
  */
 
+#include <security/pam_appl.h>
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -37,11 +39,9 @@
 #include <sys/wait.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <dlfcn.h>
 #include <syslog.h>
 
 #include "openvpn-plugin.h"
-#include "pamdl.h"
 
 #define DEBUG(verb) ((verb) >= 4) // JYFIXME (should be 7)
 
@@ -646,7 +646,6 @@ pam_server (int fd, const char *service,
 {
   struct user_pass up;
   int command;
-  static const char pam_so[] = "libpam.so";
 
   /*
    * Do initialization
@@ -655,16 +654,6 @@ pam_server (int fd, const char *service,
     fprintf (stderr, "AUTH-PAM: BACKGROUND: INIT service='%s'\n", service);
 
   /*
-   * Load PAM shared object
-   */
-  if (!dlopen_pam (pam_so))
-    {
-      fprintf (stderr, "AUTH-PAM: BACKGROUND: could not load PAM lib %s: %s\n", pam_so, dlerror());
-      send_control (fd, RESPONSE_INIT_FAILED);
-      goto done;
-    }
-
-  /*
    * Tell foreground that we initialized successfully
    */
   if (send_control (fd, RESPONSE_INIT_SUCCEEDED) == -1)
@@ -736,7 +725,6 @@ pam_server (int fd, const char *service,
     }
  done:
 
-  dlclose_pam ();
   if (DEBUG (verb))
     fprintf (stderr, "AUTH-PAM: BACKGROUND: EXIT\n");
 
#2 lore raw
Quoted 5 lines · Simon Perreault
Quoted 2 lines · Simon Perreault

I am wondering why the PAM library is dlopened by the auth-pam plugin. Why can't it simply be linked with it?

More specifically, why can't the attached patch be applied?

When I was writing the auth-pam plugin, I had problems dynamically linking the plugin to PAM unless OpenVPN itself was also dynamically linked to PAM (which I didn't want to do). Remember that PAM itself is going to dlopen, in turn, the actual PAM module. Combining dlopen with dynamic linking has got to be problematic when you are dealing with crosslinked dependencies. dlopening PAM from the plugin seemed to solve all the problems, and followed the approach that others took when accessing PAM from dynamic modules.

James

#3 lore raw
Quoted 3 lines · James Yonan

When I was writing the auth-pam plugin, I had problems dynamically linking the plugin to PAM unless OpenVPN itself was also dynamically linked to PAM (which I didn't want to do).

What kind of problem did you encounter? It seems to work perfectly for me.

Quoted 3 lines

Remember that PAM itself is going to dlopen,
in turn, the actual PAM module. Combining dlopen with dynamic linking has got to be problematic when you are dealing with crosslinked dependencies.

I'm not sure what you mean by "crosslinked dependencies". The way I see it is:

openvpn --(dlopen)--> auth-pam --(?)--> libpam.so --(dlopen)--> pam_unix.so

I don't see why (?) should be (dlopen). Can you elaborate?

dlopen(3) says:

If the library has dependencies on other shared libraries, then these are also automatically loaded by the dynamic linker using the same rules. (This process may occur recursively, if those libraries in turn have depen- dencies, and so on.)

Those rules are the same as used by ld.so, so there should be no "not found" problems.

Quoted 3 lines

dlopening PAM from the plugin seemed to solve all the problems, and followed the approach that others took when accessing PAM from dynamic modules.

Can you name examples?

The basic problem I am trying to solve (no, I'm not rummaging through this only because I have nothing better to do) is that auth-pam depends on "libpam.so" instead of "libpam.so.0.77". This means that I would need to add a dependency on pam-devel to my openvpn RPM instead of pam. This seems wrong.

I've always believed that dlopen should only be used to open libraries shipped along with the calling binary, such as plugins. I'm trying to verify that with you.

#4 lore raw
Quoted 19 lines · Simon Perreault
Quoted 3 lines · James Yonan

When I was writing the auth-pam plugin, I had problems dynamically linking the plugin to PAM unless OpenVPN itself was also dynamically linked to PAM (which I didn't want to do).

What kind of problem did you encounter? It seems to work perfectly for me.

Quoted 3 lines

Remember that PAM itself is going to dlopen,
in turn, the actual PAM module. Combining dlopen with dynamic linking has got to be problematic when you are dealing with crosslinked dependencies.

I'm not sure what you mean by "crosslinked dependencies". The way I see it is:

openvpn --(dlopen)--> auth-pam --(?)--> libpam.so --(dlopen)--> pam_unix.so

I don't see why (?) should be (dlopen). Can you elaborate?

dlopen(3) says:

Quoted 7 lines

If the library has dependencies on other shared libraries, then these are also automatically loaded by the dynamic linker using the same rules. (This process may occur recursively, if those libraries in turn have depen- dencies, and so on.)

Those rules are the same as used by ld.so, so there should be no "not found" problems.

Despite what the dlopen man page says, I had problems with shared library linkage from libpam.so --(dlopen)--> pam_unix.so when ?=dlopen (the pam lib couldn't find the plugin). It only seemed to work if I built OpenVPN itself with -lpam. I didn't figure out what what causing the problem, so it's possible that I wasn't doing something right. I didn't want OpenVPN itself to have the pam dependency, only the auth-pam plugin.

Quoted 10 lines
Quoted 3 lines

dlopening PAM from the plugin seemed to solve all the problems, and followed the approach that others took when accessing PAM from dynamic modules.

Can you name examples?

The basic problem I am trying to solve (no, I'm not rummaging through this only because I have nothing better to do) is that auth-pam depends on "libpam.so" instead of "libpam.so.0.77". This means that I would need to add a dependency on pam-devel to my openvpn RPM instead of pam. This seems wrong.

Why is this? We already have an RPM spec file for OpenVPN and it builds binary RPMs which include the auth-pam plugin and doesn't have a pam-devel dependency. (There's a pam-devel dependency only for building the binary RPM itself)

Quoted 3 lines

I've always believed that dlopen should only be used to open libraries shipped along with the calling binary, such as plugins. I'm trying to verify that with you.

If you can figure out how to do it without dlopen, feel free to submit a patch.

James

#5 lore raw
Quoted 6 lines · James Yonan

Despite what the dlopen man page says, I had problems with shared library linkage from libpam.so --(dlopen)--> pam_unix.so when ?=dlopen (the pam lib couldn't find the plugin). It only seemed to work if I built OpenVPN itself with -lpam. I didn't figure out what what causing the problem, so it's possible that I wasn't doing something right. I didn't want OpenVPN itself to have the pam dependency, only the auth-pam plugin.

I think I know what the problem was. dlopen() wasn't finding libpam.so (ie. pam-devel wasn't installed, more about this later). But because you were linking openvpn with libpam.so.0.77, which contains the symbols libpam.so contains, it wasn't failing. Linking openvpn with libpam.so.0.77 was a (wrong) solution to dlopen not finding libpam.so.

I now firmly believe there is no technical reason to dlopen libpam.so from the auth-pam plugin. Please consider applying the patch I previously sent. Try to reproduce any problem you had. I have tried to but couldn't find any flaw.

Quoted 4 lines

Why is this? We already have an RPM spec file for OpenVPN and it builds binary RPMs which include the auth-pam plugin and doesn't have a pam-devel dependency. (There's a pam-devel dependency only for building the binary RPM itself)

...which is wrong. If you try to use the auth-pam plugin when pam-devel isn't installed, it will not find libpam.so.

.so's are only included in -devel packages because their only use is when linking. Because linking is intended to be done at compile time (not dynamically as when using dlopen()), .so's make sense only in the context of a -devel package. The only .so's you will find not in -devel packages are plugins. For example, you will find .so's of PAM plugins in the pam package for this reason.

dlopen() should only be used on libraries *expecting* to be dlopened.

Quoted 2 lines

If you can figure out how to do it without dlopen, feel free to submit a patch.

Please take a look at the patch I sent previously, you seem to have overlooked it. It contains all the necessary changes.

Signature · 1 line
Simon Perreault <[email protected]...> -- http://nomis80.org
#6 lore raw
Quoted 5 lines · Simon Perreault

...which is wrong. If you try to use the auth-pam plugin when pam-devel isn't installed, it will not find libpam.so.

.so's are only included in -devel packages because their only use is when linking.

False. The linker, when linking a shared object, just writes a line "bind library libfoo.so.4.2 at run time" into the program, and this exact library (for ELF, use readelf or objdump to look for "SONAME" and "NEEDED" attributes) is then linked when the program is run.

The .so file without .4.2 suffix (the exact numbers are an arbitrary example here) is indeed only needed at link time, but the variant that matches the SONAME of the library that was linked against is also needed at runtime.

Emulating the library search characteristics of the particular system's linker is a different task though, and doing this incompletely can indeed cause trouble finding runtime libraries.

So you may have:

libfoo-1.2.3-4.rpm containing:
lib/libfoo.so.4.2
man/man7/foo.7.gz
share/doc/foo/README

libfoo-devel-1.2.3-4.rpm containing:
lib/libfoo.a (static lib)
lib/libfoo.so (convenience link to libfoo.so.4.2)
man/man3/foo.3.gz
include/foo.h

Signature · 1 line
Matthias Andree
#7 lore raw
Quoted 4 lines · Matthias Andree
Quoted 2 lines

.so's are only included in -devel packages because their only use is when linking.

False.

True. Let me explain why.

Quoted 4 lines

The linker, when linking a shared object, just writes a line "bind library libfoo.so.4.2 at run time" into the program, and this exact library (for ELF, use readelf or objdump to look for "SONAME" and "NEEDED" attributes) is then linked when the program is run.

That is true.

Quoted 4 lines

The .so file without .4.2 suffix (the exact numbers are an arbitrary example here) is indeed only needed at link time, but the variant that matches the SONAME of the library that was linked against is also needed at runtime.

That is also true.

Quoted 3 lines

Emulating the library search characteristics of the particular system's linker is a different task though, and doing this incompletely can indeed cause trouble finding runtime libraries.

That's also true.

Quoted 7 lines

So you may have:

libfoo-1.2.3-4.rpm containing:
lib/libfoo.so.4.2
man/man7/foo.7.gz
share/doc/foo/README

Quoted 5 lines

libfoo-devel-1.2.3-4.rpm containing:
lib/libfoo.a (static lib)
lib/libfoo.so (convenience link to libfoo.so.4.2)
man/man3/foo.3.gz
include/foo.h

That's also true.

You haven't explained why my statement ".so's are only included in -devel packages because their only use is when linking" is false. I have agreed with everything you have written, and I seem to be missing that critical part. You have even provided an example where libfoo.so is in the -devel rpm, just as I said.

#9 lore raw
In reply to Simon Perreault
Quoted 5 lines · Simon Perreault
Quoted 2 lines · Simon Perreault

I am wondering why the PAM library is dlopened by the auth-pam plugin. Why can't it simply be linked with it?

More specifically, why can't the attached patch be applied?

When I apply your patch and run on SuSE 9.1 with the default "login" PAM module (pam-0.77-221):

plugin openvpn-auth-pam.so "login login USERNAME password PASSWORD"

I get:

AUTH-PAM: BACKGROUND: user 'x' failed to authenticate: Module is unknown

Without the patch, the same test runs fine.

James

#10 lore raw
Quoted 8 lines · James Yonan

When I apply your patch and run on SuSE 9.1 with the default "login" PAM module (pam-0.77-221):

plugin openvpn-auth-pam.so "login login USERNAME password PASSWORD"

I get:

AUTH-PAM: BACKGROUND: user 'x' failed to authenticate: Module is unknown

I've determined that this is a bug due to the old version of pam that is present in SUSE 9.1. It was fixed in version 1.5 of the pam_unix.so Makefile, which you can see here:
http://cvs.sourceforge.net/viewcvs.py/pam/Linux-PAM/modules/pam_unix/Makefile?view=log#rev1.5.
The comment says:

"Patch from Sam Hartman to fix handling of module linking on Linux:
modules should be linked against libpam if they call any functions from the library, since not all platforms will correctly resolve symbol references otherwise."

In the /var/log/messages file, pam writes this when it fails to dlopen its modules:

Jun 21 03:28:25 (none) openvpn: PAM unable to 
dlopen(/lib/security/pam_unix2.so)
Jun 21 03:28:25 (none) openvpn: PAM [dlerror: /lib/security/pam_unix2.so: 
undefined symbol: pam_get_item]
Jun 21 03:28:25 (none) openvpn: PAM adding faulty 
module: /lib/security/pam_unix2.so
Jun 21 03:28:25 (none) openvpn: PAM unable to 
dlopen(/lib/security/pam_pwcheck.so)
Jun 21 03:28:25 (none) openvpn: PAM [dlerror: /lib/security/pam_pwcheck.so: 
undefined symbol: pam_get_item]
Jun 21 03:28:25 (none) openvpn: PAM adding faulty 

module: /lib/security/pam_pwcheck.so

You can see that the dlopening of PAM modules fails because it is not resolving the pam_get_item symbol. That symbol is used by PAM modules and not by any openvpn stuff. That should be no problem right? Because we're linking against libpam.so anyway?

The problem is that PAM modules should not have undefined symbols, even if it is assumed that the main application is linked with libpam.so. If a PAM module uses functions from libpam.so, it should link with it. It is easy to see that this is not the case on SUSE 9.1:

poste10-161:/lib/security # ldd -r pam_unix.so

    linux-gate.so.1 =>  (0xffffe000)
    libnsl.so.1 => /lib/libnsl.so.1 (0x4001a000)
    libdl.so.2 => /lib/libdl.so.2 (0x4002f000)
    libxcrypt.so.1 => /lib/libxcrypt.so.1 (0x40033000)
    libselinux.so.1 => /lib/libselinux.so.1 (0x40067000)
    libc.so.6 => /lib/tls/libc.so.6 (0x40075000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

undefined symbol: pam_get_item (./pam_unix.so)
undefined symbol: pam_set_data (./pam_unix.so)
undefined symbol: pam_putenv (./pam_unix.so)
undefined symbol: pam_set_item (./pam_unix.so)
undefined symbol: pam_get_data (./pam_unix.so)
undefined symbol: pam_get_user (./pam_unix.so)

Compare that output from this one, from Fedora Core 4, which has no missing symbols because it is linked with libpam.so.

[[email protected]... security]$ ldd -r pam_unix.so

        linux-gate.so.1 =>  (0x001a3000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x00ea5000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x007b5000)
        libselinux.so.1 => /lib/libselinux.so.1 (0x00643000)
        libc.so.6 => /lib/libc.so.6 (0x004df000)
        libpam.so.0 => /lib/libpam.so.0 (0x003f3000)
        /lib/ld-linux.so.2 (0x0068a000)
        libdl.so.2 => /lib/libdl.so.2 (0x00281000)
        libaudit.so.0 => /lib/libaudit.so.0 (0x00f44000)

FC4's pam is 0.79 while SUSE 9.1's is 0.77. I would guess that my patch would work on a more recent SUSE, where pam's bug would be fixed.

#11 lore raw
Quoted 76 lines · Simon Perreault
Quoted 8 lines · James Yonan

When I apply your patch and run on SuSE 9.1 with the default "login" PAM module (pam-0.77-221):

plugin openvpn-auth-pam.so "login login USERNAME password PASSWORD"

I get:

AUTH-PAM: BACKGROUND: user 'x' failed to authenticate: Module is unknown

I've determined that this is a bug due to the old version of pam that is present in SUSE 9.1. It was fixed in version 1.5 of the pam_unix.so Makefile, which you can see here:
http://cvs.sourceforge.net/viewcvs.py/pam/Linux-PAM/modules/pam_unix/Makefile?view=log#rev1.5.
The comment says:

"Patch from Sam Hartman to fix handling of module linking on Linux:
modules should be linked against libpam if they call any functions from the library, since not all platforms will correctly resolve symbol references otherwise."

In the /var/log/messages file, pam writes this when it fails to dlopen its modules:

Jun 21 03:28:25 (none) openvpn: PAM unable to 
dlopen(/lib/security/pam_unix2.so)
Jun 21 03:28:25 (none) openvpn: PAM [dlerror: /lib/security/pam_unix2.so: 
undefined symbol: pam_get_item]
Jun 21 03:28:25 (none) openvpn: PAM adding faulty 
module: /lib/security/pam_unix2.so
Jun 21 03:28:25 (none) openvpn: PAM unable to 
dlopen(/lib/security/pam_pwcheck.so)
Jun 21 03:28:25 (none) openvpn: PAM [dlerror: /lib/security/pam_pwcheck.so: 
undefined symbol: pam_get_item]
Jun 21 03:28:25 (none) openvpn: PAM adding faulty 

module: /lib/security/pam_pwcheck.so

You can see that the dlopening of PAM modules fails because it is not resolving the pam_get_item symbol. That symbol is used by PAM modules and not by any openvpn stuff. That should be no problem right? Because we're linking against libpam.so anyway?

The problem is that PAM modules should not have undefined symbols, even if it is assumed that the main application is linked with libpam.so. If a PAM module uses functions from libpam.so, it should link with it. It is easy to see that this is not the case on SUSE 9.1:

poste10-161:/lib/security # ldd -r pam_unix.so

    linux-gate.so.1 =>  (0xffffe000)
    libnsl.so.1 => /lib/libnsl.so.1 (0x4001a000)
    libdl.so.2 => /lib/libdl.so.2 (0x4002f000)
    libxcrypt.so.1 => /lib/libxcrypt.so.1 (0x40033000)
    libselinux.so.1 => /lib/libselinux.so.1 (0x40067000)
    libc.so.6 => /lib/tls/libc.so.6 (0x40075000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

undefined symbol: pam_get_item (./pam_unix.so)
undefined symbol: pam_set_data (./pam_unix.so)
undefined symbol: pam_putenv (./pam_unix.so)
undefined symbol: pam_set_item (./pam_unix.so)
undefined symbol: pam_get_data (./pam_unix.so)
undefined symbol: pam_get_user (./pam_unix.so)

Compare that output from this one, from Fedora Core 4, which has no missing symbols because it is linked with libpam.so.

[[email protected]... security]$ ldd -r pam_unix.so

        linux-gate.so.1 =>  (0x001a3000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x00ea5000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x007b5000)
        libselinux.so.1 => /lib/libselinux.so.1 (0x00643000)
        libc.so.6 => /lib/libc.so.6 (0x004df000)
        libpam.so.0 => /lib/libpam.so.0 (0x003f3000)
        /lib/ld-linux.so.2 (0x0068a000)
        libdl.so.2 => /lib/libdl.so.2 (0x00281000)
        libaudit.so.0 => /lib/libaudit.so.0 (0x00f44000)
Quoted 2 lines

FC4's pam is 0.79 while SUSE 9.1's is 0.77. I would guess that my patch would work on a more recent SUSE, where pam's bug would be fixed.

Simon,

Thanks for the analysis. Because of the PAM bug, I think that openvpn-auth-pam will need to support the dlopen workaround for some time to come. Maybe the solution would be to set up an alternative Makefile target that builds according to your patch, but leave the default for now as is.

James

#12 lore raw
Quoted 5 lines · James Yonan

Thanks for the analysis. Because of the PAM bug, I think that openvpn-auth-pam will need to support the dlopen workaround for some time to come. Maybe the solution would be to set up an alternative Makefile target that builds according to your patch, but leave the default for now as is.

This could be run and determined automatically by the autoconf/automake couple, automake supports conditional compilation (i. e. depending on autoconf findings, link another module into a program, or something like that).

Signature · 1 line
Matthias Andree
#15 lore raw
In reply to James Yonan
Quoted 3 lines · James Yonan

Thanks for the analysis. Because of the PAM bug, I think that openvpn-auth-pam will need to support the dlopen workaround for some time to come.

Yes, I have been thinking the same.

Quoted 3 lines

Maybe the solution would be to set up an alternative Makefile target that builds according to your patch, but leave the default for now as is.

Great idea. You'll find a new patch attached that does just this. A variable in the Makefile, DLOPEN_PAM, controls whether libpam.so is dlopened or linked in directly. I've set it to zero by default, and I suggest that you leave it that way. This makes the default Makefile work with newer, not broken PAM. The rationale is that you should have more work to do when you want to use a broken PAM than when you use a fixed one. And because broken PAM will be less often encountered as they are obsoleted by newer distro releases, this is the natural thing to do.

A good day to you!

Index: plugin/auth-pam/Makefile
===================================================================
RCS file: /cvsroot/openvpn/openvpn/plugin/auth-pam/Makefile,v retrieving revision 1.1

auth-pam/Makefile +12 -2
diff -u -p -r1.1 Makefile
@@ -2,13 +2,23 @@
 # Build the OpenVPN auth-pam plugin module.
 #
 
+# If PAM modules are not linked against libpam.so, set DLOPEN_PAM to 1. This
+# must be done on SUSE 9.1, at least.
+DLOPEN_PAM=0
+
+ifeq ($(DLOPEN_PAM),1)
+	LIBPAM=-ldl
+else
+	LIBPAM=-lpam
+endif
+
 # This directory is where we will look for openvpn-plugin.h
 INCLUDE=-I../..
 
-CC_FLAGS=-O2 -Wall
+CC_FLAGS=-O2 -Wall -DDLOPEN_PAM=$(DLOPEN_PAM)
 
 openvpn-auth-pam.so : auth-pam.o pamdl.o
-	gcc ${CC_FLAGS} -fPIC -shared -Wl,-soname,openvpn-auth-pam.so -o openvpn-auth-pam.so auth-pam.o pamdl.o -lc
+	gcc ${CC_FLAGS} -fPIC -shared -Wl,-soname,openvpn-auth-pam.so -o openvpn-auth-pam.so auth-pam.o pamdl.o -lc $(LIBPAM)
 
 auth-pam.o : auth-pam.c pamdl.h
 	gcc ${CC_FLAGS} -fPIC -c ${INCLUDE} auth-pam.c

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

auth-pam/auth-pam.c +13 -2
diff -u -p -r1.7 auth-pam.c
@@ -27,6 +27,13 @@
  * privilege model.
  */
 
+#if DLOPEN_PAM
+#include <dlfcn.h>
+#include "pamdl.h"
+#else
+#include <security/pam_appl.h>
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -37,11 +44,9 @@
 #include <sys/wait.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <dlfcn.h>
 #include <syslog.h>
 
 #include "openvpn-plugin.h"
-#include "pamdl.h"
 
 #define DEBUG(verb) ((verb) >= 7)
 
@@ -646,7 +651,9 @@ pam_server (int fd, const char *service,
 {
   struct user_pass up;
   int command;
+#if DLOPEN_PAM
   static const char pam_so[] = "libpam.so";
+#endif
 
   /*
    * Do initialization
@@ -654,6 +661,7 @@ pam_server (int fd, const char *service,
   if (DEBUG (verb))
     fprintf (stderr, "AUTH-PAM: BACKGROUND: INIT service='%s'\n", service);
 
+#if DLOPEN_PAM
   /*
    * Load PAM shared object
    */
@@ -663,6 +671,7 @@ pam_server (int fd, const char *service,
       send_control (fd, RESPONSE_INIT_FAILED);
       goto done;
     }
+#endif
 
   /*
    * Tell foreground that we initialized successfully
@@ -736,7 +745,9 @@ pam_server (int fd, const char *service,
     }
  done:
 
+#if DLOPEN_PAM
   dlclose_pam ();
+#endif
   if (DEBUG (verb))
     fprintf (stderr, "AUTH-PAM: BACKGROUND: EXIT\n");
 

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

auth-pam/pamdl.c +2 -0
diff -u -p -r1.2 pamdl.c
@@ -1,3 +1,4 @@
+#if DLOPEN_PAM
 /*
  * If you want to dynamically load libpam using dlopen() or something,
  * then dlopen( ' this shared object ' ); It takes care of exporting
@@ -177,3 +178,4 @@ int pam_chauthtok(pam_handle_t *pamh, in
     RESOLVE_PAM_FUNCTION(pam_chauthtok, int, (pam_handle_t *, int), PAM_ABORT);
     return real_pam_chauthtok(pamh, flags);
 }
+#endif

Index: plugin/auth-pam/pamdl.h
===================================================================
RCS file: /cvsroot/openvpn/openvpn/plugin/auth-pam/pamdl.h,v retrieving revision 1.1

auth-pam/pamdl.h +2 -0
diff -u -p -r1.1 pamdl.h
@@ -1,5 +1,7 @@
+#if DLOPEN_PAM
 #include <security/pam_appl.h>
 
 /* Dynamically load and unload the PAM library */
 int dlopen_pam (const char *so);
 void dlclose_pam (void);
+#endif
#13 lore raw
In reply to Matthias Andree
Quoted 4 lines · Matthias Andree

This could be run and determined automatically by the autoconf/automake couple, automake supports conditional compilation (i. e. depending on autoconf findings, link another module into a program, or something like that).

Yes, that would be nice. However, I personally don't have enough time to do this cleanly. The edit-the-makefile-if-you-have-a-broken-PAM solution satisfies me.

#16 lore raw
In reply to Simon Perreault

auth-pam/Makefile line 13

Quoted 9 lines · Simon Perreault
auth-pam/Makefile +9 -0
+# If PAM modules are not linked against libpam.so, set DLOPEN_PAM to 1. This
+# must be done on SUSE 9.1, at least.
+DLOPEN_PAM=0
+
+ifeq ($(DLOPEN_PAM),1)
+	LIBPAM=-ldl
+else
+	LIBPAM=-lpam
+endif

Is this portable to non-GNU makes?
If it is not, does OpenVPN require GNU make? I don't think it does.

Is there a broken-out C program that fails to link at run-time if the workaround is required? This would be tested from configure.{in|ac} and set DLOPEN_PAM appropriately.

Signature · 1 line
Matthias Andree
#17 lore raw
Quoted 2 lines · Matthias Andree

Is this portable to non-GNU makes?
If it is not, does OpenVPN require GNU make? I don't think it does.

I'm afraid it's non-portable. Damn.

Quoted 3 lines

Is there a broken-out C program that fails to link at run-time if the workaround is required? This would be tested from configure.{in|ac} and set DLOPEN_PAM appropriately.

If /lib/security/pam_unix.so is missing symbols then the workaround is required. We could try to link a program with /lib/security/pam_unix.so and if the linking fails (because symbols are missing) then the workaround is needed. Here's an example:

[[email protected]... ~]$ gcc main.c /lib/security/pam_unix.so [[email protected]... ~]$ gcc main.c /lib/security/pam_smb_auth.so /lib/security/pam_smb_auth.so: undefined reference to pam_get_item' /lib/security/pam_smb_auth.so: undefined reference to pam_set_item' /lib/security/pam_smb_auth.so: undefined reference to `pam_get_user'
collect2: ld returned 1 exit status

main.c contains this: int main() { return 0; }
On my system, pam_unix.so is not missing symbols while pam_smb_auth.so is.

I tried to find a way to check this using autoconf but I'm not very good with it. Maybe you can do it in a few minutes... ;)

#14 lore raw
In reply to Simon Perreault
Quoted 9 lines · Simon Perreault
Quoted 4 lines · Matthias Andree

This could be run and determined automatically by the autoconf/automake couple, automake supports conditional compilation (i. e. depending on autoconf findings, link another module into a program, or something like that).

Yes, that would be nice. However, I personally don't have enough time to do this cleanly. The edit-the-makefile-if-you-have-a-broken-PAM solution satisfies me.

I would hate to lose the simplicity of the Makefile approach for the plugin tree. I think one of the important reasons for the plugin tree in the first place is to provide a venue for extensions that doesn't inherit all of the build complexity of the main tree.

James

#8 lore raw
In reply to Simon Perreault
Quoted 5 lines · Simon Perreault

You haven't explained why my statement ".so's are only included in -devel packages because their only use is when linking" is false. I have agreed with everything you have written, and I seem to be missing that critical part. You have even provided an example where libfoo.so is in the -devel rpm, just as I said.

Sorry. I totally misread the problem that was discussed, and your assertions.

Signature · 1 line
Matthias Andree
#27 lore raw
In reply to Simon Perreault
Quoted 15 lines · Simon Perreault

poste10-161:/lib/security # ldd -r pam_unix.so

    linux-gate.so.1 =>  (0xffffe000)
    libnsl.so.1 => /lib/libnsl.so.1 (0x4001a000)
    libdl.so.2 => /lib/libdl.so.2 (0x4002f000)
    libxcrypt.so.1 => /lib/libxcrypt.so.1 (0x40033000)
    libselinux.so.1 => /lib/libselinux.so.1 (0x40067000)
    libc.so.6 => /lib/tls/libc.so.6 (0x40075000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

undefined symbol: pam_get_item (./pam_unix.so)
undefined symbol: pam_set_data (./pam_unix.so)
undefined symbol: pam_putenv (./pam_unix.so)
undefined symbol: pam_set_item (./pam_unix.so)
undefined symbol: pam_get_data (./pam_unix.so)
undefined symbol: pam_get_user (./pam_unix.so)

Quoted 2 lines

FC4's pam is 0.79 while SUSE 9.1's is 0.77. I would guess that my patch would work on a more recent SUSE, where pam's bug would be fixed.

Parts of your assumption about ldd -r or linkage of the PAM modules against libpam don't hold. I haven't yet managed to test the whole setup.

SUSE 9.2 w/ all patches: pam-0.77-227,
with this output for ldd -r /lib/security/pam_unix.so:

        linux-gate.so.1 =>  (0xffffe000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7fd0000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7fcb000)
        libxcrypt.so.1 => /lib/libxcrypt.so.1 (0xb7f97000)
        libselinux.so.1 => /lib/libselinux.so.1 (0xb7f89000)
        libc.so.6 => /lib/tls/libc.so.6 (0xb7e73000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)

undefined symbol: pam_get_item (/lib/security/pam_unix.so) undefined symbol: pam_set_data (/lib/security/pam_unix.so)
undefined symbol: pam_putenv (/lib/security/pam_unix.so)
undefined symbol: pam_set_item (/lib/security/pam_unix.so) undefined symbol: pam_get_data (/lib/security/pam_unix.so) undefined symbol: pam_get_user (/lib/security/pam_unix.so)

SUSE 9.3 w/ all patches: pam-0.78-8,
with this output for ldd -r /lib/security/pam_unix.so:
undefined symbol: pam_get_item (/lib/security/pam_unix.so) undefined symbol: pam_set_data (/lib/security/pam_unix.so)
undefined symbol: pam_putenv (/lib/security/pam_unix.so)
undefined symbol: pam_set_item (/lib/security/pam_unix.so) undefined symbol: pam_get_data (/lib/security/pam_unix.so) undefined symbol: pam_get_user (/lib/security/pam_unix.so)

        linux-gate.so.1 =>  (0xffffe000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7fb3000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7fae000)
        libxcrypt.so.1 => /lib/libxcrypt.so.1 (0xb7f7a000)
        libselinux.so.1 => /lib/libselinux.so.1 (0xb7f6b000)
        libc.so.6 => /lib/tls/libc.so.6 (0xb7e52000)
        /lib/ld-linux.so.2 (0x80000000)
Signature · 1 line
Matthias Andree
#28 lore raw
Quoted 3 lines · Matthias Andree

Parts of your assumption about ldd -r or linkage of the PAM modules against libpam don't hold. I haven't yet managed to test the whole setup.

I don't get it. The original 0.78 tarball from
http://www.kernel.org/pub/linux/libs/pam/pre/library/Linux-PAM-0.78.tar.bz2
does not have the bug, ie. pam_unix.so is linked with libpam.so. I've checked SUSE 9.3's SRPM (ftp://distro.ibiblio.org/pub/linux/distributions/suse/suse/i386/9.3/suse/src/pam-0.78-8.src.rpm) and it doesn't seem to include patches that would modify this behavior. I've
also rebuilt the RPM (on a FC4 system, because I deleted my SUSE 9.1 vmware)
and my rebuilt RPM does not have the bug! I don't know where to check
anymore.

Can you rebuild the pam RPM on your 9.3 system and verify whether %{_topdir}/BUILD/Linux-PAM-0.78/modules/pam_unix/pam_unix.so is linked against libpam.so? Thanks!

#29 lore raw
Quoted 2 lines · Simon Perreault

I've also rebuilt the RPM (on a FC4 system, because I deleted my SUSE 9.1 vmware) and my rebuilt RPM does not have the bug!

AH! I understand now! Even though pam_unix.so is *built* in the pam RPM, it is not *included* in the RPM. The real pam_unix.so comes from pam-modules and this one doesn't link against PAM. That's a bug that should be reported to SUSE.

#30 lore raw
Quoted 2 lines · Simon Perreault

That's a bug that should be reported
to SUSE.

...and here is the patch that should be submitted to them. I won't do so, I don't want to help a distro which has given me so much trouble. (Much more than only openvpn-related trouble.)

pam_unix2-1.26/src/Makefile.am renamed from pam_unix2-1.26/src/Makefile.am.orig +2 -1
@@ -24,7 +24,8 @@
 pam_PROGRAMS = pam_unix2.so
 
 pam_unix2_so_LDFLAGS = -Wl,-soname,pam_unix2.so \
-	-Wl,--version-script=$(srcdir)/pam_unix2.map
+	-Wl,--version-script=$(srcdir)/pam_unix2.map \
+	$(LIBPAM)
 
 pam_unix2_so_SOURCES = passwd_nss.c unix_acct.c get_options.c \
 	unix_passwd.c yppasswd_xdr.c passwd_nisplus.c support.c \
#18 lore raw
In reply to Simon Perreault
Quoted 11 lines · Simon Perreault
Quoted 3 lines · James Yonan

Thanks for the analysis. Because of the PAM bug, I think that openvpn-auth-pam will need to support the dlopen workaround for some time to come.

Yes, I have been thinking the same.

Quoted 3 lines

Maybe the solution would be to set up an alternative Makefile target that builds according to your patch, but leave the default for now as is.

Quoted 8 lines

Great idea. You'll find a new patch attached that does just this. A variable in the Makefile, DLOPEN_PAM, controls whether libpam.so is dlopened or linked in directly. I've set it to zero by default, and I suggest that you leave it that way. This makes the default Makefile work with newer, not broken PAM. The rationale is that you should have more work to do when you want to use a broken PAM than when you use a fixed one. And because broken PAM will be less often encountered as they are obsoleted by newer distro releases, this is the natural thing to do.

Simon,

Thanks for the patch. I'm inclined to merge it in the 2.0.x series, however I would want to set DLOPEN_PAM=1 at least initially since I would be concerned about breaking existing behavior in a stable series. (I agree in theory that you should have more work to do when you want to use a broken tool, however I tend to believe more strongly that workarounds which are needed by a significant percentage of users need to be the default.)

This will add a GNU Make requirement for the plugin which might be bad, but seems reasonable for now -- at least until someone gets a serious itch to set up an autoconf/automake build system.

James

#19 lore raw
Quoted 3 lines · James Yonan

This will add a GNU Make requirement for the plugin which might be bad, but seems reasonable for now -- at least until someone gets a serious itch to set up an autoconf/automake build system.

What is your stance in this regard?

On one hand, you say you want it simple,
on the other hand, you paint auto* as viable for the auth-pam plugin.

I don't yet have a reasonable test to stuff into autoconf though, lack of time. Perhaps tomorrow.

Signature · 1 line
Matthias Andree
#20 lore raw
Quoted 10 lines · Matthias Andree
Quoted 3 lines · James Yonan

This will add a GNU Make requirement for the plugin which might be bad, but seems reasonable for now -- at least until someone gets a serious itch to set up an autoconf/automake build system.

What is your stance in this regard?

On one hand, you say you want it simple,
on the other hand, you paint auto* as viable for the auth-pam plugin.

While I certainly prefer simplicity, if this issue is going to cause breakage down the road, I'd rather pick the solution which is simpler for the users, even if it adds some complexity for the developers.

So, yes, I think auto* is viable for the auth-pam plugin if someone wants to do it.

One concern I would have is whether the PAM version test at build time is always predictive of what the PAM version will be at run time.

James

#21 lore raw
Quoted 2 lines · James Yonan

One concern I would have is whether the PAM version test at build time is always predictive of what the PAM version will be at run time.

No, but then that would be going into the realm of cross-compiling.

#22 lore raw
Quoted 5 lines · Simon Perreault
Quoted 2 lines · James Yonan

One concern I would have is whether the PAM version test at build time is always predictive of what the PAM version will be at run time.

No, but then that would be going into the realm of cross-compiling.

I was thinking more about someone who builds a binary RPM on a machine that has a correctly linked pam-modules set causing autoconf to sense this -- now that RPM can't be used on a machine with an earlier broken version of pam-modules.

James

#23 lore raw
Quoted 4 lines · James Yonan

I was thinking more about someone who builds a binary RPM on a machine that has a correctly linked pam-modules set causing autoconf to sense this -- now that RPM can't be used on a machine with an earlier broken version of pam-modules.

...which constitutes an incentive to update to the non-broken PAM installation.

If you compile something for glibc 2.3, chances are it doesn't work with glibc 2.2 either.

Signature · 1 line
Matthias Andree
#24 lore raw
In reply to James Yonan
Quoted 4 lines · James Yonan

I was thinking more about someone who builds a binary RPM on a machine that has a correctly linked pam-modules set causing autoconf to sense this -- now that RPM can't be used on a machine with an earlier broken version of pam-modules.

That's why RPMs are built on a per-distro basis. You usually can't assume that an RPM compiled on FC3 will work on FC4. I tried to explain that point to my boss at my previous job but we ended up cross-compiling on Gentoo for Suse 9.2 and FC2 anyway. I bet they've run into a lot of trouble and now have dedicated build machines running the target distros.

Anyway, your initial concern is not a problem because RPMs are not supposed to be compatible across distro versions.

#25 lore raw
Quoted 6 lines · Simon Perreault

That's why RPMs are built on a per-distro basis. You usually can't assume that an RPM compiled on FC3 will work on FC4. I tried to explain that point to my boss at my previous job but we ended up cross-compiling on Gentoo for Suse 9.2 and FC2 anyway. I bet they've run into a lot of trouble and now have dedicated build machines running the target distros.

An RPM for an older distribution should generally work on a newer version of the same distro, so your boss's expectation was right - at least it usually works on FreeBSD and Solaris, if it doesn't work on Linux, Linux must improve in this compatibility regard.

If I need an RPM for several versions of the same distribution, I usually compile on the oldest available and it usually works.

Notable exception being kernel modules perhaps.

Signature · 1 line
Matthias Andree
#26 lore raw
Quoted 4 lines · Matthias Andree

An RPM for an older distribution should generally work on a newer version of the same distro, so your boss's expectation was right - at least it usually works on FreeBSD and Solaris, if it doesn't work on Linux, Linux must improve in this compatibility regard.

It doesn't, and Linux must improve in this regard. That's why an RPM for an older distribution can't be expected to work on a newer version.

Quoted 2 lines

If I need an RPM for several versions of the same distribution, I usually compile on the oldest available and it usually works.

It *usually* works, but you can't expect it to. At least, not when your product costs upwards of 10k and is expected to be rock-stable. You just can't take that risk, it would be stupid. Not taking the risk costs virtually nothing.

Signature · 1 line
Simon Perreault <[email protected]...> -- http://nomis80.org