-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·1093 lines (999 loc) · 33.6 KB
/
main.cpp
File metadata and controls
executable file
·1093 lines (999 loc) · 33.6 KB
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
//#RSLOGIN: remote slogin to automatically ssh into remote systems
//#RSLOGIN: Written by Jason Curtis (C)2001-2007
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <sys/utsname.h>
#include <sys/types.h>
#include <pwd.h>
#include <netdb.h>
using namespace std;
// Constants
#define PNAME "rslogin"
#define VERSION "0.32.5"
#define DEVSTATE "DEV"
#define MAX_HOSTGROUPS 50
#define MAX_HOSTS 255
//
// Global Variables and their defaults
int debug=0;
int msglvl=0;
char *CONFIG_FILENAME="/etc/rslogin.cfg";
string USERHOME;
string RSHOME;
string RSEXPECT="rslogin.x";
string RSPASS="rslogin.pass";
string RSENCPASS=RSPASS+".gpg";
string SSHBIN="/usr/bin/slogin";
string EXPECTBIN="/usr/bin/expect";
string GPGBIN="/usr/bin/gpg";
bool USE_GPG;
bool VERBOSE_LOGIN=bool(1);
bool USE_AUTOPASS;
bool USE_PASSREVERT;
bool USE_AUTOROOT;
string NAGROUP="NETAPP";
string USERLOGIN;
string USERPASS[2];
string NAPASS;
string LOGINHOST;
string LOGINHOSTGROUP;
//Create a struct "DISTRO" for parsing the password file and root distro file.
struct DISTRO {
string NAME;
string VALUE;
string UP1;
string UP2;
} HOSTGROUP[MAX_HOSTGROUPS], HOST[MAX_HOSTS], DFHOSTGROUP[MAX_HOSTGROUPS], DFHOST[MAX_HOSTS];
int HOSTCOUNT=0;
int HOSTGROUPCOUNT=0;
string gethome() {
struct utsname si;
string sysname = "Linux";
string username;
string userhome;
//set struct pw to the username based on the proccess user id
passwd *pw = getpwuid(getuid());
//call uname function to populare the struct si with operating system information
int sierr=uname(&si);
// check if uname function returned an error, if it did the default Linux will be used
if (sierr < 0) {
// debug msglvl > 3
if (msglvl > 3) {
cout << "sierr = " << sierr << endl;
}
cout << "Unable to get sysname, Default: Linux" << endl;
} else {
sysname = si.sysname;
//debug msglvl > 1
if (msglvl > 1) cout << "System Name: " << sysname << endl;
}
/* Check the sysname string to determine the type of system we are running on, then set the home direcctory base accordingly.
Currently we only support Darwin user home directories and all others will use /home. This can obviously be easily
changed in the future.
*/
if (sysname.compare("Darwin") == 0) { // Support for MacOSX home directory structure
userhome = "/Users/";
} else if (sysname.compare("SunOS") == 0) { // Support for Solaris (SunOS) home directory structure
userhome = "/export/home/";
} else { // Default to Linux home directory structure
userhome = "/home/";
}
// retrieve the username from the pw struct we previously created and append it to the string "userhome" with a trailing slash
username = pw->pw_name;
userhome.append(username);
userhome.append("/");
// return the completed user home directory
return userhome;
}
int read_config (char *config_filename) {
string cfgline;
string cfgname;
string cfgval;
size_t delpos;
//debug msglvl > 0
if (msglvl > 0) cout << "Reading User Configuration file: " << config_filename << endl;
// create conffile file stream
ifstream conffile;
//first try to open user configuration file (i.e. ~/.rslogin/rslogin.cfg)
conffile.open( config_filename );
if (conffile.is_open() == 0) {
//debug msglvl > 0
if (msglvl > 0) cout << "No User Configuration file, Trying Default: /etc/rslogin.cfg" << endl;
//opening user configuration file failed. Try to open global system configuration (i.e. /etc/rslogin.cfg)
conffile.open ( CONFIG_FILENAME );
if (conffile.is_open() == 0) {
//Couldn't open global configuration either, return with -1
cout << "Unable to open configuration file!" << endl;
return(-1);
}
}
// read config file and parse known attributes, discard lines beginning with # and unknown attribute names.
while (conffile.good()) {
getline(conffile, cfgline);
delpos = cfgline.find("=");
if (delpos != string::npos) {
cfgname = cfgline.substr(0,delpos);
cfgval = cfgline.substr(delpos+1);
if (cfgname.compare("RSEXPECT") == 0) {
RSEXPECT = cfgval;
} else if (cfgname.compare("RSENCPASS") == 0) {
RSENCPASS = cfgval;
} else if (cfgname.compare("RSPASS") == 0) {
RSPASS = cfgval;
} else if (cfgname.compare("SSHBIN") == 0) {
SSHBIN = cfgval;
} else if (cfgname.compare("EXPECTBIN") == 0) {
EXPECTBIN = cfgval;
} else if (cfgname.compare("GPGBIN") == 0) {
GPGBIN = cfgval;
} else if (cfgname.compare("USEGPG") == 0) {
USE_GPG = bool(cfgval.compare("0"));
} else if (cfgname.compare("USE_GPG") == 0) {
USE_GPG = bool(cfgval.compare("0"));
} else if (cfgname.compare("VERBOSE_LOGIN") == 0) {
VERBOSE_LOGIN = bool(cfgval.compare("0"));
} else if (cfgname.compare("USE_AUTOROOT") == 0) {
USE_AUTOROOT = bool(cfgval.compare("0"));
} else if (cfgname.compare("USE_AUTOPASS") == 0) {
USE_AUTOPASS = bool(cfgval.compare("0"));
} else if (cfgname.compare("USE_PASSREVERT") == 0) {
USE_PASSREVERT = bool(cfgval.compare("0"));
} else if (cfgname.compare("NAGROUP") == 0) {
NAGROUP = cfgval;
} else if (cfgname.compare("RSHOME") == 0) {
RSHOME = cfgval;
} else if (cfgname.compare(0,1,"#") == 0) {
//debug msglvl > 3
if (msglvl > 3) cout << "Skipping Commented Configuration Attribute" << endl;
} else {
//debug msglvl > 1
if (msglvl > 1) cout << cfgname << " is an unknown attribute, skipping" << endl;
}
}
}
conffile.close();
// if "RSHOME" was not defined in the config file. Define it based on gethome function and append .rslogin
if (RSHOME.empty()) {
RSHOME.append(USERHOME);
RSHOME.append(".rslogin");
}
//debug msgslvl > 0
if (msglvl > 0) cout << "Config Filename: " << config_filename << endl;
//debug msglvl > 1
if (msglvl > 1) {
cout << "\tRSHOME = " << RSHOME << endl;
cout << "\tRSEXPECT = " << RSEXPECT << endl;
cout << "\tRSPASS = " << RSPASS << endl;
cout << "\tRSENCPASS = " << RSENCPASS << endl;
cout << "\tSSHBIN = " << SSHBIN << endl;
cout << "\tGPGBIN = " << GPGBIN << endl;
cout << "\tEXPECTBIN = " << EXPECTBIN << endl;
cout << "\tVERBOSE_LOGIN = " << VERBOSE_LOGIN << endl;
cout << "\tUSE_AUTOROOT = " << USE_AUTOROOT << endl;
cout << "\tUSE_GPG = " << USE_GPG << endl;
cout << "\tUSE_AUTOPASS = " << USE_AUTOPASS << endl;
cout << "\tUSE_PASSREVERT = " << USE_PASSREVERT << endl;
cout << "\tNAGROUP = " << NAGROUP << endl;
}
return 0;
}
int read_pass(string password_file) {
string passline;
string passtype;
string passname;
string passval[4];
size_t delpos[4];
//Attempt to open password file
ifstream passfile;
passfile.open ( password_file.data() );
//Check if password file is open, if not return
if (passfile.is_open() == 0) {
cout << "Unable to open Password file for reading" << endl;
return(-1);
}
// read pass file looking for userlogin,userpass,host(s),hostgroup(s)
while (passfile.good()) {
passtype="";
passname="";
passval[0]="";
passval[1]="";
passval[2]="";
getline(passfile, passline);
if (passline.compare(0,1,"#") != 0) {
delpos[0] = passline.find(":");
delpos[1] = passline.find(":", delpos[0]+1);
delpos[2] = passline.find(":",delpos[1]+1);
delpos[3] = passline.find(":",delpos[2]+1);
//debug msglvl > 5
if (msglvl > 5) {
cout << "Delimeter 1: " << delpos[0] << " Delimeter 2: " << delpos[1] << endl;
}
if (delpos[0] != string::npos) {
passtype = passline.substr(0,delpos[0]);
if (delpos[1] != string::npos) {
passname = passline.substr(delpos[0]+1,delpos[1]-delpos[0]-1);
if (delpos[2] != string::npos) {
passval[0] = passline.substr(delpos[1]+1,delpos[2]-delpos[1]-1);
if (delpos[3] != string::npos) {
passval[1] = passline.substr(delpos[2]+1,delpos[3]-delpos[2]-1);
passval[2] = passline.substr(delpos[3]+1);
} else {
passval[1] = passline.substr(delpos[2]+1);
}
} else {
passval[0] = passline.substr(delpos[1]+1);
}
} else {
passname = passline.substr(delpos[0]+1);
}
if (passtype.compare("USERLOGIN") == 0) {
USERLOGIN = passname;
} else if (passtype.compare("USERPASS") == 0) {
USERPASS[0] = passname;
} else if (passtype.compare("USERPASS2") == 0) {
USERPASS[1] = passname;
} else if (passtype.compare("HOST") == 0) {
HOST[HOSTCOUNT].NAME = passname;
HOST[HOSTCOUNT].VALUE = passval[0];
HOST[HOSTCOUNT].UP1 = passval[1];
HOST[HOSTCOUNT].UP2 = passval[2];
HOSTCOUNT++;
} else if (passtype.compare("HOSTGROUP") == 0) {
HOSTGROUP[HOSTGROUPCOUNT].NAME = passname;
HOSTGROUP[HOSTGROUPCOUNT].VALUE = passval[0];
if (passname.compare(NAGROUP) == 0) {
NAPASS = passval[0];
}
HOSTGROUPCOUNT++;
}
}
}
}
passfile.close();
//debug msglvl > 4
if (msglvl > 4) {
int n;
//print userlogin and userpass
cout << "Userlogin: " << USERLOGIN << " Userpass: " << USERPASS[0] << " Userpass2: " << USERPASS[1] << endl;
//print all host/hostgroup associations
for (n=0; n<HOSTCOUNT; n++) {
cout << "Hostname: " << HOST[n].NAME << " Hostgroup: " << HOST[n].VALUE << " Userpass: " << HOST[n].UP1 << " Userpass2: " << HOST[n].UP2 << endl;
}
//print all hostgroup/password associations
for (n=0; n<HOSTGROUPCOUNT; n++) {
cout << "Hostgroup: " << HOSTGROUP[n].NAME << " Password: " << HOSTGROUP[n].VALUE << endl;
}
}
//debug msgslvl > 0
if (msglvl > 0) {
cout << "Hostgroup Count: " << HOSTGROUPCOUNT << " Host Count: " << HOSTCOUNT << endl;
}
return 0;
}
int write_expect(int logintype) {
int x;
string Rpasswd;
string expect_file = RSHOME+"/"+RSEXPECT;
//Determine root group based on hostname specified
for (x=0; x<HOSTCOUNT; x++) {
if (HOST[x].NAME.compare(LOGINHOST) == 0) {
LOGINHOSTGROUP=HOST[x].VALUE;
if (HOST[x].UP1.compare("") != 0) {
USERPASS[0] = HOST[x].UP1;
}
if (HOST[x].UP2.compare("") != 0) {
USERPASS[1] = HOST[x].UP2;
}
}
}
//Determine root password based on above root group name
for (x=0; x<HOSTGROUPCOUNT; x++) {
if (HOSTGROUP[x].NAME.compare(LOGINHOSTGROUP) == 0) {
Rpasswd=HOSTGROUP[x].VALUE;
}
}
//debug msglvl > 0
if (msglvl > 0) cout << "Writing expect data to file: " << expect_file << endl;
//open expect script file for writing
ofstream xfile;
xfile.open ( expect_file.data() );
if (xfile.is_open() == 0) {
cout << "Unable to open expect file!" << endl;
return(-1);
}
if (USERPASS[1].compare("") == 0) {
USE_AUTOPASS = 0;
USE_PASSREVERT = 0;
}
//Begin writing expect code to the script
xfile << "set timeout 30" << endl;
xfile << "set hostID " << LOGINHOST << endl;
xfile << "set KH 0" << endl;
xfile << "set KA 0" << endl;
xfile << "set NP 0" << endl;
xfile << "set OP 0" << endl;
// Check what kind of login we're doing and set username and password
if (logintype == 2) {
xfile << "set Mlogin root" << endl;
xfile << "set Mpasswd " << NAPASS << endl;
//debug msglvl > 1
if (msglvl > 1) cout << "Network Appliance Login method enabled!" << endl;
Rpasswd = NAPASS;
} else {
xfile << "set Mlogin " << USERLOGIN << endl;
xfile << "set Mpasswd " << USERPASS[0] << endl;
if (USE_AUTOPASS == 1) {
xfile << "set Npasswd " << USERPASS[1] << endl;
}
}
if (Rpasswd.compare("") == 0) {
cout << "Warning: No associated root distribution group found for " << LOGINHOST << endl;
} else {
xfile << "set Rpasswd " << Rpasswd << endl;
xfile << endl;
}
// Check what kind of login we're doing and set switch user login bit
if (logintype == 1) {
xfile << "set DOSU 1" << endl;
} else {
xfile << "set DOSU 0" << endl;
}
//finish the rest of the expect login script
xfile << endl;
xfile << "#### LOGIN PROCESS BEGINS HERE ####" << endl;
xfile << "#KH = Known Host" << endl;
xfile << "#KA = Key Authorization" << endl;
xfile << "#NP = Setting New Password" << endl;
xfile << "#OP = Reverting to Original Password" << endl;
xfile << "log_user " << VERBOSE_LOGIN << endl;
xfile << "spawn -noecho " << SSHBIN << " -l $Mlogin $hostID" << endl;
xfile << "expect {" << endl;
xfile << "\ttimeout { interact }" << endl;
xfile << "\t\"(yes/no)?\" { send \"yes\r\"; set KH 1 }" << endl;
xfile << "\t\"ssword:\" { sleep .1; send \"$Mpasswd\r\" }" << endl;
xfile << "\t\"passphrase\" { sleep .1; send \"$Mpasswd\r\" }" << endl;
xfile << "\t\"$ \" { set KA 1; send \"\r\" }" << endl;
xfile << "}" << endl;
xfile << endl;
xfile << "if $KH {" << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { interact }" << endl;
xfile << "\t\t\"ssword:\" { sleep .1; send \"$Mpasswd\r\" }" << endl;
xfile << "\t\t\"passphrase\" { sleep .1; send \"$Mpasswd\r\" }" << endl;
xfile << "\t\t\"> \" { set KA 1; send \"\r\" }" << endl;
xfile << "\t\t\"$ \" { set KA 1; send \"\r\" }" << endl;
xfile << "\t}" << endl;
xfile << "}" << endl;
xfile << endl;
if (USE_AUTOPASS == 1) {
xfile << "expect {" << endl;
xfile << "\ttimeout { interact }" << endl;
xfile << "\t\"UNIX password:\" { set NP 1; sleep .1; send \"$Mpasswd\r\" }" << endl; // Linux
xfile << "\t\" login password\" { set NP 1 }" << endl; // Solaris
xfile << "\t\"\'s password:\" { set OP 1; sleep .1; send \"$Npasswd\r\" }" << endl; // Bad Password, try secondary
xfile << "\t\"$ \" { send \"\r\" }" << endl;
xfile << "\t\"> \" { send \"\r\" }" << endl; // For Netapps
xfile << "}" << endl;
xfile << endl;
xfile << "if $NP {" << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { interact }" << endl;
xfile << "\t\t\"New Password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // Solaris
xfile << "\t\t\"New password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // Linux
xfile << "\t\t\"New UNIX password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // New Linux
xfile << "\t}" << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { interact }" << endl;
xfile << "\t\t\"Re-enter new Password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // Solaris
xfile << "\t\t\"Retype new password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // Linux
xfile << "\t\t\"Retype new UNIX password:\" { sleep .1; send \"$Npasswd\r\" }" << endl; // New Linux
xfile << "\t}" << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { interact }" << endl;
xfile << "\t\t\"$ \" { send \"exit\r\"}" << endl; //Solaris
xfile << "\t\t\" tokens updated successfully.\"" << endl; //Linux
xfile << "\t}" << endl;
xfile << "interact" << endl; // Wait for password change completion, exit code 99
xfile << "exit 99" << endl;
xfile << "}" << endl;
xfile << endl;
}
xfile << "if $DOSU {" << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { send \"exec su -\r\" }" << endl;
xfile << "\t\t\"ssword:\" { sleep .1; send \"$Mpasswd\r\" }" << endl;
xfile << "\t\t\"> \" { send \"exec su -\r\" }" << endl;
xfile << "\t\t\"$ \" { send \"exec su -\r\" }" << endl;
xfile << "\t}" << endl;
xfile << endl;
xfile << "\texpect {" << endl;
xfile << "\t\ttimeout { interact }" << endl;
xfile << "\t\t\"Password:\" { sleep .1; send \"$Rpasswd\r\" }" << endl;
xfile << "\t}" << endl;
if (USE_AUTOPASS == 1 && USE_PASSREVERT == 1) {
xfile << "\tif $OP {" << endl;
xfile << "\t\texpect {" << endl;
xfile << "\t\t\ttimeout { interact }" << endl;
xfile << "\t\t\t\"# \" { send \"passwd $Mlogin\r\" }" << endl; // Expecting root prompt
xfile << "\t\t}" << endl;
xfile << "\t\texpect {" << endl;
xfile << "\t\t\ttimeout { interact }" << endl;
xfile << "\t\t\t\"New Password:\" { sleep .1; send \"$Mpasswd\r\" }" << endl; // Solaris
xfile << "\t\t\t\"New password:\" { sleep .1; send \"$Mpasswd\r\" }" << endl; // Linux
xfile << "\t\t\t\"New UNIX password:\" { sleep .1; send \"$Mpasswd\r\" } " << endl; // New Linux prompt
xfile << "\t\t}" << endl;
xfile << "\t\texpect {" << endl;
xfile << "\t\t\ttimeout { interact }" << endl;
xfile << "\t\t\t\"Re-enter new Password:\" { sleep .1; send \"$Mpasswd\r\" }" << endl; // Solaris
xfile << "\t\t\t\"Retype new password:\" { sleep .1; send \"$Mpasswd\r\" }" << endl; // Linux
xfile << "\t\t\t\"Retype new UNIX password:\" { sleep .1; send \"$Mpasswd\r\" }" << endl; // New Linux prompt
xfile << "\t\t}" << endl;
xfile << "\t}" << endl;
}
xfile << "}" << endl;
xfile << endl;
xfile << "interact" << endl;
xfile.close();
return 0;
}
int runscript() {
int err=0;
string runfile=RSHOME+"/"+RSEXPECT;
string runcmd=EXPECTBIN+" "+runfile;
if (debug > 0) {
//debug is enabled, just display the run command to the screen, don't actually run it.
cout << runcmd << endl;
} else {
//debug is not enabled, attempt to run command
err=system( runcmd.data() );
// system call multiplys return code by 256
if (err > 0) {
err = err / 256;
}
//check for system call return code
if (err == 99) {
cout << endl << "!!! Your Password expired, it has been changed !!!" << endl;
cout << "Attempting to log back in" << endl;
err=system( runcmd.data() ) ;
// system call multiplys return code by 256
if (err > 0) {
err = err / 256;
cout << "System call exited Abnormally. Code: " << err << endl;
if (debug == 0) remove(runfile.data());
return(err);
}
} else if (err == 0) {
if (msglvl > 3) cout << "System call successful" << endl;
} else {
cout << "System call exited Abnormally. Code: " << err << endl;
if (debug == 0) remove(runfile.data());
return(err);
}
}
//delete expect script if not debugging and return successful
//debug msglvl > 0
if (debug == 0) {
if (msglvl > 0) cout << "Removing expect data file: " << runfile << endl;
remove(runfile.data());
}
return 0;
}
int checkgpg() {
string GPGCMD=GPGBIN;
string password_filename=RSHOME;
string encpassword_filename=RSHOME;
int err;
int gpgerr;
if (USE_GPG == 1) {
//Configuration has GPG encryption turned on
encpassword_filename.append("/");
encpassword_filename.append(RSENCPASS);
password_filename.append("/.");
password_filename.append(RSPASS);
password_filename.append(".tmp");
GPGCMD.append(" -q -o ");
GPGCMD.append(password_filename);
GPGCMD.append(" --decrypt ");
GPGCMD.append(encpassword_filename);
//debug msglvl > 0
if (msglvl > 0) cout << "Decrypting: " << encpassword_filename << endl;
// Run GPG Decryption program
gpgerr = system(GPGCMD.data());
// If the decryption failed, return. Otherwise call readpass
if (gpgerr == 0) {
if (msglvl > 0) cout << "Reading Passwords from: " << password_filename << endl;
err = read_pass(password_filename);
if (err != 0) {
cout << "Error reading password file!" << endl;
return(err);
}
} else {
cout << "Error decrypting password file: " << encpassword_filename << endl;
return(gpgerr);
}
// Cleanup CLEAR TEXT Password file
//debug msglvl > 0
if (msglvl > 0) cout << "Removing Cleatext Password file: " << password_filename << endl;
remove(password_filename.data());
} else {
// Configuration has GPG encryption turned off
password_filename.append("/");
password_filename.append(RSPASS);
if (msglvl > 0) cout << "Reading Passwords from: " << password_filename << endl;
err = read_pass(password_filename.data());
if (err != 0) {
cout << "Error reading password file!" << endl;
return(err);
}
}
return 0;
}
int distro_VZHG(char *distro_filename) {
string distroline;
string distroname;
string distroval;
size_t delpos[2];
size_t length;
int r=0;
int n;
//open distribution file
ifstream distro;
distro.open ( distro_filename );
//check if the distro file is open, otherwise return
if (distro.is_open() == 0) {
cout << "Unable to open distro file: " << distro_filename << endl;
return(-1);
}
//check the length of the distro file, then return to the beginning to prepare for reading
distro.seekg(0,ios::end);
int rlength = distro.tellg();
distro.clear();
distro.seekg(0);
//debug msglvl > 0
if (msglvl > 0) cout << "Reading Root Distribution List: " << distro_filename << endl;
//Read distro file line by line looking for groupnames and passwords
while (distro.tellg() < rlength) {
getline(distro, distroline);
//debug msglvl > 5
if (msglvl > 5) cout << "tellg: " << distro.tellg() << " Record: " << r << endl;
if (distroline.compare(0,4,"====") == 0) {
//New root group, increase counter and save root group name
r++;
delpos[0] = distroline.find(":");
delpos[1] = distroline.find("=",delpos[0]);
if (delpos[0] != string::npos && delpos[1] != string::npos) {
length=delpos[1]-delpos[0]-3;
DFHOSTGROUP[r].NAME = distroline.substr(delpos[0]+2, length);
//debug msglvl > 3
if (msglvl > 3) cout << "Root Group: " << DFHOSTGROUP[r].NAME << endl;
}
} else if (distroline.compare(0,4,"Root") == 0) {
//root password designation found
delpos[0] = distroline.find(">");
delpos[1] = distroline.find("(", delpos[0]);
if (delpos[0] != string::npos && delpos[1] != string::npos) {
length=delpos[1]-delpos[0]-3;
DFHOSTGROUP[r].VALUE = distroline.substr(delpos[0]+2, length);
//debug msglvl > 3
if (msglvl > 3) cout << "Root Password: " << DFHOSTGROUP[r].VALUE << endl;
}
}
}
distro.close();
// Copy HOST struct to DFHOST
for (n=0; n<MAX_HOSTS; n++) {
DFHOST[n].NAME = HOST[n].NAME;
DFHOST[n].VALUE = HOST[n].VALUE;
DFHOST[n].UP1 = HOST[n].UP1;
DFHOST[n].UP2 = HOST[n].UP2;
}
//debug msglvl > 1
if (msglvl > 2) {
for (n=1; n<MAX_HOSTGROUPS; n++) {
if (DFHOSTGROUP[n].NAME.compare("") != 0) {
cout << "Group: " << DFHOSTGROUP[n].NAME << " Value: " << DFHOSTGROUP[n].VALUE << endl;
}
}
}
return 0;
}
int distro_VZ(char *distro_filename) {
string distroline;
string distroname;
string distroval;
size_t delpos[4];
size_t length;
int r=0;
int n;
int x=0;
//open distribution file
ifstream distro;
distro.open ( distro_filename );
//check if the distro file is open, otherwise return
if (distro.is_open() == 0) {
cout << "Unable to open distro file: " << distro_filename << endl;
return(-1);
}
//check the length of the distro file, then return to the beginning to prepare for reading
distro.seekg(0,ios::end);
int rlength = distro.tellg();
distro.clear();
distro.seekg(0);
//debug msglvl > 0
if (msglvl > 0) cout << "Reading Root Distribution List: " << distro_filename << endl;
//Read distro file line by line looking for groupnames and passwords
while (distro.tellg() < rlength) {
getline(distro, distroline);
//debug msglvl > 5
if (msglvl > 5) cout << "tellg: " << distro.tellg() << " Record: " << r << endl;
if (distroline.compare(0,4,"====") == 0) {
//New root group, increase counter and save root group name
r++;
delpos[0] = distroline.find(":");
delpos[1] = distroline.find("=",delpos[0]);
if (delpos[0] != string::npos && delpos[1] != string::npos) {
length=delpos[1]-delpos[0]-3;
DFHOSTGROUP[r].NAME = distroline.substr(delpos[0]+2, length);
//debug msglvl > 3
if (msglvl > 3) cout << "Root Group: " << DFHOSTGROUP[r].NAME << endl;
}
} else if (distroline.compare(0,6,"HOSTS:") == 0) {
// Host list designation found
int lpos=7;
int hlength = 0;
while (lpos < distroline.length()) {
delpos[0] = distroline.find(" ", lpos);
delpos[1] = distroline.find(" ", delpos[0]+1);
if (delpos[0] == string::npos) {
lpos = distroline.length();
} else if (delpos[1] == string::npos) {
hlength = distroline.length() - delpos[0] - 1;
lpos = distroline.length();
//Check for CRLF line termination
string lastchar = distroline.substr(distroline.length()-1,1);
if (lastchar.compare("\r") == 0) {
hlength--;
}
//throw into struct
DFHOST[x].NAME = distroline.substr(delpos[0]+1, hlength);
DFHOST[x].VALUE = DFHOSTGROUP[r].NAME;
if (msglvl > 5) cout << "hlength: " << hlength << " lpos: " << lpos << " delpos-0: " << delpos[0] << " delpos-1: " << delpos[1] << endl;
if (msglvl > 4) cout << "DFHOST Name: " << DFHOST[x].NAME << " DFHOST Value: " << DFHOST[x].VALUE << endl;
x++;
} else {
hlength = delpos[1] - delpos[0] - 1;
lpos = delpos[1];
//throw into struct
DFHOST[x].NAME = distroline.substr(delpos[0]+1, hlength);
DFHOST[x].VALUE = DFHOSTGROUP[r].NAME;
if (msglvl > 5) cout << "hlength: " << hlength << " lpos: " << lpos << " delpos-0: " << delpos[0] << " delpos-1: " << delpos[1] << endl;
if (msglvl > 4) cout << "DFHOST Name: " << DFHOST[x].NAME << " DFHOST Value: " << DFHOST[x].VALUE << endl;
x++;
}
}
} else if (distroline.compare(0,4,"Root") == 0) {
//root password designation found
delpos[0] = distroline.find(">");
delpos[1] = distroline.find("(", delpos[0]);
if (delpos[0] != string::npos && delpos[1] != string::npos) {
length=delpos[1]-delpos[0]-3;
DFHOSTGROUP[r].VALUE = distroline.substr(delpos[0]+2, length);
//debug msglvl > 3
if (msglvl > 3) cout << "Root Password: " << DFHOSTGROUP[r].VALUE << endl;
}
}
}
distro.close();
//debug msglvl > 1
if (msglvl > 2) {
for (n=1; n<MAX_HOSTGROUPS; n++) {
if (DFHOSTGROUP[n].NAME.compare("") != 0) {
cout << "Group: " << DFHOSTGROUP[n].NAME << " Value: " << DFHOSTGROUP[n].VALUE << endl;
}
}
for (n=1; n<MAX_HOSTS; n++) {
if (DFHOST[n].NAME.compare("") != 0) {
cout << "Host: " << DFHOST[n].NAME << " Value: " << DFHOST[n].VALUE << " UP1: " << DFHOST[n].UP1 << " UP2: " << DFHOST[n].UP2 << endl;
}
}
}
return 0;
}
int parse_distro(string distro_type, char *distro_filename) {
int perr = 0;
string pconfirm="N";
if (distro_type.compare("VZHG") == 0) {
perr = distro_VZHG(distro_filename);
} else if (distro_type.compare("VZ") == 0) {
cout << "Warning: This will overwrite your current HOST configuration." << endl << "Are you sure you want to continue? [y/N]: ";
cin >> pconfirm;
if (pconfirm.compare(0,1,"n") == 0) {
cout << "User aborted import" << endl;
return -2;
} else if (pconfirm.compare(0,1,"N") == 0) {
cout << "User aborted import" << endl;
return -2;
} else if (pconfirm.compare(0,1,"y") == 0) {
perr = distro_VZ(distro_filename);
} else if (pconfirm.compare(0,1,"Y") == 0) {
perr = distro_VZ(distro_filename);
} else {
cout << "User aborted import" << endl;
return -2;
}
} else {
cout << "Unknown distribution type!" << endl;
return -1;
}
return perr;
}
int write_passfile() {
int n;
string password_filename=RSHOME+"/"+RSPASS;
cout << "Writing password file: " << password_filename << endl;
//open password file
ofstream passfile;
passfile.open( password_filename.data(), ios::trunc );
if (passfile.is_open() == 0) {
cout << "Unable to open password file!" << endl;
return -1;
}
//password file is open, write password data
passfile << "### This is a commented line and will be skipped, blank lines will also" << endl;
passfile << "### be skipped." << endl;
passfile << "USERLOGIN:" << USERLOGIN << endl;
passfile << "USERPASS:" << USERPASS[0] << endl;
if (USERPASS[1].compare("") != 0) {
passfile << "USERPASS2:" << USERPASS[1] << endl;
} else {
passfile << "#USERPASS2:the^password^2" << endl;
}
passfile << "####### Security Root Distro #######" << endl;
passfile << "## Syntax for Hostgroups:" << endl;
passfile << "## HOSTGROUP:<groupname>:<root password>" << endl;
//Write hostgroups to file
for (n=1; n<MAX_HOSTGROUPS; n++) {
if (DFHOSTGROUP[n].NAME.compare("") != 0) {
passfile << "HOSTGROUP:" << DFHOSTGROUP[n].NAME << ":" << DFHOSTGROUP[n].VALUE << endl;
}
}
passfile << "#########################################" << endl;
passfile << "### Host to Hostgroup Associations" << endl;
passfile << "### (preserved thru root distro imports)" << endl;
passfile << "## Syntax for Hosts:" << endl;
passfile << "## HOST:<hostname>:<hostgroup>:[userpass]:[userpass2]" << endl;
//Write Hosts to file
for (n=0; n<MAX_HOSTS; n++) {
if (DFHOST[n].NAME.compare("") != 0) {
passfile << "HOST:" << DFHOST[n].NAME << ":" << DFHOST[n].VALUE << ":" << DFHOST[n].UP1 << ":" << DFHOST[n].UP2 << endl;
}
}
passfile.close();
return 0;
}
int encrypt_pass() {
int gpgerr = 0;
string password_filename=RSHOME+"/"+RSPASS;
string GPGCMD=GPGBIN+" --encrypt "+password_filename;
cout << "Encrypting: " << password_filename << endl;
if (debug > 0) {
//debug - just print the gpg command
cout << GPGCMD << endl;
return 0;
} else {
//normal mode - run gpg command
gpgerr = system(GPGCMD.data());
if (gpgerr == 0) {
//gpg encryption successful, remove clear text file
remove( password_filename.data() );
return 0;
} else {
cout << "Encryption failed!" << endl;
return gpgerr;
}
}
return gpgerr;
}
void usagehelp(int ECODE) {
cout << "Usage:\t" << PNAME << " [options] [hostname]" << endl;
cout << "\t" << PNAME << " [-v] [-h]" << endl;
cout << "\t" << PNAME << " [-c <file>] [-r] [-V] <hostname>" << endl;
cout << "\t" << PNAME << " [-c <file>] [-R] [-V] <hostname>" << endl;
cout << "\t" << PNAME << " [-c <file>] [-n] [-V] <hostname>" << endl;
cout << "\t" << PNAME << " [-c <file>] [-i <file>] [-t <type>] [-e]" << endl;
cout << endl;
cout << "Options:" << endl;
cout << " -h\t\t Display help" << endl;
cout << " -v\t\t Display version" << endl;
cout << " -c <file>\t Use configuration file (default: ~/.rslogin/rslogin.cfg)" << endl;
cout << " -r\t\t Login to server as root" << endl;
cout << " -R\t\t Login to server as a normal user" << endl;
cout << " -n\t\t Special login, Server is a Network Appliance" << endl;
cout << " -V\t\t Toggle Verbose Login" << endl;
cout << " -e\t\t Encrypt rslogin password file" << endl;
cout << " -i <file>\t Import root distribution" << endl;
cout << " -t <type>\t Set import file type" << endl;
cout << endl;
cout << "Import File Types:" << endl;
cout << " VZ - Verizon Internal Root Distribution, Hosts and Hostgroups" << endl;
cout << " VZHG - Verizon Internal Root Distribution, Hostgroups only (default)" << endl;
cout << endl;
exit(ECODE);
}
void version() {
cout << PNAME << ", version " << VERSION << "-" << DEVSTATE << endl;
}
int main (int argc, char *argv[]) {
int c;
int err;
int logintype=0;
int logintypeset=0;
int pderr[2];
int pdimport=0;
int pdencrypt=0;
int toggle_verbose=0;
char *host=NULL;
char *user_conf;
char *config_filename=NULL;
char *import_filename=NULL;
string import_type="VZHG";
struct hostent *hp;
while ((c = getopt(argc, argv, "c:t:i:hVvedrRnm")) != -1) {
switch (c) {
case 'h':
usagehelp(0);
case 'v':
version();
exit(0);
case 'V':
toggle_verbose=1;
break;
case 'c':
config_filename = optarg;
break;
case 'd':
debug++;
break;
case 'm':
msglvl++;
break;
case 'r':
if (logintypeset == 0) {
logintype=1;
logintypeset=1;
break;
} else {
usagehelp(-2);
}
case 'R':
if (logintypeset == 0) {
logintype=0;
logintypeset=1;
break;
} else {
usagehelp(-2);
}
case 'n':
if (logintypeset == 0) {
logintype=2;
logintypeset=1;
break;
} else {
usagehelp(-2);
}
case 't':
import_type = optarg;
break;
case 'i':
pdimport = 1;
import_filename = optarg;
break;
case 'e':
pdencrypt = 1;
break;
default:
exit(-99);
}
}
host = argv[argc-1];
if (strncmp(host,"-",1) == 0) {
host = NULL;
} else if (strncmp(host,".",1) == 0) {
host = NULL;
} else if (strncmp(host,"/",1) == 0) {
host = NULL;
} else {
//debug msglvl > 0
if (msglvl > 0) cout << "Host: " << host << endl;
}
//if we're not importing or encrypting a password file, make sure hostname isn't blank
if (pdimport == 0 && pdencrypt == 0) {
if (host != NULL) {
LOGINHOST=host;