Back to index

courier  0.68.2
Classes | Defines | Functions
ldapaddressbook.h File Reference
#include <stdio.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ldapabook
struct  ldapabook_opts

Defines

#define SASL_SECURITY_PROPERTIES   'O'
#define SASL_AUTHENTICATION_ID   'U'
#define SASL_AUTHENTICATION_RID   'X' /* u:username, or dn:dn */
#define SASL_AUTHENTICATION_MECHANISM   'Y'
#define SASL_STARTTLS   'Z'

Functions

struct ldapabookldapabook_read (const char *)
void ldapabook_free (struct ldapabook *)
struct ldapabookldapabook_find (const struct ldapabook *, const char *)
int ldapabook_add (const char *, const struct ldapabook *)
int ldapabook_del (const char *, const char *, const char *)
int ldapabook_search (const struct ldapabook *b, const char *script, const char *search, int(*callback_func)(const char *utf8_name, const char *address, void *callback_arg), void(*callback_err)(const char *errmsg, void *callback_arg), void *callback_arg)
void ldapabook_writerec (const struct ldapabook *, FILE *)

Class Documentation

struct ldapabook

Definition at line 33 of file ldapaddressbook.h.

Collaboration diagram for ldapabook:
Class Members
char * binddn
char * bindpw
char * host
char * name
struct ldapabook * next
struct ldapabook_opts * opts
char * port
char * suffix
struct ldapabook_opts

Definition at line 47 of file ldapaddressbook.h.

Collaboration diagram for ldapabook_opts:
Class Members
struct ldapabook_opts * next
char * options

Define Documentation

#define SASL_AUTHENTICATION_ID   'U'

Definition at line 55 of file ldapaddressbook.h.

Definition at line 57 of file ldapaddressbook.h.

#define SASL_AUTHENTICATION_RID   'X' /* u:username, or dn:dn */

Definition at line 56 of file ldapaddressbook.h.

#define SASL_SECURITY_PROPERTIES   'O'

Definition at line 54 of file ldapaddressbook.h.

#define SASL_STARTTLS   'Z'

Definition at line 58 of file ldapaddressbook.h.


Function Documentation

int ldapabook_add ( const char *  ,
const struct ldapabook  
)

Definition at line 14 of file abookadd.c.

