Back to index

courier  0.68.2
pcpdauth.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001-2004 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 /*
00008 */
00009 
00010 #include      "config.h"
00011 
00012 #if    HAVE_SYS_STAT_H
00013 #include      <sys/stat.h>
00014 #endif
00015 #if HAVE_SYS_WAIT_H
00016 #include      <sys/wait.h>
00017 #endif
00018 
00019 #if    HAVE_UNISTD_H
00020 #include      <unistd.h>
00021 #endif
00022 
00023 #include      <stdio.h>
00024 #include      <stdlib.h>
00025 #include      <ctype.h>
00026 #include      <pwd.h>
00027 #if    HAVE_UNISTD_H
00028 #include      <unistd.h>
00029 #endif
00030 #include      <string.h>
00031 #include      <errno.h>
00032 #include      <sysconfdir.h>
00033 
00034 #include      "courierauth.h"
00035 
00036 #include      "pcpdauth.h"
00037 #include      "calendardir.h"
00038 
00039 char *auth_choplocalhost(const char *u)
00040 {
00041        const char *p;
00042        char *s;
00043 
00044        p=strrchr(u, '@');
00045        if (p && strcasecmp(p+1, auth_myhostname()) == 0)
00046        {
00047               s=malloc(p-u+1);
00048               if (s)
00049               {
00050                      memcpy(s, u, p-u);
00051                      s[p-u]=0;
00052               }
00053        }
00054        else
00055               s=strdup(u);
00056 
00057        if (!s)
00058               fprintf(stderr, "ALERT: malloc failed.\n");
00059        return (s);
00060 }
00061 
00062 static int badstr(const char *p)
00063 {
00064        while (*p)
00065        {
00066               if ((int)(unsigned char)*p < ' '
00067                   || *p == '\\' || *p == '\'' || *p == '"')
00068               {
00069                      errno=EIO;
00070                      return (1);
00071               }
00072               ++p;
00073        }
00074        return (0);
00075 }
00076 
00077 static int callback_userid(struct authinfo *a, void *vp)
00078 {
00079        struct userid_callback *uid=(struct userid_callback *)vp;
00080 
00081        uid->homedir=a->homedir;
00082        uid->maildir=a->maildir;
00083 
00084        if (a->sysuserid)
00085               uid->uid= *a->sysuserid;
00086        else if (!a->sysusername)
00087               return (-1);
00088        else
00089        {
00090               struct passwd *pw=getpwnam(a->sysusername);
00091 
00092               if (!pw)
00093                      return (-1);
00094               uid->uid=pw->pw_uid;
00095        }
00096        return ( (*uid->callback_func)(uid, uid->callback_arg));
00097 }
00098 
00099 int authpcp_userid(const char *u, int (*func)(struct userid_callback *, void *),
00100               void *funcarg)
00101 {
00102        char *s=NULL;
00103        int rc;
00104        struct userid_callback uinfo;
00105 
00106        if (badstr(u))
00107               return (-1);
00108 
00109        s=auth_choplocalhost(u);
00110        if (!s)
00111               return (1);
00112 
00113        memset(&uinfo, 0, sizeof(uinfo));
00114        uinfo.userid=s;
00115        uinfo.callback_func=func;
00116        uinfo.callback_arg=funcarg;
00117 
00118        rc=auth_getuserinfo("calendar", s, callback_userid, &uinfo);
00119 
00120        free(s);
00121        return (rc);
00122 }
00123 
00124 
00125 /*
00126 ** The returned mailboxid is of the form user.authdriver
00127 **
00128 ** authdriver is included so that we can find the same authentication module
00129 ** quickly, for each request.  authdriver is appended, rather than prepended,
00130 ** because the logincache hashes on the first couple of characters of the id.
00131 */
00132 
00133 int authpcp_login(const char *uid, const char *pass,
00134                 int (*func)(struct userid_callback *, void *),
00135                 void *arg)
00136 {
00137        struct userid_callback uinfo;
00138 
00139        memset(&uinfo, 0, sizeof(uinfo));
00140        uinfo.callback_func=func;
00141        uinfo.callback_arg=arg;
00142 
00143        if (badstr(uid) || badstr(pass))
00144               return (-1);
00145 
00146 
00147        return (auth_login("calendar", uid, pass, callback_userid, &uinfo));
00148 }