Update the GPL boilerplate with the new address of the FSF.
[freeradius.git] / src / modules / rlm_pam / rlm_pam.c
index e5ff6b9..10b5f54 100644 (file)
  *
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
  * Copyright 2000  The FreeRADIUS server project
  * Copyright 1997  Jeph Blaize <jblaize@kiva.net>
  * Copyright 1999  miguel a.l. paraz <map@iphil.net>
  */
 
-#include       "autoconf.h"
-#include       "libradius.h"
+#include       <freeradius-devel/autoconf.h>
 
 #include       "config.h"
 
 #include       <syslog.h>
 #endif
 
-#include       "radiusd.h"
-#include       "modules.h"
+#include       <freeradius-devel/radiusd.h>
+#include       <freeradius-devel/modules.h>
 
 typedef struct rlm_pam_t {
        const char *pam_auth_name;
 } rlm_pam_t;
 
-static CONF_PARSER module_config[] = {
+static const CONF_PARSER module_config[] = {
        { "pam_auth",    PW_TYPE_STRING_PTR, offsetof(rlm_pam_t,pam_auth_name),
          NULL, "radiusd" },
        { NULL, -1, 0, NULL, NULL }
@@ -121,28 +120,24 @@ static int PAM_conv (int num_msg,
                      const struct pam_message **msg,
                      struct pam_response **resp,
                      void *appdata_ptr) {
-  int count = 0, replies = 0;
-  struct pam_response *reply = NULL;
-  int size = sizeof(struct pam_response);
+  int count;
+  struct pam_response *reply;
   my_PAM *pam_config = (my_PAM *) appdata_ptr;
-  
-#define GET_MEM if (reply) realloc(reply, size); else reply = rad_malloc(size); \
-  size += sizeof(struct pam_response)
+
+/* strdup(NULL) doesn't work on some platforms */
 #define COPY_STRING(s) ((s) ? strdup(s) : NULL)
-                                    
+
+  reply = rad_malloc(num_msg * sizeof(struct pam_response));
+  memset(reply, 0, num_msg * sizeof(struct pam_response));
   for (count = 0; count < num_msg; count++) {
     switch (msg[count]->msg_style) {
     case PAM_PROMPT_ECHO_ON:
-      GET_MEM;
-      reply[replies].resp_retcode = PAM_SUCCESS;
-      reply[replies++].resp = COPY_STRING(pam_config->username);
-      /* PAM frees resp */
+      reply[count].resp_retcode = PAM_SUCCESS;
+      reply[count].resp = COPY_STRING(pam_config->username);
       break;
     case PAM_PROMPT_ECHO_OFF:
-      GET_MEM;
-      reply[replies].resp_retcode = PAM_SUCCESS;
-      reply[replies++].resp = COPY_STRING(pam_config->password);
-      /* PAM frees resp */
+      reply[count].resp_retcode = PAM_SUCCESS;
+      reply[count].resp = COPY_STRING(pam_config->password);
       break;
     case PAM_TEXT_INFO:
       /* ignore it... */
@@ -150,12 +145,20 @@ static int PAM_conv (int num_msg,
     case PAM_ERROR_MSG:
     default:
       /* Must be an error of some sort... */
-      free (reply);
+      for (count = 0; count < num_msg; count++) {
+        if (reply[count].resp) {
+          /* could be a password, let's be sanitary */
+          memset(reply[count].resp, 0, strlen(reply[count].resp));
+          free(reply[count].resp);
+        }
+      }
+      free(reply);
       pam_config->error = 1;
       return PAM_CONV_ERR;
     }
   }
-  if (reply) *resp = reply;
+  *resp = reply;
+  /* PAM frees reply (including reply[].resp) */
 
   return PAM_SUCCESS;
 }
@@ -171,7 +174,7 @@ static int PAM_conv (int num_msg,
  *************************************************************************/
 
 /* cjd 19980706
- * 
+ *
  * for most flexibility, passing a pamauth type to this function
  * allows you to have multiple authentication types (i.e. multiple
  * files associated with radius in /etc/pam.d)
@@ -204,7 +207,7 @@ static int pam_pass(const char *name, const char *passwd, const char *pamauth)
     if (retval != PAM_SUCCESS) {
       DEBUG("pam_pass: function pam_authenticate FAILED for <%s>. Reason: %s",
            name, pam_strerror(pamh, retval));
-      pam_end(pamh, 0);
+      pam_end(pamh, retval);
       return -1;
     }
 
@@ -217,13 +220,13 @@ static int pam_pass(const char *name, const char *passwd, const char *pamauth)
     if (retval != PAM_SUCCESS) {
       DEBUG("pam_pass: function pam_acct_mgmt FAILED for <%s>. Reason: %s",
            name, pam_strerror(pamh, retval));
-      pam_end(pamh, 0);
+      pam_end(pamh, retval);
       return -1;
     }
 #endif
 
     DEBUG("pam_pass: authentication succeeded for <%s>", name);
-    pam_end(pamh, 0);
+    pam_end(pamh, retval);
     return 0;
 }
 
@@ -268,15 +271,15 @@ static int pam_auth(void *instance, REQUEST *request)
         *      for backwards compatibility.
         */
        pair = pairfind(request->config_items, PAM_AUTH_ATTR);
-       if (pair) pam_auth_string = (char *)pair->strvalue;
+       if (pair) pam_auth_string = (char *)pair->vp_strvalue;
 
-       r = pam_pass((char *)request->username->strvalue,
-                    (char *)request->password->strvalue,
+       r = pam_pass((char *)request->username->vp_strvalue,
+                    (char *)request->password->vp_strvalue,
                     pam_auth_string);
 
 #ifdef HAVE_SYSLOG_H
        if (!strcmp(radlog_dir, "syslog")) {
-               openlog(progname, LOG_PID, syslog_facility);
+               openlog(progname, LOG_PID, mainconfig.syslog_facility);
        }
 #endif
 
@@ -287,21 +290,20 @@ static int pam_auth(void *instance, REQUEST *request)
 }
 
 module_t rlm_pam = {
-  "Pam",
-  RLM_TYPE_THREAD_UNSAFE,      /* The PAM libraries are not thread-safe */
-  NULL,                                /* initialize */
-  pam_instantiate,             /* instantiation */
-  {
-         pam_auth,             /* authenticate */
-         NULL,                 /* authorize */
-         NULL,                 /* pre-accounting */
-         NULL,                 /* accounting */
-         NULL,                 /* checksimul */
-         NULL,                 /* pre-proxy */
-         NULL,                 /* post-proxy */
-         NULL                  /* post-auth */
-  },
-  pam_detach,                  /* detach */
-  NULL,                                /* destroy */
+       RLM_MODULE_INIT,
+       "pam",
+       RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
+       pam_instantiate,                /* instantiation */     
+       pam_detach,                     /* detach */
+       {
+               pam_auth,               /* authenticate */
+               NULL,                   /* authorize */
+               NULL,                   /* pre-accounting */
+               NULL,                   /* accounting */
+               NULL,                   /* checksimul */
+               NULL,                   /* pre-proxy */
+               NULL,                   /* post-proxy */
+               NULL                    /* post-auth */
+       },
 };