Logo Search packages:      
Sourcecode: nbsmtp version File versions  Download package

int fileconfig_parse ( const char *  path,
servinfo_t serverinfo 
)

Parses the configuration file.

Parameters:
[in] path Where to find the config file
[out] serverinfo servinfo_t struct to store the values
Returns:
1 in case of error, 2 if file not found, otherwise 0

Definition at line 141 of file fileconfig.c.

References servinfo::auth_mech, servinfo::auth_pass, servinfo::auth_user, debug, servinfo::domain, servinfo::fromaddr, servinfo::host, log_msg(), and servinfo::port.

Referenced by fileconfig_parse_all(), and parse_options().

{
      const char *sep = "\n\t =";
      char buf[BUF_SIZE];
      char *tempo, *opts[2], **p;
      FILE *fd;
#ifndef HAVE_STRSEP
      char *token;
#endif

      if ((fd = fopen(path,"r"))==NULL)
      {
            /* No such file or directory */
            if (errno==ENOENT)
            {
                  return 2;
            }
            perror("open");
            return 1;
      }

      while(fgets(buf,sizeof(buf),fd))
      {
            /* Strip comments and malformed lines */
            if (buf[0]=='#')
            {
                  continue;
            }
            else if ((tempo = strchr(buf,'#')))
            {
                  *tempo = (char)NULL;
            }
            else if(strchr(buf,'=') == (char*)NULL)
            {
                  continue;
            }

            /* Set helper pointers */
            p = opts;
            tempo = buf;

            /*
             * We store both parts (key/value) on opts[]. We are
             * also good enough to make a version using strtok for
             * those 'weak' OSes with no strsep().
             */
#ifdef HAVE_STRSEP
            while((*p = strsep(&tempo,sep))!=NULL)
            {
                  if (**p!='\0' && ++p >= &opts[2])
                  {
                        break;
                  }
            }
#else
            for ( token = strtok(tempo,sep) ; token ; token = strtok(NULL,sep) )
            {
                  *(p++) = token;
            }
#endif

            /* Avoid segfault in case of opts[1] being NULL */
            if (opts[1]==NULL)
            {
                  log_msg(LOG_WARNING,"Empty option [%s] in %s",opts[0],path);
                  continue;
            }

            /* And finally we check for the var */
            if CHECK_OPTION("relayhost")
            {
                  SERVINFO_RELEASE_OPTION(serverinfo->host);
                  serverinfo->host = (char*)strdup(opts[1]);
            }
            else if CHECK_OPTION("fromaddr")
            {
                  SERVINFO_RELEASE_OPTION(serverinfo->fromaddr);
                  serverinfo->fromaddr = (char*)strdup(opts[1]);
            }
            else if CHECK_OPTION("domain")
            {
                  SERVINFO_RELEASE_OPTION(serverinfo->domain);
                  serverinfo->domain = (char*)strdup(opts[1]);
            }
            else if CHECK_OPTION("auth_user")
            {
                  SERVINFO_RELEASE_OPTION(serverinfo->auth_user);
                  serverinfo->auth_user = (char*)strdup(opts[1]);
            }
            else if CHECK_OPTION("auth_pass")
            {
                  SERVINFO_RELEASE_OPTION(serverinfo->auth_pass);
                  serverinfo->auth_pass = (char*)strdup(opts[1]);
            }
            else if CHECK_OPTION("auth_mech")
            {
                  if (strlen(opts[1])==1)
                  {
                        log_msg(LOG_WARNING,"Setting auth_mech with a single character is " \
                                    "discouraged and will be removed soon. Please take a look " \
                                    "at nbsmtprc(5) to see the new format.");

                        switch(*opts[1])
                        {
                              case 'l':
                                    serverinfo->auth_mech = SASL_LOGIN;
                                    break;
                              case 'p':
                                    serverinfo->auth_mech = SASL_PLAIN;
                                    break;
                              case 'c':
#ifdef HAVE_SSL
                                    serverinfo->auth_mech = SASL_CRAMMD5;
#else
                                    log_msg(LOG_WARNING,"CRAMMD5 only available when SSL compiled into nbSMTP, using default mechanism");
                                    serverinfo->auth_mech = SASL_DEFAULT;
#endif
                              default:
                                    serverinfo->auth_mech = SASL_DEFAULT;
                                    break;
                        }
                  }
                  else
                  {
                        if CHECK_VALUE("plain")
                        {
                              serverinfo->auth_mech = SASL_PLAIN;
                        }
                        else if CHECK_VALUE("login")
                        {
                              serverinfo->auth_mech = SASL_LOGIN;
                        }
                        else if (CHECK_VALUE("crammd5") || CHECK_VALUE("cram-md5"))
                        {
#ifdef HAVE_SSL
                              serverinfo->auth_mech = SASL_CRAMMD5;
#else
                              log_msg(LOG_WARNING,
                                          "CRAM-MD5 only available when compiled with SSL, using " \
                                          "default mechanism");
                              serverinfo->auth_mech = SASL_DEFAULT;
#endif
                        }
                        else
                        {
                              log_msg(LOG_WARNING,"Unknown auth_mech, setting to default value");
                              serverinfo->auth_mech = SASL_DEFAULT;
                        }
                  }
            }
            else if CHECK_OPTION("port")
            {
                  serverinfo->port = atoi(opts[1]);
            }
            else if CHECK_OPTION("use_tls")
            {
#ifdef HAVE_SSL
                  if CHECK_TRUE()
                  {
                        serverinfo->use_tls = True;
                  }
                  else if CHECK_FALSE()
                  {
                        serverinfo->use_tls = False;
                  }
#else
                  log_msg(LOG_WARNING,"SSL support not compiled into nbSMTP, ignoring use_tls");
#endif
            }
            else if CHECK_OPTION("use_starttls")
            {
#ifdef HAVE_SSL
                  if CHECK_TRUE()
                  {
                        serverinfo->use_starttls = True;
                        serverinfo->use_tls = True;
                  }
                  else if CHECK_FALSE()
                  {
                        serverinfo->use_starttls = False;
                  }
#else
                  log_msg(LOG_WARNING,"SSL support not compiled into nbSMTP, ignoring use_starttls");
#endif
            }
            else if CHECK_OPTION("debug")
            {
                  debug = atoi(opts[1]);
            }
            else
            {
                  log_msg(LOG_WARNING,"Unknown option [%s]",opts[0]);
            }
      }

      fclose(fd);

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index