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

int parse_options ( int  argc,
char *  argv[],
servinfo_t serverinfo 
)

Parses command line setting serverinfo members to its values.

Parameters:
[in] argc An integer with the number of arguments
[in] argv A pointer to a char array holding all arguments
[out] serverinfo A pointer to a servinfo_t struct
Returns:
Returns 0 if everything went ok, 2 if we got -H (print long_help) and 1 in case of error

Definition at line 54 of file original.c.

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

Referenced by main().

{
      int c;
      char buffer[BUF_SIZE];
      bool_t read_syswide = True;
      bool_t read_localconf = True;

      for (c = 0 ; c < argc ; c++)
      {
            if (strncmp("-n",argv[c],strlen("-n"))==0)
            {
                  read_localconf = False;
            }
            else if (strncmp("-N",argv[c],strlen("-N"))==0)
            {
                  read_syswide = False;
            }
      }

      /* Parse all config files */
      if (fileconfig_parse_all(read_syswide,read_localconf,serverinfo))
      {
            perror("fileconfig_parse_all");
            return 1;
      }

      /* Then read the options */
      while ((c = getopt(argc,argv,"h:d:f:c:p:U:P:M:sSvVDHnN")) != -1)
      {
            switch(c)
            {
                  case 'h':
                        SERVINFO_RELEASE_OPTION(serverinfo->host);
                        serverinfo->host = (char *)strdup(optarg);
                        break;
                  case 'd':
                        SERVINFO_RELEASE_OPTION(serverinfo->domain);
                        serverinfo->domain = (char *)strdup(optarg);
                        break;
                  case 'f':
                        SERVINFO_RELEASE_OPTION(serverinfo->fromaddr);
                        serverinfo->fromaddr = (char *)strdup(optarg);
                        break;
                  case 'c':
                        switch (fileconfig_parse(optarg,serverinfo))
                        {
                              case 1:
                                    perror("fileconfig_parse");
                                    return 1;
                                    break;
                              case 2:
                                    log_msg(LOG_WARNING,"Could not load '%s': File not found",optarg);
                                    break;
                        }
                        break;
                  case 'p':
                        serverinfo->port = atoi(optarg);
                        break;
                  case 'D':
                        debug = 1;
                        break;
                  case 's':
#ifdef HAVE_SSL
                        serverinfo->use_tls = True;
#else
                        log_msg(LOG_WARNING,"SSL support not compiled into nbSMTP, ignoring -s");
#endif
                        break;
                  case 'S':
#ifdef HAVE_SSL
                        serverinfo->use_starttls = True;
                        serverinfo->use_tls = True;
#else
                        log_msg(LOG_WARNING,"SSL support not compiled into nbSMTP, ignoring -S");
#endif
                        break;
                  case 'U':
                        SERVINFO_RELEASE_OPTION(serverinfo->auth_user);
                        serverinfo->auth_user = (char *)strdup(optarg);
                        break;
                  case 'P':
                        SERVINFO_RELEASE_OPTION(serverinfo->auth_pass);
                        serverinfo->auth_pass = (char *)strdup(optarg);
                        break;
                  case 'M':
                        switch(*optarg)
                        {
                              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
                                    break;
                              default:
                                    serverinfo->auth_mech = SASL_DEFAULT;
                                    break;
                        }
                        break;
                  case 'H':
                        print_help(argv[0]);
                        return 2;
                        break;
                  case 'V':
                        debug = 2;
                        break;
                  case 'v':
                        printf("nbSMTP version %s\n",PACKAGE_VERSION);
                        return 2;
                        break;
                  case 'n':
                  case 'N':
                        /* Left intentionally blank */
                        break;
                  case ':':
                  case '?':
                  default:
                        print_usage(argv[0]);
                        return 1;
                        break;
            }
      }

      /* If domain isn't specified, use machines hostname */
      if (serverinfo->domain==NULL)
      {
            if (gethostname(buffer,MAXHOSTNAMELEN)==-1)
            {
                  perror("gethostname");
                  return 1;
            }
            else
            {
                  serverinfo->domain = (char *)strdup(buffer);
            }
      }

      /* If from address isn't specified, build up one */
      if (serverinfo->fromaddr==NULL)
      {
            struct passwd *user = getpwuid(getuid());

            snprintf(buffer,sizeof(buffer),"%s@%s%c",user->pw_name,serverinfo->domain,'\0');

            serverinfo->fromaddr = (char *)strdup(buffer);
      }

      /* If we haven't enough info, print_usage and exit */
      if (serverinfo->domain==NULL || serverinfo->fromaddr==NULL || serverinfo->host==NULL)
      {
            print_usage(argv[0]);
            return 1;
      }

#ifdef HAVE_SSL
      if ((serverinfo->port==0)&&(serverinfo->use_tls==True)&&(serverinfo->use_starttls==False))
      {
            serverinfo->port = 465;
      }
      else if ((serverinfo->port==0)&&(serverinfo->use_tls==True)&&(serverinfo->use_starttls==True))
      {
            serverinfo->port = 25;
      }
#endif

      if (serverinfo->port==0)
      {
            serverinfo->port = 25;
      }

#ifdef HAVE_SSL
      if (serverinfo->port==465 && serverinfo->use_tls==False && serverinfo->use_starttls==False)
      {
            log_msg(LOG_WARNING,"Port specified (465) normally needs an SSL " \
                        "connection but you didn't ask for it, use -s flag");
      }
#else
      if (serverinfo->port==465)
      {
            log_msg(LOG_WARNING,"Port specified (465) normally needs an SSL " \
                        "connection, but SSL is not compiled in");
      }
#endif

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index