summaryrefslogtreecommitdiffstats
path: root/lib/plugins/stonith/baytech.c
blob: 33093addd576fad200187584097aa7887f05456f (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
 *	Stonith module for BayTech Remote Power Controllers (RPC-x devices)
 *
 *	Copyright (c) 2000 Alan Robertson <alanr@unix.sh>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <lha_internal.h>
#define	DEVICE	"BayTech power switch"

#define DOESNT_USE_STONITHKILLCOMM	1

#include "stonith_plugin_common.h"

#define PIL_PLUGIN              baytech
#define PIL_PLUGIN_S            "baytech"
#define PIL_PLUGINLICENSE 	LICENSE_LGPL
#define PIL_PLUGINLICENSEURL 	URL_LGPL
#include <pils/plugin.h>

#include "stonith_signal.h"

static StonithPlugin *	baytech_new(const char *);
static void		baytech_destroy(StonithPlugin *);
static int		baytech_set_config(StonithPlugin *, StonithNVpair *);
static const char * const *	baytech_get_confignames(StonithPlugin * s);
static const char *	baytech_get_info(StonithPlugin * s, int InfoType);
static int		baytech_status(StonithPlugin *);
static int		baytech_reset_req(StonithPlugin * s, int request, const char * host);
static char **		baytech_hostlist(StonithPlugin  *);

static struct stonith_ops baytechOps ={
	baytech_new,			/* Create new STONITH object	*/
	baytech_destroy,		/* Destroy STONITH object	*/
	baytech_get_info,		/* Return STONITH info string	*/
	baytech_get_confignames,	/* Return STONITH config vars */
	baytech_set_config,		/* set configuration from vars	*/
	baytech_status,			/* Return STONITH device status	*/
	baytech_reset_req,		/* Request a reset */
	baytech_hostlist,		/* Return list of supported hosts */
};

PIL_PLUGIN_BOILERPLATE2("1.0", Debug)
static const PILPluginImports*  PluginImports;
static PILPlugin*               OurPlugin;
static PILInterface*		OurInterface;
static StonithImports*		OurImports;
static void*			interfprivate;

#include "stonith_expect_helpers.h"

#define	MAXOUTLET		32

PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports);

PIL_rc
PIL_PLUGIN_INIT(PILPlugin*us, const PILPluginImports* imports)
{
	/* Force the compiler to do a little type checking */
	(void)(PILPluginInitFun)PIL_PLUGIN_INIT;

	PluginImports = imports;
	OurPlugin = us;

	/* Register ourself as a plugin */
	imports->register_plugin(us, &OurPIExports);  

	/*  Register our interface implementation */
 	return imports->register_interface(us, PIL_PLUGINTYPE_S
	,	PIL_PLUGIN_S
	,	&baytechOps
	,	NULL		/*close */
	,	&OurInterface
	,	(void*)&OurImports
	,	&interfprivate); 
}

/*
 *	I have an RPC-5.  This code has been tested with this switch.
 *
 *	The BayTech switches are quite nice, but the dialogues are a bit of a
 *	pain for mechanical parsing.
 */

struct pluginDevice {
	StonithPlugin			sp;
	const char *			pluginid;
	char *				idinfo;
	char *				unitid;
	const struct BayTechModelInfo*	modelinfo;
	pid_t				pid;
	int				rdfd;
	int				wrfd;
	char *				device;
	char *				user;
	char *				passwd;
};

struct BayTechModelInfo {
	const char *	type;		/* Baytech model info */
	size_t		socklen;	/* Length of socket name string */
	struct Etoken *	expect;		/* Expect string before outlet list */
};

static int		parse_socket_line(struct pluginDevice*,const char *
,			int *, char *);

static const char * pluginid = "BayTech-Stonith";
static const char * NOTpluginID = "BayTech device has been destroyed";

/*
 *	Different expect strings that we get from the Baytech
 *	Remote Power Controllers...
 */

#define BAYTECHASSOC	"Bay Technical Associates"

