Back to index

courier  0.68.2
ldaplist.c
Go to the documentation of this file.
00001 #include "config.h"
00002 /*
00003 ** Copyright 2000-2006 Double Precision, Inc.  See COPYING for
00004 ** distribution information.
00005 */
00006 
00007 /*
00008 */
00009 #include      "sqwebmail.h"
00010 #include      <stdio.h>
00011 #include      <errno.h>
00012 #include      <stdlib.h>
00013 #include      <ctype.h>
00014 #if    HAVE_UNISTD_H
00015 #include      <unistd.h>
00016 #endif
00017 #include      <string.h>
00018 #include      "cgi/cgi.h"
00019 #include      "ldapaddressbook/ldapaddressbook.h"
00020 #include      "maildir/maildircreate.h"
00021 #include      "numlib/numlib.h"
00022 #include      "htmllibdir.h"
00023 #include      "addressbook.h"
00024 #include      "pref.h"
00025 #include      "rfc2045/base64.h"
00026 
00027 #define       LOCALABOOK    "sqwebmail-ldapaddressbook"
00028 
00029 extern void output_scriptptrget();
00030 extern void output_attrencoded(const char *);
00031 extern void output_attrencoded_oknl(const char *p);
00032 extern void output_urlencoded(const char *);
00033 extern void output_attrencoded_fp(const char *, FILE *);
00034 extern void output_attrencoded_oknl_fp(const char *, FILE *);
00035 
00036 extern const char *sqwebmail_content_charset;
00037 
00038 #include      "unicode/unicode.h"
00039 
00040 void   ldaplist()
00041 {
00042 struct ldapabook *abooks[2];
00043 int    i;
00044 struct ldapabook *p;
00045 const char    *delabook=getarg("DELABOOK");
00046 const char    *sysbook=getarg("SYSBOOK");
00047 
00048        if (!delabook)       delabook="";
00049        if (!sysbook) sysbook="";
00050 
00051        if (*cgi("addabook"))
00052        {
00053               struct ldapabook newbook;
00054 
00055               memset(&newbook, 0, sizeof(newbook));
00056 
00057               newbook.name=(char *)cgi("name");
00058               newbook.host=(char *)cgi("host");
00059               newbook.port=(char *)cgi("port");
00060               newbook.suffix=(char *)cgi("suffix");
00061 
00062               if (*newbook.name && *newbook.host &&
00063                      ldapabook_add(LOCALABOOK, &newbook) < 0)
00064               {
00065                      printf("<pre>\n");
00066                      perror("ldapabook_add");
00067                      printf("</pre>\n");
00068               }
00069        }
00070 
00071        if (*cgi("delabook"))
00072        {
00073               struct maildir_tmpcreate_info createInfo;
00074               int fd;
00075               maildir_tmpcreate_init(&createInfo);
00076 
00077               createInfo.uniq="abook";
00078 
00079               if ((fd=maildir_tmpcreate_fd(&createInfo)) >= 0)
00080               {
00081                      close(fd);
00082                      unlink(createInfo.tmpname);
00083                      ldapabook_del(LOCALABOOK, createInfo.tmpname,
00084                                   cgi("ABOOK"));
00085                      maildir_tmpcreate_free(&createInfo);
00086               }
00087        }
00088 
00089        abooks[0]=ldapabook_read(LDAPADDRESSBOOK);
00090        abooks[1]=ldapabook_read(LOCALABOOK);
00091 
00092        printf("<table border=\"0\" cellpadding=\"8\" width=\"100%%\">\n");
00093        for (i=0; i<2; i++)
00094        {
00095               for (p=abooks[i]; p; p=p->next)
00096               {
00097                      printf("<tr valign=\"top\"><td align=\"right\">");
00098                      printf("<input type=\"radio\" name=\"ABOOK\"");
00099 
00100                      if (pref_ldap && strcmp(pref_ldap, p->name) == 0)
00101                             printf(" checked=\"checked\"");
00102 
00103                      printf(" value=\"");
00104                      output_attrencoded(p->name);
00105                      printf("\" /></td><td><font size=\"+1\""
00106                             " class=\"ldaplist-name\">%s</font><br />"
00107                             "&nbsp;&nbsp;&nbsp;<span class=\"tt\"><font size=\"-2\""
00108                             " class=\"ldaplist-ldapurl\">ldap://", p->name);
00109                      if (*p->binddn || *p->bindpw)
00110                      {
00111                             printf("%s", p->binddn);
00112                             if (*p->bindpw)
00113                                    printf(":%s", p->bindpw);
00114                             printf("@");
00115                      }
00116                      printf("%s", p->host);
00117                      if (atoi(p->port) != 389)
00118                             printf(":%s", p->port);
00119                      if (*p->suffix)
00120                      {
00121                      char   *q;
00122 
00123                             printf("/");
00124                             q=cgiurlencode_noeq(p->suffix);
00125                             if (q)
00126                             {
00127                                    printf("%s", q);
00128                                    free(q);
00129                             }
00130                      }
00131                      printf("</font></span>%s</td></tr>",
00132                             i ? "":sysbook);
00133               }
00134        }
00135 
00136        if (abooks[1])
00137        {
00138               printf("<tr><td></td><td>");
00139               printf("<input type=\"submit\" name=\"delabook\" value=\"%s\" />",
00140                             delabook);
00141               printf("</td></tr>\n");
00142        }
00143        printf("</table>\n");
00144        ldapabook_free(abooks[0]);
00145        ldapabook_free(abooks[1]);
00146 }
00147 
00148 static char *getfilter()
00149 {
00150        const char *p;
00151 
00152        if (!*cgi("ldapsearch"))
00153               return NULL;
00154 
00155        p=cgi("value");
00156 
00157        if (!*p)
00158               return NULL;
00159 
00160        return libmail_u_convert_toutf8(p, sqwebmail_content_charset, NULL);
00161 }
00162 
00163 struct search_info {
00164        FILE *fpw;
00165        char errmsgbuf[1024];
00166        unsigned counter;
00167 };
00168 
00169 int    ldapsearch()
00170 {
00171        char *p;
00172 
00173        if (*cgi("ABOOK") == 0) return (-1);
00174 
00175        if ((p=getfilter()) == NULL)
00176               return -1;
00177        free(p);
00178        return 0;
00179 }
00180 
00181 static void save_errmsg(const char *errmsg,
00182                      void *voidarg)
00183 {
00184        struct search_info *si=(struct search_info *)voidarg;
00185 
00186        si->errmsgbuf[0]=0;
00187        strncat(si->errmsgbuf, errmsg, sizeof(si->errmsgbuf)-1);
00188 }
00189 
00190 
00191 static int parsesearch(const char *cn, const char *mail,
00192                      void *voidarg)
00193 {
00194        struct search_info *si=(struct search_info *)voidarg;
00195 
00196        char   numbuf[NUMBUFSIZE];
00197        char   numbuf2[NUMBUFSIZE+10];
00198 
00199        char *cn_native;
00200 
00201        cn_native=libmail_u_convert_fromutf8(cn, sqwebmail_content_charset,
00202                                         NULL);
00203 
00204        if (cn_native)
00205               cn=cn_native;
00206  
00207        fprintf(si->fpw, "<tr valign=\"top\"><td><input type=\"checkbox\" "
00208                             "name=\"%s\" value=\"&lt;",
00209               strcat(strcpy(numbuf2, "ADDY"),
00210                      libmail_str_size_t(si->counter++, numbuf)));
00211 
00212        output_attrencoded_fp(mail, si->fpw);
00213        fprintf(si->fpw, "&gt;");
00214 
00215        output_attrencoded_fp(cn, si->fpw);
00216 
00217        fprintf(si->fpw, "&quot;\" /></td><td><font size=\"+1\" class=\"ldapsearch-name\">\"");
00218 
00219        output_attrencoded_fp(cn, si->fpw);
00220        fprintf(si->fpw,
00221               "\"</font><font size=\"+1\" class=\"ldapsearch-addr\">&lt;");
00222        output_attrencoded_fp(mail, si->fpw);
00223        fprintf(si->fpw, "&gt;</font>");
00224        fprintf(si->fpw, "</td></tr>\n");
00225 
00226        if (cn_native)
00227               free(cn_native);
00228        return 0;
00229 }
00230 
00231 void   doldapsearch()
00232 {
00233 char   *f;
00234 struct ldapabook *abooks[2];
00235 const struct ldapabook *ptr;
00236 
00237        abooks[0]=ldapabook_read(LDAPADDRESSBOOK);
00238        abooks[1]=ldapabook_read(LOCALABOOK);
00239 
00240        ptr=ldapabook_find(abooks[0], cgi("ABOOK"));
00241        if (!ptr)
00242               ptr=ldapabook_find(abooks[1], cgi("ABOOK"));
00243 
00244        if (ptr && (f=getfilter()) != 0)
00245        {
00246               char   *tmpname=0;
00247               struct search_info si;
00248               struct maildir_tmpcreate_info createInfo;
00249 
00250               pref_setldap(ptr->name);
00251               printf("<pre>");
00252               fflush(stdout);
00253 
00254 
00255               si.fpw=NULL;
00256               si.errmsgbuf[0]=0;
00257               si.counter=0;
00258 
00259               maildir_tmpcreate_init(&createInfo);
00260               createInfo.uniq="ldap";
00261               createInfo.doordie=1;
00262 
00263               si.fpw=maildir_tmpcreate_fp(&createInfo);
00264 
00265               tmpname=createInfo.tmpname;
00266               createInfo.tmpname=NULL;
00267               maildir_tmpcreate_free(&createInfo);
00268 
00269               if (ldapabook_search(ptr, LDAPSEARCH, f, parsesearch,
00270                                  save_errmsg, &si) == 0)
00271               {
00272                      int    c;
00273 
00274                      printf("</pre>");
00275 
00276                      if (si.counter == 0)
00277                             printf("%s", getarg("NOTFOUND"));
00278 
00279                      printf("<table border=\"0\" cellpadding=\"4\">\n");
00280               
00281                      fflush(si.fpw);
00282                      rewind(si.fpw);
00283                      while ((c=getc(si.fpw)) != EOF)
00284                             putchar(c);
00285 
00286                      printf("<tr><td colspan=\"2\"><hr width=\"90%%\" />"
00287                             "<input type=\"hidden\" name=\"ADDYCNT\" value=\"%u\" />\n"
00288                             "</td></tr>\n", si.counter);
00289                      printf("<tr><td colspan=\"2\"><table>");
00290 
00291                      printf("<tr><td align=\"right\">%s</td><td>"
00292                             "<select name=\"nick1\"><option value=\"\"></option>\n", getarg("ADD"));
00293                      ab_listselect_fp(stdout);
00294                      printf("</select></td></tr>\n");
00295                      printf("<tr><td align=\"right\">%s</td><td>"
00296                             "<input type=\"text\" name=\"nick2\" /></td></tr>\n", getarg("CREATE"));
00297                      printf("<tr><td></td><td>"
00298                             "<input type=\"submit\" name=\"import\" value=\"%s\" /></td></tr>",
00299                             getarg("SUBMIT"));
00300                      printf("</table></td></tr></table>\n");
00301               }
00302               else if (si.errmsgbuf[0])
00303               {
00304                      output_attrencoded_oknl(si.errmsgbuf);
00305                      printf("</pre>");
00306 
00307               }
00308               fclose(si.fpw);
00309 
00310               if (tmpname)
00311               {
00312                      unlink(tmpname);
00313                      free(tmpname);
00314               }
00315        }
00316 }