summaryrefslogtreecommitdiffstats
path: root/rascontrol/rascontrol.cc
blob: 0b186f9d77ab633f490e31f7827ff37b000f7cc0 (plain)
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
337
338
339
340
341
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/**
 * SOURCE: rascontrol.cc
 *
 * MODULE: rascontrol
 * CLASS:  
 *
 * PURPOSE:
 *  RasControl is the remote control utility for the rasmgr
 *    
 * COMMENTS:
 * - should normalize cmd interpretation with rasmgr: strcmp vs strncmp
*/

#define DEBUG_MAIN		// to allocate static vars for trace macros
#include "debug-clt.hh"

#include "rascontrol.hh"
#include <fstream>
#include <signal.h>

#ifndef RMANVERSION
#error "Please specify RMANVERSION variable!"
#endif

#ifndef COMPDATE
#error "Please specify the COMPDATE variable!"
/*
COMPDATE=`date +"%d.%m.%Y %H:%M:%S"`

and -DCOMPDATE="\"$(COMPDATE)\"" when compiling
*/
#endif

UserLogin        userLogin;
EditLine         editLine;
RasMgrClientComm httpClient;
RascontrolConfig config;

bool redirStdin  = false;
bool redirStdout = false;

const int NOERROR      = 0;
const int CANTCONNECT  = 1;
const int LOGINERROR   = 2;
const int ACCESSDENIED = 3;

// flag to indicate whether conf or auth file have been modified.
// if true files must be saved before terminating.
static bool confFileDirty = false;
static bool authFileDirty = false;

int  interactiveWork();
int  loginOnly();
int  batchMode();
int testLogin();
     
int main(int argc, char **argv)
{ 
  if(config.interpretArguments(argc,argv)==false) return 1;
  
  // /*for debug only: */ config.printDebugInfo(); return 0; 
  if(config.beQuiet()==false)
    {
      std::cout << "rascontrol: rasdaman server remote control utility. rasdaman v" << RMANVERSION / 1000. << " -- generated on " << COMPDATE << "." << std::endl;
      std::cout << " Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann rasdaman GmbH." << std::endl
          << "Rasdaman community is free software: you can redistribute it and/or modify "
             "it under the terms of the GNU General Public License as published by "
             "the Free Software Foundation, either version 3 of the License, or "
             "(at your option) any later version. \n"
             "Rasdaman community is distributed in the hope that it will be useful, "
             "but WITHOUT ANY WARRANTY; without even the implied warranty of "
             "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
             "GNU General Public License for more details. \n\n";
      std::cout << "This software contains software which is in the public domain:" << std::endl;
      std::cout << "- openssl 0.96c (C) 1998-2002 The OpenSSL Project, (C) 1995-1998 Eric A. Young, Tim J. Hudson" << std::endl;
     }
  if(config.showHelp())
  {
    config.printHelp();
    return 0;
  }
     
  if(testIsMessageDigestAvailable("MD5")==false)
    {
      std::cout<<"Error: Message Digest MD5 not available."<<std::endl;
      return 2;
    } 
     
  if(!isatty(0) )
      redirStdin =true;
  if(!isatty(1))
      redirStdout=true;

  httpClient.setRasMgrHost(config.getRasMgrHost(), config.getRasMgrPort());  
  
  int loginOK = (config.getLoginModus()==LGIINTERACTIV)  ? userLogin.interactiveLogin() : userLogin.environmentLogin();
    
  if(loginOK<0)
  {
    std::cout<<"Login error."<<std::endl;
    return LOGINERROR;
  }
  
  httpClient.setUserIdentification(userLogin.getUserName(),userLogin.getEncrPass());

  int answer=NOERROR;
  switch(config.getWorkModus())
    { 
      case WKMINTERACTIV: answer=interactiveWork(); break;
      case WKMLOGIN     : answer=loginOnly(); break;
      case WKMBATCH     : answer=batchMode(); break;
      case WKMTESTLOGIN : answer=testLogin(); break;
     }

  if(config.beQuiet()==false)
    std::cout<< "rascontrol terminated." <<std::endl;
  
  return answer;   
  } // main()
  
// check whether command fits with given keyword (case insensitive), maybe after stripping leading whitespace
bool isCommand( const char *command, const char *key)
  {
    bool result;

    if (command == NULL || key == NULL)
        result = false;
    else
      {
          while (*command == ' ' || *command == '\t')	// skip white space; newline cannot occur
              command++;
          result = strcasecmp( command, key );
      }

    return result;
  }

