|
root / base / usr / src / cmd / cmd-inet / usr.bin / pppd / asppp2pppd
asppp2pppd Plain Text 1611 lines 45.5 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
#!/usr/bin/perl

# This utility translates from aspppd configuration to Solaris PPP 4.0
# (or ANU ppp-2.4.0; aka pppd).  It can also revert to previous aspppd
# configuration (discarding the pppd configuration), but does not
# translate new configuration files into old.
#
# This script provides only a suggested translation for your existing
# aspppd configuration.  You will need to evaluate for yourself
# whether the translation is appropriate for your operating
# environment.

# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# Steps in translation:
#	- parse /etc/asppp.cf
#	- check for aspppls in /etc/passwd (or NIS) 
#	- read in current /etc/ppp/options configuration file
#	- read list of configured serial ports from pmadm
#	- read in UUCP configuration files
#	- create translated configuration
#	- write files back out

# Known issues:
#	- translation with point-to-multipoint is incomplete

use Getopt::Std;
use Fcntl;
use POSIX qw(tmpnam ENOSYS);
use Sys::Hostname;

# Secure the path if we're running under RBAC.
$ENV{PATH} = ( "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/ucb" )
    if $< != $>;

# General path names that can be configured.
local($rootetc) =	"/etc/";
local($passwd) =	$rootetc . "passwd";
local($passwdlck) =	$rootetc . ".pwd.lock";
local($asfile) =	$rootetc . "asppp.cf";
local($astemp) =	$rootetc . "asppp.temp.cf";
local($asmoved) =	$rootetc . "asppp.saved.cf";
local($uucpdir) =	$rootetc . "uucp/";
local($Devices) =	$uucpdir . "Devices";
local($Devconfig) =	$uucpdir . "Devconfig";
local($Dialers) =	$uucpdir . "Dialers";
local($Dialcodes) =	$uucpdir . "Dialcodes";
local($Limits) =	$uucpdir . "Limits";
local($Sysfiles) =	$uucpdir . "Sysfiles";
local($Systems) =	$uucpdir . "Systems";
local($pppdir) =	$rootetc . "ppp/";
local($options) =	$pppdir . "options";
local($ttyprefix) =	$pppdir . "options.";
local($peersdir) =	$pppdir . "peers/";
local($initd) =		$rootetc . "init.d/";
local($asctl) =		$initd . "asppp";
local($pppdctl) =	$initd . "pppd";
local($sedpasswd) =	"/tmp/sed-passwd";

# Fake asppp keyword used to keep track of dial-in paths.
local($isdialin) = "-is-dial-in";

# Limits and Sysfiles are keyed on "service=".
# Devconfig is keyed on "service=" and "device=".
# Dialcodes, Dialers, Systems, and Devices are single keyword files.
# Devices alone may have multiple entries for a given key.

# Internal data structures
local(@sysfiles,@limits,@devconfig);
local(@sysdefault) = ( "systems=" . $Systems, "dialers=" . $Dialers,
		       "devices=" . $Devices );
local(@limitdefault) = ( "max=-1" );
local(@devdefault) = ( "pop=", "push=", "connecttime=-1", "expecttime=-1",
		       "msgtime=-1" );

# List of keywords for which ifconfig takes an additional parameter.
local($ifconfigtakes) = (
	addif => 1,
	removeif => 1,
	auth_algs => 1,
	encr_algs => 1,
	encr_auth_algs => 1,
	broadcast => 1,
	destination => 1,
	index => 1,
	metric => 1,
	modinsert => 1,
	modremove => 1,
	mtu => 1,
	netmask => 1,
	set => 1,
	subnet => 1,
	tdst => 1,
	tsrc => 1,
	wait => 1,

# These are keywords, but do not take an additional parameter.
	ether => 0,
	inet => 0,
	inet6 => 0,
	arp => 0,
	-arp => 0,
	auto-revarp => 0,
	modlist => 0,
	plumb => 0,
	unplumb => 0,
	private => 0,
	-private => 0,
	nud => 0,
	-nud => 0,
	trailers => 0,
	-trailers => 0,
	up => 0,
	down => 0,
	xmit => 0,
	-xmit => 0,
	auto-dhcp => 0,
	dhcp => 0,
	primary => 0,
	drop => 0,
	extend => 0,
	inform => 0,
	ping => 0,
	release => 0,
	start => 0,
	status => 0
);

# print number of something in English.
sub nof
{
    local($num, $item, @rest) = @_;
    print "No ", $item, "s", @rest if $num == 0;
    print "1 ", $item, @rest if $num == 1;
    print $num, " ", $item, "s", @rest if $num > 1;
}

# ask a yes or no question.
sub yesno
{
    local ($query, $default) = @_;
    local ($ans, $defans);

    return $default unless (-t STDIN) && (-t STDOUT) && !$opt_n;
    $defans = $default ? "Yn" : "yN";
    while (1) {
	print $query, " [", $defans, "]? ";
	chomp($ans = <STDIN>);
	return $default unless $ans;
	return 1 if $ans =~ /^[Yy1Tt]/;
	return 0 if $ans =~ /^[Nn0Ff]/;
	print "Please enter 'y' or 'n'.\n";
    }
}

# Put quotes around a string, if necessary.
# The tests here aren't perfect -- they think that \\\' isn't an
# escaped quote -- but they're good enough.
sub requote
{
    local($_) = @_;
    if (/^$/) {
	"\"\"";
    } elsif (/^'/ || /[^\\]'/ || /\\\\'/) {
# Has unescaped quotes; must use " or redo quoting.
	if (/^"/ || /[^\\]"/ || /\\\\"/) {
# Both kinds of quotes; redo the quoting.
	    s/^"/\\"/;
	    s/([^\\]|\\\\)"/$1\\"/g;
	}
	"\"" . $_ . "\"";
    } elsif (/^"/ || /[^\\]"/ || /\\\\"/) {
	"'" . $_ . "'";
    } elsif (/\s/) {
	"\"" . $_ . "\"";
    } else {
	$_;
    }
}

