root/nepenthes/trunk/modules/submit-norman/submit-norman.cpp

Revision 1699, 7.0 kB (checked in by common, 2 weeks ago)

nepenthes

  • missing includes for g++ 4.3 by Philipp Bescht, thanks
  • Property svn:keywords set to id rev
Line 
1 /********************************************************************************
2  *                              Nepenthes
3  *                        - finest collection -
4  *
5  *
6  *
7  * Copyright (C) 2005  Paul Baecher & Markus Koetter
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22  *
23  *
24  *             contact nepenthesdev@users.sourceforge.net 
25  *
26  *******************************************************************************/
27
28  /* $Id$ */
29
30
31 #include "submit-norman.hpp"
32 #include "Download.hpp"
33 #include "DownloadUrl.hpp"
34 #include "DownloadBuffer.hpp"
35 #include "Utilities.hpp"
36 #include "SubmitManager.hpp"
37 #include "LogManager.hpp"
38 #include "EventManager.hpp"
39
40 #include "Config.hpp"
41
42 #include <cstdlib>
43
44 using namespace nepenthes;
45
46
47 /**
48  * as we may need a global pointer to our Nepenthes in our modules,
49  * and cant access the cores global pointer to nepenthes
50  * we have to use a own global pointer to nepenthes per module
51  * we need this pointer for logInfo() etc
52  */
53 Nepenthes *g_Nepenthes;
54
55
56 /**
57  * Constructor
58  * creates a new SubmitNorman Module, where SubmitNorman is public Module, public SubmitHanvler
59  * - sets the ModuleName
60  * - sets the ModuleDescription
61  * - sets the SubmitHandlerName
62  * - sets the SubmitHandlerDescription
63  * - sets the Modules global pointer to the Nepenthes
64  *
65  * @param nepenthes pointer to our nepenthes master class
66  */
67 SubmitNorman::SubmitNorman(Nepenthes *nepenthes)
68 {
69         m_ModuleName        = "submit-norman";
70         m_ModuleDescription = "submit files to sandbox.norman.no";
71         m_ModuleRevision    = "$Rev$";
72         m_Nepenthes = nepenthes;
73
74         m_SubmitterName = "submit-norman";
75         m_SubmitterDescription = "submit files to sandbox.norman.no";
76
77         m_EventHandlerName = "submit-norman";
78         m_EventHandlerDescription = "timeout handler for submit-norman";
79
80         g_Nepenthes = nepenthes;
81
82         m_Timeout = time(NULL);
83         m_Queued = 0;
84 }
85
86
87 /**
88  * exerything important happens in ::Exit() as we have a return value there
89  */
90 SubmitNorman::~SubmitNorman()
91 {
92
93 }
94
95 /**
96  * Module::Init()
97  * register the submitter
98  *
99  * @return return true if everything was fine, else false
100  *         false indicates a module loading error
101  */
102 bool SubmitNorman::Init()
103 {
104         logPF();
105
106         if ( m_Config == NULL )
107         {
108                 logCrit("I need a config\n");
109                 return false;
110         }
111
112         StringList sList;
113        
114         try
115         {
116                 m_Email = m_Config->getValString("submit-norman.email");
117                 sList = *m_Config->getValStringList("submit-norman.urls");
118
119     } catch ( ... )
120         {
121                 logCrit("Error setting needed vars, check your config\n");
122                 return false;
123         }
124
125         uint8_t i = 0;
126         while ( i < sList.size() )
127         {
128                 logInfo("Submitting via http post to %s\n",sList[i]);
129                 m_UrlList.push_back(string(sList[i]));
130                 i++;
131         }
132
133
134         m_ModuleManager = m_Nepenthes->getModuleMgr();
135
136     if((m_CurlStack = curl_multi_init()) == NULL )
137         {
138                  logCrit("Could not init Curl Multi Perform Stack %s\n",strerror(errno));
139                  return false;
140         }
141
142         REG_SUBMIT_HANDLER(this);
143         REG_EVENT_HANDLER(this);
144         return true;
145 }
146
147
148 /**
149  * Module::Exit()
150  *
151  * unregister the Submitter
152  *
153  * @return return true if everything was fine, else false
154  *         false indicates a heavy error
155  */
156 bool SubmitNorman::Exit()
157 {
158         curl_multi_cleanup(m_CurlStack);
159         return true;
160 }
161
162
163 /**
164  * SubmitHandler::Submit(Download *down)
165  *
166  * get and submit a file.
167  * here we just hexdump it to shell
168  *
169  * @param down   the download to hexdump
170  */
171 void SubmitNorman::Submit(Download *down)
172 {
173         logPF();
174
175         if(m_Events.test(EV_TIMEOUT) == false)
176         m_Events.set(EV_TIMEOUT);
177
178         list <string>::iterator it;
179
180         for ( it = m_UrlList.begin(); it != m_UrlList.end(); it++ )
181         {
182                 CURL *curl;
183
184                 NormanContext *norm = new NormanContext((char *)m_Email.c_str(),
185                                                                                                 down->getDownloadUrl()->getFile(),
186                                                                                                 down->getDownloadBuffer()->getSize(),
187                                                                                                 down->getDownloadBuffer()->getData(),
188                                                                                                 (char *)down->getMD5Sum().c_str());
189                 curl = curl_easy_init();
190                 if ( curl )
191                 {
192                         /* what URL that receives this POST */
193                         curl_easy_setopt(curl, CURLOPT_HTTPHEADER               , norm->m_HeaderList);
194                         curl_easy_setopt(curl, CURLOPT_HTTPPOST                 , norm->m_FormPost);
195                         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST   , false);
196                         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER   , false);
197                         curl_easy_setopt(curl, CURLOPT_URL                              , it->c_str());
198                         curl_easy_setopt(curl, CURLOPT_USERAGENT                , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
199                         curl_easy_setopt(curl, CURLOPT_PRIVATE                  , (char *) norm);
200                         curl_easy_setopt(curl, CURLOPT_WRITEDATA                , norm);
201                         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION    , SubmitNorman::WriteCallback);
202
203                         curl_multi_add_handle(m_CurlStack, curl);
204                         m_Queued++;
205                 }
206         }
207
208 }
209
210
211 size_t SubmitNorman::WriteCallback(char *buffer, size_t size, size_t nitems, void *userp)
212 {
213         int32_t iSize = size * nitems;
214 //      printf("%.*s\n",iSize,buffer);
215         return iSize;
216 }
217
218 /**
219  * SubmitHandler::Hitt(Download *down)
220  *
221  * get and submit a file.
222  *
223  *
224  * @param down   the download to hexdump
225  */
226 void SubmitNorman::Hit(Download *down)
227 {
228         return;
229 }
230
231 uint32_t SubmitNorman::handleEvent(Event *event)
232 {
233         logPF();
234         if(event->getType() != EV_TIMEOUT)
235         {
236                 logCrit("Unwanted event %i\n",event->getType());
237                 return 1;
238         }
239
240         int32_t iQueue=0;
241         while ( curl_multi_perform(m_CurlStack, (int *)&iQueue) == CURLM_CALL_MULTI_PERFORM );
242
243         if ( m_Queued > iQueue )
244         {
245                 logSpam("m_Queued  (%i) > (%i) iQueue\n", m_Queued, iQueue);
246                 CURLMsg * pMessage;
247
248                 while ( (pMessage = curl_multi_info_read(m_CurlStack, (int *)&iQueue)) )
249                 {
250                         if ( pMessage->msg == CURLMSG_DONE )
251                         {
252                                 NormanContext *norm;
253                                 char *cnorm;
254                                 char * szUrl;
255
256                 curl_easy_getinfo(pMessage->easy_handle, CURLINFO_PRIVATE, (char * *) &cnorm);
257                 norm = (NormanContext *)cnorm;
258                
259                                 if ( pMessage->data.result )
260                                 {
261                     logInfo("Upload Error %s on getting file %s \n", curl_easy_strerror(pMessage->data.result), norm->getMD5Sum());
262                                 } else
263                                 {
264                                         curl_easy_getinfo(pMessage->easy_handle, CURLINFO_EFFECTIVE_URL, &szUrl);
265                                         logInfo("Submitted file %s to sandbox %s\n",norm->getMD5Sum(),szUrl);
266                                 }
267                                 CURL *curl = pMessage->easy_handle;
268                                 curl_multi_remove_handle(m_CurlStack, pMessage->easy_handle);
269                                 delete norm;
270                                 curl_easy_cleanup(curl);
271                                 --m_Queued;
272                         }
273                 }
274         }
275
276         if(m_Queued == 0)
277         m_Events.reset(EV_TIMEOUT);
278        
279         m_Timeout = time(NULL) + 1;
280         return 0;
281 }
282
283 extern "C" int32_t module_init(int32_t version, Module **module, Nepenthes *nepenthes)
284 {
285         if(version == MODULE_IFACE_VERSION)
286         {
287                 *module = new SubmitNorman(nepenthes);
288                 return 1;
289         } else
290         {
291                 return 0;
292         }
293 }
Note: See TracBrowser for help on using the browser.