static struct Etoken BayTechAssoc[] =	{ {BAYTECHASSOC, 0, 0}, {NULL,0,0}};
static struct Etoken UnitId[] =		{ {"Unit ID: ", 0, 0}, {NULL,0,0}};
static struct Etoken login[] =		{ {"username>", 0, 0} ,{NULL,0,0}};
static struct Etoken password[] =	{ {"password>", 0, 0}
					, {"username>", 0, 0} ,{NULL,0,0}};
static struct Etoken Selection[] =	{ {"election>", 0, 0} ,{NULL,0,0}};
static struct Etoken RPC[] =		{ {"RPC", 0, 0} ,{NULL,0,0}};
static struct Etoken LoginOK[] =	{ {"RPC", 0, 0}, {"Invalid password", 1, 0}
					,	{NULL,0,0}};
static struct Etoken GTSign[] =		{ {">", 0, 0} ,{NULL,0,0}};
static struct Etoken Menu[] =		{ {"Menu:", 0, 0} ,{NULL,0,0}};
static struct Etoken Temp[] =		{ {"emperature: ", 0, 0}
					,	{NULL,0,0}};
static struct Etoken Break[] =		{ {"Status", 0, 0}
					,	{NULL,0,0}};
static struct Etoken PowerApplied[] =	{ {"ower applied to outlet", 0, 0}
					,	{NULL,0,0}};

/* We may get a notice about rebooting, or a request for confirmation */
static struct Etoken Rebooting[] =	{ {"ebooting selected outlet", 0, 0}
					,	{"(Y/N)>", 1, 0}
					,	{"already off.", 2, 0}
					,	{NULL,0,0}};

static struct Etoken TurningOnOff[] =	{ {"RPC", 0, 0}
					,	{"(Y/N)>", 1, 0}
					,	{"already ", 2, 0}
					,	{NULL,0,0}};


static struct BayTechModelInfo ModelInfo [] = {
	{"BayTech RPC-5",	18, Temp},/* This first model will be the default */
	{"BayTech RPC-3",	10, Break},	
	{"BayTech RPC-3A",	10, Break},
	{NULL,		0,  NULL},
};

#include "stonith_config_xml.h"

static const char *baytechXML = 
  XML_PARAMETERS_BEGIN
    XML_IPADDR_PARM
    XML_LOGIN_PARM
    XML_PASSWD_PARM
  XML_PARAMETERS_END;

static int	RPC_connect_device(struct pluginDevice * bt);
static int	RPCLogin(struct pluginDevice * bt);
static int	RPCRobustLogin(struct pluginDevice * bt);
static int	RPCNametoOutletList(struct pluginDevice*, const char * name
,		int outletlist[]);
static int	RPCReset(struct pluginDevice*, int unitnum, const char * rebootid);
static int	RPCLogout(struct pluginDevice * bt);


static int	RPC_onoff(struct pluginDevice*, int unitnum, const char * unitid
,		int request);

/* Login to the Baytech Remote Power Controller (RPC) */

