OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / pptp / pptpsetup
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Long;
5
6 use vars qw($VERSION);
7 $VERSION = '0.03';
8
9 # Command-line parameters:
10
11 # actions
12 my ( $_create, $_delete, $_help );
13
14 # values
15 my ( $SERVER, $DOMAIN, $USERNAME, $PASSWORD, $ENCRYPT, $START );
16
17 my $result = GetOptions(
18     "create|c=s"   => \$_create,    # --create foo -> &create("foo")
19     "delete=s"     => \$_delete,    # --delete foo -> &delete("foo")
20     "help|h"       => \$_help,      # --help       -> &help()
21     "server|s=s"   => \$SERVER,
22     "domain|d=s"   => \$DOMAIN,
23     "username|u=s" => \$USERNAME,
24     "password|p=s" => \$PASSWORD,
25     "encrypt|e"    => \$ENCRYPT,
26     "start"        => \$START,
27 );
28
29 if ($_create) {
30     &create($_create);
31 } elsif ($_delete) {
32     &delete($_delete);
33 } elsif ($_help) {
34     &help();
35 } else {
36     die "$0: too few arguments.\nTry '$0 --help' for more information.\n";
37 }
38
39 exit;
40
41 ####
42
43 sub create {
44     my $TUNNEL = shift;
45
46     # system checking
47     &Check_MPPE_in_kernel
48         or die "$0: couldn't find MPPE support in kernel.\n";
49
50     &Check_MPPE_in_pppd
51         or die "$0: couldn't find MPPE support in pppd.\n";
52
53     # input validation
54     ($TUNNEL) = $TUNNEL =~ m{^(\w+)$}
55       or die "$0: invalid tunnel name.\nTry '$0 --help' for more information.\n";
56     ($SERVER) = $SERVER =~ m{^(.+)$}
57       or die "$0: invalid server.\nTry '$0 --help' for more information.\n";
58     ($USERNAME) = $USERNAME =~ m{^(.+)$}
59       or die "$0: invalid username.\nTry '$0 --help' for more information.\n";
60
61     # ask password
62     if ( !$PASSWORD ) {
63         print "Password: ";
64         $PASSWORD = <STDIN>;
65         chomp $PASSWORD;
66     }
67
68     # put '\' between domain and username IF specified a domain
69     $DOMAIN = "$DOMAIN\\" if $DOMAIN;
70
71     # create or add lines to the /etc/ppp/chap-secrets file,
72     # which holds usernames and passwords
73     my $chap_secrets_file = '/etc/ppp/chap-secrets';
74     open( FILE, ">>$chap_secrets_file" )
75       or die "$0: can't write to '$chap_secrets_file': $!\n";
76
77     print FILE "\n# added by pptpsetup for $TUNNEL\n$DOMAIN$USERNAME $TUNNEL \"$PASSWORD\" *\n";
78
79     close FILE;
80
81     # create a /etc/ppp/peers/$TUNNEL file
82     my $tunnel_file = "/etc/ppp/peers/$TUNNEL";
83     open( FILE, ">$tunnel_file" )
84       or die "$0: can't write to '$tunnel_file': $!\n";
85
86     print FILE <<"TUNNEL";
87 # written by pptpsetup
88 pty "pptp $SERVER --nolaunchpppd"
89 lock
90 noauth
91 nobsdcomp
92 nodeflate
93 name $DOMAIN$USERNAME
94 remotename $TUNNEL
95 ipparam $TUNNEL
96 TUNNEL
97
98     print FILE "require-mppe-128\n" if $ENCRYPT;
99
100     close FILE;
101
102     # start tunneling
103     if ($START) {
104         system("pppd call $TUNNEL updetach");
105     }
106 }
107
108 ####
109
110 sub help {
111     print <<'EOF';
112 pptpsetup --create <TUNNEL> --server <SERVER> [--domain <DOMAIN>]
113           --username <USERNAME> [--password <PASSWORD>]
114           [--encrypt] [--start]
115
116 pptpsetup --delete <TUNNEL> 
117
118 Options:
119
120 * the name you wish to use to refer to the tunnel (you choose it),
121 * the IP address or host name of the server,
122 * the authentication domain name (optional),
123 * the username you are to use,
124 * the password you are to use,
125 * whether encryption is required,
126 * whether to start the connection after configuration.
127
128 pptpsetup - Point-to-Point Tunneling Protocol setup
129
130 Copyright (C) 2006 Nelson Ferraz
131
132 pptpsetup comes with ABSOLUTELY NO WARRANTY; for details see source.
133 This is free software, and you are welcome to redistribute it
134 under certain conditions; see source for details.
135
136 EOF
137
138     exit;
139 }
140
141 ####
142
143 sub delete {
144     my $tunnel = shift;
145
146     # input validation
147     ($tunnel) = $tunnel =~ m{^(\w+)$}
148       or die "$0: invalid tunnel name.\nTry '$0 --help' for more information.\n";
149
150     # delete tunnel file
151     my $tunnel_file = "/etc/ppp/peers/$tunnel";
152     unlink $tunnel_file
153         or die "$0: can't delete '$tunnel_file': $!\n";
154
155     # delete entry from chap-secrets
156     my $chap_file = '/etc/ppp/chap-secrets';
157
158     open( FILE, $chap_file )
159         or die "$0: can't read '$chap_file': $!\n";
160     my @chap = <FILE>;
161     close FILE;
162
163     my $new_chap = '';
164     foreach (@chap) {
165         $new_chap .= $_ unless /\b$tunnel\b/;
166     }
167
168     # backup
169     rename( $chap_file, "$chap_file.bkp" );
170
171     # write new chap-secrets
172     open( FILE, ">$chap_file" )
173         or die "$0: can't write '$chap_file': $!\n";
174     print FILE $new_chap;
175     close FILE;
176
177     exit;
178 }
179
180 ### AUXILIAR SUBS ###
181
182 sub Check_MPPE_in_kernel {
183     my $command = q/modprobe ppp-compress-18/;
184     if (system( $command ) == 0) {
185         # no error!
186         return 1;
187     } else {
188         return 0;
189     }
190 }
191
192 sub Check_MPPE_in_pppd {
193     my $command = q/strings `which pppd`|grep -i mppe|wc --lines/;
194     my $answer  = `$command`;
195     if ($answer > 0) {
196         # ok!
197         return 1;
198     } else {
199         return 0;
200     }
201 }
202
203 __END__
204
205 =head1 NAME
206
207 pptpsetup - Point-to-Point Tunneling Protocol setup
208
209 =head1 SYNOPSIS
210
211     pptpsetup --create <TUNNEL> --server <SERVER> [--domain <DOMAIN>]
212               --username <USERNAME> [--password <PASSWORD>]
213               [--encrypt] [--start]
214
215     pptpsetup --delete <TUNNEL>
216
217 =head1 DESCRIPTION
218
219 PPTP Client is a Linux, FreeBSD, NetBSD and OpenBSD client for the 
220 proprietary Microsoft Point-to-Point Tunneling Protocol, PPTP. 
221
222 This script configures PPTP Client on Linux.
223
224 =head1 OPTIONS
225
226 =over 16
227
228 =item --create TUNNEL
229
230 create a tunnel named TUNNEL
231
232 =item --delete TUNNEL
233
234 delete the file /etc/ppp/peers/TUNNEL and any lines from 
235 /etc/ppp/chap-secrets that contains "TUNNEL" as a single word
236
237 =item --server SERVER
238
239 the IP address or host name of the server
240
241 =item --domain DOMAIN
242
243 the authentication domain name (optional)
244
245 =item --username USERNAME
246
247 the username you are to use
248
249 =item --password PASSWORD
250
251 the password you are to use. If you don't specify a password, 
252 pptpsetup will ask for one.
253
254 =item --encrypt
255
256 whether encryption is required
257
258 =item --start
259
260 whether the connection should be started after configuration.
261
262 =back
263
264 =head1 AUTHOR
265
266 Nelson Ferraz <nferraz at gmail.com>,
267 based on James Cameron's PPTP Client Debian HOWTO.
268
269 =head1 SEE ALSO
270
271 =over 16
272
273 =item PPTP Client Debian HOWTO
274
275 http://pptpclient.sourceforge.net/howto-debian.phtml
276
277 =item PPTP Client Diagnosis HOWTO
278
279 http://pptpclient.sourceforge.net/howto-diagnosis.phtml
280
281 =back
282
283 =head1 COPYRIGHT
284
285 pptpsetup - Point-to-Point Tunneling Protocol setup
286
287 Copyright (C) 2006 Nelson Ferraz
288
289 This program is free software; you can redistribute it and/or modify
290 it under the terms of the GNU General Public License as published by
291 the Free Software Foundation; either version 2 of the License, or
292 (at your option) any later version.
293
294 This program is distributed in the hope that it will be useful,
295 but WITHOUT ANY WARRANTY; without even the implied warranty of
296 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
297 GNU General Public License for more details.
298
299 You should have received a copy of the GNU General Public License
300 along with this program; if not, write to the Free Software
301 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA