File: Service_SecVirtual.cc

package info (click to toggle)
postman 2.1-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,248 kB
  • ctags: 3,249
  • sloc: cpp: 33,376; ansic: 2,014; sh: 768; makefile: 300
file content (336 lines) | stat: -rw-r--r-- 13,374 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*                               
Service_SecVirtual.cc
*/

#include "Service_SecVirtual.h"
#include "ClientHTTP.h"

extern Language *L;
extern Configuration *Conf;
extern Skin *skin;

bool Service_SecVirtual_Initialized = false;
TBuffer URL2Get;

#define SECVIRTUAL_HOST "bancuv.uv.es"
#define SECVIRTUAL_URL_BEGIN  "/pls/uv0/iniciowww.comprueba_usu2"
#define SECVIRTUAL_PORT 80
//In seconds
#define SECVIRTUAL_TIMEOUT 240

class HTMLPages: public HTMLBasic
  {
  private:
    //
  public:
    bool esHorarioDescanso (void);
    void PrintSecVirtualDisplayPage (const char *lang, const char *user, const char *connid, UserOptions *uo, 
                                     StringList *SLHead, StringList *SLBody);
  };

//retrieve the url for the Service icon
const char *GetService_SecVirtual_ICO_URL (void)
  {
  return skin->getI(L_SECVIRTUAL);
  }

//Retrieve the ALT tag for the Service icon
const char *GetService_SecVirtual_ICO_ALT (void)
  {
  return L->get(L_SECVIRTUAL);
  }

//Retrieve the Service name
const char *GetService_SecVirtual_NAME (void)
  {
  return L->get(L_SECVIRTUAL);
  }

//Retrieve the Service Description
const char *GetService_SecVirtual_DESCRIPTION (void)
  {
  return L->get(L_SECVIRTUAL_DESCRIPTION);
  }

//Entry point to service init
bool DoService_SecVirtual_BEGIN (TProgData *pd)
  {
  TBuffer file2show;
  if (CheckServiceDisabled (TranslateServiceR(pd->service), file2show) == true) 
    {pd->html->dumpFile (file2show, "TEXT/HTML"); return false;}
  if (Service_SecVirtual_Initialized == true) {return true;}
  LOG ("Initializating Service SecVirtual for user=%s", pd->username);
  Service_SecVirtual_Initialized = true;
  xstrncpy (URL2Get, CMAXBUFFER, SECVIRTUAL_URL_BEGIN);    
  return true;
  }

//Entry point to service end
bool DoService_SecVirtual_END (TProgData *pd)
  {
  Service_SecVirtual_Initialized = false;  
  return true;
  }

