Back to index

courier  0.68.2
Defines | Functions | Variables
logindomainlist.c File Reference
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "logindomainlist.h"

Go to the source code of this file.

Defines

#define LINELEN   500
#define NUM_WC_EN_MODS   2

Functions

static char * mystrsep (char **stringp, int delim)
static int ldl_haswildcard (char *stringp, int wildcard)
static void ldl_getfields (char *statementp, char *firstfieldp, char *secondfieldp, char *thirdfieldp)
static int ldl_invalidstatement (char *statementp)
static int extract_wildcardvalue (char *wildcardedstringp, char *realstringp, int wildcard)
static int replace_wildcard (char *wildcardedstringp, char *wildcardvaluep, int wildcard)
static void get_defaultdomainfields (FILE *fp, char *domainp, char *modifyp)
static void ldl_displayhiddenfield (char *defaultdomainp)
static void ldl_displaytextfield (char *defaultdomainp)
static void ldl_displaydropdown (FILE *fp, char *defaultdomainp, char *defaultgroupp)
void print_logindomainlist (FILE *fp)

Variables

static const int g_delim = ':'
static const int g_wildcard_char = '*'
static const char g_wc_en_mods [NUM_WC_EN_MODS][2] = { "@", "-" }

Define Documentation

#define LINELEN   500

Definition at line 19 of file logindomainlist.c.

#define NUM_WC_EN_MODS   2

Definition at line 20 of file logindomainlist.c.


Function Documentation

static int extract_wildcardvalue ( char *  wildcardedstringp,
char *  realstringp,
int  wildcard 
) [static]

