Back to index

courier  0.68.2
authenticate_auth.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2008 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "config.h"
00008 #endif
00009 #include      <string.h>
00010 #include      <stdlib.h>
00011 #if    HAVE_UNISTD_H
00012 #include      <unistd.h>
00013 #endif
00014 #include      "imaptoken.h"
00015 #include      "imapwrite.h"
00016 #include      "courierauth.h"
00017 #include      "courierauthsasl.h"
00018 #include      "courierauthdebug.h"
00019 
00020 
00021 extern int main_argc;
00022 extern char **main_argv;
00023 
00024 extern int login_callback(struct authinfo *ainfo, void *dummy);
00025 
00026 extern const char *imap_externalauth();
00027 
00028 static char *send_auth_reply(const char *q, void *dummy)
00029 {
00030        struct imaptoken *tok;
00031        char   *p;
00032 
00033 #if SMAP
00034        const char *cp=getenv("PROTOCOL");
00035 
00036        if (cp && strcmp(cp, "SMAP1") == 0)
00037               writes("> ");
00038        else
00039 #endif
00040 
00041        {
00042               writes("+ ");
00043        }
00044        writes(q);
00045        writes("\r\n");
00046        writeflush();
00047        read_timeout(SOCKET_TIMEOUT);
00048        tok=nexttoken_nouc();
00049 
00050        switch (tok->tokentype)     {
00051        case IT_ATOM:
00052        case IT_NUMBER:
00053               p=my_strdup(tok->tokenbuf);
00054               break;
00055        case IT_EOL:
00056               p=my_strdup("");
00057               break;
00058        default:
00059               return (0);
00060        }
00061        if (!p)
00062        {
00063               perror("malloc");
00064               return (0);
00065        }
00066 
00067        if (nexttoken()->tokentype != IT_EOL)
00068        {
00069               free(p);
00070               fprintf(stderr, "Invalid SASL response\n");
00071               return (0);
00072        }
00073        read_eol();
00074        return (p);
00075 }
00076 
00077 int authenticate(const char *tag, char *methodbuf, int methodbuflen)
00078 {
00079 struct imaptoken *tok=nexttoken();
00080 char   *authmethod;
00081 char   *initreply=0;
00082 char   *authtype, *authdata;
00083 char   authservice[40];
00084 char   *p ;
00085 int    rc;
00086 
00087        switch (tok->tokentype)     {
00088        case IT_ATOM:
00089        case IT_QUOTED_STRING:
00090               break;
00091        default:
00092               return (0);
00093        }
00094 
00095        authmethod=my_strdup(tok->tokenbuf);
00096        if (methodbuf)
00097               snprintf(methodbuf, methodbuflen, "%s", authmethod);
00098 
00099        tok=nexttoken_nouc();
00100        if (tok->tokentype != IT_EOL)
00101        {
00102               switch (tok->tokentype)     {
00103               case IT_ATOM:
00104               case IT_NUMBER:
00105                      break;
00106               default:
00107                      return (0);
00108               }
00109               initreply=my_strdup(tok->tokenbuf);
00110               if (strcmp(initreply, "=") == 0)
00111                      *initreply=0;
00112               tok=nexttoken_nouc();
00113        }
00114 
00115        if (tok->tokentype != IT_EOL)      return (0);
00116 
00117        read_eol();
00118        if ((rc = auth_sasl_ex(authmethod, initreply, imap_externalauth(),
00119                             &send_auth_reply, NULL,
00120                             &authtype, &authdata)) != 0)
00121        {
00122               free(authmethod);
00123               if (initreply)
00124                      free(initreply);
00125               return (rc);
00126        }
00127 
00128        free(authmethod);
00129        if (initreply)
00130               free(initreply);
00131 
00132        strcat(strcpy(authservice, "AUTHSERVICE"),
00133                       getenv("TCPLOCALPORT"));
00134        p=getenv(authservice);
00135 
00136        if (!p || !*p)
00137               p="imap";
00138 
00139        rc=auth_generic(p, authtype, authdata, login_callback, (void *)tag);
00140        free(authtype);
00141        free(authdata);
00142        return (rc);
00143 }