int interactiveWork()
  {
    int result = 0;
    const char *answer = NULL;
    int comm = COMM_CONT;			// COMM_* values ; should be enum

    std::ofstream history;
    bool historyFile=config.histRequest();

    comm = httpClient.sendMessageGetAnswer( RASMGRCMD_HELLO, &answer );
    
    if(answer)
        std::cout<<"    "<<answer<<std::endl;
    
    if(comm==COMM_ERR)
     { std::cout<<"Cannot connect to main host "<<httpClient.getRasMgrHost()<<std::endl;
       return CANTCONNECT;
      }
    if(comm==COMM_ACDN)
     { //std::cout<<"Access denied."<<std::endl;
       return ACCESSDENIED;
      }

    // shifted here, after return, so that it always will be closed
    if(historyFile)
     {
        TALK( "RasControl::interactiveWork: using history file " << config.getHistFileName() );
        history.open(config.getHistFileName(),std::ios::out|std::ios::trunc);
     }

    do{
        TALK( "RasControl::interactiveWork: entering new request cycle." );

        const char *command=editLine.interactiveCommand(config.getPrompt());
        if(command == NULL || strlen(command)==0)	// empty cmd line
            continue;

	if(historyFile)
            history<<command<<std::endl;

        TALK( "RasControl::interactiveWork: cmd=" << command );

        // send message to rasmgr & receive answer
        const char *answer = NULL;
        if ( (isCommand(command,"exit") == 0)
          || (isCommand(command,"quit") == 0)
          || (isCommand(command,"bye" ) == 0) )
          {
            // normalize "quit", "bye" to "exit"; do so after history to maintain original cmd there
            comm = httpClient.sendMessageGetAnswer( RASMGRCMD_EXIT, &answer );
            comm = COMM_EXIT;			// ignore any result, we have to terminate anyway
          }
        else
          {
            comm = httpClient.sendMessageGetAnswer( command, &answer );
          }

        if(answer!=0 && strlen(answer)>0)
	    std::cout<<"    "<<answer<<std::endl;
        else
            answer = "Internal error: cannot decode rasmgr result.";

	// EXPERIMENTAL: close socket as early and as always as possible
        //TALK( "RasControl::interactiveWork: closing socket." );
        //httpClient.closeSocket();
      
        TALK( "RasControl::interactiveWork: comm=" << comm );

    }while( comm != COMM_EXIT && comm != COMM_ACDN);	// COMM_ERR is no reason to terminate interactive session
  
    if(historyFile)
        history.close();

    switch(comm)
    {
      case COMM_ERR:  result = CANTCONNECT;  break;
      case COMM_ACDN: result = ACCESSDENIED; break;
      default:        result = NOERROR;      break;
    }
    return result;
  }
   			     		 	
int loginOnly()
  {
    std::cout<<userLogin.getUserName()<<':'<<userLogin.getEncrPass()<<std::endl;
    // Thats all
    return NOERROR;
   }
int batchMode()
  {
    int result = 0;                             // COMM_* values

    bool fromCommandLine = strlen(config.getCommand()) ? true: false;
    const char *prompt   = !redirStdin ? config.getPrompt():"";
    
    //std::cout<<"batch mode "<<fromCommandLine<<" comm="<<config.getCommand()<<std::endl;
    
    int comm=0;
    do{
      const char *command= fromCommandLine ? config.getCommand():editLine.fromStdinCommand(prompt);  
      
      TALK( "batch: Command=" << command );
    
      if(command == NULL ) return CANTCONNECT;
      
      if(strlen(command)==0 ) 
          if( fromCommandLine)
              return CANTCONNECT;
	  else
              continue; 
    
      bool printCommand=false;	    
    
      if( redirStdout ) printCommand =  true;
      else 
        { if(redirStdin) printCommand =  true;
         }
      
      if(printCommand == true)
          std::cout<<config.getPrompt()<<command<<std::endl;		
    
      // normalize "quit", "bye" to "exit"
      const char *answer = NULL;
      if ( (isCommand(command,"exit") == 0)
        || (isCommand(command,"quit") == 0)
        || (isCommand(command,"bye" ) == 0) )
        {
          // normalize "quit", "bye" to "exit"; do so after history to maintain original cmd there
          comm = httpClient.sendMessageGetAnswer( RASMGRCMD_EXIT, &answer );
          comm = COMM_EXIT;                   // ignore any result, we have to terminate anyway
        }
      else
        {
          comm = httpClient.sendMessageGetAnswer( command, &answer );
        }

      if(answer==0 || strlen(answer)==0)
          answer = "Error: cannot decode rasmgr result.";
      else
        {
          if(comm==COMM_ERR)
              std::cout<<"    "<<"Cannot connect to main host."<<std::endl;
          else if(comm==COMM_ACDN)
              std::cout<<"    "<<"Access denied by rasmgr."<<std::endl;
          else
              std::cout<<"    "<<answer<<std::endl;
        }

    }while(fromCommandLine==false && redirStdin
           && comm != COMM_ERR
           && comm != COMM_ACDN
           && comm != COMM_EXIT); //process the whole  "< file"

    switch(comm)
    {
      case COMM_ERR:  result = CANTCONNECT;  break;
      case COMM_ACDN: result = ACCESSDENIED; break;
      default:        result = NOERROR;      break;
    }
    return result; 
   }

int testLogin()
  {
    int comm = 0;                             // COMM_* values
    const char *answer = NULL;

    comm = httpClient.sendMessageGetAnswer( RASMGRCMD_HELLO, &answer );
    
    if(comm==COMM_ERR)
       { //std::cout<<"Cannot connect to main host "<<httpClient.getRasMgrHost()<<std::endl;
         return CANTCONNECT;
        }
    if(comm==COMM_ACDN)
       { //std::cout<<"Acces denied."<<std::endl;
        return ACCESSDENIED;
        }
   
    std::cout<<"Login OK."<<std::endl;
    return NOERROR;
   }