"Fossies" - the Fresh Open Source Software Archive

Member "courier-1.2.2/libs/ldapaddressbook/abookread.c" (20 Jan 2022, 4033 Bytes) of package /linux/misc/courier-1.2.2.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "abookread.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 **
    3 ** Copyright 2003-2006, Double Precision Inc.
    4 **
    5 ** See COPYING for distribution information.
    6 */
    7 
    8 #include    "config.h"
    9 #include    "ldapaddressbook.h"
   10 
   11 #include    <stdio.h>
   12 #include    <string.h>
   13 #include    <stdlib.h>
   14 
   15 static void dequote(char *);
   16 
   17 struct ldapabook *ldapabook_read(const char *filename)
   18 {
   19 char    buf[BUFSIZ];
   20 FILE    *fp;
   21 struct  ldapabook *list, *last;
   22 char    *s;
   23 char    *name;
   24 char    *host;
   25 char    *port;
   26 char    *suffix;
   27 char    *binddn;
   28 char    *bindpw;
   29 
   30     if ((fp=fopen(filename, "r")) == 0) return (0);
   31 
   32     list=last=0;
   33     while (fgets(buf, sizeof(buf), fp))
   34     {
   35     struct  ldapabook *p;
   36 
   37     struct  ldapabook_opts *opts=NULL, *lastopt=NULL;
   38     struct ldapabook_opts *nextopt;
   39 
   40         s=strchr(buf, '\n');
   41         if (s)  *s=0;
   42         if ((s=strchr(buf, '#')) != 0)  *s=0;
   43         name=buf;
   44         s=strchr(buf, '\t');
   45         if (!s) continue;
   46         *s++=0;
   47         host=s;
   48         s=strchr(s, '\t');
   49         if (s) *s++=0;
   50         port=s;
   51         if (s)  s=strchr(s, '\t');
   52         if (s)  *s++=0;
   53         suffix=s;
   54         if (s)  s=strchr(s, '\t');
   55         if (s)  *s++=0;
   56         binddn=s;
   57         if (s)  s=strchr(s, '\t');
   58         if (s)  *s++=0;
   59         bindpw=s;
   60         if (!port || !*port)    port="389";
   61         if (!suffix)    suffix="";
   62         if (!binddn)    binddn="";
   63         if (!bindpw)    bindpw="";
   64 
   65         if (s)  s=strchr(s, '\t');
   66         if (s)  *s++=0;
   67 
   68         while (s && *s)
   69         {
   70             char *t;
   71 
   72             t=strchr(s, ',');
   73             if (t)
   74                 *t++=0;
   75 
   76             if ((nextopt=(struct ldapabook_opts *)
   77                  malloc(sizeof(struct ldapabook_opts))) == NULL
   78                 || (nextopt->options=strdup(s)) == NULL)
   79             {
   80                 if (nextopt)
   81                     free(nextopt);
   82                 ldapabook_free(list);
   83                 fclose(fp);
   84                 return (NULL);
   85             }
   86 
   87             dequote(nextopt->options);
   88             if (!lastopt)
   89                 opts=nextopt;
   90             else
   91                 lastopt->next=nextopt;
   92             nextopt->next=NULL;
   93             lastopt=nextopt;
   94             s=t;
   95         }
   96 
   97         if ((p=malloc(sizeof(struct ldapabook))) == 0)
   98         {
   99             struct ldapabook_opts *nextopt;
  100 
  101             while ((nextopt=opts) != NULL)
  102             {
  103                 opts=nextopt->next;
  104                 free(nextopt->options);
  105                 free(nextopt);
  106             }
  107 
  108             ldapabook_free(list);
  109             fclose(fp);
  110             return (0);
  111         }
  112 
  113         memset(p, 0, sizeof(*p));
  114         p->opts=opts;
  115 
  116         if ( (p->name=strdup(name)) != 0)
  117         {
  118             if ((p->host=strdup(host)) != 0)
  119             {
  120                 if ((p->port=strdup(port)) != 0)
  121                 {
  122                     if ((p->suffix=strdup(suffix)) != 0)
  123                     {
  124                         if ((p->binddn=strdup(binddn))
  125                             != 0)
  126                         {
  127                             if ((p->bindpw=strdup
  128                                 (bindpw)) != 0)
  129                             {
  130                                 if (!list)
  131                                    list=last=p;
  132                                 else
  133                                    last->next=p;
  134                                 last=p;
  135                                 p->next=0;
  136                                 continue;
  137                             }
  138                             free(p->binddn);
  139                         }
  140                         free(p->suffix);
  141                     }
  142                     free(p->port);
  143                 }
  144                 free(p->host);
  145             }
  146             free(p->name);
  147         }
  148         free(p);
  149         while ((nextopt=opts) != NULL)
  150         {
  151             opts=nextopt->next;
  152             free(nextopt->options);
  153             free(nextopt);
  154         }
  155         ldapabook_free(list);
  156         fclose(fp);
  157         return (0);
  158     }
  159     fclose(fp);
  160     return (list);
  161 }
  162 
  163 void ldapabook_free(struct ldapabook *p)
  164 {
  165     while (p)
  166     {
  167         struct ldapabook *n=p->next;
  168         struct ldapabook_opts *opts;
  169 
  170         while ((opts=p->opts) != NULL)
  171         {
  172             p->opts=opts->next;
  173             free(opts->options);
  174             free(opts);
  175         }
  176 
  177         free(p->bindpw);
  178         free(p->binddn);
  179         free(p->suffix);
  180         free(p->port);
  181         free(p->host);
  182         free(p->name);
  183         free(p);
  184         p=n;
  185     }
  186 }
  187 
  188 static const char hex[]="0123456789ABCDEF";
  189 
  190 static int nybble(char c)
  191 {
  192     char    *p=strchr(hex, c);
  193 
  194     if (p)  return (p-hex);
  195     return (0);
  196 }
  197 
  198 static void dequote(char *p)
  199 {
  200     char *q;
  201 
  202     for (q=p; *q; q++)
  203     {
  204         if (*q == '+' && q[1] && q[2])
  205         {
  206             *p++=nybble(q[1])*16 + nybble(q[2]);
  207             q += 2;
  208             continue;
  209         }
  210         *p++=*q;
  211     }
  212     *p=0;
  213 }
  214 
  215 void ldapabook_writerec(const struct ldapabook *b, FILE *fp)
  216 {
  217     struct  ldapabook_opts *opts;
  218     char    *sep="\t";
  219 
  220     fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%s", b->name, b->host,
  221         b->port ? b->port:"", b->suffix,
  222         b->binddn ? b->binddn:"",
  223         b->bindpw ? b->bindpw:"");
  224 
  225     for (opts=b->opts; opts; opts=opts->next)
  226     {
  227         char *p;
  228 
  229         fprintf(fp, "%s", sep);
  230         sep=",";
  231 
  232         for (p=opts->options; *p; p++)
  233         {
  234             if (*p <= ' ' || *p >= 127 ||
  235                 *p == ',' || *p == '+')
  236             {
  237                 fprintf(fp, "+%02X", (int)(unsigned char)*p);
  238                 continue;
  239             }
  240             putc(*p, fp);
  241         }
  242     }
  243     fprintf(fp, "\n");
  244 }