Import Debian changes 0.69.0-2
[hcoop/debian/courier-authlib.git] / authsqlite.cpp
similarity index 56%
rename from authsqlite.c
rename to authsqlite.cpp
index 9d4a85f..3152d79 100644 (file)
 #include       <unistd.h>
 #endif
 
+extern "C" {
 #include       "auth.h"
-#include       "authsqlite.h"
 #include       "courierauthstaticlist.h"
+#include       "courierauth.h"
 #include       "courierauthdebug.h"
 #include       "libhmac/hmac.h"
-#include       "cramlib.h"
+}
+
+#include       "authsqlite.h"
+
+static int auth_sqlite_pre(const char *user, const char *service,
+                          int (*callback)(struct authinfo *, void *), void *arg)
+{
+       authsqliteuserinfo authinfo;
+       struct  authinfo        aa;
 
+       if (!auth_sqlite_getuserinfo(user, service, authinfo))
+               // Fatal error - such as Sqlite being down
+               return (1);
+
+       if (authinfo.home.empty()) // User not found
+               return (-1);
+
+       memset(&aa, 0, sizeof(aa));
+
+       /*aa.sysusername=user;*/
+       aa.sysuserid= &authinfo.uid;
+       aa.sysgroupid= authinfo.gid;
+       aa.homedir=authinfo.home.c_str();
+       aa.maildir=authinfo.maildir.empty() ? NULL:authinfo.maildir.c_str();
+       aa.address=authinfo.username.c_str();
+       aa.passwd=authinfo.cryptpw.c_str();
+       aa.clearpasswd=authinfo.clearpw.c_str();
+       aa.fullname=authinfo.fullname.c_str();
+       aa.quota=authinfo.quota.empty() ? NULL:authinfo.quota.c_str();
+       aa.options=authinfo.options.c_str();
+       return ((*callback)(&aa, arg));
+}
 
 extern void auth_sqlite_enumerate( void(*cb_func)(const char *name,
                                                  uid_t uid,
@@ -31,78 +62,83 @@ extern void auth_sqlite_enumerate( void(*cb_func)(const char *name,
                                                  void *void_arg),
                                   void *void_arg);
 
-static int auth_sqlite_login(const char *service, char *authdata,
-                            int (*callback_func)(struct authinfo *, void *),
-                            void *callback_arg)
+static bool docheckpw(authsqliteuserinfo &authinfo, const char *pass)
 {
-       char *user, *pass;
-       struct authsqliteuserinfo *authinfo;
-       struct  authinfo        aa;
-
-
-       if ((user=strtok(authdata, "\n")) == 0 ||
-               (pass=strtok(0, "\n")) == 0)
-       {
-               errno=EPERM;
-               return (-1);
-       }
-
-       authinfo=auth_sqlite_getuserinfo(user, service);
-
-       if (!authinfo)          /* Fatal error - such as Sqlite being down */
+       if (!authinfo.cryptpw.empty())
        {
-               errno=EACCES;
-               return (1);
-       }
-
-       if (authinfo->cryptpw)
-       {
-               if (authcheckpassword(pass,authinfo->cryptpw))
+               if (authcheckpassword(pass, authinfo.cryptpw.c_str()))
                {
                        errno=EPERM;
-                       return (-1);    /* User/Password not found. */
+                       return false;   /* User/Password not found. */
                }
        }
-       else if (authinfo->clearpw)
+       else if (!authinfo.clearpw.empty())
        {
-               if (strcmp(pass, authinfo->clearpw))
+               if (authinfo.clearpw != pass)
                {
                        if (courier_authdebug_login_level >= 2)
                        {
                                DPRINTF("supplied password '%s' does not match clearpasswd '%s'",
-                                       pass, authinfo->clearpw);
+                                       pass, authinfo.clearpw.c_str());
                        }
                        else
                        {
                                DPRINTF("supplied password does not match clearpasswd");
                        }
                        errno=EPERM;
-                       return (-1);
+                       return false;
                }
        }
        else
        {
                DPRINTF("no password available to compare");
                errno=EPERM;
-               return (-1);            /* Username not found */
+               return false;           /* Username not found */
+       }
+       return true;
+}
+
+static int auth_sqlite_login(const char *service, char *authdata,
+                            int (*callback_func)(struct authinfo *, void *),
+                            void *callback_arg)
+{
+       char *user, *pass;
+       authsqliteuserinfo authinfo;
+       struct  authinfo        aa;
+
+
+       if ((user=strtok(authdata, "\n")) == 0 ||
+               (pass=strtok(0, "\n")) == 0)
+       {
+               errno=EPERM;
+               return (-1);
        }
 
+       if (!auth_sqlite_getuserinfo(user, service, authinfo))
+               // Fatal error - such as Sqlite being down
+       {
+               errno=EACCES;
+               return (1);
+       }
+
+       if (!docheckpw(authinfo, pass))
+               return (-1);
+
        memset(&aa, 0, sizeof(aa));
 
-       aa.sysuserid= &authinfo->uid;
-       aa.sysgroupid= authinfo->gid;
-       aa.homedir=authinfo->home;
-       aa.maildir=authinfo->maildir && authinfo->maildir[0] ?
-               authinfo->maildir:0;
-       aa.address=authinfo->username;
-       aa.quota=authinfo->quota && authinfo->quota[0] ?
-               authinfo->quota:0;
-       aa.fullname=authinfo->fullname;
-       aa.options=authinfo->options;
+       aa.sysuserid= &authinfo.uid;
+       aa.sysgroupid= authinfo.gid;
+       aa.homedir=authinfo.home.c_str();
+       aa.maildir=authinfo.maildir.empty() ? NULL:authinfo.maildir.c_str();
+       aa.address=authinfo.username.c_str();
+       aa.quota=authinfo.quota.empty() ? NULL:authinfo.quota.c_str();
+       aa.fullname=authinfo.fullname.c_str();
+       aa.options=authinfo.options.c_str();
        aa.clearpasswd=pass;
-       aa.passwd=authinfo->cryptpw;
+       aa.passwd=authinfo.cryptpw.c_str();
        courier_authdebug_authinfo("DEBUG: authsqlite: ", &aa,
-                           authinfo->clearpw, authinfo->cryptpw);
+                                  authinfo.clearpw.c_str(),
+                                  authinfo.cryptpw.c_str());
 
        return (*callback_func)(&aa, callback_arg);
 }