static int
RPCLogin(struct pluginDevice * bt)
{
	char		IDinfo[128];
	static char	IDbuf[128];
	char *		idptr = IDinfo;
	char *		delim;
	int		j;

	EXPECT(bt->rdfd, RPC, 10);

	/* Look for the unit type info */
	if (EXPECT_TOK(bt->rdfd, BayTechAssoc, 2, IDinfo
	,	sizeof(IDinfo), Debug) < 0) {
		LOG(PIL_CRIT, "No initial response from %s.", bt->idinfo);
		return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS);
	}
	idptr += strspn(idptr, WHITESPACE);
	/*
	 * We should be looking at something like this:
         *	RPC-5 Telnet Host
    	 *	Revision F 4.22, (C) 1999
    	 *	Bay Technical Associates
	 */

	/* Truncate the result after the RPC-5 part */
	if ((delim = strchr(idptr, ' ')) != NULL) {
		*delim = EOS;
	}
	snprintf(IDbuf, sizeof(IDbuf), "BayTech RPC%s", idptr);
	REPLSTR(bt->idinfo, IDbuf);
	if (bt->idinfo == NULL) {
		return(S_OOPS);
	}

	bt->modelinfo = &ModelInfo[0];

	for (j=0; ModelInfo[j].type != NULL; ++j) {
		/*
		 * TIMXXX - 
		 * Look at device ID as this really describes the model.
		 */
		if (strcasecmp(ModelInfo[j].type, IDbuf) == 0) {
			bt->modelinfo = &ModelInfo[j];
			break;
		}
	}

	/* Look for the unit id info */
	EXPECT(bt->rdfd, UnitId, 10);
	SNARF(bt->rdfd, IDbuf, 2);
	delim = IDbuf + strcspn(IDbuf, WHITESPACE);
	*delim = EOS;
	REPLSTR(bt->unitid, IDbuf);
	if (bt->unitid == NULL) {
		return(S_OOPS);
	}

	/* Expect "username>" */
	EXPECT(bt->rdfd, login, 2);

	SEND(bt->wrfd, bt->user);
	SEND(bt->wrfd, "\r");

	/* Expect "password>" */

	switch (StonithLookFor(bt->rdfd, password, 5)) {
		case 0:	/* Good! */
			break;

		case 1:	/* OOPS!  got another username prompt */
			LOG(PIL_CRIT, "Invalid username for %s.", bt->idinfo);
			return(S_ACCESS);

		default:
			return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS);
	}

	SEND(bt->wrfd, bt->passwd);
	SEND(bt->wrfd, "\r");

	/* Expect "RPC-x Menu" */

	switch (StonithLookFor(bt->rdfd, LoginOK, 5)) {

		case 0:	/* Good! */
			break;

		case 1:	/* Uh-oh - bad password */
			LOG(PIL_CRIT, "Invalid password for %s.", bt->idinfo);
			return(S_ACCESS);

		default:
			return(errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS);
	}
	EXPECT(bt->rdfd, Menu, 2);

	return(S_OK);
}

static int
RPCRobustLogin(struct pluginDevice * bt)
{
	int	rc=S_OOPS;
	int	j;

	for (j=0; j < 20 && rc != S_OK; ++j) {


		if (RPC_connect_device(bt) != S_OK) {
			continue;
		}

		rc = RPCLogin(bt);
	}
	return rc;
}

/* Log out of the Baytech RPC */

static int
RPCLogout(struct pluginDevice* bt)
{
	int	rc;

	/* Make sure we're in the right menu... */
	SEND(bt->wrfd, "\r");

	/* Expect "Selection>" */
	rc = StonithLookFor(bt->rdfd, Selection, 5);

	/* Option 6 is Logout */
	SEND(bt->wrfd, "6\r");

	close(bt->wrfd);
	close(bt->rdfd);
	bt->wrfd = bt->rdfd = -1;
	return(rc >= 0 ? S_OK : (errno == ETIMEDOUT ? S_TIMEOUT : S_OOPS));
}

