summaryrefslogtreecommitdiffstats
path: root/src/wsman.c
blob: 64bc59bddcd45d2901a7df6c473eaf51cfaa540f (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
342
343
344
345
346
347
348
349
350
/*******************************************************************************
 * Copyright (C) 2004-2006 Intel Corp. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of Intel Corp. nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

/**
 * @author Anas Nashif
 * @author Eugene Yarmosh
 * @author Vadim Revyakin
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#include "u/libu.h"
#include "wsman-client-api.h"
#include "wsman-client-transport.h"
#include "wsman-client-options.h"





static void wsman_output(WsXmlDocH doc)
{
  FILE *f;
  const char *filename = wsman_options_get_output_file();
  if (!doc) {
    error("doc with NULL content");
    return;
  }
  if (filename) {
    f  = fopen(filename , "w+");
    if (f == NULL)
      error("Could not open file for writing");
    else
      ws_xml_dump_node_tree(f, ws_xml_get_doc_root(doc));
  } else {
    ws_xml_dump_node_tree(stdout, ws_xml_get_doc_root(doc));
  }
  return;
}


static void
debug_message_handler (const char *str, debug_level_e level, void  *user_data)
{
  if (wsman_debug_level_debugged(level)) 
  {
    struct tm *tm;
    time_t now;
    char timestr[128];

    time (&now);
    tm = localtime (&now);
    strftime (timestr, 128, "%b %e %T", tm);
    fprintf (stderr, "%s  %s\n", timestr, str);
  }
}

static void
initialize_logging (void)
{
  debug_add_handler (debug_message_handler, DEBUG_LEVEL_ALWAYS, NULL);
} 



int main(int argc, char** argv)
{
  int retVal = 0;
  int op;
  char *filename;
  dictionary       *ini = NULL;
  WsManClient *cl;
  WsXmlDocH doc;
  char *enumContext;
  WsXmlDocH rqstDoc;
  actionOptions options;    
   
  char *enumeration_mode, *binding_enumeration_mode, *resource_uri_with_selectors;
  char *resource_uri = NULL;

 

  filename = (char *)wsman_options_get_config_file();
  if (filename)  {
    ini = iniparser_load(filename);
    if (ini==NULL) {
      fprintf(stderr, "cannot parse file [%s]", filename);
      exit(EXIT_FAILURE);
    } else if (!wsman_read_client_config(ini)) {
      fprintf(stderr, "Configuration file not found\n");
      exit(EXIT_FAILURE);
    }
  }
  if (!wsman_parse_options(argc, argv)) {
    exit(EXIT_FAILURE);
  }

  wsman_setup_transport_and_library_options();

  initialize_logging ();
  wsman_client_transport_init(NULL); 
  initialize_action_options(&options);

  debug( "Certificate: %s", wsman_options_get_cafile());

  cl = wsman_create_client( wsman_options_get_server(),
                      wsman_options_get_server_port(),
                      wsman_options_get_path(),
                      wsman_options_get_cafile() ? "https" : "http",
                      wsman_options_get_username(),
                      wsman_options_get_password());



  if (cl == NULL) {
    error("Null Client");
    exit(EXIT_FAILURE);
  }


  /*
   * Setup Resource URI and Selectors
   */
  resource_uri_with_selectors = wsman_options_get_resource_uri();
  if (resource_uri_with_selectors) 
  {
    wsman_set_options_from_uri(resource_uri_with_selectors, &options);
    wsman_remove_query_string(resource_uri_with_selectors, &resource_uri);
  }

  op = wsman_options_get_action();

  if (wsman_options_get_dump_request()) {   
    wsman_set_action_option(&options,FLAG_DUMP_REQUEST ); 
  }

  if (wsman_options_get_max_envelope_size()) {
    options.max_envelope_size = wsman_options_get_max_envelope_size();
  }

  if (wsman_options_get_operation_timeout()) {
    options.timeout = wsman_options_get_operation_timeout();
  }

  if (wsman_options_get_fragment()) {
    options.fragment = wsman_options_get_fragment();
  }

  if (wsman_options_get_filter()) {
    options.filter = wsman_options_get_filter();
  }

  if (wsman_options_get_dialect()) {
    options.dialect = wsman_options_get_dialect();
  }
  if (wsman_options_get_selectors()) {
    wsman_add_selectors_from_query_string(&options,
                wsman_options_get_selectors());
  }

  options.properties = wsman_options_get_properties();
  options.cim_ns = wsman_options_get_cim_namespace();


  switch (op) {
  case  WSMAN_ACTION_TEST:
    rqstDoc = wsman_client_read_file(cl,
                               wsman_options_get_test_file(), "UTF-8", 0);
    wsman_send_request(cl, rqstDoc);
    doc = wsman_build_envelope_from_response(cl);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_IDENTIFY:
    doc = wsman_identify(cl, options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_CUSTOM:
       
    options.properties = wsman_options_get_properties();
    doc = wsman_invoke(cl, resource_uri, wsman_options_get_invoke_method(), options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_TRANSFER_CREATE:
    doc = ws_transfer_create(cl, resource_uri, options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_TRANSFER_PUT:
    doc = ws_transfer_put(cl, resource_uri, options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_TRANSFER_GET:
    doc = ws_transfer_get(cl, resource_uri, options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;
  case  WSMAN_ACTION_PULL:
    doc = wsenum_pull(cl, resource_uri, wsman_options_get_enum_context(), options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;        
  case  WSMAN_ACTION_RELEASE:
    doc = wsenum_release(cl, resource_uri, wsman_options_get_enum_context(), options);
    if (doc) {
      wsman_output(doc);
      ws_xml_destroy_doc(doc);
    }
    break;        
  case WSMAN_ACTION_ENUMERATION:

    enumeration_mode = wsman_options_get_enum_mode();
    binding_enumeration_mode = wsman_options_get_binding_enum_mode();

    if (enumeration_mode) {
      if (strcmp(enumeration_mode, "epr") == 0)
        wsman_set_action_option(&options, FLAG_ENUMERATION_ENUM_EPR);
      else
        wsman_set_action_option(&options,FLAG_ENUMERATION_ENUM_OBJ_AND_EPR);
    }

    if (binding_enumeration_mode) {
      if (strcmp(binding_enumeration_mode, "include") == 0)
        wsman_set_action_option(&options, FLAG_IncludeSubClassProperties);
      else if (strcmp(binding_enumeration_mode, "exclude") == 0)
        wsman_set_action_option(&options, FLAG_ExcludeSubClassProperties);
      else if (strcmp(binding_enumeration_mode, "none") == 0)
        wsman_set_action_option(&options, FLAG_POLYMORPHISM_NONE);
    }

    if (wsman_options_get_optimize_enum()) 
    {
      wsman_set_action_option(&options, FLAG_ENUMERATION_OPTIMIZATION);            
    }
    options.max_elements = wsman_options_get_max_elements();

    if (wsman_options_get_estimate_enum()) {
      wsman_set_action_option(&options, FLAG_ENUMERATION_COUNT_ESTIMATION);
    }
        
    WsXmlDocH enum_response = wsenum_enumerate(cl,
                                               resource_uri,  options);
    if (enum_response) {
      if (wsman_get_client_response_code(cl) == 200 ||
                wsman_get_client_response_code(cl) == 500) {
        wsman_output(enum_response);
      } else {
          break;
      }
      enumContext = wsenum_get_enum_context(enum_response);
      ws_xml_destroy_doc(enum_response);
    } else {
      break;
    }
       
    if (!wsman_options_get_step_request()) 
    {
      while (enumContext !=NULL) {
        doc = wsenum_pull(cl, resource_uri, enumContext, options);
 
        if (wsman_get_client_response_code(cl) != 200 &&
                wsman_get_client_response_code(cl) != 500) {
          break;
        }
        wsman_output(doc);
        enumContext = wsenum_get_enum_context(doc);
        if (doc) {
          ws_xml_destroy_doc(doc);
        }
      }
    }

    break;
  default:
    fprintf(stderr, "Action not supported\n");
    retVal = 1;
  }

  if (wsman_get_client_response_code(cl) != 200) {
    fprintf(stderr, "Connection failed. response code = %ld\n",
            wsman_get_client_response_code(cl));
    if (wsman_client_get_fault_string(cl)) {
        fprintf(stderr, "%s\n", wsman_client_get_fault_string(cl));
    }
  }

  destroy_action_options(&options);
  wsman_release_client(cl);

  wsman_client_transport_fini();
  if (ini) {
    iniparser_freedict(ini);
  }
#ifdef DEBUG_VERBOSE
  printf("     ******   Transfer Time = %ull usecs ******\n", get_transfer_time());
#endif
  return retVal;

}