Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / authsasl.c
index 42a932e..72c31ca 100644 (file)
@@ -1,13 +1,13 @@
-/* $Id: authsasl.c,v 1.5 2008/07/10 02:43:55 mrsam Exp $ */
 
 /*
-** Copyright 1998 - 2008 Double Precision, Inc.  See COPYING for
+** Copyright 1998 - 2013 Double Precision, Inc.  See COPYING for
 ** distribution information.
 */
 
 #include       "courier_auth_config.h"
 #include       "courierauthsasl.h"
 #include       "authsaslclient.h"
+#include       "cramlib.h"
 #include       <stdlib.h>
 #include       <ctype.h>
 #include       <string.h>
@@ -93,10 +93,13 @@ int auth_sasl_ex(const char *method,
                                 authtype_ptr,
                                 authdata_ptr);
 
-       if (initresponse && *initresponse)
+       if (!externalauth || !*externalauth)
                return AUTHSASL_ERROR;
 
-       if (!externalauth || !*externalauth)
+       if (initresponse && !*initresponse)
+               initresponse=NULL;
+
+       if (initresponse && strcmp(initresponse, externalauth))
                return AUTHSASL_ERROR;
 
        if (!initresponse)
@@ -137,3 +140,47 @@ int auth_sasl_ex(const char *method,
 
        return 0;
 }
+
+char *auth_sasl_extract_userid(const char *authtype,
+                              const char *authdata)
+{
+       struct  cram_callback_info      cci;
+       char *p;
+       char *t;
+       char *d;
+
+       if (strcmp(authtype, AUTHTYPE_LOGIN) == 0)
+       {
+               char *q;
+
+               p=strdup(authdata);
+
+               if (!p)
+                       return NULL;
+
+               q=strchr(p, '\n');
+               if (q) *q=0;
+               return p;
+       }
+
+       if ((t=strdup(authtype)) == NULL)
+               return NULL;
+
+       if ((d=strdup(authdata)) == NULL)
+       {
+               free(t);
+               return NULL;
+       }
+
+       if (auth_get_cram_silent(t, d, &cci))
+       {
+               free(d);
+               free(t);
+               return (NULL);
+       }
+
+       p=strdup(cci.user);
+       free(d);
+       free(t);
+       return p;
+}