Back to index

courier  0.68.2
ldapaliasdrc.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000-2003 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #if HAVE_CONFIG_H
00007 #include "config.h"
00008 #endif
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <ctype.h>
00012 #include <string.h>
00013 #include <errno.h>
00014 #if HAVE_UNISTD_H
00015 #include <unistd.h>
00016 #endif
00017 #include <sys/types.h>
00018 #if TIME_WITH_SYS_TIME
00019 #include        <sys/time.h>
00020 #include        <time.h>
00021 #else
00022 #if HAVE_SYS_TIME_H
00023 #include        <sys/time.h>
00024 #else
00025 #include        <time.h>
00026 #endif
00027 #endif
00028 #if HAVE_SYS_STAT_H
00029 #include <sys/stat.h>
00030 #endif
00031 #include <sys/socket.h>
00032 #include <sys/un.h>
00033 #if HAVE_SYSLOG_H
00034 #include <syslog.h>
00035 #else
00036 #define syslog(a,b)
00037 #endif
00038 
00039 #include      "ldapaliasdrc.h"
00040 #include      "sysconfdir.h"
00041 #include      "localstatedir.h"
00042 
00043 
00044 static char *ldapauth=0;
00045 static size_t ldapauth_size=0;
00046 
00047 static int readconfigfile()
00048 {
00049        FILE   *f=fopen(LDAPALIASDCONFIGFILE, "r");
00050        struct stat   buf;
00051        size_t i;
00052        if (!f)
00053        {
00054               if (errno != ENOENT)
00055                      perror(LDAPALIASDCONFIGFILE);
00056               return (-1);
00057        }
00058 
00059        if (fstat(fileno(f), &buf) ||
00060            (ldapauth=malloc(buf.st_size+2)) == 0)
00061        {
00062               fclose(f);
00063               return (-1);
00064        }
00065        if (fread(ldapauth, buf.st_size, 1, f) != 1)
00066        {
00067               free(ldapauth);
00068               ldapauth=0;
00069               fclose(f);
00070               return (-1);
00071        }
00072        ldapauth[ldapauth_size=buf.st_size]=0;
00073 
00074        for (i=0; i<ldapauth_size; i++)
00075               if (ldapauth[i] == '\n')
00076                      ldapauth[i]=0;
00077        fclose(f);
00078        return (0);
00079 }
00080 
00081 void ldapaliasd_configchanged()
00082 {
00083        if (ldapauth)
00084        {
00085               free(ldapauth);
00086               ldapauth=0;
00087               ldapauth_size=0;
00088        }
00089 }
00090 
00091 const char *ldapaliasd_config(const char *configname)
00092 {
00093        size_t i;
00094        int    l=strlen(configname);
00095        char   *p=0;
00096 
00097        if (!ldapauth && readconfigfile())
00098               return ("");
00099 
00100        for (i=0; i<ldapauth_size; )
00101        {
00102               p=ldapauth+i;
00103               if (memcmp(p, configname, l) == 0 &&
00104                      isspace((int)(unsigned char)p[l]))
00105               {
00106                      p += l;
00107                      while (*p && *p != '\n' &&
00108                             isspace((int)(unsigned char)*p))
00109                             ++p;
00110                      return (p);
00111               }
00112 
00113               while (i < ldapauth_size)
00114                      if (ldapauth[i++] == 0)     break;
00115        }
00116 
00117        return ("");
00118 }
00119 
00120 FILE *ldapaliasd_connect()
00121 {
00122        int     fd=socket(PF_UNIX, SOCK_STREAM, 0);
00123        struct  sockaddr_un skun;
00124        FILE   *fp;
00125 
00126         if (fd < 0)
00127        {
00128               return (NULL);
00129        }
00130 
00131         skun.sun_family=AF_UNIX;
00132         strcpy(skun.sun_path, SOCKETFILE);
00133 
00134        if (connect(fd, (const struct sockaddr *)&skun, sizeof(skun)) < 0)
00135        {
00136               close(fd);
00137               return (NULL);
00138        }
00139 
00140        fp=fdopen(fd, "r+");
00141 
00142        if (!fp)
00143        {
00144               close(fd);
00145               syslog(LOG_DAEMON | LOG_CRIT, "fdopen() failed; %m");
00146               return (NULL);
00147        }
00148        return (fp);
00149 }