@@ -111,39 +147,21 @@ static int auth_sqlite_changepw(const char *service, const char *user,
                                const char *pass,
                                const char *newpass)
 {
-       struct authsqliteuserinfo *authinfo;
-
-       authinfo=auth_sqlite_getuserinfo(user, service);
+       authsqliteuserinfo authinfo;
 
-       if (!authinfo)
+       if (!auth_sqlite_getuserinfo(user, service, authinfo))
        {
                errno=ENOENT;
                return (-1);
        }
 
-       if (authinfo->cryptpw)
-       {
-               if (authcheckpassword(pass,authinfo->cryptpw))
-               {
-                       errno=EPERM;
-                       return (-1);    /* User/Password not found. */
-               }
-       }
-       else if (authinfo->clearpw)
-       {
-               if (strcmp(pass, authinfo->clearpw))
-               {
-                       errno=EPERM;
-                       return (-1);
-               }
-       }
-       else
+       if (!docheckpw(authinfo, pass))
        {
                errno=EPERM;
-               return (-1);
+               return (-1);    /* User/Password not found. */
        }
 
-       if (auth_sqlite_setpass(user, newpass, authinfo->cryptpw))
+       if (auth_sqlite_setpass(user, newpass, authinfo.cryptpw.c_str()))
        {
                errno=EPERM;
                return (-1);
@@ -192,8 +210,9 @@ static struct authstaticinfo authsqlite_info={
        auth_sqlite_cleanup,
        auth_sqlite_enumerate};
 
-
-struct authstaticinfo *courier_authsqlite_init()
-{
-       return &authsqlite_info;
+extern "C" {
+       struct authstaticinfo *courier_authsqlite_init()
+       {
+               return &authsqlite_info;
+       }
 }