rmanage.c 11.8 KB
Newer Older
1
2
3
4
5
6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2006 University of Utah and the Flux Group.
 * All rights reserved.
 */

7
8
#include "rmcp.h"

9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
14
#ifndef NO_EMULAB
15
16
#include <mysql/mysql.h>
#include "tbdb.h"
17
#endif
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

extern char *optarg;
extern int optopt;
extern int opterr;
extern int optind;

#define COMMAND_PING         "ping"
#define COMMAND_CAPABILITIES "capabilities"
#define COMMAND_STATE        "state"
#define COMMAND_NOP          "nop"
#define COMMAND_RESET        "reset"
#define COMMAND_POWER_CYCLE  "cycle"
#define COMMAND_POWER_DOWN   "off"
#define COMMAND_POWER_UP     "on"
#define COMMAND_COUNT        0x08

static char *commands[COMMAND_COUNT][2] = 
    {
	{ COMMAND_PING, "Send an RMCP ping and display supported modes." },
	{ COMMAND_CAPABILITIES, "Get and display RMCP capabilities." },
	{ COMMAND_STATE, "Get and display current node power state." },
	{ COMMAND_NOP, "Open a session if in secure mode; else nothing." },
	{ COMMAND_RESET, "Send a warm reset command." },
	{ COMMAND_POWER_CYCLE, "Send a full power cycle command." },
	{ COMMAND_POWER_DOWN, "Send a power down command." },
	{ COMMAND_POWER_UP, "Send a power up command." }
    };

#define DEFAULT_TIMEOUT 3
#define DEFAULT_RETRIES 3

void usage(char *prog) {
    int i;

    fprintf(stderr,
53
	    "Usage: %s [-hHdsE] [-tmrkgu <arg>] <clientname> <command>\n"
54
55
56
57
58
	    "\n"
	    "  -h         Print this message\n"
	    "  -d         Turn on debugging (more d's mean more debug info)\n"
	    "  -s         Use secure RMCP\n"
	    "  -H         Interpret keys as hex strings instead of char strings\n"
59
#ifndef NO_EMULAB
60
	    "  -E         Resolve client and keys using the Emulab database\n"
61
#endif
62
	    "               (this option implies -H)\n"
63
64
65
66
67
	    "  -t timeout Timeout (in seconds) for individual RMCP messages\n"
	    "               (default: %d)\n"
	    "  -m retries Retry N times for unacknowledged RMCP sends\n"
	    "               (default: %d)\n"
	    "  -r role    Use this role (either 'operator' or 'administrator')\n"
68
	    "               (default 'administrator')\n"
69
70
71
72
	    "  -k key     Use this key with the role specified by '-r'\n"
	    "  -g key     Use this generation key\n"
	    "  -u uid     Send the specified username\n"
	    "\n"
73
74
75
76
	    "  clientname IP or hostname\n"
#ifndef NO_EMULAB
	    "    (or Emulab node_id if -E specified)\n"
#endif
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
	    "  command    This argument performs an operation on the managed\n"
	    "             client.  The available commands are:\n",
	    prog,DEFAULT_TIMEOUT,DEFAULT_RETRIES);
    for (i = 0; i < COMMAND_COUNT; ++i) {
	fprintf(stderr,
		"    %s\t[%s]\n",
		commands[i][0],commands[i][1]);
    }
    fprintf(stderr,"\n");

    return;
}

