/
bric_ftpd
executable file
·194 lines (140 loc) · 5.08 KB
/
bric_ftpd
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
#!/usr/bin/perl -w
=head1 Name
bric_ftpd - Bricolage virtual FTP server
=head1 Synopsis
su nobody -c "bric_ftpd -S"
=head1 Description
This program controls the Bricolage vitual FTP server, which can be used to
allow access to Bricolage templates. This may provide a more comfortable
development environment for programmers working on templates. Many editors
provide FTP modes that make editing files via FTP almost as easy as editing
files locally (ange-ftp for Emacs, for example).
There are security risks to consider - FTP sends passwords unencrypted over
the network. If you are running Bricolage behind a firewall this might not be
a problem.
To enable the FTP server you'll need to install the Net::FTPServer Perl
module. After that you can configure the FTP server with the following
F<bricolage.conf> directives:
=over
=item *
B<ENABLE_FTP_SERVER:> Set this to 1 if you want to use the FTP
server. Defaults to 0.
=item *
B<FTP_PORT:> The port that the FTP server will run on. Users will need to know
this port to connect to the FTP server. Since you won't be running the server
as root you'll need to choose a port above 1024. Defaults to 2121.
=item *
B<FTP_ADDRESS:> Normally the FTP server will bind to all the available IP
addresses for your machine. If you set this variable then the server will only
bind to the specified address.
=item *
B<FTP_LOG:> Set this to the file where you want to FTP logs written
to. Defaults to C<$BRICOLAGE_ROOT/ftp.log> if not set.
=item *
B<FTP_PID_FILE:> Set this to the file where you want to the FTP daemon PID
stored. Defaults to C<$BRICOLAGE_ROOT/ftp.pid> if not set.
=item *
B<FTP_DEBUG:> If you set this to 1 you'll see a lot of useful tracing
information in the FTP logfile. This can help diagnose errors but is probably
only of interest to developers. Defaults to 0.
=back
Next you'll need to start the FTP server as the same user that Apache runs as.
For example, if you run Apache as "nobody" you would use this command to start
the FTP server:
su nobody -c "bric_ftpd -S"
The C<-S> option to ftpd runs the FTP server in the background. For other
possibilities see the documentation for Net::FTPServer.
Once the server is up and running connect to it with an FTP client and start
editing templates. For more information see
L<Bric::Util::FTP::Server|Bric::Util::FTP::Server>.
To kill the FTP server in daemon mode, use the C<-k> option:
su nobody -c bric_ftpd -k"
=head1 Author
Sam Tregar <stregar@about-inc.com>
=head1 See Also
L<Bric::Admin|Bric::Admin>, L<Net::FTPServer|Net::FTPServer>,
L<Bric::Util::FTP::Server|Bric::Util::FTP::Server>.
=cut
use strict;
use File::Spec::Functions qw(catdir);
BEGIN {
# $BRICOLAGE_ROOT defaults to /usr/local/bricolage
$ENV{BRICOLAGE_ROOT} ||= "/usr/local/bricolage";
# use $BRICOLAGE_ROOT/lib if exists
my $lib = catdir($ENV{BRICOLAGE_ROOT}, "lib");
if (-e $lib) {
$ENV{PERL5LIB} = defined $ENV{PERL5LIB} ?
"$ENV{PERL5LIB}:$lib" : $lib;
unshift @INC, $lib;
}
# make sure Bric is found
eval { require Bric };
die <<"END" if $@;
######################################################################
Cannot load Bricolage libraries. Please set the environment
variable BRICOLAGE_ROOT to the location of your Bricolage
installation or set the environment variable PERL5LIB to the
directory where Bricolage's libraries are installed.
The specific error encountered was as follows:
$@
######################################################################
END
}
use Bric::Config qw(:ftp :sys_user);
if ($> == 0) {
# Switch from root to the system user (set in bricolage.conf).
$) = SYS_GROUP;
die "Unable to set effective group id ". SYS_GROUP . "\n"
unless $) == SYS_GROUP;
$> = SYS_USER;
die "Unable to set effective user id ". SYS_USER . "\n"
unless $> == SYS_USER;
}
# Kill the server if -k is passed.
if (grep { $_ eq '-k' } @ARGV) {
unless (-e FTP_PID_FILE) {
print "\nCannot find ", FTP_PID_FILE, "; no bric_ftpd running?\n\n";
exit 1
}
open PID, FTP_PID_FILE or die "Cannot open ", FTP_PID_FILE, ": $!\n";
my $pid = <PID>;
chomp $pid;
close PID;
system "kill", $pid and exit;
unlink FTP_PID_FILE;
exit;
}
# make sure FTP is enabled
BEGIN {
unless (ENABLE_FTP_SERVER) {
print "\nENABLE_FTP_SERVER is off! Please read Bric::Admin " .
"for instructions on how to setup the FTP server.\n";
exit 1;
}
}
# Make sure there isn't a server running already.
if (-e FTP_PID_FILE) {
print "\nFile '", FTP_PID_FILE,
"' exits. Is another bric_ftpd already running?\n";
exit 1;
}
# load the server
use Bric::Util::FTP::Server;
# setup parameters
# no conf file used
push(@ARGV, '-C', '/dev/null');
# set port and address if specified
push(@ARGV, '-p', FTP_PORT);
if (FTP_ADDRESS() ne "") {
push(@ARGV, '-o', 'local address=' . FTP_ADDRESS);
}
# setup log and PID file.
push @ARGV,
'-o', 'xfer logging=' . FTP_LOG,
'-o', 'error log=' . FTP_LOG,
'-P', FTP_PID_FILE;
# set debug mode
if (FTP_DEBUG) {
push(@ARGV, '-d');
}
Bric::Util::FTP::Server->run;