/* Reset (power-cycle) the given outlet number */
static int
RPCReset(struct pluginDevice* bt, int unitnum, const char * rebootid)
{
	char		unum[32];


	SEND(bt->wrfd, "\r");

	/* Make sure we're in the top level menu */

	/* Expect "RPC-x Menu" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, Menu, 5);

	/* OK.  Request sub-menu 1 (Outlet Control) */
	SEND(bt->wrfd, "1\r");

	/* Verify that we're in the sub-menu */

	/* Expect: "RPC-x>" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, GTSign, 5);


	/* Send REBOOT command for given outlet */
	snprintf(unum, sizeof(unum), "REBOOT %d\r", unitnum);
	SEND(bt->wrfd, unum);

	/* Expect "ebooting "... or "(Y/N)" (if confirmation turned on) */

	retry:
	switch (StonithLookFor(bt->rdfd, Rebooting, 5)) {
		case 0: /* Got "Rebooting" Do nothing */
			break;

		case 1: /* Got that annoying command confirmation :-( */
			SEND(bt->wrfd, "Y\r");
			goto retry;

		case 2:	/* Outlet is turned off */
			LOG(PIL_CRIT, "Host is OFF: %s.", rebootid);
			return(S_ISOFF);

		default:
			return(errno == ETIMEDOUT ? S_RESETFAIL : S_OOPS);
	}
	LOG(PIL_INFO,	"Host %s (outlet %d) being rebooted."
	,	rebootid, unitnum);
	
	/* Expect "ower applied to outlet" */
	if (StonithLookFor(bt->rdfd, PowerApplied, 30) < 0) {
		return(errno == ETIMEDOUT ? S_RESETFAIL : S_OOPS);
	}

	/* All Right!  Power is back on.  Life is Good! */
	
	LOG(PIL_INFO,	"Power restored to host %s (outlet %d)."
	,	rebootid, unitnum);

	/* Expect: "RPC-x>" */
	EXPECT(bt->rdfd, RPC,5);
	EXPECT(bt->rdfd, GTSign, 5);

	/* Pop back to main menu */
	SEND(bt->wrfd, "MENU\r");
	return(S_OK);
}

static int
RPC_onoff(struct pluginDevice* bt, int unitnum, const char * unitid, int req)
{
	char		unum[32];

	const char *	onoff = (req == ST_POWERON ? "on" : "off");
	int	rc;


	if ((rc = RPCRobustLogin(bt) != S_OK)) {
		LOG(PIL_CRIT, "Cannot log into %s."
		,	bt->idinfo ? bt->idinfo : DEVICE);
		return(rc);
	}
	SEND(bt->wrfd, "\r");

	/* Make sure we're in the top level menu */

	/* Expect "RPC-x Menu" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, Menu, 5);

	/* OK.  Request sub-menu 1 (Outlet Control) */
	SEND(bt->wrfd, "1\r");

	/* Verify that we're in the sub-menu */

	/* Expect: "RPC-x>" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, GTSign, 5);


	/* Send ON/OFF command for given outlet */
	snprintf(unum, sizeof(unum), "%s %d\r"
	,	onoff, unitnum);
	SEND(bt->wrfd, unum);

	/* Expect "RPC->x "... or "(Y/N)" (if confirmation turned on) */

	if (StonithLookFor(bt->rdfd, TurningOnOff, 10) == 1) {
		/* They've turned on that annoying command confirmation :-( */
		SEND(bt->wrfd, "Y\r");
		EXPECT(bt->rdfd, TurningOnOff, 10);
	}

	EXPECT(bt->rdfd, GTSign, 10);

	/* All Right!  Command done. Life is Good! */
	LOG(PIL_INFO, "Power to host %s (outlet %d) turned %s."
	,	unitid, unitnum, onoff);
	/* Pop back to main menu */
	SEND(bt->wrfd, "MENU\r");
	return(S_OK);
}

/*
 *	Map the given host name into an (AC) Outlet number on the power strip
 */

static int
RPCNametoOutletList(struct pluginDevice* bt, const char * name
,		int outletlist[])
{
	char	NameMapping[128];
	int	sockno;
	char	sockname[32];
	int	maxfound = 0;



	/* Verify that we're in the top-level menu */
	SEND(bt->wrfd, "\r");

	/* Expect "RPC-x Menu" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, Menu, 5);


	/* OK.  Request sub-menu 1 (Outlet Control) */
	SEND(bt->wrfd, "1\r");

	/* Verify that we're in the sub-menu */

	/* Expect: "RPC-x>" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, GTSign, 5);

	/* The status command output contains mapping of hosts to outlets */
	SEND(bt->wrfd, "STATUS\r");

	/* Expect: "emperature:" so we can skip over it... */
 	EXPECT(bt->rdfd, bt->modelinfo->expect, 5);
 	EXPECT(bt->rdfd, CRNL, 5);

	/* Looks Good!  Parse the status output */

	do {
		char *	last;
		NameMapping[0] = EOS;
		SNARF(bt->rdfd, NameMapping, 5);

		if (!parse_socket_line(bt, NameMapping, &sockno, sockname)) {
			continue;
		}

		last = sockname+bt->modelinfo->socklen;
		*last = EOS;
		--last;

		/* Strip off trailing blanks */
		for(; last > sockname; --last) {
			if (*last == ' ') {
				*last = EOS;
			}else{
				break;
			}
		}
		if (strcasecmp(name, sockname) == 0) {
			outletlist[maxfound] = sockno;
			++maxfound;
		}
	} while (strlen(NameMapping) > 2  && maxfound < MAXOUTLET);

	/* Pop back out to the top level menu */
	SEND(bt->wrfd, "MENU\r");
	return(maxfound);
}

