Back to index

courier  0.68.2
dotcourier.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include      "config.h"
00008 #endif
00009 #include      "courier.h"
00010 #include      <stdlib.h>
00011 #include      <string.h>
00012 #include      <errno.h>
00013 #include      <sys/types.h>
00014 #if    HAVE_SYS_STAT_H
00015 #include      <sys/stat.h>
00016 #endif
00017 #if    HAVE_FCNTL_H
00018 #include      <fcntl.h>
00019 #endif
00020 #if    HAVE_UNISTD_H
00021 #include      <unistd.h>
00022 #endif
00023 
00024 char *local_extension()
00025 {
00026 static char *ext=0;
00027 
00028        if (!ext)
00029        {
00030        char   *f=config_localfilename("dotextension");
00031 
00032               ext=config_read1l(f);
00033               free(f);
00034        }
00035 
00036        if (!ext || !*ext)
00037        {
00038               if (ext)      free(ext);
00039               ext="courier";
00040        }
00041        return (ext);
00042 }
00043 
00044 char *local_dotcourier(const char *homedir, const char *ext,
00045        const char **defaultptr)
00046 {
00047 char   *extbuf;
00048 char   *x=local_extension();
00049 
00050        extbuf=(char *)courier_malloc(sizeof("." "--default")
00051                      +strlen(x) +strlen(ext));
00052 
00053        strcat(strcpy( extbuf, "." ), x);
00054        if (defaultptr)      *defaultptr=ext;
00055 
00056 #if LOCAL_EXTENSIONS
00057 
00058        if (*ext)
00059        {
00060        char   *c;
00061        char   *filebuf=courier_malloc(sizeof("/." "--default")
00062                      +strlen(x) +strlen(ext)+strlen(homedir));
00063        int    isfirst=1;
00064        struct stat   stat_buf;
00065 
00066               strcat(strcat(extbuf, "-"), ext);
00067               for (c=extbuf+1; *c; c++)
00068                      if (*c == '.')       *c=':';
00069 
00070               if (defaultptr)
00071                      *defaultptr=0;
00072 
00073               while (lstat(
00074                      strcat(strcat(strcpy(filebuf, homedir), "/"),
00075                             extbuf), &stat_buf) < 0)
00076               {
00077                      /* We simply may not have the permissions to read
00078                      ** someone's home directory.  This can happen if
00079                      ** rewriting is carried out in a non-privileged
00080                      ** process which is submitting a mail message.
00081                      **
00082                      ** Unless we have an ENOENT at this point, assume
00083                      ** that this is the case, and presume that the
00084                      ** address is valid.  The downshot is that the
00085                      ** message will be accepted, and an internal bounce
00086                      ** is later generated.
00087                      */
00088 
00089                      if (errno != ENOENT)
00090                      {
00091                             free(extbuf);
00092                             free(filebuf);
00093                             return (0);
00094                      }
00095 
00096                      *strrchr(extbuf, '-')=0;
00097                      if (!isfirst)
00098                      {
00099                             if ((c=strrchr(extbuf, '-')) == 0)
00100                             {
00101                                    free(extbuf);
00102                                    free(filebuf);
00103                                    return (0);
00104                             }
00105                             *c=0;
00106                      }
00107                      isfirst=0;
00108                      if (defaultptr)
00109                             *defaultptr=ext+ (strlen(extbuf)-sizeof(".")
00110                                    -strlen(x)+1);
00111                      strcat(extbuf, "-default");
00112               }
00113               free(filebuf);
00114        }
00115 #endif
00116        return (extbuf);
00117 }