{
FILE   *fp=fopen(f, "a");

       if (!fp)      return (-1);

       ldapabook_writerec(a, fp);

       if (fflush(fp) || fclose(fp))
       {
              fclose(fp);
              return (-1);
       }
       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ldapabook_del ( const char *  ,
const char *  ,
const char *   
)

Definition at line 15 of file abookdel.c.

{
/* This is cheating, but we won't really have many abooks, come on... */
struct ldapabook *a=ldapabook_read(filename), *b;

FILE   *fp;

       if (!a)       return (0);

       if ((fp=fopen(tempname, "w")) == 0)
       {
              ldapabook_free(a);
              return (-1);
       }

       for (b=a; b; b=b->next)
       {
              if (strcmp(b->name, delname) == 0) continue;

              ldapabook_writerec(b, fp);
       }
       ldapabook_free(a);

       if (fflush(fp) || fclose(fp))
       {
              fclose(fp);
              unlink(tempname);
              return (-1);
       }

       if (rename(tempname, filename))
       {
              unlink(tempname);
              return (-1);
       }

       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct ldapabook* ldapabook_find ( const struct ldapabook ,
const char *   
) [read]

Definition at line 14 of file abookfind.c.

{
       while (l)
       {
              if (strcmp(l->name, n) == 0)       return (l);

              l=l->next;
       }
       return (0);
}

Here is the caller graph for this function:

void ldapabook_free ( struct ldapabook )

Definition at line 163 of file abookread.c.

{
       while (p)
       {
              struct ldapabook *n=p->next;
              struct ldapabook_opts *opts;

              while ((opts=p->opts) != NULL)
              {
                     p->opts=opts->next;
                     free(opts->options);
                     free(opts);
              }

              free(p->bindpw);
              free(p->binddn);
              free(p->suffix);
              free(p->port);
              free(p->host);
              free(p->name);
              free(p);
              p=n;
       }
}

Here is the caller graph for this function:

struct ldapabook* ldapabook_read ( const char *  ) [read]

Definition at line 17 of file abookread.c.

{
char   buf[BUFSIZ];
FILE   *fp;
struct ldapabook *list, *last;
char   *s;
char   *name;
char   *host;
char   *port;
char   *suffix;
char   *binddn;
char   *bindpw;

       if ((fp=fopen(filename, "r")) == 0)       return (0);

       list=last=0;
       while (fgets(buf, sizeof(buf), fp))
       {
       struct ldapabook *p;

       struct ldapabook_opts *opts=NULL, *lastopt=NULL;
       struct ldapabook_opts *nextopt;

              s=strchr(buf, '\n');
              if (s) *s=0;
              if ((s=strchr(buf, '#')) != 0)     *s=0;
              name=buf;
              s=strchr(buf, '\t');
              if (!s)       continue;
              *s++=0;
              host=s;
              s=strchr(s, '\t');
              if (s) *s++=0;
              port=s;
              if (s) s=strchr(s, '\t');
              if (s) *s++=0;
              suffix=s;
              if (s) s=strchr(s, '\t');
              if (s) *s++=0;
              binddn=s;
              if (s) s=strchr(s, '\t');
              if (s) *s++=0;
              bindpw=s;
              if (!port || !*port) port="389";
              if (!suffix)  suffix="";
              if (!binddn)  binddn="";
              if (!bindpw)  bindpw="";

              if (s) s=strchr(s, '\t');
              if (s) *s++=0;

              while (s && *s)
              {
                     char *t;

                     t=strchr(s, ',');
                     if (t)
                            *t++=0;

                     if ((nextopt=(struct ldapabook_opts *)
                          malloc(sizeof(struct ldapabook_opts))) == NULL
                         || (nextopt->options=strdup(s)) == NULL)
                     {
                            if (nextopt)
                                   free(nextopt);
                            ldapabook_free(list);
                            fclose(fp);
                            return (NULL);
                     }

                     dequote(nextopt->options);
                     if (!lastopt)
                            opts=nextopt;
                     else
                            lastopt->next=nextopt;
                     nextopt->next=NULL;
                     lastopt=nextopt;
                     s=t;
              }

              if ((p=malloc(sizeof(struct ldapabook))) == 0)
              {
                     struct ldapabook_opts *nextopt;

                     while ((nextopt=opts) != NULL)
                     {
                            opts=nextopt->next;
                            free(nextopt->options);
                            free(nextopt);
                     }

                     ldapabook_free(list);
                     fclose(fp);
                     return (0);
              }

              memset(p, 0, sizeof(*p));
              p->opts=opts;

              if ( (p->name=strdup(name)) != 0)
              {
                     if ((p->host=strdup(host)) != 0)
                     {
                            if ((p->port=strdup(port)) != 0)
                            {
                                   if ((p->suffix=strdup(suffix)) != 0)
                                   {
                                          if ((p->binddn=strdup(binddn))
                                                 != 0)
                                          {
                                                 if ((p->bindpw=strdup
                                                        (bindpw)) != 0)
                                                 {
                                                        if (!list)
                                                           list=last=p;
                                                        else
                                                           last->next=p;
                                                        last=p;
                                                        p->next=0;
                                                        continue;
                                                 }
                                                 free(p->binddn);
                                          }
                                          free(p->suffix);
                                   }
                                   free(p->port);
                            }
                            free(p->host);
                     }
                     free(p->name);
              }
              free(p);
              while ((nextopt=opts) != NULL)
              {
                     opts=nextopt->next;
                     free(nextopt->options);
                     free(nextopt);
              }
              ldapabook_free(list);
              fclose(fp);
              return (0);
       }
       fclose(fp);
       return (list);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ldapabook_search ( const struct ldapabook b,
const char *  script,
const char *  search,
int(*)(const char *utf8_name, const char *address, void *callback_arg)  callback_func,
void(*)(const char *errmsg, void *callback_arg)  callback_err,
void *  callback_arg 
)

Definition at line 30 of file abooksearch.c.

{
       int    pipefd[2];
       pid_t  p;
       const char *argv[40];
       char   buf1[BUFSIZ];
       char   buf2[BUFSIZ];
       FILE   *t, *fp;
       int    rc_code=0;
       pid_t p2;
       int waitstat;

       signal(SIGCHLD, SIG_DFL);

       if (pipe(pipefd) < 0)       return (-1);

       if ((t=tmpfile()) == NULL)
       {
              close(pipefd[0]);
              close(pipefd[1]);
              return (-1);
       }

       if ((p=fork()) == -1)
       {
              fclose(t);
              close(pipefd[0]);
              close(pipefd[1]);
              return (-1);
       }

       if (p == 0)
       {
              dup2(pipefd[1], 1);
              close(pipefd[0]);
              close(pipefd[1]);

              dup2(fileno(t), 2);
              fclose(t);

              argv[0]=script;
              argv[1]=b->host;
              argv[2]=b->port;
              argv[3]=b->suffix;
              argv[4]=search;
              argv[5]=NULL;

              execvp(script, (char **)argv);
              perror(script);
              exit(1);
       }

       fp=fdopen(pipefd[0], "r");
       close(pipefd[1]);

       if (!fp)
       {
              sprintf(buf1, "%1.256s", strerror(errno));

              close(pipefd[0]);

              while ((p2=wait(NULL)) != p)
                     ;
              fclose(t);

              (*callback_err)(buf1, callback_arg);
              return -1;
       }

       while (fgets(buf1, sizeof(buf1), fp) != NULL &&
              fgets(buf2, sizeof(buf2), fp) != NULL)
       {
              char *p=strchr(buf1, '\n');

              if (p) *p=0;

              p=strchr(buf2, '\n');
              if (p) *p=0;

              if (rc_code == 0)
                     rc_code=(*callback_func)(buf1, buf2, callback_arg);
       }

       fclose(fp);
       close(pipefd[0]);

       while ((p2=wait(&waitstat)) != p)
              ;

       if (waitstat && rc_code == 0)
       {
              rc_code= -1;
              fseek(t, 0L, SEEK_SET);

              if (fgets(buf1, sizeof(buf1), t))
              {
                     char *p=strchr(buf1, '\n');
                     if (p) *p=0;

                     (*callback_err)(buf1, callback_arg);
              }
       }
       fclose(t);
       return rc_code;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ldapabook_writerec ( const struct ldapabook ,
FILE *   
)

Definition at line 215 of file abookread.c.

{
       struct ldapabook_opts *opts;
       char   *sep="\t";

       fprintf(fp, "%s\t%s\t%s\t%s\t%s\t%s", b->name, b->host,
              b->port ? b->port:"", b->suffix,
              b->binddn ? b->binddn:"",
              b->bindpw ? b->bindpw:"");

       for (opts=b->opts; opts; opts=opts->next)
       {
              char *p;

              fprintf(fp, "%s", sep);
              sep=",";

              for (p=opts->options; *p; p++)
              {
                     if (*p <= ' ' || *p >= 127 ||
                         *p == ',' || *p == '+')
                     {
                            fprintf(fp, "+%02X", (int)(unsigned char)*p);
                            continue;
                     }
                     putc(*p, fp);
              }
       }
       fprintf(fp, "\n");
}

Here is the caller graph for this function: