Back to index

courier  0.68.2
islocal.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      "dbobj.h"
00008 #include      <string.h>
00009 #include      <stdlib.h>
00010 #include      <ctype.h>
00011 
00012 #ifndef ISLOCAL_MAX_DOT_COUNT
00013 #define ISLOCAL_MAX_DOT_COUNT 5
00014 #endif
00015 
00016 
00017 /*
00018        local.islocal: If the given address is a local one, strip off the '@'
00019        part, otherwise remove the entire address.
00020 */
00021 
00022 static const char *control_locals=0;
00023 
00024 /* Return config locals file */
00025 
00026 static const char *get_control_locals()
00027 {
00028        if (!control_locals)
00029        {
00030        char *filename=config_localfilename("locals");
00031        char *buf=readfile(filename, 0);
00032 
00033               free(filename);
00034               if (!buf)
00035                      control_locals=config_me();
00036               else
00037               {
00038                      removecomments(buf);
00039                      control_locals=buf;
00040               }
00041        }
00042        return (control_locals);
00043 }
00044 
00045 static int isinit=0;
00046 static struct dbobj db;
00047 
00048 int config_islocal(const char *address, char **domainp)
00049 {
00050 char   *k;
00051 char   *v;
00052 size_t vl;
00053 size_t kl;
00054 
00055 const char *p;
00056 size_t pl;
00057 char   *lcaddress;
00058 int    dotcount;
00059 
00060        if (domainp)  *domainp=0;
00061 
00062        if (config_is_indomain(address, get_control_locals()))
00063               return (1);
00064 
00065 
00066        if (!isinit)
00067        {
00068        char    *filename;
00069 
00070               dbobj_init(&db);
00071               filename=config_localfilename("hosteddomains.dat");
00072               isinit=1;
00073               dbobj_open(&db, filename, "R");
00074               free(filename);
00075         }
00076 
00077        if (!dbobj_isopen(&db))
00078               return (0);
00079 
00080        kl=strlen(address);
00081 
00082        k=strcpy(courier_malloc(kl+1), address);
00083        for (v=k; *v; v++)
00084               *v=tolower((int)(unsigned char)*v);
00085 
00086        lcaddress = k;
00087        dotcount = 0;
00088 
00089        while ( (v=dbobj_fetch(&db, k, kl, &vl, "")) == NULL &&
00090                (k = strchr(k+1,'.')) != NULL &&
00091                dotcount++ < ISLOCAL_MAX_DOT_COUNT )
00092                   kl = strlen(k);
00093 
00094        if (k) address += k-lcaddress;
00095        free(lcaddress);
00096        if (!v)       return (0);
00097 
00098        /* Default entry is "1", so punt it to k*/
00099 
00100        p=v;
00101        pl=vl;
00102 
00103        if (vl <= 1)
00104        {
00105               p=address;
00106               pl=kl;
00107        }
00108 
00109        if (domainp)
00110        {
00111               *domainp=courier_malloc(pl+1);
00112               memcpy( *domainp, p, pl);
00113               (*domainp)[pl]=0;
00114        }
00115        free(v);
00116        return (1);
00117 }
00118 
00119 int config_is_indomain(const char *address, const char *localp)
00120 {
00121        while (*localp)
00122        {
00123        unsigned i;
00124 
00125               for (i=0; localp[i] && localp[i] != '\n' && localp[i] != '\r';
00126                      ++i)
00127                      ;
00128 
00129               if ( i > 0 && localp[0] == '!' && *address != '!' &&
00130                    config_domaincmp(address, localp+1, i-1) == 0)
00131                      return (0);
00132 
00133               if ( config_domaincmp(address, localp, i) == 0)
00134                      return (1);
00135               localp += i;
00136               if (*localp)  ++localp;
00137        }
00138        return (0);
00139 }