# Get a single line from a UUCP configuration file and return as a
# reference to an array of words.  Removes comments and escapes.
# (This is a modified version of the standard Perl shellwords function
# that understands C escape sequences and continuation lines.)
# Optionally returns lead-in, source text, and trailing component
# for editing.
sub uucpline
{
    local($input, $file, $triplet) = @_;
    local(@words,$snippet,$field,$havefield,$cont,@triparray,$maytrail);

    $cont = "";
    $maytrail = "";
    while (<$input>) {
	# remove leading whitespace
	if (s/^(\s+)//) {
	    $maytrail .= $1;
	}
	if ($cont eq "") {
	    if (s/^(\#(.|\n)*)$//) {
		$triparray[0] .= $maytrail . $1;
		$maytrail = "";
		next;
	    }
	    if (s/^(\\?\n?)$//) {
		$triparray[0] .= $maytrail . $1;
		$maytrail = "";
		next;
	    }
	    $triparray[0] .= $maytrail;
	    $maytrail = "";
	}
	$snippet = $_;
	if (s/^(([^\#\\]|\\.)*)\\\n$//) {
	    $maytrail .= $snippet;
	    $cont .= $1;
	    next;
	}
	if (/^(([^\\\#]|\\[^\#])*)(\#?(.|\n)*)$/) {
	    $_ = $maytrail . $1;
	    $maytrail = $3;
	    if (s/((\s|\n)*)$//) {
		$maytrail = $1 . $maytrail;
	    }
	    $triparray[1] = $_;
	}
	$_ = $cont . $snippet;
	$cont = "";
	s/\n$//;
	while ($_ ne '') {
	    for (;;) {
		if (s/^#.*//) {
		    last;
		} elsif (s/^"(([^"\\]|\\.)*)"//) {
		    $snippet = $1;
		} elsif (s/^"//) {
		    warn "Unmatched double quote in $file: \"$_\n";
		} elsif (s/^'(([^'\\]|\\.)*)'//) {
		    $snippet = $1;
		} elsif (s/^'//) {
		    warn "Unmatched single quote in $file: '$_\n";
		} elsif (s/^\\s//) {
# \s works in chat, but not in the pppd option files
		    $snippet = " ";
		} elsif (s/^(\\.)//) {
		    $snippet = $1;
		} elsif (s/^([^\s\\'"#]+)//) {
		    $snippet = $1;
		} else {
		    s/^\s+//;
		    last;
		}
		$havefield = 1;
		$field .= $snippet;
	    }
	    push(@words, $field) if $havefield;
	    $havefield = 0;
	    $field = '';
	}
	last;
    }
    $triparray[2] .= $maytrail;
    @$triplet = @triparray;
    warn "Bad continuation line in $file: $cont\n" if $cont ne '';
    \@words;
}

# Given a logical UUCP file name, return a list of all of the files
# that should be read.
sub uucpfiles
{
    local ($file) = @_;
    local (@flist, $value) = ();

    for $value (@sysfiles, @sysdefault) {
	if ($value =~ /^$file=/i) {
	    $value =~ s/^$file=//i;
	    for $file (split /:/, $value) {
		$file = $uucpdir . $file if $file !~ /^\//;
		push @flist, $file;
	    }
	    last;
	}
    }
    @flist;
}

# Given a file name and some key words, parse the contents of the file
# and return a reference to a hash constructed from this.  All keys
# except the last must match exactly.  The last is used to order the
# hash.
sub uucpkeyfile
{
    local($file,@keylist) = @_;
    local($lastkey,$keyval,$words,$i,$flag,%byservice);

    open(SVCFILE, '<' . $file) || return undef;
    $lastkey = pop @keylist;
    while (@{$words = uucpline(SVCFILE, $file)}) {
	$flag = 1;
	foreach $keyval (@keylist) {
	    $flag = 0;
	    $i = 0;
	    while ($i < @$words) {
		if ($$words[$i] eq $keyval) {
		    splice @$words, $i, 1;
		    $flag = 1;
		    last;
		}
		$i++;
	    }
	    last unless $flag;
	}
	next unless $flag;
	foreach $i (0 .. @{$words}) {
	    if (@{$words}[$i] =~ /^$lastkey(.*)/i) {
		splice @{$words}, $i, 1;
		$byservice{$1} = $words;
		last;
	    }
	}
    }
    close SVCFILE;
    \%byservice;
}

# This reads a UUCP file that is keyed on the first token on each
# line.  Duplicates are not permitted; the first encountered is used
# and the rest are silently discarded.  A hash indexed on the first
# token and containing an array of tokens in each bucket is returned.
# Used for Dialcodes, Dialers, and Systems.
sub uucpposfiles
{
    local(@files) = @_;
    local($keyval,$words,%keyeddata);

    foreach $file (@files) {
	if (!open(POSFILE, '<' . $file)) {
	    warn "$file: $!\n";
	    next;
	}
	while (@{$words = uucpline(POSFILE, $file)}) {
	    $keyval = shift @{$words};
	    next if $keyeddata{$keyval};
	    $keyeddata{$keyval} = $words;
	}
	close POSFILE;
    }
    \%keyeddata;
}

# This reads a UUCP file that is keyed on the first token on each line
# and may have duplicate entries.  Each entry of the hash contains an
# array.  Each entry of that array points to an array of tokens
# representing one parsed line.  Used for the Devices file.
sub uucpdevices
{
    local(@files) = @_;
    local($keyval,$words,%keyeddata);

    foreach $file (@files) {
	if (!open(POSFILE, '<' . $file)) {
	    warn "$file: $!\n";
	    next;
	}
	while (@{$words = uucpline(POSFILE, $file)}) {
	    $keyval = shift @{$words};
	    push @{$keyeddata{$keyval}}, $words;
	}
	close POSFILE;
    }
    \%keyeddata;
}

# For a path defined in asppp.cf, copy over defaults, validate the
# required options, and save in the hash to be returned.
sub savepath
{
    local($paths, $options, $defref) = @_;
    local($peer,$intf);

    return if $options == $defref;
    foreach $key (keys %$defref) {
	$$options{$key} = $$defref{$key} unless defined($$options{$key});
    }
    $peer = $$options{"peer_system_name"};
    warn("Discarded path with no peer system name.\n"), return
	unless defined($peer);
    $intf = $$options{"interface"};
    warn("Missing interface on path to peer \"$peer\".\n"), return
	unless defined($intf);
    warn("Bad interface $intf on path to peer \"$peer\".\n"), return
	unless $intf =~ /^ipd([0-9]+|ptp[0-9]+|ptp\*)$/;
    warn("Missing peer IP address for point-to-multipoint path to \"",
	$peer, "\".\n"), return
	if $intf =~ /^ipd[0-9]+$/ && !defined($$options{"peer_ip_address"});
    warn "Multiple definitions of path to peer \"$peer\".\n",
	if defined($paths{$peer});
    warn "Odd version number ", $$options{"version"},
	" encountered in path to peer \"", $peer, "\" (ignored).\n"
	if defined($$options{"version"}) && $$options{"version"} != 1;
    $paths{$peer} = $options;
}

# Parse through asppp.cf. Unlike the UUCP files, lines don't matter
# here.  The parsing is modal, with "path" introducing a PPP session
# description and "defaults" reverting back to global definitions.
sub readaspppcf
{
    local($aspppcf) = @_;
    local(%aspppdkey) = (
	chap_name		=> 1,
	chap_peer_secret	=> 1,
	chap_peer_name		=> 1,
	chap_secret		=> 1,
	debug_level		=> 1,
	default_route		=> 0,
	ifconfig		=> -1,
	inactivity_timeout	=> 1,
	interface		=> 1,
# sic; aspppd is seriously confused!  ACCM isn't in IPCP.
	ipcp_async_map		=> 1,
	ipcp_compression	=> 1,
	lcp_compression		=> 1,
	lcp_mru			=> 1,
	negotiate_address	=> 1,
	pap_id			=> 1,
	pap_password		=> 1,
	pap_peer_id		=> 1,
	pap_peer_password	=> 1,
	peer_ip_address		=> 1,
	peer_system_name	=> 1,
	require_authentication	=> 2,
	version			=> 1,
	will_do_authentication	=> 2
    );
    local($words,$word,$prevword,$i,$errors,%defaults,%ifconfig,%paths);
    local($options);

    open ASPPPD, "<" . $aspppcf || die "$aspppcf: $!\n";
    print "Reading configuration from $aspppcf\n" if $opt_v;
    $defaults{inactivity_timeout} = 120;
    $defaults{ipcp_compression} = "vj";
    $defaults{lcp_compression} = "on";
    $options = \%defaults;
    while (@{$words = uucpline(ASPPPD, $aspppcf)}) {
	if ($$words[0] =~ /^ifconfig$/i) {
	    warn "$prevword with missing argument ignored.\n"
		if defined($prevword);
	    undef $prevword;
	    shift @$words;	# discard 'ifconfig' keyword
	    $word = shift @$words;
	    warn("Bad interface on ifconfig $word.\n"), next
		unless $word =~ /^ipd([0-9]+|ptp[0-9]+)$/;
	    $ifconfig{$word} = \@$words;
	    next;
	}
	unshift @{$words}, $prevword if defined($prevword);
	undef $prevword;
	while ($word = lc(shift @{$words})) {
	    $_ = $word;
	    if (/^defaults$/i) {
		savepath(\%paths, $options, \%defaults);
		$options = \%defaults;
		next ;
	    }
	    if (/^path$/i) {
		local(%pathopts);
		savepath(\%paths, $options, \%defaults);
		$options = \%pathopts;
		next;
	    }
	    if (!defined($i = $aspppdkey{$word})) {
		die "Too many errors in $aspppcf; aborting.\n"
		    if ++$errors > 5;
		warn "Ignoring unknown keyword $word in $aspppcf\n";
		next;
	    }
	    warn("$_ unexpected; remainder of line ignored.\n"),
		last if $i == -1;
	    warn("Duplicate $_ in path ignored.\n"), next
		if $options != \%defaults && defined($$options{$_});
	    $$options{$_} = 1 if $i == 0;
	    next if $i == 0;
	    $prevword = $_, last unless defined($word = shift @{$words});
	    $$options{$_} = $word if $i == 1;
	    if ($i == 2) {
		undef $$options{$_}, next if $word =~ /^off$/;
		$$options{$_} = $word;
		if ($word = shift @{$words}) {
		    if ($word =~ /^(p|ch)ap$/) {
			$$options{$_} .= " " . $word;
		    } else {
			unshift @{$words}, $word;
		    }
		}
	    }
	}
    }
    warn "Odd trailing keyword \"$prevword\" ignored in $aspppcf\n"
	if $prevword;
    savepath(\%paths, $options, \%defaults);
    die "No paths defined for aspppd.\n" if 0+(keys %paths) == 0;
    die "No interfaces defined for aspppd.\n" if 0+(keys %ifconfig) == 0;
    if ($opt_v) {
	nof 0+(keys %paths), "path", " and ";
	nof 0+(keys %ifconfig), "interface", " defined for aspppd.\n";
    }
    close ASPPPD;
    ( \%ifconfig, \%paths );
}

# Read /etc/passwd (or NIS) and return hash of users for whom
# the default shell is aspppls.  Each hash entry contains the user's
# home directory path.
sub readpasswd
{
    local(%users,@pwe);

    setpwent();
    while (@pwe = getpwent()) {
	$users{$pwe[0]} = $pwe[7] if $pwe[8] =~ /\/aspppls$/;
    }
    endpwent();
    nof 0+(keys %users), "aspppd dial in user", " found.\n"
	if $opt_v;
    \%users;
}

# Parse through pmadm output to find enabled serial ports.
# Field 9 has 'I' (modem dial-out only or initialize only), 'b'
# (bidirectional) or is blank (modem dial-in only or terminal-hardwired).
# For that latter case, field 18 (software-carrier) has 'y'.
# Field 3 has 'x' if disabled.
sub getserialports
{
    local(%dialin, %dialout);

    open PMADM, "pmadm -L|" || (warn "pmadm: $!\n", return undef);
    while (<PMADM>) {
	split /:/;
	if ($_[3] !~ /x/) {
	    $dialin{$_[2]} = $_[8] if $_[9] ne "I";
	    $dialout{$_[2]} = $_[8] if $_[9] ne "";
	}
    }
    close PMADM;
    ( \%dialin, \%dialout );
}

# Convert an ifconfig statement into a local and remote address pair.
sub ifconf_addr
{
    local($ifconf) = @_;
    local($arg, $narg, $lcladdr, $remaddr);

    shift @$ifconf;	# lose the interface name
    while (@$ifconf) {
	local($arg, $narg);
	$arg = shift @$ifconf;
	$narg = shift @$ifconf if $ifconfigtakes{$arg};
	if (exists($ifconfigtakes{$arg})) {
	    if ($arg eq "set") {
		$lcladdr = $narg;
	    } elsif ($arg eq "destination") {
		$remaddr = $narg;
	    }
	} elsif (!defined($lcladdr)) {
	    $lcladdr = $arg;
	} elsif (!defined($remaddr)) {
	    $remaddr = $arg;
	}
    }
    ( $lcladdr, $remaddr );
}

# Convert a hash of aspppd options into an array of pppd options.  The
# third argument ($chatpath) is undef for dial-in or a path to the
# chat script file otherwise.
sub convert_options
{
    local ($pppdargs, $opts, $chatpath, $user) = @_;

# Do the pppd option conversions.
    push(@$pppdargs, "defaultroute") if $$opts{default_route};
    push(@$pppdargs, "idle " . $$opts{inactivity_timeout})
	if $$opts{inactivity_timeout} != 0;
    push(@$pppdargs, "asyncmap " . $$opts{ipcp_async_map})
	if $$opts{ipcp_async_map};
    push(@$pppdargs, "novj") if !$$opts{ipcp_compression};
    local($peer);
    if ($$opts{require_authentication}) {
	local (@authopts);
	if ($$opts{require_authentication} =~ /chap/) {
	    push(@authopts, "require-chap");
	    $peer = $$opts{chap_peer_name};
	} else {
	    push(@authopts, "require-pap");
	    $peer = $$opts{pap_peer_id};
	}
	push(@authopts, "remotename " . requote($peer)) if $peer;
	push(@authopts, "auth");
	if ($chatpath) {
	    push(@$pppdargs, @authopts);
	} elsif ($dialin_auth == 3) {
# mixed authentication; must use wrapper script.
	    local($sfile) = $pppdir . "dial-in." . $user;
	    $scriptfiles{$sfile}  = "#!/bin/sh\n";
	    $scriptfiles{$sfile} .= "exec /usr/bin/pppd @authopts\n";
	    $dialinshell{$user} = $sfile;
	}
    } elsif ($dialin_auth < 2) {
	push(@$pppdargs, "noauth");
    }
    push(@$pppdargs, "noaccomp nopcomp") if !$$opts{lcp_compression};
    push(@$pppdargs, "mru " . $$opts{lcp_mru}) if $$opts{lcp_mru};
    push(@$pppdargs, "noipdefault ipcp-accept-local")
	if $$opts{negotiate_address};
    local($myname,$csecret,$psecret,$passopt);
    if ($$opts{will_do_authentication} =~ /chap/i) {
	$myname = $$opts{chap_name};
	$csecret = $$opts{chap_secret};
    }
    $myname = "" if !$myname;
    if ($$opts{will_do_authentication} =~ /pap/i) {
	if ($myname ne "" && $$opts{pap_id} ne "" &&
	    $myname ne $$opts{pap_id}) {
	    warn "pppd cannot have separate local names for PAP and CHAP; using CHAP name:\n";
	    warn "\t\"$myname\"\n";
	} else {
	    $myname = $$opts{pap_id} if $$opts{pap_id} ne "";
	}
	$psecret = $$opts{pap_password};
    }
    if ($$opts{will_do_authentication}) {
	if ($chatpath &&
	    ($$opts{will_do_authentication} !~ /chap/i ||
	     $$opts{will_do_authentication} !~ /pap/i ||
	     $csecret eq $psecret)) {
	    push(@$pppdargs,
		"password " . requote($csecret ? $csecret : $psecret));
	    $passopt = 1;
	}
	push @$pppdargs, "user " . requote($myname);
	push(@$pppdargs, "remotename " . requote($peer))
	  if $peer && !$$opts{require_authentication};
    } else {
	$myname = "NeverAuthenticate";
    }

    push(@$pppdargs, "debug") if $$opts{debug_level} >= 8;
    push(@$pppdargs, "kdebug 3") if $$opts{debug_level} >= 9;
    local($lcladdr, $remaddr) = ifconf_addr($$ifconfig{$$opts{interface}});
    if ($chatpath) {
	if ($$opts{debug_level} >= 4) {
	    push(@pppdargs, "connect \"/usr/bin/chat -vf $chatpath\"");
	} else {
	    push(@pppdargs, "connect \"/usr/bin/chat -f $chatpath\"");
	}
	push(@$pppdargs, "user " . requote($myname));
	local($str) = $remaddr;
	$str = $$opts{peer_ip_address} if $$opts{peer_ip_address};
	push(@$pppdargs, $lcladdr . ":" . $str)
	    if !$opt_s && ($lcladdr || $str);
    } else {
	push(@$pppdargs, "user " . requote($myname))
	    if $myname eq "NeverAuthenticate";
    }
    if ($$opts{interface} && $opt_s) {
	if ($$opts{interface} =~ /^ipd[0-9]+$/) {
	    push(@$pppdargs, $lcladdr . ":") if $lcladdr;
	} elsif ($$opts{interface} =~ /^ipd(|ptp)([0-9]+)$/) {
	    push(@pppdargs, "unit " . $2);
	} else {
	    push(@pppdargs, "plumbed");
	}
    }

# Convert the secrets
    if ($chatpath) {
	$addsecret = "";
    } elsif ($$opts{peer_ip_address}) {
	$addsecret = " " . $$opts{peer_ip_address};
    } else {
	$addsecret = " *";
    }
    if ($$opts{require_authentication}) {
	local($lclname, $secret, $authf);
	$lclname = $$opts{will_do_authentication} ? $myname : hostname();
	if ($$opts{require_authentication} =~ /chap/) {
	    $secret = $$opts{chap_peer_secret};
	    $authf = \%chapsecrets;
	} else {
	    $secret = $$opts{pap_peer_password};
	    $authf = \%papsecrets;
	}
	${$$authf{$peer}}{$lclname} = requote($secret) . $addsecret;
    }
    if ($$opts{will_do_authentication} && !$passopt) {
	${$chapsecrets{$myname}}{$peer} = requote($csecret) if $csecret;
	${$papsecrets{$myname}}{$peer} = requote($psecret) if $psecret;
    }
}

# Translate options for a dial-in user.
sub translatedialin
{
    local($peer) = @_;

    $optname = $$dialinusers{$peer};
    $optname .= "/" if $optname !~ /\/$/;
    $optname .= ".ppprc";
    if (exists($optfiles{$optname})) {
	warn "Home directories of $peer and $optuser{$optname} are the same ($optfiles{$optname}\n";
	warn "Ignoring configuration for $peer.\n";
	return;
    }
    $optuser{$optname} = $peer;
    $dialinshell{$peer} = "/usr/bin/pppd";
    local (@pppdargs);
    convert_options(\@pppdargs, $$paths{$peer}, undef, $peer);
    push @pppdargs, "nologfd";
    $optfiles{$optname} = \@pppdargs;
}

# Translate ifconfig entries in asppp.cf into either an ifconfig
# script for strict translation or a set of per-port IP addresses
# for normal translation.  Also translate ipdX interfaces into
# Ethernet aliases to make routing daemon happy.
sub translateifconfig
{
    local (@ifconfiglist,$cstr,$etherif,$intf,$ifconf,$addstr,$port);
    
    open IFLIST, "/sbin/ifconfig -au4|" || die "cannot run ifconfig: $!\n";
    while (<IFLIST>) {
	$etherif = $1 if !$etherif && /^([^:]*).*UP.*BROADCAST/;
    }
    close IFLIST;
    $etherif = $1 if !$etherif && glob("/etc/hostname.*") =~ /[^\.]*.(.*)/;
    $etherif = $1 if !$etherif && glob("/etc/dhcp.*") =~ /[^\.]*.(.*)/;
    $etherif = "hme0" if !$etherif;
    
    $cstr = "";
    foreach $intf (keys %$ifconfig) {
	$ifconf = $$ifconfig{$intf};
	if ($intf =~ /ipd[0-9]+/) {
	    shift @$ifconf;
	    $cstr .= "ifconfig $etherif addif @$ifconf\n";
	}
    }

    $ndialin = 0+(keys %$dialin);
    $addstr = "";
    foreach $intf (keys %downif) {
	$ifconf = $downif{$intf};
	if ($intf =~ /ipdptp([0-9]+)/ && --$ndialin >= 0) {
	    push @ifconfiglist, $ifconf;
	    $addstr .= "ifconfig sppp" . $1 . " @$ifconf\n";
	}
    }
    if ($ndialin > 0) {
	if (@ifconfiglist) {
	    print "Fewer ifconfigs (", $#ifconfiglist+1,
	    ") than dial-in lines (", $ndialin+$#ifconfiglist+1, ")\n";
	} else {
	    print "No ifconfigs for ";
	    nof $ndialin, "dial-in port", ".\n";
	}
    } elsif ($ndialin < 0) {
	if (@ifconfiglist) {
	    print "Ignoring ", -$ndialin, " of ";
	    nof $#ifconfiglist-$ndialin+1, "ifconfig",
	    " (too few dial-in ports)\n";
	} else {
	    print "Ignoring all ";
	    nof -$ndialin, "ifconfig line", " (no dial-in ports)\n";
	}
    }

    if ($opt_s) {
	# Strict translation uses pre-plumbed interfaces.
	$cstr .= $addstr;
    } else {
	foreach $port (values %$dialin) {
	    last if !@ifconfiglist;
	    $port =~ s+/dev/++;
	    $port =~ s+/+.+g;
	    $optfile = $ttyprefix . $port;
	    $ifconf = pop @ifconfiglist;
	    local ($lcladdr, $remaddr) = ifconf_addr($ifconf);
	    next if !defined($lcladdr) || !defined($remaddr);
	    local (@pppdargs) = $lcladdr . ":" . $remaddr;
	    $optfiles{$optfile} = \@pppdargs;
	}
    }
    $scriptfiles{$pppdir . "ifconfig"} = $cstr if $cstr;
}

# Attempt to modify global passwd file using sed script stored in
# the $sedpasswd temporary file.
sub rewrite_passwd
{
    print "Updating local passwd file (if any).\n" if $opt_v;
    if (!sysopen(PWDLCK, $passwdlck, O_WRONLY|O_CREAT, 0600)) {
	warn "Unable to lock password file: $!\n";
    } else {
	$lockstr = pack "ssLLiiLLLL", F_WRLCK, 0, 0, 0, 0, 0, 0, 0, 0, 0;
	eval {
	    local $SIG{ARLM} = sub {
		die "alarm while locking password file\n"
	    };
	    alarm 15;
	    fcntl PWDLCK, F_SETLKW, $lockstr ||
	      die "cannot lock password file: $!\n";
	    alarm 0;
	};
	if ($@) {
	    warn $@;
	} else {
	    warn "Password update failed.\n"
	      if (system("sed -f $sedpasswd < $passwd > ${passwd}.new") ||
		  !(rename "${passwd}.new", $passwd));
	}
	$lockstr = pack "ssLLiiLLLL", F_UNLCK, 0, 0, 0, 0, 0, 0, 0, 0, 0;
	fcntl PWDLCK, F_SETLK, $lockstr;
	close PWDLCK;
    }
    if (($ypmaster = `/usr/bin/ypwhich 2>/dev/null`) && $? == 0) {
	$ypmaster =~ /(.*)\n/;
	($ypmaster) = gethostbyname($1);
	($thishost) = gethostbyname(hostname);
	if ($ypmaster eq $thishost) {
	    system("cd /var/yp && make")
	      if yesno("Rebuild NIS/YP maps", $opt_y);
	} else {
	    warn "Not running on NIS/YP master $1; unable to update user shells\n";
	    print "Use 'sed -f $sedpasswd <$passwd >${passwd}.new' on the master\n";
	    print "and then remake the NIS/YP database.\n";
	    undef $sedpasswd;
	}
    }
    unlink $sedpasswd if $sedpasswd;
}

# Show usage message.
sub usage
{
    print "Usage:\n\n";
    print "\t$0 [-rsvy]\n\n";
    print "    -n - non-interactive mode.\n";
    print "    -r - revert back to aspppd configuration.\n";
    print "    -s - use strict translation.\n";
    print "    -v - print more detail of the operations performed.\n";
    print "    -y - assume 'yes' as default answer where reasonable.\n";
    exit;
}

# Correct an environment variable so that it points at either a useful
# executable program, or nothing at all.
sub fixpath
{
    local ($prog, $deflt) = @_;

    $prog = $deflt if $prog eq "";
    if ($prog !~ /^(\.\/|\/)/) {
	local ($_) = $ENV{PATH};
	$_ = "/bin:/usr/bin:/sbin:/usr/sbin" if $_ eq "";
	split /:/;
	foreach (@_) {
	    $prog = $_ . "/" . $prog, last if -x $_ . "/" . $prog;
	}
    }
    $prog = "" if !(-x $prog);
    $prog;
}

getopts('nrsvy') || usage;

die "Need permission to modify system files.\n"
    unless ($> == 0 || yesno "This script should be run as root.  Continue");

if ($opt_r) {
    local ($intemp);

# Revert to previous configuration.  Just rename the aspppd file back
# and undo changes to the passwd file.

    die "No saved aspppd configuration exists.\n" unless -f $asmoved;
    if (-e $astemp) {
	die "$astemp is not a file\n" unless -f $asfile;
	unlink $astemp || die "Cannot remove temporary $astemp: $!\n";
    }
    $intemp = 0;
    if (-e $asfile) {
	die "$asfile is not a file\n" unless -f $asfile;
	die "Not modifying configuration.\n"
	    unless yesno "Remove existing $asfile", $opt_y;
	rename $asfile, $astemp || die "Cannot rename existing $asfile: $!\n";
	$intemp = 1;
    }

    if (rename $asmoved, $asfile) {
	unlink $astemp || warn "$astemp: $!\n" if $intemp;
    } else {
	$failure = "Cannot rename $asmoved to $asfile: $!\n";
	rename $astemp, $asfile ||
	    die "$failure\nand cannot recover: $!\n" .
		"Saved current asppp.cf in $astemp\n"
		    if $intemp;
	die $failure;
    }

    $( = $);
    $< = $>;

    system($pppdctl, "stop") if -x $pppdctl;
    # remove pppd autostart files.
    unlink $pppdir . "ifconfig";
    unlink $pppdir . "demand";

    system($asctl, "start") if -x $asctl;

    open SEDFILE, ">$sedpasswd" || die "Cannot write $sedpasswd: $!\n";
    local ($escdir) = $pppdir;
    $escdir =~ s+/+\\/+g;
    print SEDFILE "/${escdir}dial-in\\./s+[^:]*\$+/usr/sbin/aspppls+\n";
    print SEDFILE "/\\/usr\\/bin\\/pppd/s+[^:]*\$+/usr/sbin/aspppls+\n";
    close SEDFILE;

    rewrite_passwd;

    exit 0;
}

$aspppcf = $asfile;
if (!(-f $asfile)) {
    die "No aspppd configuration exists; nothing to convert.\n"
	unless -f $asmoved;
    die "No changes made.\n"
	unless yesno "Already converted; rerun anyway";
    $aspppcf = $asmoved;
}

print "This script provides only a suggested translation for your existing aspppd\n";
print "configuration.  You will need to evaluate for yourself whether the translation\n";
print "is appropriate for your operating environment.\n";
die "No changes made.\n"
  unless yesno "Continue", 1;

# Read in the asppp.cf file first; there's no reason to continue on to
# the UUCP files if this file isn't readable or has no paths defined.
local($ifconfig, $paths) = readaspppcf($aspppcf);

# Loop over the ifconfigs and build a list of the down ones.
foreach $intf (keys %$ifconfig) {
    local(@words) = @{$$ifconfig{$intf}};
    while ($word = shift @words) {
	shift @words if $ifconfigtakes{$word};
	if ($word =~ /^down$/) {
	    warn("Why is $intf declared down?\n"), last
		if $intf =~ /^ipd[0-9]+$/;
	    $downif{$intf} = $$ifconfig{$intf};
	    delete $$ifconfig{$intf};
	    last;
	}
    }
}

# Read /etc/passwd for dial-in users configured for aspppd.
local($dialinusers) = readpasswd;

# Read in existing pppd configuration.  All we really care about
# is the setting of the "auth" option.
undef $authoption;
if (open(OPTIONS,"<" . $options)) {
    while (@{$words = uucpline(OPTIONS, $options)}) {
	while ($_ = pop @$words) {
	    $authoption = $_ if /auth/i;
	}
    }
    close OPTIONS;
    $authoption = "unknown" if !defined($authoption);
}

$dialin_auth = 0;
if ($authoption =~ /^auth$/i) {
    $dialin_auth = 1;
} elsif ($authoption =~ /^noauth$/i) {
    $dialin_auth = 2;
} elsif (defined($authoption)) {
    $dialin_auth = 3;
}

# Check that there's a path for each dial in user
foreach $user (keys %$dialinusers) {
    if (!defined($$paths{$user})) {
	warn "Dial-in user ", $user,
	    " does not have a corresponding dial-in path.\n";
	delete $$dialinusers{$user};
	next;
    }
    $intf = ${$$paths{$user}}{"interface"};
    if ($intf eq "ipdptp*") {
	if (0+keys(%downif) == 0) {
	    warn "Dial-in user $path has no available \"down\" interfaces.\n";
	    delete $$dialinusers{$user};
	    next;
	}
    } else {
	if (!defined($downif{$intf}) && !defined($$ifconfig{$intf})) {
	    warn "Dial-in path $user has undefined $intf; deleted.\n";
	    delete $$dialinusers{$user};
	    next;
	}
    }
    ${$$paths{$user}}{$isdialin} = 1;
# 0 - no info (no options file, "noauth" on call)
# 1 - all auth ("auth" in options, "noauth" on call)
# 2 - all noauth ("noauth" in options)
# 3 - mixed; use auth ("noauth" in options, wrapper script for "auth")
    if (${$$paths{$user}}{require_authentication}) {
	if ($dialin_auth == 2) {
	    $dialin_auth = 3;
	} elsif ($dialin_auth == 0) {
	    $dialin_auth = 1;
	}
    } else {
	if ($dialin_auth == 1) {
	    $dialin_auth = 3;
	} elsif ($dialin_auth == 0) {
	    $dialin_auth = 2;
	}
    }
}

# Get lists of usable dial-in and dial-out ports.
local($dialin,$dialout) = getserialports;

# Read and parse the UUCP Sysfiles, Devconfig, and Limits files.
# These are keyed with the "service=" string.  The Sysfiles file can
# augment or override the list of files read for a given service.
print "Reading UUCP configuration.\n" if $opt_v;
@sysfiles = @{${uucpkeyfile($Sysfiles,"service=")}{"ppp"}};
@limits = @{${uucpkeyfile($Limits,"service=")}{"ppp"}};
%devconfig = %{uucpkeyfile($Devconfig,"service=ppp","device=")};

# Now read in the UUCP files corresponding to this service.
$systems = uucpposfiles(uucpfiles("systems"));
$dialers = uucpposfiles(uucpfiles("dialers"));
$dialcodes = uucpposfiles($Dialcodes);
$devices = uucpdevices(uucpfiles("devices"));

# just to make sure
$$dialcodes{""} = ();

# Loop over paths.  Dial-out only paths are translated into demand-dial
# configurations.  Dial-in only paths are translated into appropriate
# log-in entries.
local (@bidirectional);
foreach $peer (keys %$paths) {
    if (exists($$systems{$peer})) {
	$sline = $$systems{$peer};
	if ($$sline[0] eq "Never") {
	    if (${$$paths{$peer}}{$isdialin}) {
		translatedialin($peer);
	    } else {
		print "We never call $peer, and he never calls us.\n"
		    if $opt_v;
	    }
	    delete $$paths{$peer};
	    next;
	}
	push @bidirectional, $peer if ${$$paths{$peer}}{$isdialin};
	print "Ignoring time restriction on $peer\n"
	    if $$sline[0] ne "Any";
	$dlist = $$devices{$$sline[1]};
	$class = $$sline[2];
	$i = 0;
	while ($i < @$dlist) {
	    local($dev) = $$dlist[$i];
	    if ($$dev[1] ne "-") {
		print "Ignoring device $$dev[0]; 801-type not supported.\n";
		splice @$dlist, $i, 1;
		next;
	    }
	    $i++;

	    # Make sure that classes match.
	    next if $$dev[2] ne "Any" && $class ne "Any" && $$dev[2] ne $class;
	    # Prepend "/dev/" if it's not present in the device name.
	    if (exists($$dialout{$$dev[0]})) {
		# This just seems odd.
		$dname = $$dialout{$$dev[0]};
		$dname =~ s+/dev/term/+/dev/cua/+;
	    } else {
		$dname = ($$dev[0] =~ m+^/+ ? $$dev[0] : ("/dev/" . $$dev[0]));
	    }
	    # Skip devices that aren't supposed to be used for dial-out.
	    next if $dname =~ m+^/dev/term/+;
	    next if $dname =~ m+^/dev/tty[a-z]$+;
	    # Make sure this is a character device and we have access to it.
	    next unless -w $dname && -c $dname;
	    warn "Dialer for $$dev[3] is missing.\n"
		unless exists($warned{$$dev[3]}) ||
		    exists($$dialers{$$dev[3]});
	    $warned{$$dev[3]} = 1;

	    # Expand keywords from Dialcodes file.  Should have \T or \D.
	    $phone = $$sline[3];
	    $xphone = ($$dev[4] eq "\\T" && $phone =~ /^([A-Za-z]*)(.*)$/ ?
	        "@{$$dialcodes{$1}}" . $2 : $phone);

	    # Make a copy of the dialing script.
	    local(@dials) = @{$$dialers{$$dev[3]}};

	    # Translate dial tone and wait characters from Dialers file.
	    $_ = shift @dials;
	    s[(.)(.)]{
		local($from,$to) = ($1,$2);
		$phone =~ s+(^|[^\\])$from+$1$to+gx;
		$xphone =~ s+(^|[^\\])$from+$1$to+gx;
	    }ge;

	    # Translate escapes in dial specification.  Chat has a \T,
	    # but uses \U instead of \D.
	    local($needt, $needu, $isexpect, @chats) = ("", "", 1);
	    foreach $str (@dials) {
		push(@chats, "") if $str eq "";
		local ($ostr) = "";
		if ($isexpect) {
		    while ($str =~ s/([^\\]*)\\(.)//) {
			local($lead, $_) = ($1, $2);
			/[Mm]/ ? ($ostr .= $lead) :
			/[Ee]/ ? ($sorrye = 1, $ostr .= $lead) :
			($ostr .= $lead . "\\" . $_);
		    }
		} else {
		    while ($str =~ s/([^\\]*)\\(.)//) {
			local($lead, $_) = ($1, $2);
			/T/ ? ($needt = " -T '$xphone'",
			       $ostr .= $lead . "\\T") :
			/D/ ? ($needu = " -U '$phone'",
			       $ostr .= $lead . "\\U") :
			/M/ ? ($ostr .= $lead,
			       ($ostr ne "" ? push(@chats, $ostr, "\\c"):0),
			       push(@chats, "HANGUP", "OFF"), $ostr = "") :
			/m/ ? ($ostr .= $lead,
			       ($ostr ne "" ? push(@chats, $ostr, "\\c"):0),
			       push(@chats, "HANGUP", "ON"), $ostr = "") :
			/[Ee]/ ? ($sorrye = 1, $ostr .= $lead) :
			/[dp]/ ? ($ostr .= $lead . "\\" . $_ . "\\" . $_) :
			/c/ ? ($str eq "" ? ($ostr .= $lead . "\\c") : 0) :
			($ostr .= $lead . "\\" . $_);
		    }
		}
		$ostr .= $str;
		push @chats, $ostr if $ostr ne "";
		$isexpect = !$isexpect;
	    }

	    # Pad out dial list if we're missing a "send" string and tack
	    # on the chat list from the Systems file.
	    if (defined $$sline[4]) {
		push @chats, "\\c" if !$isexpect;
		push @chats, (splice @$sline, 4);
	    }

	    $chatfile = $pppdir . "chat.$peer.$$dev[3]";
	    if (-e $chatfile) {
		print "$chatfile already exists.\n";
		if (!yesno("Should it be overwritten",$opt_y)) {
		    if (yesno("Should it be used as-is")) {
			warn "Using $chatfile as-is; it may not be correct.\n";
		    } else {
			for ($n = 0; ; $n++) {
			    last if !(-e $chatfile . "." . $n);
			}
			$chatfile .= "." . $n;
			print "Using $chatfile instead.\n";
			$chatfiles{$chatfile} = \@chats;
		    }
		} else {
		    $overwrite{$chatfile} = 1;
		    $chatfiles{$chatfile} = \@chats;
		}
	    } else {
		$chatfiles{$chatfile} = \@chats;
	    }

	    push @pppdargs, $dname;
	    push @pppdargs, $class if $class =~ /^[0-9]+$/;
	    push @pppdargs, "demand";
	    convert_options(\@pppdargs,$$paths{$peer},
		$chatfile . $needt . $needu, undef);

	    $optname = $peersdir . $peer;
	    if (-e $optname) {
		print "$optname already exists.\n";
		if (!yesno("Should it be overwritten", $opt_y)) {
		    if (yesno("Should it be used as-is")) {
			warn "Using $optname as-is; it may not be correct.\n";
		    } else {
			for ($n = 0; ; $n++) {
			    last if !(-e $optname . "." . $n);
			}
			$optname .= "." . $n;
			print "Using $optname instead.\n";
			$optfiles{$optname} = \@pppdargs;
		    }
		} else {
		    $overwrite{$optname} = 1;
		    $optfiles{$optname} = \@pppdargs;
		}
	    } else {
		$optfiles{$optname} = \@pppdargs;
	    }
	    $scriptfiles{$pppdir . "demand"} .= "/usr/bin/pppd file $optname\n";
	    last;
	}
    } elsif (${$$paths{$peer}}{$isdialin}) {
    	translatedialin($peer);
    } else {
	warn "Path $peer has no dial-in user nor Systems file entry.\n";
	delete $$paths{$peer};
    }
}

warn "Chat cannot do echo checking; requests for this removed.\n" if $sorrye;

if (@bidirectional) {
    print "\nWarning:  The following paths are bidirectional:\n";
    print "\t@bidirectional\n\n";
    print "Bidirectional paths (with entries in both Systems and passwd) do not translate\n";
    print "into Solaris PPP 4.0 semantics in an exact manner.  The dial-out portion will\n";
    print "use the designated interface, but the dial-in portion will use any available\n";
    print "interface.\n";
    while ($peer = pop @bidirectional) {
	delete $ {$$paths{$peer}}{interface};
	translatedialin($peer);
    }
}

translateifconfig;

# Create an /etc/ppp/options if we need to.
if (!defined($authoption) && $dialin_auth > 0) {
    local (@pppdopts);
    push @pppdopts, "lock";
    push @pppdopts, "auth" if $dialin_auth == 1;
    push @pppdopts, "noauth" if $dialin_auth > 1;
    $optfiles{$options} = \@pppdopts;
}
# Translate option files to plain text.
foreach $file (keys %optfiles) {
    local ($opts) = $optfiles{$file};
    local ($cstr) = "";
    $cstr .= shift(@$opts) . "\n" while @$opts;
    $optfiles{$file} = $cstr;
}
# Change "auth" to "noauth" or add "noauth" to /etc/ppp/options.
if (defined($authoption) && $authoption ne "noauth" && $dialin_auth == 3) {
    local(@triplet, $cstr);
    if ($authoption eq "unknown") {
	warn "Adding 'noauth' to $options\n";
    } else {
	warn "Changing 'auth' in $options to 'noauth'\n";
    }
    open(OPTIONS,"<" . $options) || die "$options disappeared: $!\n";
    while (@{$words = uucpline(OPTIONS, $options, \@triplet)}) {
	$cstr .= $triplet[0];
	if (grep(/auth/, @$words)) {
	    local(@newwords) = map { $_ = "noauth" if /auth/; $_ } @$words;
	    $cstr .= "@newwords";
	} else {
	    $cstr .= $triplet[1];
	}
	while (pop @$words) {
	    $authoption = $_ if /auth/i;
	}
	$cstr .= $triplet[2];
    }
    $cstr .= $triplet[0] . $triplet[2];
    close OPTIONS;
    $cstr .= "\n" if $cstr !~ /\n$/;
    $cstr .= "noauth\n" if $authoption eq "unknown";
    $optfiles{$options} = $cstr;
}

# Create a sed script to fix the users' shell paths.
if (0+(keys %dialinshell) != 0) {
    $cstr = "";
    foreach $peer (keys %dialinshell) {
	$cstr .= "/^$peer:/s+[^:]*/aspppls\$+$dialinshell{$peer}+\n";
    }
    $scriptfiles{$sedpasswd} = $cstr;
}

print "\nPreparing to write out translated configuration:\n";

# Enumerate the files we'll write.
$nfiles = 0;
if (0+(keys %chatfiles) != 0) {
    print "    ";
    nof 0+(keys %chatfiles), "chat file", ":\n";
    foreach $file (keys %chatfiles) {
	$nfiles++;
	print "\t$nfiles.  $file\n";
	local ($chats) = $chatfiles{$file};
	local ($cstr) = "";
	while (@$chats) {
	    $cstr .= requote(shift(@$chats));
	    $cstr .= " " . requote(shift(@$chats)) if @$chats;
	    $cstr .= "\n";
	}
	local (@filerec) = ( $file, $cstr );
	push @allfiles, \@filerec;
    }
}
if (0+(keys %optfiles) != 0) {
    print "    ";
    nof 0+(keys %optfiles), "option file", ":\n";
    foreach $file (keys %optfiles) {
	$nfiles++;
	print "\t$nfiles.  $file\n";
	local (@filerec) = ( $file, $optfiles{$file} );
	push @allfiles, \@filerec;
    }
}
if (0+(keys %scriptfiles) != 0) {
    print "    ";
    nof 0+(keys %scriptfiles), "script file", ":\n";
    foreach $file (keys %scriptfiles) {
	$nfiles++;
	print "\t$nfiles.  $file\n";
	local (@filerec) = ( $file, $scriptfiles{$file} );
	push @allfiles, \@filerec;
    }
}

# Merge new secrets needed with existing ones, if any.
sub merge_secrets
{
    local ($addsecrets, $fname) = @_;
    local ($file, $cstr, @triplet, $newsecret);

    $nfiles++;
    $file = $pppdir . $fname;
    print "\t$nfiles.  $file\n";
    if (open(SECRETS, '<' . $pppdir . $fname)) {
	while (@{$words = uucpline(SECRETS, $pppdir . $fname, \@triplet)}) {
	    $cstr .= $triplet[0];
	    $newsecret = $ {$$addsecrets{$$words[0]}}{$$words[1]};
	    if (defined $newsecret) {
		$cstr .= requote($$words[0]) . " " . requote($$words[1]) .
		  " " . $newsecret;
		delete $ {$$addsecrets{$$words[0]}}{$$words[1]};
	    } else {
		$cstr .= $triplet[1];
	    }
	    $cstr .= $triplet[2];
	}
	close SECRETS;
	$cstr .= $triplet[0] . $triplet[2];
    }
    foreach $key1 (keys (%$addsecrets)) {
	foreach $key2 (keys (%{$$addsecrets{$key1}})) {
	    $cstr .= requote($key1) . " " . requote($key2) . " " .
	      $ {$$addsecrets{$key1}}{$key2} . "\n";
	}
    }
    local (@filerec) = ( $file, $cstr );
    push @allfiles, \@filerec;
}

$nchap = 0+(keys %chapsecrets) != 0;
$npap = 0+(keys %papsecrets) != 0;
if ($nchap != 0 || $npap != 0) {
    print "    ";
    nof $nchap + $npap, "secrets file", ":\n";
    merge_secrets(\%chapsecrets, "chap-secrets") if $nchap != 0;
    merge_secrets(\%papsecrets, "pap-secrets") if $npap != 0;
}

die "Nothing to write back; I'm done.\n" if $nfiles == 0;

$PAGER = fixpath($ENV{PAGER}, "/usr/bin/less");
$EDITOR = fixpath($ENV{EDITOR}, "/usr/bin/vi");
$SHELL = fixpath($ENV{SHELL}, "/usr/bin/ksh");

END {
    if ($tempname) {
	unlink($tempname) or
	    die "Cannot remove temporary file $tempname: $!\n";
    }
}

sub show_file_options
{
    print "\nEnter option number:\n";
    print "\t1 - view contents of file on standard output\n";
    print "\t2 - view contents of file using $PAGER\n" if $PAGER ne "";
    print "\t3 - edit contents of file using $EDITOR\n" if $EDITOR ne "";
    print "\t4 - delete/undelete file from list\n";
    print "\t5 - rename file in list\n";
    print "\t6 - show file list again\n";
    print "\t7 - escape to shell (or \"!cmd\")\n";
    print "\t8 - abort without saving anything\n";
    print "\t9 - save all files and exit (default)\n";
}

# If interactive, then allow user to view and modify converted data.
if ((-t STDIN) && (-t STDOUT) && !$opt_n) {
    show_file_options();
    while (1) {
	print "Option:  ";
	chomp($ans = <STDIN>);
	if ($ans eq "?" || $ans =~ /^h/i) {
	    show_file_options();
	    next;
	}
	if ($ans eq "") {
	    last if yesno "Saving all files.  Are you sure";
	    next;
	}
	last if $ans == 9;
	print("Aborted.\n"), exit if $ans == 8;
	if ($ans =~ /^!/ || $ans == 7) {
	    if ($ans =~ /^!(.+)/) {
		system($1);
	    } else {
		print("Interactive shell access not permitted here.\n"), next
		    if $< != $>;
		system($SHELL);
	    }
	} elsif ($ans == 6) {
	    for ($i = 0; $i < $nfiles; $i++) {
		print "\t", $i+1, ".  $allfiles[$i][0]",
		    ($deleted[$i] ? "   (deleted)" : ""), "\n";
	    }
	} elsif ($ans > 0 && $ans < 6) {
	    $fnum = 0;
	    if ($nfiles > 1) {
		print "File number (1 .. $nfiles):  ";
		chomp($fnum = <STDIN>);
		if ($fnum < 1 || $fnum > $nfiles) {
		    print "Unknown file (must be 1 to $nfiles).\n";
		    next;
		}
		$fnum--;
	    }
	    if ($ans == 5) {
		print "Current name is $allfiles[$fnum][0]\n";
		print "New name:  ";
		chomp($fname = <STDIN>);
		print("Unchanged\n"), next if $fname eq "";
		$allfiles[$fnum][0] = $fname;
	    }
	    if ($deleted[$fnum]) {
		if (yesno("File " . $fnum+1 .
		   " ($allfiles[$fnum][0]) is deleted; undelete",1)) {
		    undef $deleted[$fnum];
		}
		next;
	    }
	    if ($ans == 1) {
		print $allfiles[$fnum][1];
	    } elsif ($ans == 2 && $PAGER ne "") {
		$i = 0;
		do {
		    if (++$i > 5) {
			warn "Unable to open temporary file: $!";
			undef $tempname;
			last;
		    }
		    $tempname = tmpnam();
		} until sysopen(FH, $tempname, O_RDWR|O_CREAT|O_EXCL);
		next if !$tempname;
		print FH $allfiles[$fnum][1];
		close FH;
		system($PAGER, $tempname);
		unlink($tempname) ||
		    warn "Trouble removing temporary file: $!";
		undef $tempname;
	    } elsif ($ans == 3 && $EDITOR ne "") {
		$i = 0;
		do {
		    if (++$i > 5) {
			warn "Unable to open temporary file: $!";
			undef $tempname;
			last;
		    }
		    $tempname = tmpnam();
		} until sysopen(FH, $tempname, O_RDWR|O_CREAT|O_EXCL);
		next if !$tempname;
		chown $<, $(, $tempname;
		print FH $allfiles[$fnum][1];
		close FH;
		$i = system($EDITOR, $tempname);
		if ($i == 0) {
		    if (open FH, "<" . $tempname) {
			read FH, $allfiles[$fnum][1], (-s $tempname);
			close FH;
		    }
		} else {
		    print "Editor dropped core.\n" if $? & 128;
		    print "Editor terminated on signal ", $? & 127, "\n"
			if $? & 127;
		    print "Editor returned error ", $? >> 8, "\n"
			if $? >> 8;
		}
		unlink($tempname) ||
		    warn "Trouble removing temporary file: $!";
		undef $tempname;
	    } elsif ($ans == 4) {
		$deleted[$fnum] = 1;
	    }
	}
    }
}

print "\n";

# Interactive part is over.  Become real.
$( = $);
$< = $>;

print "Stopping aspppd\n" if $opt_v;
system($asctl, "stop") if -x $asctl;

print "Saving all files\n" if $opt_v;
for ($i = 0; $i < $nfiles; $i++) {
    $filerec = $allfiles[$i];
    if ($deleted[$i]) {
	delete $scriptfiles{$$filerec[0]};
	next;
    }
    print "Saving $$filerec[0]\n" if $opt_v;
    $$filerec[0] =~ m+(.*)/+;
    if ($1 eq "") {
	# this is ok; just a top level file
    } elsif (!(-d $1)) {
	local ($exdir) = $1;
	while ($exdir && !(-d $exdir)) {
	    $exdir =~ m+(.*)/+;
	    $exdir = $1;
	}
	if ($exdir) {
	    local ($dir) = $1;
	    $dir =~ m+$exdir/([^/]*)(.*)+;
	    local ($tomake, $rest) = ($1, $2);
	    mkdir $exdir . "/" . $tomake, 0775;
	    if ($! == ENOSYS) {
		warn "Unable to make directory $exdir/$tomake; automount point.\n";
		next;
	    }
	    if ($! != 0) {
		warn "Unable to make directory $exdir/$tomake: $!\n";
		next;
	    }
	    if (system("mkdir", "-p", $dir) != 0) {
		warn "Failed to make $dir\n";
		next;
	    }
	} else {
	    warn "$1 doesn't appear to have a useful path.\n";
	    next;
	}
    }
    undef $fileerr;
    local ($fname) = $$filerec[0];
    if (-e $fname && !$overwrite{$chatfile}) {
	print "$fname already exists.\n"
	  if (-t STDIN) && (-t STDOUT) && !$opt_n;
	if (!yesno("Should it be overwritten",$opt_y)) {
	    warn "Using $fname as-is; it may not be correct.\n";
	    next;
	}
    }
    if (sysopen(OUTFILE, $$filerec[0], O_WRONLY|O_CREAT|O_TRUNC, 0600)) {
	print OUTFILE $$filerec[1] || ($fileerr = $!);
	close OUTFILE || ($fileerr = $!);
    } else {
	$fileerr = $!;
    }
    warn "Unable to write $$filerec[0]: $fileerr\n" if $fileerr;
}

local(@scripts) = keys %scriptfiles;
if (@scripts) {
    print "Making scripts executable\n" if $opt_v;
    system("chmod", "u+x", @scripts);
}

rewrite_passwd if exists($scriptfiles{$sedpasswd});

# clean up after a previous translation.
unlink $pppdir . "ifconfig" if !$scriptfiles{$pppdir . "ifconfig"};
unlink $pppdir . "demand" if !$scriptfiles{$pppdir . "demand"};

(rename($asfile, $asmoved) || warn "Cannot move $asfile: $!\n")
  if $aspppcf ne $astemp;

system($pppdctl, "start") if -x $pppdctl;

# use Dumpvalue;
# my $dumper = new Dumpvalue;
# $dumper->set(globPrint => 1);
# $dumper->dumpValue($ifconfig);