Back to index

courier  0.68.2
Functions | Variables
authenticate_auth.c File Reference
#include <string.h>
#include <stdlib.h>
#include "imaptoken.h"
#include "imapwrite.h"
#include "courierauth.h"
#include "courierauthsasl.h"
#include "courierauthdebug.h"

Go to the source code of this file.

Functions

int login_callback (struct authinfo *ainfo, void *dummy)
const char * imap_externalauth ()
static char * send_auth_reply (const char *q, void *dummy)
int authenticate (const char *tag, char *methodbuf, int methodbuflen)

Variables

int main_argc
char ** main_argv

Function Documentation

int authenticate ( const char *  tag,
char *  methodbuf,
int  methodbuflen 
)

Definition at line 77 of file authenticate_auth.c.

{
struct imaptoken *tok=nexttoken();
char   *authmethod;
char   *initreply=0;
char   *authtype, *authdata;
char   authservice[40];
char   *p ;
int    rc;

       switch (tok->tokentype)     {
       case IT_ATOM:
       case IT_QUOTED_STRING:
              break;
       default:
              return (0);
       }

       authmethod=my_strdup(tok->tokenbuf);
       if (methodbuf)
              snprintf(methodbuf, methodbuflen, "%s", authmethod);

       tok=nexttoken_nouc();
       if (tok->tokentype != IT_EOL)
       {
              switch (tok->tokentype)     {
              case IT_ATOM:
              case IT_NUMBER:
                     break;
              default:
                     return (0);
              }
              initreply=my_strdup(tok->tokenbuf);
              if (strcmp(initreply, "=") == 0)
                     *initreply=0;
              tok=nexttoken_nouc();
       }

       if (tok->tokentype != IT_EOL)      return (0);

       read_eol();
       if ((rc = auth_sasl_ex(authmethod, initreply, imap_externalauth(),
                            &send_auth_reply, NULL,
                            &authtype, &authdata)) != 0)
       {
              free(authmethod);
              if (initreply)
                     free(initreply);
              return (rc);
       }

       free(authmethod);
       if (initreply)
              free(initreply);

       strcat(strcpy(authservice, "AUTHSERVICE"),
                      getenv("TCPLOCALPORT"));
       p=getenv(authservice);

       if (!p || !*p)
              p="imap";

       rc=auth_generic(p, authtype, authdata, login_callback, (void *)tag);
       free(authtype);
       free(authdata);
       return (rc);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* imap_externalauth ( )

Definition at line 72 of file capability.c.

{
       const char *p;

       if ((p=getenv("IMAP_TLS")) && atoi(p))
              return externalauth();

       return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int login_callback ( struct authinfo *  ainfo,
void *  dummy 
)

Definition at line 143 of file imaplogin.c.

{
       int rc;
       const char *tag=(const char *)dummy;
       char *p;

       p=getenv("IMAP_PROXY");

       if (p && atoi(p))
       {
              if (ainfo->options == NULL ||
                  (p=auth_getoption(ainfo->options,
                                  "mailhost")) == NULL)
              {
                     fprintf(stderr, "WARN: proxy enabled, but no proxy"
                             " host defined for %s\n",
                             ainfo->address);

                     /* Fallthru to account login */

              }
              else if (ainfo->clearpasswd == NULL)
              {
                     free(p);
                     fprintf(stderr, "WARN: proxy enabled, but no password"
                             " for %s\n", ainfo->address);
                     return -1;
              }
              else
              {
                     struct proxyinfo pi;
                     struct imapproxyinfo ipi;
                     struct servent *se;
                     int fd;

                     se=getservbyname("imap", NULL);

                     pi.host=p;
                     pi.port=se ? ntohs(se->s_port):143;

                     ipi.uid=ainfo->address;
                     ipi.pwd=ainfo->clearpasswd;
                     ipi.tag=tag;

                     pi.connected_func=login_imap;
                     pi.void_arg=&ipi;

                     if ((fd=connect_proxy(&pi)) < 0)
                     {
                            free(p);
                            return -1;
                     }
                     free(p);
                     if (fd > 0)
                     {
                            alarm(0);
                            proxyloop(fd);
                            exit(0);
                     }

                     /* FALLTHRU */
              }
       }

       rc=auth_callback_default(ainfo);

       if (rc == 0)
       {
              p=malloc(sizeof("OPTIONS=") + strlen(ainfo->options ?
                                               ainfo->options:""));

              if (p)
              {
                     strcat(strcpy(p, "OPTIONS="),
                            ainfo->options ? ainfo->options:"");
                     putenv(p);

                     p=malloc(sizeof("IMAPLOGINTAG=")+strlen(tag));
                     if (p)
                     {
                            strcat(strcpy(p, "IMAPLOGINTAG="), tag);
                            putenv(p);

                            p=malloc(sizeof("AUTHENTICATED=")+
                                    strlen(ainfo->address));
                            if (p)
                            {
                                   strcat(strcpy(p, "AUTHENTICATED="),
                                          ainfo->address);
                                   putenv(p);
                                   alarm(0);
                                   execl(imapd, imapd,
                                         ainfo->maildir ?
                                         ainfo->maildir:defaultmaildir,
                                         NULL);
                                   fprintf(stderr, "ERR: exec(%s) failed!!\n",
                                                         imapd);
                            }
                     }
              }
       }

       return(rc);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* send_auth_reply ( const char *  q,
void *  dummy 
) [static]

Definition at line 28 of file authenticate_auth.c.

{
       struct imaptoken *tok;
       char   *p;

#if SMAP
       const char *cp=getenv("PROTOCOL");

       if (cp && strcmp(cp, "SMAP1") == 0)
              writes("> ");
       else
#endif

       {
              writes("+ ");
       }
       writes(q);
       writes("\r\n");
       writeflush();
       read_timeout(SOCKET_TIMEOUT);
       tok=nexttoken_nouc();

       switch (tok->tokentype)     {
       case IT_ATOM:
       case IT_NUMBER:
              p=my_strdup(tok->tokenbuf);
              break;
       case IT_EOL:
              p=my_strdup("");
              break;
       default:
              return (0);
       }
       if (!p)
       {
              perror("malloc");
              return (0);
       }

       if (nexttoken()->tokentype != IT_EOL)
       {
              free(p);
              fprintf(stderr, "Invalid SASL response\n");
              return (0);
       }
       read_eol();
       return (p);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int main_argc

Definition at line 54 of file imaplogin.c.

char** main_argv

Definition at line 55 of file imaplogin.c.