1 /*-
2  ***********************************************************************
3  *
4  * $Id: getmode.c,v 1.29 2014/07/18 06:40:44 mavrik Exp $
5  *
6  ***********************************************************************
7  *
8  * Copyright 2000-2014 The FTimes Project, All Rights Reserved.
9  *
10  ***********************************************************************
11  */
12 #include "all-includes.h"
13 
14 /*-
15  ***********************************************************************
16  *
17  * GetModeInitialize
18  *
19  ***********************************************************************
20  */
21 int
GetModeInitialize(FTIMES_PROPERTIES * psProperties,char * pcError)22 GetModeInitialize(FTIMES_PROPERTIES *psProperties, char *pcError)
23 {
24   const char          acRoutine[] = "GetModeInitialize()";
25   char                acLocalError[MESSAGE_SIZE] = "";
26   int                 iError;
27 
28   /*-
29    *******************************************************************
30    *
31    * Initialize variables.
32    *
33    *********************************************************************
34    */
35   psProperties->pFileLog = stderr;
36   psProperties->pFileOut = stdout;
37 
38   /*-
39    *******************************************************************
40    *
41    * Read the config file.
42    *
43    *******************************************************************
44    */
45   iError = PropertiesReadFile(psProperties->acConfigFile, psProperties, acLocalError);
46   if (iError != ER_OK)
47   {
48     snprintf(pcError, MESSAGE_SIZE, "%s: %s", acRoutine, acLocalError);
49     return iError;
50   }
51 
52   return ER_OK;
53 }
54 
55 
56 /*-
57  ***********************************************************************
58  *
59  * GetModeCheckDependencies
60  *
61  ***********************************************************************
62  */
63 int
GetModeCheckDependencies(FTIMES_PROPERTIES * psProperties,char * pcError)64 GetModeCheckDependencies(FTIMES_PROPERTIES *psProperties, char *pcError)
65 {
66   const char          acRoutine[] = "GetModeCheckDependencies()";
67 #ifdef USE_SSL
68   char                acLocalError[MESSAGE_SIZE] = "";
69 #endif
70 
71   if (psProperties->acBaseName[0] == 0)
72   {
73     snprintf(pcError, MESSAGE_SIZE, "%s: Missing BaseName.", acRoutine);
74     return ER_MissingControl;
75   }
76 
77   if (psProperties->bGetAndExec)
78   {
79     if (psProperties->acGetFileName[0] == 0)
80     {
81       snprintf(pcError, MESSAGE_SIZE, "%s: Missing GetFileName.", acRoutine);
82       return ER_MissingControl;
83     }
84   }
85 
86   if (psProperties->acURLGetRequest[0] == 0)
87   {
88     snprintf(pcError, MESSAGE_SIZE, "%s: Missing URLGetRequest.", acRoutine);
89     return ER_MissingControl;
90   }
91 
92   if (psProperties->psGetURL == NULL)
93   {
94     snprintf(pcError, MESSAGE_SIZE, "%s: Missing URLGetURL.", acRoutine);
95     return ER_MissingControl;
96   }
97 
98   if (psProperties->psGetURL->pcPath[0] == 0)
99   {
100     snprintf(pcError, MESSAGE_SIZE, "%s: Missing path in URLGetURL.", acRoutine);
101     return ER_MissingControl;
102   }
103 
104 #ifdef USE_SSL
105   if (SSLCheckDependencies(psProperties->psSslProperties, acLocalError) != ER_OK)
106   {
107     snprintf(pcError, MESSAGE_SIZE, "%s: %s", acRoutine, acLocalError);
108     return ER_MissingControl;
109   }
110 #endif
111 
112   return ER_OK;
113 }
114 
115 
116 /*-
117  ***********************************************************************
118  *
119  * GetModeFinalize
120  *
121  ***********************************************************************
122  */
123 int
GetModeFinalize(FTIMES_PROPERTIES * psProperties,char * pcError)124 GetModeFinalize(FTIMES_PROPERTIES *psProperties, char *pcError)
125 {
126   const char          acRoutine[] = "GetModeFinalize()";
127 
128   /*-
129    *********************************************************************
130    *
131    * Establish Log file stream, and update Message Handler.
132    *
133    *********************************************************************
134    */
135   strncpy(psProperties->acLogFileName, "stderr", FTIMES_MAX_PATH);
136   psProperties->pFileLog = stderr;
137 
138   MessageSetNewLine(psProperties->acNewLine);
139   MessageSetOutputStream(psProperties->pFileLog);
140   MessageSetAutoFlush(MESSAGE_AUTO_FLUSH_ON);
141 
142   /*-
143    *******************************************************************
144    *
145    * Establish Out file stream.
146    *
147    *******************************************************************
148    */
149   if (psProperties->acGetFileName[0])
150   {
151     if ((psProperties->pFileOut = fopen(psProperties->acGetFileName, "wb")) == NULL)
152     {
153       snprintf(pcError, MESSAGE_SIZE, "%s: GetFile = [%s]: %s", acRoutine, psProperties->acGetFileName, strerror(errno));
154       return ER_fopen;
155     }
156     strncpy(psProperties->acOutFileName, psProperties->acGetFileName, FTIMES_MAX_PATH);
157   }
158   else
159   {
160     strncpy(psProperties->acOutFileName, "stdout", FTIMES_MAX_PATH);
161     psProperties->pFileOut = stdout;
162   }
163 
164   /*-
165    *********************************************************************
166    *
167    * Display properties.
168    *
169    *********************************************************************
170    */
171   PropertiesDisplaySettings(psProperties);
172 
173   return ER_OK;
174 }
175 
176 
177 /*-
178  ***********************************************************************
179  *
180  * GetModeWorkHorse
181  *
182  ***********************************************************************
183  */
184 int
GetModeWorkHorse(FTIMES_PROPERTIES * psProperties,char * pcError)185 GetModeWorkHorse(FTIMES_PROPERTIES *psProperties, char *pcError)
186 {
187   const char          acRoutine[] = "GetModeWorkHorse()";
188   char                acLocalError[MESSAGE_SIZE] = "";
189   int                 iError;
190 
191   iError = URLGetRequest(psProperties, acLocalError);
192   if (iError != ER_OK)
193   {
194     snprintf(pcError, MESSAGE_SIZE, "%s: %s", acRoutine, acLocalError);
195     return ER_URLGetRequest;
196   }
197 
198   if (psProperties->acGetFileName[0])
199   {
200 #ifdef UNIX
201     if (psProperties->pFileOut && psProperties->pFileOut != stdout)
202     {
203       fchmod(fileno(psProperties->pFileOut), 0600);
204     }
205 #endif
206     fclose(psProperties->pFileOut);
207   }
208 
209   return ER_OK;
210 }
211 
212 
213 /*-
214  ***********************************************************************
215  *
216  * GetModeFinishUp
217  *
218  ***********************************************************************
219  */
220 int
GetModeFinishUp(FTIMES_PROPERTIES * psProperties,char * pcError)221 GetModeFinishUp(FTIMES_PROPERTIES *psProperties, char *pcError)
222 {
223   char                acMessage[MESSAGE_SIZE];
224 
225   /*-
226    *********************************************************************
227    *
228    * Print output filenames.
229    *
230    *********************************************************************
231    */
232   snprintf(acMessage, MESSAGE_SIZE, "LogFileName=%s", psProperties->acLogFileName);
233   MessageHandler(MESSAGE_QUEUE_IT, MESSAGE_INFORMATION, MESSAGE_PROPERTY_STRING, acMessage);
234 
235   snprintf(acMessage, MESSAGE_SIZE, "OutFileName=%s", psProperties->acOutFileName);
236   MessageHandler(MESSAGE_QUEUE_IT, MESSAGE_INFORMATION, MESSAGE_PROPERTY_STRING, acMessage);
237 
238   /*-
239    *********************************************************************
240    *
241    * Write out the statistics.
242    *
243    *********************************************************************
244    */
245   SupportDisplayRunStatistics(psProperties);
246 
247   return ER_OK;
248 }
249 
250 
251 /*-
252  ***********************************************************************
253  *
254  * GetModeFinalStage
255  *
256  ***********************************************************************
257  */
258 int
GetModeFinalStage(FTIMES_PROPERTIES * psProperties,char * pcError)259 GetModeFinalStage(FTIMES_PROPERTIES *psProperties, char *pcError)
260 {
261   const char          acRoutine[] = "GetModeFinalStage()";
262   char                acMode[6]; /* strlen("--map") + 1 */
263   int                 iError;
264 
265   /*-
266    *********************************************************************
267    *
268    * Restart FTimes in the new run mode, if requested.
269    *
270    *********************************************************************
271    */
272   if (psProperties->bGetAndExec)
273   {
274     switch (psProperties->iNextRunMode)
275     {
276     case FTIMES_MAPMODE:
277       strcpy(acMode, "--map");
278       break;
279     case FTIMES_DIGMODE:
280       strcpy(acMode, "--dig");
281       break;
282     default:
283       snprintf(pcError, MESSAGE_SIZE, "%s: Invalid RunMode. That shouldn't happen.", acRoutine);
284       return ER_BadValue;
285       break;
286     }
287 
288     iError = execlp(psProperties->pcProgram, psProperties->pcProgram, acMode, psProperties->acGetFileName, (char *) 0);
289     if (iError == ER)
290     {
291       snprintf(pcError, MESSAGE_SIZE, "%s: Command = [%s %s %s]: execlp(): %s", acRoutine, psProperties->pcProgram, acMode, psProperties->acGetFileName, strerror(errno));
292       return ER_execlp;
293     }
294   }
295 
296   return ER_OK;
297 }
298