static int
baytech_status(StonithPlugin  *s)
{
	struct pluginDevice*	bt;
	int	rc;

	ERRIFNOTCONFIGED(s,S_OOPS);

	bt = (struct pluginDevice*) s;
	
	if ((rc = RPCRobustLogin(bt) != S_OK)) {
		LOG(PIL_CRIT, "Cannot log into %s."
		,	bt->idinfo ? bt->idinfo : DEVICE);
		return(rc);
	}

	/* Verify that we're in the top-level menu */
	SEND(bt->wrfd, "\r");

	/* Expect "RPC-x Menu" */
	EXPECT(bt->rdfd, RPC, 5);
	EXPECT(bt->rdfd, Menu, 5);

	return(RPCLogout(bt));
}
/*
 *	Return the list of hosts (outlet names) for the devices on this BayTech unit
 */

static char **
baytech_hostlist(StonithPlugin  *s)
{
	char		NameMapping[128];
	char*		NameList[64];
	unsigned int	numnames = 0;
	char **		ret = NULL;
	struct pluginDevice*	bt;
	unsigned int	i;

	ERRIFNOTCONFIGED(s,NULL);

	bt = (struct pluginDevice*) s;
	
	if (RPCRobustLogin(bt) != S_OK) {
		LOG(PIL_CRIT, "Cannot log into %s."
		,	bt->idinfo ? bt->idinfo : DEVICE);
		return(NULL);
	}

	/* Verify that we're in the top-level menu */
	SEND(bt->wrfd, "\r");

	/* Expect "RPC-x Menu" */
	NULLEXPECT(bt->rdfd, RPC, 5);
	NULLEXPECT(bt->rdfd, Menu, 5);

	/* OK.  Request sub-menu 1 (Outlet Control) */
	SEND(bt->wrfd, "1\r");

	/* Verify that we're in the sub-menu */

	/* Expect: "RPC-x>" */
	NULLEXPECT(bt->rdfd, RPC, 5);
	NULLEXPECT(bt->rdfd, GTSign, 5);

	/* The status command output contains mapping of hosts to outlets */
	SEND(bt->wrfd, "STATUS\r");

	/* Expect: "emperature:" so we can skip over it... */
	NULLEXPECT(bt->rdfd, bt->modelinfo->expect, 5);
	NULLEXPECT(bt->rdfd, CRNL, 5);

	/* Looks Good!  Parse the status output */

	do {
		int	sockno;
		char	sockname[64];
		char *	last;
		char *	nm;

		NameMapping[0] = EOS;

		NULLSNARF(bt->rdfd, NameMapping, 5);

		if (!parse_socket_line(bt, NameMapping, &sockno, sockname)) {
			continue;
		}

		last = sockname+bt->modelinfo->socklen;
		*last = EOS;
		--last;

		/* Strip off trailing blanks */
		for(; last > sockname; --last) {
			if (*last == ' ') {
				*last = EOS;
			}else{
				break;
			}
		}
		if (numnames >= DIMOF(NameList)-1) {
			break;
		}
		if ((nm = (char*)STRDUP(sockname)) == NULL) {
			goto out_of_memory;
		}
		strdown(nm);
		NameList[numnames] = nm;
		++numnames;
		NameList[numnames] = NULL;
	} while (strlen(NameMapping) > 2);

	/* Pop back out to the top level menu */
	SEND(bt->wrfd, "MENU\r");
	if (numnames >= 1) {
		ret = (char **)MALLOC((numnames+1)*sizeof(char*));
		if (ret == NULL) {
			goto out_of_memory;
		}else{
			memcpy(ret, NameList, (numnames+1)*sizeof(char*));
		}
	}
	(void)RPCLogout(bt);
	return(ret);

out_of_memory:
	LOG(PIL_CRIT, "out of memory");
	for (i=0; i<numnames; i++) {
		FREE(NameList[i]);
	}
	return(NULL);
}