int main(int argc,char **argv) {
    rmcp_error_t retval;
    rmcp_ctx_t *ctx;
    rmcp_asf_supported_t *supported;
    rmcp_asf_capabilities_t *capabilities;
    rmcp_asf_sysstate_t *sysstate;
    
    int c;
    int debug = 0;
    int secure = 0;
    int hex_mode = 0;
    char *client = NULL;
102
    u_int8_t *rkey = NULL;
103
    int rkey_len;
104
    u_int8_t *gkey = NULL;
105
106
107
    int gkey_len;
    int timeout = DEFAULT_TIMEOUT;
    int retries = DEFAULT_RETRIES;
108
    char *role = "administrator";
109
110
111
    int roleno;
    char *uid = NULL;
    char *command = NULL;
112
113
    int emulab = 0;
    char emip[16];
114
#ifndef NO_EMULAB
115
116
117
    MYSQL_RES *res;
    MYSQL_ROW row;
    int nrows;
118
#endif
119
120
    char *src;
    int clen;
121

122
    while ((c = getopt(argc,argv,"t:m:r:k:g:u:hdsHE")) != -1) {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	switch (c) {
	case 'h':
	    usage(argv[0]);
	    exit(0);
	    break;
	case 'd':
	    ++debug;
	    break;
	case 's':
	    secure = 1;
	    break;
	case 'H':
	    hex_mode = 1;
	    break;
137
138
139
140
	case 'E':
	    emulab = 1;
	    /* assume keys in the Emulab db are in hex mode... */
	    hex_mode = 1;
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
	    break;
	case 't':
	    timeout = atoi(optarg);
	    break;
	case 'm':
	    retries = atoi(optarg);
	    break;
	case 'r':
	    role = optarg;
	    if (strcmp("operator",role) && strcmp("administrator",role)) {
		fprintf(stderr,"ERROR: unknown role '%s'!\n",role);
		exit(-1);
	    }
	    break;
	case 'k':
/*  	    rkey = (u_int8_t *)malloc((strlen(optarg)-1)*sizeof(u_int8_t)); */
/*  	    memcpy(rkey,optarg,strlen(optarg)-1); */
/*  	    rkey_len = strlen(optarg)-1; */
	    rkey = optarg;
	    rkey_len = strlen(optarg);
	    break;
	case 'g':
	    gkey = optarg;
	    gkey_len = strlen(optarg);
/*  	    gkey = (u_int8_t *)malloc((strlen(optarg)-1)*sizeof(u_int8_t)); */
/*  	    memcpy(gkey,optarg,strlen(optarg)-1); */
/*  	    gkey_len = strlen(optarg)-1; */
	    break;
	case 'u':
	    uid = optarg;
	    break;
	default:
	    fprintf(stderr,"ERROR: unknown option '%c'!\n",c);
	    usage(argv[0]);
	    exit(-1);
	    break;
	}
    }

    argc -= optind;
    argv += optind;
    
183
184
185
    if (argc != 2) {
	fprintf(stderr,
		"ERROR: must supply both clientname and command args!\n");
186
187
188
	exit(-1);
    }
    else {
189
190
	client = argv[0];
	command = argv[1];
191
192
193
194
195
196
197
198
199
    }

    if (!secure && (!strcmp(command,"reset") || !strcmp(command,"cycle") 
		   || !strcmp(command,"on") || !strcmp(command,"off"))) {
	fprintf(stderr,"Command '%s' requires secure RMCP.\n",command);
	exit(-3);
    }
    
    rmcp_set_debug_level(debug);
200
201
    rmcp_set_enable_warn_err(1);

202
203
    ctx = rmcp_ctx_init(timeout,retries);

204
#ifndef NO_EMULAB
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
    if (emulab) {
	dbinit();
	/* Attempt lookup in Emulab db */
	if (debug) {
	    fprintf(stderr,"Resolving %s... ",client);
	}
	if (!mydb_nodeidtoip(client,emip)) {
	    fprintf(stderr,"ERROR: could not resolve node_id %s",client);
	    exit(9);
	}
	if (debug) {
	    fprintf(stderr,"%s.\n",emip);
	}

	/* 
	 * If we're in secure mode, grab keys out of the db too if we
	 * need them.
	 */
	if (rkey == NULL) {
	    res = mydb_query("select mykey from outlets_remoteauth"
			     "  where node_id='%s' and key_role='%s'",
			     1,client,role);
	    if (!res) {
		fprintf(stderr,
			"ERROR: could not find keys for %s in Emulab db!\n",
			client);
		exit(10);
	    }
	    
	    nrows = (int)mysql_num_rows(res);
	    if (nrows != 1) {
		fprintf(stderr,"ERROR: could not find role key for Emulab"
			" nodeid %s\n",client);
		exit(11);
	    }

	    row = mysql_fetch_row(res);
	    rkey = (char *)malloc(strlen(row[0])+1);
	    src = row[0];
	    clen = strlen(row[0]) + 1;
	    if (!strncmp("0x",row[0],2)) {
		src = &row[0][2];
		clen -= 2;
	    }
	    strncpy(rkey,src,clen);
	    rkey_len = strlen(rkey);
	    mysql_free_result(res);
	}

	if (gkey == NULL) {
	    res = mydb_query("select mykey from outlets_remoteauth"
                             "  where node_id='%s' and key_role='%s'",
                             1,client,"generation");
            if (!res) {
                fprintf(stderr,
                        "ERROR: could not find gkey for %s in Emulab db!\n",
                        client);
                exit(10);
            }

            nrows = (int)mysql_num_rows(res);
            if (nrows != 1) {
                fprintf(stderr,"ERROR: could not find generation key for"
			" Emulab nodeid %s\n",client);
                exit(11);
            }

            row = mysql_fetch_row(res);
            gkey = (char *)malloc(strlen(row[0])+1);
            src = row[0];
            clen = strlen(row[0]) + 1;
            if (!strncmp("0x",row[0],2)) {
                src = &row[0][2];
                clen -= 2;
            }
            strncpy(gkey,src,clen);
	    gkey_len = strlen(gkey);
	    mysql_free_result(res);
        }

	client = emip;
    }
287
#endif
288

289
290
291
292
293
294
295
296
    if (secure) {
	if (strcmp(role,"administrator") == 0) {
	    roleno = RMCP_ROLE_ADM;
	}
	else if (strcmp(role,"operator") == 0) {
	    roleno = RMCP_ROLE_OP;
	}
	else {
297
	    fprintf(stderr,"Unknown role '%s'!\n",role);
298
299
300
	    exit(-4);
	}

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
	if (hex_mode) {
            char *rkey_tmp = malloc(sizeof(char)*20);
            char *gkey_tmp = malloc(sizeof(char)*20);
            int lpc;
            int outer_lpc = 0;
            for (lpc = 0; lpc < rkey_len; lpc += 2) {
                char cbite[3];
                cbite[2] = '\0';
                if ((lpc + 2) == rkey_len) {
		    strncpy(cbite,&rkey[lpc],1);
		    cbite[1] = '\0';
                }
                else {
		    strncpy(cbite,&rkey[lpc],2);
                }
                int bite = (int)strtol(cbite,NULL,16);
                rkey_tmp[outer_lpc++] = (char)bite;
            }
            rkey_len = outer_lpc;
            outer_lpc = 0;
            for (lpc = 0; lpc < gkey_len; lpc += 2) {
		char cbite[3];
		cbite[2] = '\0';
		if ((lpc + 2) == gkey_len) {
		    strncpy(cbite,&gkey[lpc],1);
		    cbite[1] = '\0';
		}
		else {
		    strncpy(cbite,&gkey[lpc],2);
		}
		int bite = (int)strtol(cbite,NULL,16);
		gkey_tmp[outer_lpc++] = (char)bite;
            }
            gkey_len = outer_lpc;
            gkey = gkey_tmp;
            rkey = rkey_tmp;
        }
	
339
340
	rmcp_ctx_setsecure(ctx,
			   roleno,
341
342
			   rkey,rkey_len,
			   gkey,gkey_len);
343
344
345
346
347
348
349
350
	if (uid) {
	    rmcp_ctx_setuid(ctx,uid,strlen(uid)-1);
	}
    }

    retval = rmcp_open(ctx,client);
    if (retval != RMCP_SUCCESS) {
	fprintf(stderr,
351
352
		"Could not open connection to client %s: error %s.\n",
		client,rmcp_error_tostr(retval));
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
	exit(-5);
    }

    if (secure) {
	retval = rmcp_start_secure_session(ctx);
	if (retval != RMCP_SUCCESS) {
	    fprintf(stderr,
		    "Could not start secure session: error 0x%x\n",
		    retval);
	    exit(-6);
	}
    }

    if (!strcmp(command,COMMAND_PING)) {
	/* send a ping and print the supported protocols */
	retval = rmcp_asf_ping(ctx,&supported);
	if (retval != RMCP_SUCCESS) {
	    fprintf(stderr,
371
		    "Command '%s' failed: %s\n",
372
		    command,
373
		    rmcp_error_tostr(retval));
374
375
376
377
378
379
380
381
382
	    exit(-16);
	}
	rmcp_print_asf_supported(supported,NULL);
    }
    else if (!strcmp(command,COMMAND_CAPABILITIES)) {
	/* get capabilities and print them */
	retval = rmcp_asf_get_capabilities(ctx,&capabilities);
	if (retval != RMCP_SUCCESS) {
	    fprintf(stderr,
383
		    "Command '%s' failed: %s\n",
384
		    command,
385
		    rmcp_error_tostr(retval));
386
387
388
389
390
391
392
393
394
	    exit(-16);
	}
	rmcp_print_asf_capabilities(capabilities,NULL);
    }
    else if (!strcmp(command,COMMAND_STATE)) {
	/* get state and print it */
	retval = rmcp_asf_get_sysstate(ctx,&sysstate);
	if (retval != RMCP_SUCCESS) {
	    fprintf(stderr,
395
		    "Command '%s' failed: %s\n",
396
		    command,
397
		    rmcp_error_tostr(retval));
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
	    exit(-16);
	}
	rmcp_print_asf_sysstate(sysstate,NULL);
    }
    else if (!strcmp(command,COMMAND_NOP)) {
	/* do nothing */
	fprintf(stdout,
		"As requested, nothing done%s.\n",
		(secure)?" (except secure session establishment)":"");
    }
    else if (!strcmp(command,COMMAND_RESET)) {
	/* reset */
	retval = rmcp_asf_reset(ctx);
	if (retval == RMCP_SUCCESS) {
	    fprintf(stdout,"Reset sent successfully.\n");
	}
	else {
415
416
	    fprintf(stderr,"Reset unsuccessful: %s\n",
		    rmcp_error_tostr(retval));
417
418
419
420
421
422
423
424
425
426
	    exit(-12);
	}
    }
    else if (!strcmp(command,COMMAND_POWER_CYCLE)) {
	/* power cycle */
	retval = rmcp_asf_power_cycle(ctx);
	if (retval == RMCP_SUCCESS) {
	    fprintf(stdout,"Power cycle sent successfully.\n");
	}
	else {
427
428
	    fprintf(stderr,"Power cycle unsuccessful: %s\n",
		    rmcp_error_tostr(retval));
429
430
431
432
433
434
435
436
437
438
	    exit(-12);
	}	
    }
    else if (!strcmp(command,COMMAND_POWER_UP)) {
	/* power up */
	retval = rmcp_asf_power_up(ctx);
	if (retval == RMCP_SUCCESS) {
	    fprintf(stdout,"Power up sent successfully.\n");
	}
	else {
439
440
	    fprintf(stderr,"Power up unsuccessful: %s\n",
		    rmcp_error_tostr(retval));
441
442
443
444
445
446
447
448
449
450
	    exit(-12);
	}	
    }
    else if (!strcmp(command,COMMAND_POWER_DOWN)) {
	/* power down */
	retval = rmcp_asf_power_down(ctx);
	if (retval == RMCP_SUCCESS) {
	    fprintf(stdout,"Power down sent successfully.\n");
	}
	else {
451
452
	    fprintf(stderr,"Power down unsuccessful: %s\n",
		    rmcp_error_tostr(retval));
453
454
455
456
457
458
459
	    exit(-12);
	}	
    }

    retval = rmcp_finalize(ctx);
    if (retval != RMCP_SUCCESS) {
	fprintf(stderr,
460
461
		"Could not close session/connection: %s\n",
		rmcp_error_tostr(retval));
462
463
464
465
466
467
468
469
470
471
	exit(-10);
    }

    exit(0);
}