Back to index

courier  0.68.2
abookread.c
Go to the documentation of this file.
00001 /*
00002 **
00003 ** Copyright 2003-2006, Double Precision Inc.
00004 **
00005 ** See COPYING for distribution information.
00006 */
00007 
00008 #include      "config.h"
00009 #include      "ldapaddressbook.h"
00010 
00011 #include      <stdio.h>
00012 #include      <string.h>
00013 #include      <stdlib.h>
00014 
00015 static void dequote(char *);
00016 
00017 struct ldapabook *ldapabook_read(const char *filename)
00018 {
00019 char   buf[BUFSIZ];
00020 FILE   *fp;
00021 struct ldapabook *list, *last;
00022 char   *s;
00023 char   *name;
00024 char   *host;
00025 char   *port;
00026 char   *suffix;
00027 char   *binddn;
00028 char   *bindpw;
00029 
00030        if ((fp=fopen(filename, "r")) == 0)       return (0);
00031 
00032        list=last=0;
00033        while (fgets(buf, sizeof(buf), fp))
00034        {
00035        struct ldapabook *p;
00036 
00037        struct ldapabook_opts *opts=NULL, *lastopt=NULL;
00038        struct ldapabook_opts *nextopt;
00039 
00040               s=strchr(buf, '\n');
00041               if (s) *s=0;
00042               if ((s=strchr(buf, '#')) != 0)     *s=0;
00043               name=buf;
00044               s=strchr(buf, '\t');
00045               if (!s)       continue;
00046               *s++=0;
00047               host=s;
00048               s=strchr(s, '\t');
00049               if (s) *s++=0;
00050               port=s;
00051               if (s) s=strchr(s, '\t');
00052               if (s) *s++=0;
00053               suffix=s;
00054               if (s) s=strchr(s, '\t');
00055               if (s) *s++=0;
00056               binddn=s;
00057               if (s) s=strchr(s, '\t');
00058               if (s) *s++=0;
00059               bindpw=s;
00060               if (!port || !*port) port="389";
00061               if (!suffix)  suffix="";
00062               if (!binddn)  binddn="";
00063               if (!bindpw)  bindpw="";
00064 
00065               if (s) s=strchr(s, '\t');
00066               if (s) *s++=0;
00067 
00068               while (s && *s)
00069               {
00070                      char *t;
00071 
00072                      t=strchr(s, ',');
00073                      if (t)
00074                             *t++=0;
00075 
00076                      if ((nextopt=(struct ldapabook_opts *)
00077                           malloc(sizeof(struct ldapabook_opts))) == NULL
00078                          || (nextopt->options=strdup(s)) == NULL)
00079                      {
00080                             if (nextopt)
00081                                    free(nextopt);
00082                             ldapabook_free(list);
00083                             fclose(fp);
00084                             return (NULL);
00085                      }
00086 
00087                      dequote(nextopt->options);
00088                      if (!lastopt)
00089                             opts=nextopt;
00090                      else
00091                             lastopt->next=nextopt;
00092                      nextopt->next=NULL;
00093                      lastopt=nextopt;
00094                      s=t;
00095               }
00096 
00097               if ((p=malloc(sizeof(struct ldapabook))) == 0)
00098               {
00099                      struct ldapabook_opts *nextopt;
00100 
00101                      while ((nextopt=opts) != NULL)
00102                      {
00103                             opts=nextopt->next;
00104                             free(nextopt->options);
00105                             free(nextopt);
00106                      }
00107 
00108                      ldapabook_free(list);
00109                      fclose(fp);
00110                      return (0);
00111               }
00112 
00113               memset(p, 0, sizeof(*p));
00114               p->opts=opts;
00115 
00116               if ( (p->name=strdup(name)) != 0)
00117               {
00118                      if ((p->host=strdup(host)) != 0)
00119                      {
00120                             if ((p->port=strdup(port)) != 0)
00121                             {
00122                                    if ((p->suffix=strdup(suffix)) != 0)
00123                                    {
00124                                           if ((p->binddn=strdup(binddn))
00125                                                  != 0)
00126                                           {
00127                                                  if ((p->bindpw=strdup
00128                                                         (bindpw)) != 0)
00129                                                  {
00130                                                         if (!list)
00131                                                            list=last=p;
00132                                                         else
00133                                                            last->next=p;
00134                                                         last=p;
00135                                                         p->next=0;
00136                                                         continue;
00137                                                  }
00138                                                  free(p->binddn);
00139                                           }
00140                                           free(p->suffix);
00141                                    }
00142                                    free(p->port);
00143                             }
00144                             free(p->host);
00145                      }
00146                      free(p->name);
00147               }
00148               free(p);
00149               while ((nextopt=opts) != NULL)
00150               {
00151                      opts=nextopt->next;
00152                      free(nextopt->options);
00153                      free(nextopt);
00154               }
00155               ldapabook_free(list);
00156               fclose(fp);
00157               return (0);
00158        }
00159        fclose(fp);
00160        return (list);
00161 }
00162 
00163 void ldapabook_free(struct ldapabook *p)
00164 {
00165        while (p)
00166        {
00167               struct ldapabook *n=p->next;
00168               struct ldapabook_opts *opts;
00169 
00170               while ((opts=p->opts) != NULL)
00171               {
00172                      p->opts=opts->next;
00173                      free(opts->options);
00174                      free(opts);
00175               }
00176 
00177               free(p->bindpw);
00178               free(p->binddn);
00179               free(p->suffix);
00180               free(p->port);
00181               free(p->host);
00182               free(p->name);
00183               free(p);
00184               p=n;
00185        }
00186 }
00187 
00188 static const char hex[]="0123456789ABCDEF";
00189 
00190 static int nybble(char c)
00191 {
00192        char   *p=strchr(hex, c);
00193 
00194        if (p) return (p-hex);
00195        return (0);
00196 }
00197 
00198 static void dequote(char *p)
00199 {
00200        char *q;
00201 
00202        for (q=p; *q; q++)
00203        {
00204               if (*q == '+' && q[1] && q[2])
00205               {
00206                      *p++=nybble(q[1])*16 + nybble(q[2]);
00207                      q += 2;
00208                      continue;
00209               }
00210               *p++=*q;
00211        }
00212        *p=0;
00213 }
00214 
00215 void ldapabook_writerec(const struct ldapabook *b, FILE *fp)
00216 {
00217        struct ldapabook_opts *opts;
00218        char   *sep="\t";
00219 
00220        fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%s", b->name, b->host,
00221               b->port ? b->port:"", b->suffix,
00222               b->binddn ? b->binddn:"",
00223               b->bindpw ? b->bindpw:"");
00224 
00225        for (opts=b->opts; opts; opts=opts->next)
00226        {
00227               char *p;
00228 
00229               fprintf(fp, "%s", sep);
00230               sep=",";
00231 
00232               for (p=opts->options; *p; p++)
00233               {
00234                      if (*p <= ' ' || *p >= 127 ||
00235                          *p == ',' || *p == '+')
00236                      {
00237                             fprintf(fp, "+%02X", (int)(unsigned char)*p);
00238                             continue;
00239                      }
00240                      putc(*p, fp);
00241               }
00242        }
00243        fprintf(fp, "\n");
00244 }