/*
 *	Connect to the given BayTech device.
 *	We should add serial support here eventually...
 */
static int
RPC_connect_device(struct pluginDevice * bt)
{
	int fd = OurImports->OpenStreamSocket(bt->device
	,	TELNET_PORT, TELNET_SERVICE);

	if (fd < 0) {
		return(S_OOPS);
	}
	bt->rdfd = bt->wrfd = fd;
	return(S_OK);
}

/*
 *	Reset the given host on this Stonith device.
 */
static int
baytech_reset_req(StonithPlugin * s, int request, const char * host)
{
	int	rc = S_OK;
	int	lorc = 0;
	struct pluginDevice*	bt;

	ERRIFNOTCONFIGED(s,S_OOPS);

	bt = (struct pluginDevice*) s;

	if ((rc = RPCRobustLogin(bt)) != S_OK) {
		LOG(PIL_CRIT, "Cannot log into %s."
		,	bt->idinfo ? bt->idinfo : DEVICE);
	}else{
		int	noutlets;
		int	outlets[MAXOUTLET];
		int	j;
		noutlets = RPCNametoOutletList(bt, host, outlets);

		if (noutlets < 1) {
			LOG(PIL_CRIT,	"%s %s doesn't control host [%s]"
			,	bt->idinfo, bt->unitid, host);
			return(S_BADHOST);
		}
		switch(request) {

		case ST_POWERON:
		case ST_POWEROFF:
			for (j=0; rc == S_OK && j < noutlets;++j) {
				rc = RPC_onoff(bt, outlets[j], host, request);
			}
			break;
		case ST_GENERIC_RESET:
			/*
			 * Our strategy here:
			 *   1. Power off all outlets except the last one
			 *   2. reset the last outlet
			 *   3. power the other outlets back on
			 */

			for (j=0; rc == S_OK && j < noutlets-1; ++j) {
				rc = RPC_onoff(bt,outlets[j],host
				,	ST_POWEROFF);
			}
			if (rc == S_OK) {
				rc = RPCReset(bt, outlets[j], host); 
			}
			for (j=0; rc == S_OK && j < noutlets-1; ++j) {
				rc = RPC_onoff(bt, outlets[j], host
				,	ST_POWERON);
			}
			break;
		default:
			rc = S_INVAL;
			break;
		}
	}

	lorc = RPCLogout(bt);

	return(rc != S_OK ? rc : lorc);
}

static const char * const *
baytech_get_confignames(StonithPlugin * s)
{
	static const char * ret[] = {ST_IPADDR, ST_LOGIN, ST_PASSWD, NULL};
	return ret;
}


/*
 *	Parse the config information in the given string, and stash it away...
 */
static int
baytech_set_config(StonithPlugin* s, StonithNVpair* list)
{
	struct pluginDevice* bt = (struct pluginDevice *)s;
	int		rc;
	StonithNamesToGet	namestocopy [] =
	{	{ST_IPADDR,	NULL}
	,	{ST_LOGIN,	NULL}
	,	{ST_PASSWD,	NULL}
	,	{NULL,		NULL}
	};

	ERRIFWRONGDEV(s, S_OOPS);
	if (bt->sp.isconfigured) {
		return S_OOPS;
	}

	if ((rc =OurImports->CopyAllValues(namestocopy, list)) != S_OK) {
		return rc;
	}
	bt->device = namestocopy[0].s_value;
	bt->user = namestocopy[1].s_value;
	bt->passwd = namestocopy[2].s_value;

	return(S_OK);
}

