"Fossies" - the Fresh Open Source Software Archive

Member "hermes-1.9/src/Spf.cpp" (29 Jun 2014, 3059 Bytes) of package /linux/privat/hermes-1.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Spf.cpp" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * hermes antispam proxy
    3  * Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
    4  *
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation; version 2 of the License
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License along
   15  * with this program; if not, write to the Free Software Foundation, Inc.,
   16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17  *
   18  * @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
   19  */
   20 #include "Spf.h"
   21 
   22 SPF_server_t *Spf::spfserver=NULL;
   23 
   24 /**
   25  * constructor
   26  *
   27  * it will create a spfserver if this is the first created object of the class.
   28  * if it isn't, then we just create an spfrequest
   29  */
   30 Spf::Spf():spfrequest(NULL),spfresponse(NULL)
   31 {
   32   pthread_mutex_init(&mutex,NULL);
   33   if(NULL==spfserver)
   34     if(NULL==(spfserver=SPF_server_new(SPF_DNS_CACHE,0)))
   35       throw Exception(_("Can't initialize SPF library"),__FILE__,__LINE__);
   36 
   37   if(NULL==(spfrequest=SPF_request_new(spfserver)))
   38     throw Exception(_("Can't initialize SPF request"),__FILE__,__LINE__);
   39 }
   40 
   41 /**
   42  * destructor
   43  *
   44  * frees the memory of the spfrequest
   45  */
   46 Spf::~Spf()
   47 {
   48   pthread_mutex_destroy(&mutex);
   49   if(NULL!=spfrequest) SPF_request_free(spfrequest);
   50 }
   51 
   52 /**
   53  * frees all memory related to the spf class
   54  *
   55  * this is needed because the common things are only initialized
   56  * once (and are static), and when we close the program we need
   57  * to deinitialize them
   58  */
   59 void Spf::deinitialize()
   60 {
   61   if(NULL!=spfserver)
   62     SPF_server_free(spfserver);
   63 }
   64 
   65 /**
   66  * make a query to the dns system for an spf record
   67  *
   68  * highly inspired from fakehermes' source
   69  *
   70  * @param ip    the ip of the remote server
   71  * @param helo  the hello string of the remote server
   72  * @param from  the envelope from address
   73  *
   74  * @returns true if it is not incorrect
   75  */
   76 bool Spf::query(string ip,string helo,string from)
   77 {
   78   bool retval=false;
   79 
   80   if(SPF_request_set_ipv4_str(spfrequest,ip.c_str()))
   81     throw Exception(_("Error configuring IP for SPF request"),__FILE__,__LINE__);
   82   if(SPF_request_set_helo_dom(spfrequest,helo.c_str()))
   83     throw Exception(_("Error configuring HELO for SPF request"),__FILE__,__LINE__);
   84   if(SPF_request_set_env_from(spfrequest,from.c_str()))
   85     throw Exception(_("Error configuring FROM for SPF request"),__FILE__,__LINE__);
   86 
   87   //make the actual query
   88   pthread_mutex_lock(&mutex);
   89   SPF_request_query_mailfrom(spfrequest,&spfresponse);
   90   pthread_mutex_unlock(&mutex);
   91 
   92   if(NULL!=spfresponse)
   93   {
   94     retval=(SPF_RESULT_FAIL==SPF_response_result(spfresponse)||SPF_RESULT_SOFTFAIL==SPF_response_result(spfresponse))?false:true;
   95     SPF_response_free(spfresponse);
   96   }
   97 
   98   return retval;
   99 }