//Entry point to execute a service command
bool DoService_SecVirtual_CMD (TProgData *pd)
  {
  ClientHTTP *CliHTTP;
  HTMLPages *html;
  int cmd, numslash = 0, ppos = 0, httpcode;
  StringList *SLHTTPHead, *SLHTTPBody;
  bool b;
  char clang[2];
  XString XS;
  TBuffer postdata2send,
          PATH_INFO, QUERY_STRING, REQUEST_METHOD, SERVER_PROTOCOL, header2send, HTTP_ACCEPT,
          CONTENT_TYPE, CONTENT_LENGTH, SERVER_SOFTWARE, REMOTE_HOST, SERVER_NAME,
          HTTP_COOKIE, REMOTE_USER, SERVER_PORT;

  html = (HTMLPages *)pd->html;
  cmd = pd->cmd;

  TBuffer file2show, abuf;
  if (CheckServiceDisabled (TranslateServiceR(pd->service), file2show) == true) 
    {html->dumpFile (file2show, "TEXT/HTML"); return false;}

  //25-Feb-2005 Quito el horario de descanso
  //if (html->esHorarioDescanso () == true) {return false;}

  SLHTTPHead = new StringList;    
  SLHTTPBody = new StringList;
  
  cgienv_GetSimpleVarDef (pd->envfile, "PATH_INFO",       PATH_INFO, "");
  cgienv_GetSimpleVarDef (pd->envfile, "QUERY_STRING",    QUERY_STRING, "");
  cgienv_GetSimpleVarDef (pd->envfile, "CONTENT_LENGTH",  CONTENT_LENGTH, "");
  cgienv_GetSimpleVarDef (pd->envfile, "REQUEST_METHOD",  REQUEST_METHOD, "GET");
  cgienv_GetSimpleVarDef (pd->envfile, "SERVER_PROTOCOL", SERVER_PROTOCOL, "HTTP/1.0");
  cgienv_GetSimpleVarDef (pd->envfile, "HTTP_ACCEPT",     HTTP_ACCEPT, "");
  cgienv_GetSimpleVarDef (pd->envfile, "CONTENT_TYPE",    CONTENT_TYPE, "");
  cgienv_GetSimpleVarDef (pd->envfile, "SERVER_SOFTWARE", SERVER_SOFTWARE, "");   
  cgienv_GetSimpleVarDef (pd->envfile, "REMOTE_HOST",     REMOTE_HOST, "");
  cgienv_GetSimpleVarDef (pd->envfile, "SERVER_NAME",     SERVER_NAME, "");
  cgienv_GetSimpleVarDef (pd->envfile, "HTTP_COOKIE",     HTTP_COOKIE, "");
  cgienv_GetSimpleVarDef (pd->envfile, "REMOTE_USER",     REMOTE_USER, "");
    if (IsEmpty(REMOTE_USER)) {xstrncpy (REMOTE_USER, CMAXBUFFER, pd->username);}
  cgienv_GetSimpleVarDef (pd->envfile, "SERVER_PORT",     SERVER_PORT, "");

  if (strcmp (URL2Get, SECVIRTUAL_URL_BEGIN) != 0)
    {
    int SLASHTOCUT = 10;
    if (Cstrstr (PATH_INFO, "pls/uv0/pls/uv0") > 0) {SLASHTOCUT = 12;}
    for (int i = 0; i < (signed)strlen(PATH_INFO); ++i) 
      {
      if (PATH_INFO[i] == '/') 
        {
        ++numslash; ppos = i; 
        if (numslash == SLASHTOCUT) 
          {
          break;
          }
        }
      }
    XS = XString (PATH_INFO).substring (ppos);  
    xstrncpy (URL2Get, CMAXBUFFER, XS.cstr());
    }
  else
    {
    /*I will pass the language of Postman to Secretaria Virtual*/
    if      (L->getLang() == LANG_SPANISH) xstrncpy (clang, CMAXBUFFER, "C");   //Castellano
    else if (L->getLang() == LANG_CATALA)  xstrncpy (clang, CMAXBUFFER, "V");   //Valenciano
    else if (L->getLang() == LANG_ENGLISH) xstrncpy (clang, CMAXBUFFER, "I");   //Ingles
    else xstrncpy (clang, CMAXBUFFER, "C");                                     //Castellano
    xstrncat(URL2Get, CMAXBUFFER, "?widioma="); xstrncat(URL2Get, CMAXBUFFER, clang);     
    }  
  if ((QUERY_STRING == NULL) || (strcmp(QUERY_STRING, " ") == 0) || (strcmp(QUERY_STRING, "") == 0)) 
    {/*nothing to do*/} 
  else 
    {strcat (URL2Get, "?"); strcat (URL2Get, QUERY_STRING);} 
  if (strcmp (URL2Get, "/") == 0) 
    {
    strcpy (URL2Get, SECVIRTUAL_URL_BEGIN);
    /*I will pass the language of Postman to Secretaria Virtual*/
    if      (L->getLang() == LANG_SPANISH) xstrncpy (clang, CMAXBUFFER, "C");   //Castellano
    else if (L->getLang() == LANG_CATALA)  xstrncpy (clang, CMAXBUFFER, "V");   //Valenciano
    else if (L->getLang() == LANG_ENGLISH) xstrncpy (clang, CMAXBUFFER, "I");   //Ingles
    else xstrncpy (clang, CMAXBUFFER, "C");                                     //Castellano
    xstrncat(URL2Get, CMAXBUFFER, "?widioma="); xstrncat(URL2Get, CMAXBUFFER, clang); 
    }
  //DEBUG ("URL2Get=--%s--", URL2Get);
  sprintf (header2send, "%s %s %s\n", REQUEST_METHOD, URL2Get, SERVER_PROTOCOL);
  if (!IsEmpty(HTTP_ACCEPT))    {xstrncat(header2send, CMAXBUFFER, "Accept: ");         xstrncat (header2send, CMAXBUFFER, HTTP_ACCEPT);     xstrncat(header2send, CMAXBUFFER, "\n");}
  if (!IsEmpty(CONTENT_TYPE))   {xstrncat(header2send, CMAXBUFFER, "Content-type: ");   xstrncat (header2send, CMAXBUFFER, CONTENT_TYPE);    xstrncat(header2send, CMAXBUFFER, "\n");}
  if (!IsEmpty(CONTENT_LENGTH)) 
    {
    xstrncat(header2send, CMAXBUFFER, CONTENT_LINE_STRING);
    xstrncat (header2send, CMAXBUFFER, CONTENT_LENGTH);  
    xstrncat(header2send, CMAXBUFFER, "\n");
    }  
  if (!IsEmpty(SERVER_SOFTWARE)){xstrncat(header2send, CMAXBUFFER, "Server: ");         xstrncat (header2send, CMAXBUFFER, SERVER_SOFTWARE); xstrncat(header2send, CMAXBUFFER, "\n");}  
  if (!IsEmpty(REMOTE_HOST))    {xstrncat(header2send, CMAXBUFFER, "Host: ");           xstrncat (header2send, CMAXBUFFER, REMOTE_HOST);     xstrncat(header2send, CMAXBUFFER, "\n");}  
  if (!IsEmpty(SERVER_NAME))    {xstrncat(header2send, CMAXBUFFER, "Host: ");           xstrncat (header2send, CMAXBUFFER, SERVER_NAME);     xstrncat(header2send, CMAXBUFFER, "\n");}  
  if (!IsEmpty(REMOTE_USER))    {xstrncat(header2send, CMAXBUFFER, "Remote-user: ");    xstrncat (header2send, CMAXBUFFER, REMOTE_USER);     xstrncat(header2send, CMAXBUFFER, "\n");}  
  
  //Send the url to back to postman to http server.
  TBuffer url2back;
  xsnprintf (url2back, CMAXBUFFER, "%s/%s/root/noop/%s/%s/%s/%d/%d/%d/%ld/", 
                                   PATH_CGI, SERVICE_MAIN, pd->stlang, pd->username, pd->connid,
                                   pd->uo->getValidator(), pd->uo->getPageId(), pd->uo->getLastCmd(), pd->uo->getCurrentPosition());
  //LOG ("url2back=--%s--", url2back);

  //DEBUG ("HTTP_COOKIE=%s", HTTP_COOKIE);
  if (!IsEmpty(HTTP_COOKIE))
    {
    xstrncat(header2send, CMAXBUFFER, "Cookie: "); xstrncat (header2send, CMAXBUFFER, HTTP_COOKIE); 
    if (!IsEmpty(REMOTE_USER))    
      {xstrncat(header2send, CMAXBUFFER, "; usu="); xstrncat (header2send, CMAXBUFFER, REMOTE_USER);} 
    if (!IsEmpty(SERVER_PORT))
      {xstrncat(header2send, CMAXBUFFER, "; xport="); xstrncat (header2send, CMAXBUFFER, SERVER_PORT);}        
    xstrncat(header2send, CMAXBUFFER, "; Backurl="); xstrncat (header2send, CMAXBUFFER, url2back);
    xstrncat(header2send, CMAXBUFFER, ";\n");
    }
  else 
    {
    if (!IsEmpty(REMOTE_USER) || !IsEmpty(SERVER_PORT))
      {
      xstrncat(header2send, CMAXBUFFER, "Cookie: "); 
      if (!IsEmpty(REMOTE_USER)) {xstrncat(header2send, CMAXBUFFER, "usu="); xstrncat (header2send, CMAXBUFFER, REMOTE_USER); xstrncat(header2send, CMAXBUFFER, "; ");}
      if (!IsEmpty(SERVER_PORT)) {xstrncat(header2send, CMAXBUFFER, "xport="); xstrncat (header2send, CMAXBUFFER, SERVER_PORT); xstrncat(header2send, CMAXBUFFER, "; ");}      
      xstrncat(header2send, CMAXBUFFER, "; Backurl="); xstrncat (header2send, CMAXBUFFER, url2back);
      xstrncat(header2send, CMAXBUFFER, "\n");
      }
    }    
  //DEBUG ("HTTP_COOKIE=--%s--", HTTP_COOKIE);  
  xstrncat (header2send, CMAXBUFFER, "Connection: close\n");
  xstrncat (header2send, CMAXBUFFER, "\n");
  //DEBUG ("header2send=--%s--", header2send);
  
  switch (pd->cmd)    
    {    
    case (CMD_SECVIRTUAL_GOTO):  
      {       
      CliHTTP = new ClientHTTP (SECVIRTUAL_HOST, SECVIRTUAL_PORT, SECVIRTUAL_TIMEOUT);
      if (strcmp (CONTENT_LENGTH, "") == 0)
        {
        //DEBUG ("Haciendo un GET");
        b = CliHTTP->doGetHTTPSL (header2send, SLHTTPHead, SLHTTPBody, &httpcode);
        if (b == false)
          {
          LOG ("ERROR: Service_SecVirtual: Get: %s, user='%s'", CliHTTP->getErrorMsg(), REMOTE_USER);
          pd->html->ErrorPage (CliHTTP->getErrorMsg());
          }
        }
      else
        {
        cgienv_GetAllSimpleVarINPUTPC (pd->envfile, postdata2send, false);
        //escape_url (postdata2send, abuf);
        
        //We will replace the old content-length with the new calculated (because we have unescape several chars and this number has changed)
        XString XS;
        TBuffer buf1, buf2, buf3;
        strcpy (buf1, CONTENT_LINE_STRING); strcat (buf1, CONTENT_LENGTH);
        strcpy (buf2, CONTENT_LINE_STRING); strcat (buf2, xitoa(strlen(postdata2send), buf3));
        XS = header2send;
        XS.replace (buf1, buf2);
        xstrncpy (abuf, CMAXBUFFER, XS.cstr());
        
        /*
        DEBUG ("Haciendo un POST");
        DEBUG ("CONTENT_LENGTH=%s", CONTENT_LENGTH);
        DEBUG ("header2send=--%s--", header2send);
        DEBUG ("abuf=--%s--", abuf);
        TBuffer kk, kk2; strcpy (kk, ""); for (int i=0; i < (signed)strlen(postdata2send); ++i) {strcat (kk, xitoa(postdata2send[i], kk2)); strcat (kk, "-");}
        DEBUG ("kk=%s", kk);
        DEBUG ("postdata2send=--%s--", postdata2send);
        */
        
        b = CliHTTP->doPostHTTPSL (abuf, postdata2send, SLHTTPHead, SLHTTPBody, &httpcode);
        if (b == false)
          {
          LOG ("ERROR: Service_SecVirtual: Post: %s, user='%s'", CliHTTP->getErrorMsg(), REMOTE_USER);
          pd->html->ErrorPage (CliHTTP->getErrorMsg());
          }          
        }
      delete CliHTTP;
      html->PrintSecVirtualDisplayPage  (pd->stlang, pd->username, pd->connid, pd->uo, SLHTTPHead, SLHTTPBody);
      break;
      }
    default: 
      {    
      LOG ("ERROR: Service_SecVirtual: Here we can not go! cmd=%d, stcmd=--%s--, stsubcmd=--%s--", pd->cmd, pd->stcmd, pd->stsubcmd);
      pd->html->PrintInvalidCommand (pd->stlang, pd->username, pd->connid, pd->uo);
      break;
      }                                   
    }  
  initStr (URL2Get);

  delete SLHTTPHead;  
  delete SLHTTPBody;
  return false;  
  }  