static const char *
baytech_get_info(StonithPlugin * s, int reqtype)
{
	struct pluginDevice* bt;
	const char *		ret;

	ERRIFWRONGDEV(s,NULL);

	bt = (struct pluginDevice *)s;

	switch (reqtype) {

		case ST_DEVICEID:		/* What type of device? */
			ret = bt->idinfo;
			break;

		case ST_DEVICENAME:		/* Which particular device? */
			ret = bt->device;
			break;

		case ST_DEVICEDESCR:		/* Description of dev type */
			ret = "Bay Technical Associates (Baytech) RPC "
			"series power switches (via telnet).\n"
			"The RPC-5, RPC-3 and RPC-3A switches are well tested"
			".";
			break;

		case ST_DEVICEURL:		/* Manufacturer's web site */
			ret = "http://www.baytech.net/";
			break;

		case ST_CONF_XML:		/* XML metadata */
			ret = baytechXML;
			break;

		default:
			ret = NULL;
			break;
	}
	return ret;
}

/*
 *	Baytech Stonith destructor...
 */
static void
baytech_destroy(StonithPlugin *s)
{
	struct pluginDevice* bt;

	VOIDERRIFWRONGDEV(s);

	bt = (struct pluginDevice *)s;

	bt->pluginid = NOTpluginID;
	if (bt->rdfd >= 0) {
		close(bt->rdfd);
		bt->rdfd = -1;
	}
	if (bt->wrfd >= 0) {
		close(bt->wrfd);
		bt->wrfd = -1;
	}
	if (bt->device != NULL) {
		FREE(bt->device);
		bt->device = NULL;
	}
	if (bt->user != NULL) {
		FREE(bt->user);
		bt->user = NULL;
	}
	if (bt->passwd != NULL) {
		FREE(bt->passwd);
		bt->passwd = NULL;
	}
	if (bt->idinfo != NULL) {
		FREE(bt->idinfo);
		bt->idinfo = NULL;
	}
	if (bt->unitid != NULL) {
		FREE(bt->unitid);
		bt->unitid = NULL;
	}
	FREE(bt);
}

/* Create a new BayTech Stonith device. */

static StonithPlugin *
baytech_new(const char *subplugin)
{
	struct pluginDevice*	bt = ST_MALLOCT(struct pluginDevice);

	if (bt == NULL) {
		LOG(PIL_CRIT, "out of memory");
		return(NULL);
	}
	memset(bt, 0, sizeof(*bt));
	bt->pluginid = pluginid;
	bt->pid = -1;
	bt->rdfd = -1;
	bt->wrfd = -1;
	REPLSTR(bt->idinfo, DEVICE);
	if (bt->idinfo == NULL) {
		FREE(bt);
		return(NULL);
	}
	bt->modelinfo = &ModelInfo[0];
	bt->sp.s_ops = &baytechOps;

	return &(bt->sp);	/* same as "bt" */
}

static int
parse_socket_line(struct pluginDevice * bt, const char *NameMapping
,	int *sockno, char *sockname)
{
#if 0
	char format[64];
	snprintf(format, sizeof(format), "%%7d       %%%dc"
	,	bt->modelinfo->socklen);
	/* 7 digits, 7 blanks, then 'socklen' characters */
	/* [0-6]: digits, NameMapping[13] begins the sockname */
	/* NameMapping strlen must be >= socklen + 14 */

	if (sscanf(NameMapping, format, sockno, sockname) != 2) {
		return FALSE;
	}
#else
#	define	OFFSET 14

	if (sscanf(NameMapping, "%7d", sockno) != 1
	||	strlen(NameMapping) < OFFSET+bt->modelinfo->socklen) {
		return FALSE;
	}
	strncpy(sockname, NameMapping+OFFSET, bt->modelinfo->socklen);
	sockname[bt->modelinfo->socklen] = EOS;
#endif
	return TRUE;
}