Definition at line 201 of file logindomainlist.c.

                                                                                            {
       char wildcardedstring[LINELEN]="";
       char *beforewildcardp;
       char *afterwildcardp;
       char *wildcardedstringpp=NULL;


       /* Continue only if there is actually a wildcard character in wildcardedstring */
       if (ldl_haswildcard(wildcardedstringp, wildcard)) {
              /* Copy argument to buffer so as not to modify the original. */
              strcpy(wildcardedstring, wildcardedstringp);

              /* create a pointer to a pointer of a copy*/     
              wildcardedstringpp = wildcardedstring;
              

              /* tokenize wildcardstring with '\0's */
              beforewildcardp=mystrsep(&wildcardedstringpp, wildcard);
              afterwildcardp=mystrsep(&wildcardedstringpp, wildcard);


              if (beforewildcardp != NULL && strcmp(beforewildcardp, "") != 0)
              {
                     /* If beforewildcardp string exists at the absolute
                      * beginning of realstring */
                     if (strstr(realstringp, beforewildcardp) == realstringp)
                     {
                            char *tmpp=realstringp;
                            char *p;

                            /* move pointer to the end of beforewildcardp in
                             * realstring. */
                            tmpp=tmpp + strlen(beforewildcardp);

                            /* Now, "delete" beforewildcardp from the very
                             * beginning of realstring. Note that we're not
                             * actually deleting it. We're just copying the
                             * remaining string in realstring from the
                             * location that tmpp points to in realstring.
                             * However, this has the same effect as if we
                             * had somehow "deleted" beforewildcardp from
                             * the beginning of realstring. */
                            
                            p=realstringp;

                            while ((*p++= *tmpp++) != 0)
                                   ;
                     }
                     else
                     {
                            /* if beforewildcardp does not exist at the
                             * beginning of realstringp, return non zero. */
                            return 0;
                     }
              }


              if (afterwildcardp != NULL && strcmp(afterwildcardp, "") != 0)
              {
                     /* If afterwildcardp exists at the very end of realstring */
                     size_t n=strlen(realstringp);
                     size_t o=strlen(afterwildcardp);

                     if (n >= o &&
                         strcmp(realstringp+n-o, afterwildcardp) == 0)
                     {
                            char *tmpp=realstringp;

                            /* move temp pointer to the end of the NUL
                             * terminated string in realstringp, then
                             * backspace the length of afterwildcardp
                             * and write a NUL character.
                             *
                             * This effectively "deletes"
                             * afterwildcardp from realstringp as far
                             * as any string manipulation functions
                             * that rely on a terminating NUL character
                             * are concerned. */
                            tmpp=tmpp + n - o;
                            *tmpp='\0';
                     }
                     else
                     {
                            /* if afterwildcardp does not exist at the
                             * end of realstringp, return non zero. */
                            return 0;
                     }
              }


              /* if we made it here, then we must have
               * successfully removed everything but
               * the value of the wildcard in
               * wildcardedstringp from realstringp.
               *
               * Return zero. */
              return 1;
       } else {
              /* no wildcard in wildcardedstringp */
              return 0;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void get_defaultdomainfields ( FILE *  fp,
char *  domainp,
char *  modifyp 
) [static]

Definition at line 397 of file logindomainlist.c.

{
       char buf[LINELEN]="";
       char *serveraddr=getenv("SERVER_ADDR");
       char *httphost=getenv("HTTP_HOST");

       if (!serveraddr) serveraddr="";
       if (!httphost) httphost="";

       /* rewind the file pointer */
       rewind(fp);

       /* Read one line at a time from fp */
       while (fgets(buf, sizeof(buf), fp))
       {
              int count = 0;
              char firstfield[LINELEN]="";
              char secondfield[LINELEN]="";
              char thirdfield[LINELEN]="";
              /* get the position of the newline (if it exists) */
              char *p=strchr(buf, '\n');

              /* replace the newline with NUL */
              if (*p) *p='\0';

              /* ignore comments, empty lines, etc... */
              if (ldl_invalidstatement(buf)) continue;


              /* get individual fields from line */
              ldl_getfields(buf, firstfield, secondfield, thirdfield);

              /* process any wildcard enabled modifiers */
              for (count = 0; count < NUM_WC_EN_MODS; count++)
              {
                     const char *current_modifier = &g_wc_en_mods[count][0];
              
                     /* If this record is using wildcard domain mapping... */
                     if (strcmp(thirdfield, current_modifier) == 0)
                     {
                            int  wildcard = g_wildcard_char;

                            /* If either the first or second field contains a wildcard char */
                            if (ldl_haswildcard(firstfield, wildcard) ||
                                ldl_haswildcard(secondfield, wildcard))
                            {
                                   char finaldomain[LINELEN] = "";
                                   char wildcardvalue[LINELEN] = "";
                                   char tempbuf[LINELEN] = "";

                                   /* extract the string that the wildcard in
                                    * secondfield represents when compared
                                    * with currentdomain */

                                   /* seed wildcardvalue with contents of
                                    * httphost. Sorry - no IP wildcarding. */
                                   strcpy( wildcardvalue, httphost);

                                   /* if secondfield and wildcardvalue match,
                                    * extract_wildcardvalue will "chop off"
                                    * the text before and after the wildcard
                                    * character in secondfield from
                                    * wildcardvalue */
                                   if (extract_wildcardvalue(secondfield, wildcardvalue, wildcard))
                                   {
                                          /* wildcardvalue may now contain the final
                                           * domain name to use as default domain if
                                           * firstfield does NOT contain a wildcard.
                                           *
                                           * That is why we save the contents of
                                           * wildcardvalue in finaldomain here. */
                                          strcpy(finaldomain, wildcardvalue);
                                   }
                                   else
                                   {
                                          /* Make sure this wildcardless record
                                           * actually matches httphost before doing
                                           * anything else */
                                          if (strcmp(secondfield, httphost) != 0) continue;

                                          /* we don't have a wildcard in the second
                                           * field, so just do a straight copy */
                                          strcpy(finaldomain, secondfield);
                                   }

                                   
                                   /* seed tempbuf with contents of firstfield */
                                   strcpy(tempbuf, firstfield);

                                   /* Replace wildcard character in tempbuf
                                    * with contents of wildcardvalue */
                                   if (replace_wildcard(tempbuf, wildcardvalue, wildcard))
                                   {

                                          /* The above replace_wildcard() must have
                                           * been seccessful if we're still here,
                                           * so save the contents of tempbuf in
                                           * finaldomain. */
                                          strcpy(finaldomain, tempbuf);
                                   }
                                   else
                                   {
                                          /* we don't have a wildcard in the first
                                           * field, so just do a straight copy */
                                          strcpy(finaldomain, firstfield);
                                   }

                                   /* return default domain */
                                   strcpy(domainp, finaldomain);
                                   strcpy(modifyp, thirdfield);
                                   return;
                            }
                            else
                            {
                                   /* Fall through to matching against serveraddr
                                    * and httphost if no wildcards exist in either
                                    * firstfield or secondfield */
                            }
                     }
              }

                     
              /* This is reached if the third field (modifier) is NOT a wildcard */
              /* compare second field against CGI variables */
              if (strcmp(secondfield, serveraddr) == 0 ||
                  strcmp(secondfield, httphost) == 0)
              {
                     strcpy(domainp, firstfield);
                     strcpy(modifyp, thirdfield);
                     return;
              }

       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void ldl_displaydropdown ( FILE *  fp,
char *  defaultdomainp,
char *  defaultgroupp 
) [static]

Definition at line 594 of file logindomainlist.c.

                                                                                      {

       char buf[LINELEN];
       
       /* This is a flag that is toggled once the first match has been
        * made. */
       int firstmatch=0;


       /* rewind file pointer */
       rewind(fp);


       /* Read one line at a time from fp */
       while (fgets(buf, sizeof(buf), fp))
       {
              char firstfield[LINELEN]="";
              char secondfield[LINELEN]="";
              char thirdfield[LINELEN]="";
              /* get the position of the newline (if it exists) */
              char *p=strchr(buf, '\n');

              /* replace the newline with NUL */
              if (*p) *p='\0';

              /* ignore comments, empty lines, etc... */
              if (ldl_invalidstatement(buf)) continue;

              /* get individual fields from file */
              ldl_getfields(buf, firstfield, secondfield, thirdfield);

              /* only display this option if it's group field (thirdfield)
               * is identical to defaultgroupp. */
              if (strcmp(thirdfield, defaultgroupp) == 0)
              {
                     /* Only display the select tag the first time we
                      * find a match. */
                     if (firstmatch == 0)
                     {
                            printf("@<select name=\"logindomain\"><option value=\"\">&nbsp;</option>\n");
                            firstmatch=1;
                     }

                     /* Do not display options with empty first fields. */
                     if (strlen(firstfield) > 0)
                     {
                            /* If 'defaultdomainp' is identical to firstfield
                             * then set this option as 'selected'. */
                            if (strcmp(defaultdomainp, firstfield) == 0)
                            {
                                   printf("<option value=\"%s\" selected=\"selected\">%s</option>\n",
                                                 firstfield, firstfield);
                            }
                            else
                            {
                                   printf("<option value=\"%s\">%s</option>\n",
                                                 firstfield, firstfield);
                            }
                     }
              }      
       }

       /* Display a closing select tag only if we displayed
        * a starting select tag */
       if (firstmatch > 0) printf("</select>");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void ldl_displayhiddenfield ( char *  defaultdomainp) [static]

Definition at line 541 of file logindomainlist.c.

                                                          {
       
       if (strlen(defaultdomainp) > 0)
       {
              /* This is displayed only if defaultdomain is NOT
               * empty */
              printf("<input type=\"hidden\" name=\"logindomain\" value=\"%s\" />@%s",
                            defaultdomainp, defaultdomainp);
       }
       else
       {
              /* Do nothing. This is here so that nothing will
               * be displayed if defaultdomain is empty. */
       }
}

Here is the caller graph for this function:

static void ldl_displaytextfield ( char *  defaultdomainp) [static]

Definition at line 566 of file logindomainlist.c.

                                                        {
       
       if (strlen(defaultdomainp) > 0)
       {
              /* This is displayed only if defaultdomain is NOT
               * empty */
              printf("@<input type=\"text\" name=\"logindomain\" value=\"%s\" size=\"%d\" />",
                            defaultdomainp, (int)strlen(defaultdomainp)+2);
       }
       else
       {
              /* Do nothing. This is here so that nothing will
               * be displayed if defaultdomain is empty. */
       }
}

Here is the caller graph for this function:

static void ldl_getfields ( char *  statementp,
char *  firstfieldp,
char *  secondfieldp,
char *  thirdfieldp 
) [static]

Definition at line 103 of file logindomainlist.c.

{
       int  delim=g_delim;
       char *tempfirst;
       char *tempsecond;
       char *tempthird;
       char *bufp=statementp;


       /* get pointers to individual fields from file */
       tempfirst=mystrsep(&bufp, delim);
       tempsecond=mystrsep(&bufp, delim);
       tempthird=mystrsep(&bufp, delim);

       /* fix NULL pointers */
       if (tempfirst == NULL) tempfirst="";
       if (tempsecond == NULL) tempsecond="";
       if (tempthird == NULL) tempthird="";

       /* copy strings */
       strcpy(firstfieldp, tempfirst);
       strcpy(secondfieldp, tempsecond);
       strcpy(thirdfieldp, tempthird);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int ldl_haswildcard ( char *  stringp,
int  wildcard 
) [static]

Definition at line 77 of file logindomainlist.c.

{
       char *wildcardp;

       /* get pointer to wildcard within stringp */
       wildcardp = strchr(stringp, wildcard);

       if (wildcardp == NULL) return 0;

       return 1;
}

Here is the caller graph for this function:

static int ldl_invalidstatement ( char *  statementp) [static]

Definition at line 143 of file logindomainlist.c.

{
       const int TRUE = 1;
       const int FALSE = 0;
       
       /* comments aren't valid statements */
       if (statementp[0] == '#') return TRUE;

       /* empty lines aren't valid statements */
       if (statementp[0] == ' ' ||
           statementp[0] == '\t' ||
           statementp[0] == '\n') return TRUE;

       /* extra long lines aren't valid statements either... */
       if (strlen(statementp) > LINELEN) return TRUE;

       /* might be a valid statement line */
       return FALSE;
}

Here is the caller graph for this function:

static char* mystrsep ( char **  stringp,
int  delim 
) [static]

Definition at line 43 of file logindomainlist.c.

{
       char *orig_stringp=*stringp;

       /* Make sure we return NULL if we are given NULL */
       if (*stringp == NULL) return orig_stringp;

       /* Locate first occurance of delim in stringp */
       *stringp=strchr(*stringp, delim);
       
       /* If no more delimiters were found, return the last substring */
       if (*stringp == NULL) return orig_stringp;

       /* Set that first occurance to NUL */
       **stringp='\0';
       
       /* move pointer in front of NUL character */
       ++(*stringp);
       
       /* Return original value of *stringp */
       return orig_stringp;

}

Here is the caller graph for this function:

void print_logindomainlist ( FILE *  fp)

Definition at line 670 of file logindomainlist.c.

{
       char defaultdomain[LINELEN]="";
       char modifierfield[LINELEN]="";

       /* get default domain field and the corresponding default
        * group field (if applicable) from fp. */       
       get_defaultdomainfields(fp, defaultdomain, modifierfield);


       /* There are basically two ways to graphically display the
        * default domain.
        *
        * 1.) As a hidden field with descriptive text.
        * 2.) As a drop down menu with a defaulted option.
        *
        * The modifiers '@' and '*' display a hidden field.
        *
        * If the modifier field contains anything else, then we
        * consider it's contents a 'group' identifier and we
        * display a drop down. */
       if (strcmp(modifierfield, "@") == 0)
       {
              /* ----------------------
               * DISPLAY HIDDEN FIELD
               * ---------------------- */
              
              ldl_displayhiddenfield(defaultdomain);
       }
       else if (strcmp(modifierfield, "-") == 0)
       {
              /* ----------------------
               * DISPLAY TEXT FIELD
               * ---------------------- */

              ldl_displaytextfield(defaultdomain);
       }
       else
       {
              /* ----------------------
               * DISPLAY DROP DOWN MENU
               * ---------------------- */

              ldl_displaydropdown(fp, defaultdomain, modifierfield);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int replace_wildcard ( char *  wildcardedstringp,
char *  wildcardvaluep,
int  wildcard 
) [static]

Definition at line 313 of file logindomainlist.c.

                                                                                          {
       char wildcardedstring[LINELEN]="";
       char *beforewildcardp;
       char *afterwildcardp;
       char *wildcardedstringpp=NULL;

       
       /* Continue only if there is actually a wildcard in wildcardedstringp */
       if (ldl_haswildcard(wildcardedstringp, wildcard)) {
              /* Copy wildcardedstringp so as not to modify the original. */
              strcpy(wildcardedstring, wildcardedstringp);

              /* create a pointer to a pointer of a copy */    
              wildcardedstringpp = wildcardedstring;

              
              /* tokenize first field */
              beforewildcardp=mystrsep(&wildcardedstringpp, wildcard);
              afterwildcardp=mystrsep(&wildcardedstringpp, wildcard);

              /* start with a clean slate */
              strcpy(wildcardedstringp, "");

              if (beforewildcardp != NULL &&
                  strcmp(beforewildcardp, "") != 0)
              {
                     /* Place string contents of beforewildcardp in wildcardedstringp */
                     strcpy(wildcardedstringp, beforewildcardp);
              }

              /* Add wildcardvaluep string to end of wildcardedstringp */
              strncat(wildcardedstringp, wildcardvaluep,
                     LINELEN-1-strlen(wildcardedstringp));

              if (afterwildcardp != NULL &&
                  strcmp(afterwildcardp, "") != 0)
              {
                     /* Add afterwildcardp string to end of wildcardedstringp */
                     strncat(wildcardedstringp, afterwildcardp,
                            LINELEN-1-strlen(wildcardedstringp));
              }

              /* all is well */
              return 1;

       } else {
              /* no wildcard in wildcardedstringp */
              return 0;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const int g_delim = ':' [static]

Definition at line 23 of file logindomainlist.c.

const char g_wc_en_mods[NUM_WC_EN_MODS][2] = { "@", "-" } [static]

Definition at line 27 of file logindomainlist.c.

const int g_wildcard_char = '*' [static]

Definition at line 24 of file logindomainlist.c.