void HTMLPages::PrintSecVirtualDisplayPage (const char *lang, const char *user, const char *connid, UserOptions *uo, 
                                            StringList *SLHead, StringList *SLBody)
  { 
  TBuffer linea;
  XString XS;
  /*
  static int kk = 0;
  TBuffer abuf;
  xsnprintf (abuf, CMAXBUFFER, "/tmp/head_%d.txt", kk); SLHead->SaveToFile (abuf, true); 
  xsnprintf (abuf, CMAXBUFFER, "/tmp/body_%d.txt", kk); SLBody->SaveToFile (abuf, true); 
  ++kk; 
  */
  for (int i = 0; i < SLHead->Count(); ++i) 
    {
    xstrncpy (linea, CMAXBUFFER, SLHead->getString (i).cstr());
    if (strstr (linea, "Set-Cookie: ") != NULL)
      {
      if (strstr (linea, ";Path=") != NULL)
        {
        XS = XString (linea);
        XS.replace (";Path=/pls/uv0/", ";Path=/cgi-bin/");
        WRITE ("%s\n", XS.cstr());
        }
      else
        {
        xstrncat (linea, CMAXBUFFER, ";Path=/cgi-bin/");
        WRITE ("%s\n", linea);
        }        
      }
    else
      {
      WRITE ("%s\n", linea);
      }
    }  
  WRITE ("\n");
  for (int i = 0; i < SLBody->Count(); ++i)
    {
    WRITE ("%s\n", SLBody->getString (i).cstr());
    }
  }

/*COMPRUEBO QUE ES BUENO EL HORARIO DE CONSULTAS, POR LO DE LAS COPIAS NOCTURNAS*/
bool HTMLPages::esHorarioDescanso (void)
  {
#define SERVICE_DESCANSO "/usr/local/etc/secvirtual.disabled_noc.html"
  time_t now;
  struct tm *atm;
  char stnow[21];

  now = time (NULL); atm = localtime(&now);
  strftime (stnow, 20, "%H", atm);  
  if (
      (strcmp (stnow, "22") == 0) || (strcmp (stnow, "23") == 0) ||
      (strcmp (stnow, "00") == 0) || (strcmp (stnow, "01") == 0) ||
      (strcmp (stnow, "02") == 0) || (strcmp (stnow, "03") == 0)
      )
    {
    dumpFile (SERVICE_DESCANSO, "TEXT/HTML");
    return true;
    }
  return false;
  }