-
Notifications
You must be signed in to change notification settings - Fork 2
/
EcControl.m
3667 lines (3362 loc) · 90.3 KB
/
EcControl.m
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
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 3 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
Library 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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import "EcAlarm.h"
#import "EcAlarmSinkSNMP.h"
#import "EcAlerter.h"
#import "EcClientI.h"
#import "EcHost.h"
#import "EcProcess.h"
#import "EcUserDefaults.h"
#import "NSFileHandle+Printf.h"
#include "config.h"
#if defined(HAVE_LIBCRYPT)
extern char *crypt(const char *key, const char *salt);
#endif
/*
* Catagory so that NSHost objects can be safely used as dictionary keys.
*/
@implementation NSHost (ControlExtension)
- (id) copyWithZone: (NSZone*)z
{
return RETAIN(self);
}
@end
static NSString *controlKey = nil;
static EcAlarmSinkSNMP *sink = nil;
static NSMutableDictionary *lastAlertInfo = nil;
static NSTimeInterval pingDelay = 240.0;
static int alertAlarmThreshold = EcAlarmSeverityMajor;
static int reminderInterval = 0;
static uint64_t alarmsAlerted = 0;
static uint64_t alarmsIgnored = 0;
static int comp_len = 0;
static int comp(NSString *s0, NSString *s1)
{
if ([s0 length] > [s1 length])
{
comp_len = -1;
return -1;
}
if ([s1 compare: s0
options: NSCaseInsensitiveSearch|NSLiteralSearch
range: NSMakeRange(0, [s0 length])] == NSOrderedSame)
{
comp_len = [s0 length];
if (comp_len == [s1 length])
{
return 0;
}
else
{
return 1;
}
}
else
{
comp_len = -1;
return -1;
}
}
static BOOL matchCmd(NSString *word, NSString *reference, NSArray *allow)
{
if (comp(word, reference) < 0)
{
return NO;
}
if (nil == allow || [allow containsObject: reference])
{
return YES;
}
return NO;
}
static NSString* cmdWord(NSArray* a, unsigned int pos)
{
if (a != nil && [a count] > pos)
{
return [a objectAtIndex: pos];
}
else
{
return @"";
}
}
@interface CommandInfo : EcClientI
{
NSArray *servers;
}
- (NSString*) serverByAbbreviation: (NSString*)s;
- (NSArray*) servers;
- (void) setServers: (NSArray*)s;
@end
@implementation CommandInfo
- (void) dealloc
{
DESTROY(servers);
[super dealloc];
}
- (NSString*) serverByAbbreviation: (NSString*)s
{
NSString *svr;
int i;
int best_pos = -1;
int best_len = 0;
/*
* Special case - a numeric value is used as an index into the array.
*/
if (isdigit(*[s cString]))
{
i = [s intValue];
if (i >= 0 && i < [servers count])
{
svr = [servers objectAtIndex: i];
return svr;
}
}
for (i = 0; i < [servers count]; i++)
{
svr = [servers objectAtIndex: i];
if (comp(s, svr) == 0)
{
return svr;
}
if (comp_len > best_len)
{
best_len = comp_len;
best_pos = i;
}
}
if (best_pos >= 0)
{
svr = [servers objectAtIndex: best_pos];
return svr;
}
return nil;
}
- (NSArray*) servers
{
return servers;
}
- (void) setServers: (NSArray*)s
{
ASSIGN(servers, s);
}
@end
@interface ConsoleInfo : EcClientI
{
NSString *cserv;
NSString *chost;
NSString *pass;
BOOL general;
BOOL warnings;
BOOL errors;
BOOL alerts;
BOOL audits;
BOOL mark;
BOOL prompt;
}
- (id) initFor: (id)o
name: (NSString*)n
with: (id<CmdClient>)s
pass: (NSString*)p;
- (NSString*) chost;
- (NSString*) cserv;
- (BOOL) getAlerts;
- (BOOL) getAudits;
- (BOOL) getErrors;
- (BOOL) getGeneral;
- (BOOL) getMark;
- (BOOL) getPrompt;
- (BOOL) getWarnings;
- (NSString*) pass;
- (NSString*) promptAfter: (NSString*)msg from: (NSString*)sender;
- (void) setAlerts: (BOOL)flag;
- (void) setAudits: (BOOL)flag;
- (void) setConnectedHost: (NSString*)c;
- (void) setConnectedServ: (NSString*)c;
- (void) setErrors: (BOOL)flag;
- (void) setGeneral: (BOOL)flag;
- (void) setMark: (BOOL)flag;
- (void) setPrompt: (BOOL)flag;
- (void) setWarnings: (BOOL)flag;
@end
@implementation ConsoleInfo
- (NSString*) chost
{
return chost;
}
- (NSString*) cserv
{
return cserv;
}
- (void) dealloc
{
DESTROY(chost);
DESTROY(cserv);
DESTROY(pass);
[super dealloc];
}
- (BOOL) getAlerts
{
return alerts;
}
- (BOOL) getAudits
{
return audits;
}
- (BOOL) getErrors
{
return errors;
}
- (BOOL) getGeneral
{
return general;
}
- (BOOL) getMark
{
return mark;
}
- (BOOL) getPrompt
{
return prompt;
}
- (BOOL) getWarnings
{
return warnings;
}
- (id) initFor: (id)o
name: (NSString*)n
with: (id<CmdClient>)s
pass: (NSString*)p
{
self = [super initFor: o name: n with: s];
if (self != nil)
{
pass = [p copy];
chost = nil;
cserv = nil;
general = NO;
warnings = NO;
alerts = YES;
audits = NO;
errors = YES;
mark = NO;
prompt = YES;
}
return self;
}
- (NSString*) pass
{
return pass;
}
- (NSString*) promptAfter: (NSString*)msg from: (NSString*)sender
{
NSString *sMark = @"";
NSString *eMark = @"";
if (sender && mark)
{
sMark = [NSString stringWithFormat: @"[start message from %@]\n", sender];
eMark = [NSString stringWithFormat: @"\n[end message from %@]", sender];
}
if (NO == prompt)
{
return [NSString stringWithFormat: @"%@%@%@",
sMark, msg, eMark];
}
else if (chost && cserv)
{
return [NSString stringWithFormat: @"%@%@%@\n%@:%@> ",
sMark, msg, eMark, chost, cserv];
}
else if (chost)
{
return [NSString stringWithFormat: @"%@%@%@\n%@:> ",
sMark, msg, eMark, chost];
}
else if (cserv)
{
return [NSString stringWithFormat: @"%@%@%@\n:%@> ",
sMark, msg, eMark, cserv];
}
else
{
return [NSString stringWithFormat: @"%@%@%@\nControl> ",
sMark, msg, eMark];
}
}
- (void) setAlerts: (BOOL)flag
{
alerts = flag;
}
- (void) setAudits: (BOOL)flag
{
audits = flag;
}
- (void) setConnectedHost: (NSString*)c
{
ASSIGNCOPY(chost, c);
}
- (void) setConnectedServ: (NSString*)c
{
ASSIGNCOPY(cserv, c);
}
- (void) setErrors: (BOOL)flag
{
errors = flag;
}
- (void) setGeneral: (BOOL)flag
{
general = flag;
}
- (void) setMark: (BOOL)flag
{
mark = (flag ? YES : NO);
}
- (void) setPrompt: (BOOL)flag
{
prompt = (flag ? YES : NO);
}
- (void) setWarnings: (BOOL)flag
{
warnings = flag;
}
@end
@interface EcControl : EcProcess <Control>
{
NSFileManager *mgr;
NSMutableArray *commands;
NSMutableArray *consoles;
NSString *logname;
NSDictionary *config;
NSDictionary *controlConfig;
NSMutableDictionary *operators;
NSMutableDictionary *fileBodies;
NSMutableDictionary *fileDates;
NSTimer *timer;
NSTimer *terminating;
unsigned commandPingPosition;
unsigned consolePingPosition;
NSString *configFailed;
NSString *configIncludeFailed;
NSRegularExpression *alarmFilter;
EcAlerter *alerter;
}
- (NSFileHandle*) openLog: (NSString*)lname;
- (oneway void) cmdGnip: (id <CmdPing>)from
sequence: (unsigned)num
extra: (NSData*)data;
- (oneway void) cmdPing: (id <CmdPing>)from
sequence: (unsigned)num
extra: (NSData*)data;
- (oneway void) cmdQuit: (NSInteger)status;
- (void) command: (NSData*)dat
from: (NSString*)f;
- (BOOL) connection: (NSConnection*)ancestor
shouldMakeNewConnection: (NSConnection*)newConn;
- (id) connectionBecameInvalid: (NSNotification*)notification;
- (EcClientI*) findIn: (NSArray*)a
byAbbreviation: (NSString*)s;
- (EcClientI*) findIn: (NSArray*)a
byName: (NSString*)s;
- (EcClientI*) findIn: (NSArray*)a
byObject: (id)s;
- (void) information: (NSString*)inf
type: (EcLogType)t
to: (NSString*)to
from: (NSString*)from;
- (NSString*) messageForAlarm: (EcAlarm*)alarm;
- (NSData*) registerCommand: (id<Command>)c
name: (NSString*)n;
- (NSString*) registerConsole: (id<Console>)c
name: (NSString*)n
pass: (NSString*)p;
- (void) reply: (NSString*) msg to: (NSString*)n from: (NSString*)c;
- (void) reportAlarm: (EcAlarm*)alarm
withMessage: (NSString*)message
isCleared: (BOOL)cleared
reminder: (int)reminder;
- (void) reportAlarms;
- (void) servers: (NSData*)d
on: (id<Command>)s;
- (void) timedOut: (NSTimer*)t;
- (void) unregister: (id)obj;
- (BOOL) update;
- (void) updateConfig: (NSData*)dummy;
- (id) getInclude: (id)s;
- (id) recursiveInclude: (id)o;
- (id) tryInclude: (id)s multi: (BOOL*)flag;
@end
@implementation EcControl
- (oneway void) alarm: (in bycopy EcAlarm*)alarm
{
EcAlarmSeverity severity;
EcAlarm *old;
NSString *mesg;
NSString *desc;
NSRange range;
if (NO == [NSThread isMainThread])
{
[self performSelectorOnMainThread: _cmd
withObject: alarm
waitUntilDone: NO];
return;
}
severity = [alarm perceivedSeverity];
old = [sink latest: alarm];
if (old
&& (EcAlarmSeverityCleared == severity
&& [old perceivedSeverity] == severity))
{
return; // Duplicate clear ignored.
}
else
{
NSRegularExpression *re;
[self ecDoLock];
re = AUTORELEASE(RETAIN(alarmFilter));
[self ecUnLock];
mesg = [self messageForAlarm: alarm];
if (re != nil)
{
NSRange r;
r = [re rangeOfFirstMatchInString: mesg
options: 0
range: NSMakeRange(0, [mesg length])];
if (r.length > 0)
{
NSLog(@"AlarmFilter config removes alarm: %@", mesg);
return;
}
}
}
desc = [alarm description];
/* A local copy of an alarm will have an address differing from that of the
* alarm in the originating process. Avoid reporting that here.
*/
if ((range = [desc rangeOfString: @"0x"]).length > 0)
{
desc = [desc substringFromIndex: NSMaxRange(range)];
range = [desc rangeOfString: @" "];
desc = [desc substringFromIndex: range.location];
desc = [_(@"Alarm with") stringByAppendingString: desc];
}
if (EcAlarmSeverityCleared != severity
&& EcAlarmSeverityIndeterminate != severity)
{
NSArray *a = [NSArray arrayWithArray: consoles];
NSUInteger i = [a count];
/*
* Work with a copy of the consoles array in case one goes away
* or is added while we are doing this!
*/
while (i-- > 0)
{
ConsoleInfo *c = [a objectAtIndex: i];
if ([consoles indexOfObjectIdenticalTo: c] == NSNotFound)
{
continue;
}
if (EcAlarmSeverityWarning == severity && [c getWarnings] == NO)
{
continue;
}
if ((EcAlarmSeverityMajor == severity
|| EcAlarmSeverityMinor == severity) && [c getErrors] == NO)
{
continue;
}
if (EcAlarmSeverityCritical == severity && [c getAlerts] == NO)
{
continue;
}
NS_DURING
{
[[c obj] information: [c promptAfter: desc from: nil]];
}
NS_HANDLER
{
NSLog(@"Caught: %@", localException);
}
NS_ENDHANDLER
}
}
[[self cmdLogFile: logname] puts: desc];
if (EcAlarmSeverityCleared == severity)
{
NSArray *a = [sink alarms];
NSUInteger index = [a indexOfObject: alarm];
if (NSNotFound != index)
{
EcAlarm *old = [a objectAtIndex: index];
int notificationID = [old notificationID];
severity = [old perceivedSeverity];
if (severity <= alertAlarmThreshold && notificationID > 0)
{
NSDictionary *info;
NSString *key;
int reminder;
key = [NSString stringWithFormat: @"%d", notificationID];
mesg = [self messageForAlarm: old];
if (nil == (info = [lastAlertInfo objectForKey: key]))
{
BOOL cleared;
/* Alarm not yet reported ... report it before clearing.
*/
if ([old perceivedSeverity] == EcAlarmSeverityCleared)
{
cleared = YES;
}
else
{
cleared = NO;
}
[self reportAlarm: old
withMessage: mesg
isCleared: cleared
reminder: 0];
}
/* Report the clearing of the alarm.
*/
reminder = [[info objectForKey: @"Reminder"] intValue];
[lastAlertInfo removeObjectForKey: key];
[self reportAlarm: old
withMessage: mesg
isCleared: YES
reminder: reminder];
}
}
}
/* Now, send the alarm to the alarm sink.
*/
[sink alarm: alarm];
}
- (NSString*) description
{
return [NSString stringWithFormat: @"%@ running since %@\n"
@" Alarms which generated alerts: %"PRIu64"\n"
@" Alarms ignored (low severity): %"PRIu64"\n%@\n%@",
[super description], [self ecStarted],
alarmsAlerted, alarmsIgnored, alerter, sink];
}
- (oneway void) domanage: (NSString*)name
{
[sink domanage: name];
}
- (NSFileHandle*) openLog: (NSString*)lname
{
NSFileHandle *lf;
if ([mgr isWritableFileAtPath: lname] == NO)
{
if ([mgr createFileAtPath: lname
contents: nil
attributes: nil] == NO)
{
NSLog(@"Log file '%@' is not writable and can't be created", lname);
return nil;
}
}
lf = [NSFileHandle fileHandleForUpdatingAtPath: lname];
if (lf == nil)
{
NSLog(@"Unable to log to %@", lname);
return nil;
}
[lf seekToEndOfFile];
return lf;
}
- (void) cmdDefaultsChanged: (NSNotification*)n
{
NSRegularExpression *re = nil;
NSString *str;
[super cmdDefaultsChanged: n];
str = [[self cmdDefaults] stringForKey: @"AlarmFilter"];
if (str)
{
re = [[NSRegularExpression alloc] initWithPattern: str
options: 0
error: NULL];
}
[self ecDoLock];
ASSIGN(alarmFilter, re);
[self ecUnLock];
}
- (oneway void) cmdGnip: (id <CmdPing>)from
sequence: (unsigned)num
extra: (NSData*)data
{
EcClientI *r;
/*
* See if we have a fitting client - and update records.
*/
r = [self findIn: commands byObject: (id)from];
if (r == nil)
{
r = [self findIn: consoles byObject: (id)from];
}
if (r != nil)
{
[r gnip: num];
}
}
- (BOOL) cmdIsClient
{
return NO; // Control is not a client of Command
}
- (oneway void) cmdPing: (id <CmdPing>)from
sequence: (unsigned)num
extra: (NSData*)data
{
[from cmdGnip: self sequence: num extra: nil];
}
- (oneway void) cmdQuit: (NSInteger)status
{
[sink setMonitor: nil];
if (alerter)
{
[alerter shutdown];
NSLog(@"Uninstalled alerter: %@",
NSStringFromClass([alerter class]));
DESTROY(alerter);
}
[sink shutdown];
DESTROY(sink);
exit (status);
}
- (void) command: (NSData*)dat
from: (NSString*)f
{
NSMutableArray *cmd;
ConsoleInfo *console;
BOOL silent = NO;
cmd = [NSPropertyListSerialization
propertyListWithData: dat
options: NSPropertyListMutableContainers
format: 0
error: 0];
if ([[cmd firstObject] isEqual: @"silent"])
{
silent = YES;
[cmd removeObjectAtIndex: 0];
}
console = (ConsoleInfo*)[self findIn: consoles byName: f];
if (console == nil)
{
NSString *m;
m = [NSString stringWithFormat: cmdLogFormat(LT_ERROR,
@"command from unregistered console (from %@)"), f];
[self information: m
type: LT_ERROR
to: nil
from: nil];
}
else if (cmd == nil || [cmd count] == 0)
{
NSString *m;
m = [NSString stringWithFormat: cmdLogFormat(LT_AUDIT,
@"no command entered (from %@ %@)"), f, [console name]];
[self information: m
type: LT_AUDIT
to: [console name]
from: nil];
}
else
{
NSMutableString *full;
NSArray *allow;
NSString *hname = nil;
NSString *m = @"";
NSString *wd = cmdWord(cmd, 0);
unsigned count = [cmd count];
unsigned i;
BOOL connected = NO;
/*
* Log this command!
*/
full = [[NSMutableString alloc] initWithCapacity: 1024];
for (i = 0; i < count; i++)
{
[full appendFormat: @" %@", [cmd objectAtIndex: i]];
}
[[self cmdLogFile: logname]
printf: @"Console (%@): %@ Cmd -%@\n",
[console name], [NSDate date], full];
RELEASE(full);
if ([console chost] != nil || [console cserv] != nil)
{
connected = YES;
}
/* If we have a 'control' command - act as if the console was not
* connected to a host or server.
*/
if (comp(wd, @"control") == 0)
{
[cmd removeObjectAtIndex: 0];
wd = cmdWord(cmd, 0);
connected = NO;
}
#if !defined(HAVE_LIBCRYPT)
if (comp(wd, @"password") == 0)
{
NSRange r = [[console name] rangeOfString: @":"];
NSString *s = [[console name] substringToIndex: r.location];
NSMutableDictionary *d;
NSString *p;
NSString *path;
if ([cmd count] != 3)
{
[self information: @"parameters are old password and new one.\n"
type: LT_CONSOLE
to: [console name]
from: nil];
return;
}
if (operators == nil)
{
operators = [NSMutableDictionary dictionaryWithCapacity: 1];
RETAIN(operators);
[operators setObject: @"" forKey: s];
}
d = AUTORELEASE([[operators objectForKey: s] mutableCopy]);
p = [d objectForKey: @"Password"];
if ([p length] == 0 || [p isEqual: [cmd objectAtIndex: 1]])
{
[d setObject: [cmd objectAtIndex: 2] forKey: @"Password"];
[operators setObject: d forKey: s];
p = [operators description];
path = [[self cmdDataDirectory] stringByAppendingPathComponent:
@"Operators.plist"];
[p writeToFile: path atomically: YES];
[self information: @"new password set.\n"
type: LT_CONSOLE
to: [console name]
from: nil];
return;
}
else
{
[self information: @"old password is incorrect.\n"
type: LT_CONSOLE
to: [console name]
from: nil];
return;
}
}
#endif
/*
* If we are not connected, but have an 'on ....' at the start of
* the command line, then we send to the specified host as if we
* were connected to it.
*/
if (connected)
{
hname = [console chost];
}
else if (comp(wd, @"on") == 0)
{
[cmd removeObjectAtIndex: 0];
wd = cmdWord(cmd, 0);
hname = AUTORELEASE(RETAIN(wd));
if ([hname length] > 0)
{
CommandInfo *c;
c = (CommandInfo*)[self findIn: commands byAbbreviation: hname];
if (c)
{
hname = [c name];
[cmd removeObjectAtIndex: 0];
}
else
{
[self information: @"Attempt to work on unknown host"
type: LT_CONSOLE
to: [console name]
from: nil];
return;
}
wd = cmdWord(cmd, 0);
}
else
{
hname = nil;
}
}
/* Find the commands allowed for this user.
*/
allow = [self ecCommands: [console name]];
if (connected == YES || hname != nil)
{
NSArray *hosts;
/* Make an array of hosts to work with - the connected host
* or all hosts. If the connected host has gone away - disconnect.
*/
if (hname == nil)
{
hosts = [NSArray arrayWithArray: commands];
}
else
{
CommandInfo *c;
c = (CommandInfo*)[self findIn: commands byName: hname];
if (c)
{
hosts = [NSArray arrayWithObject: c];
}
else
{
if (connected)
{
[console setConnectedHost: nil];
}
[self information: @"Host has gone away"
type: LT_ERROR
to: [console name]
from: nil];
return;
}
}
if ([wd length] == 0)
{
/* Quietly ignore. */
}
else
{
BOOL foundServer = NO;
int i;
m = nil; /* Let remote host generate messages. */
/* Perform operation on connected host (or all hosts if
* not connected to a host).
*/
for (i = 0; i < [hosts count]; i++)
{
CommandInfo *c = [hosts objectAtIndex: i];
if ([commands indexOfObjectIdenticalTo: c] != NSNotFound)
{
if (NO == connected || [console cserv] == nil)
{
foundServer = YES;
NS_DURING
{
NSData *dat = [NSPropertyListSerialization
dataFromPropertyList: cmd
format: NSPropertyListBinaryFormat_v1_0
errorDescription: 0];
[[c obj] command: dat
to: nil
from: [console name]];
}
NS_HANDLER
{
NSLog(@"Caught: %@", localException);
}
NS_ENDHANDLER
}
else
{
NSString *to;
to = [c serverByAbbreviation: [console cserv]];
if (to)
{
foundServer = YES;
NS_DURING
{
NSData *dat;
dat = [NSPropertyListSerialization
dataFromPropertyList: cmd
format: NSPropertyListBinaryFormat_v1_0