Report abuse


			
#!/usr/bin/perl -w

use strict;

# ³Æ¼ïÀßÄê¥Õ¥¡¥¤¥ë¤Î¾ì½ê
use constant SHAREINFO => '/etc/melco/shareinfo';
use constant RSYNCD_CONF => '/etc/rsyncd.conf';
use constant RSYNCD_SECRET => '/etc/rsyncd.secret';
use constant HOSTS_ALLOW => '/etc/hosts.allow';

# rsync¤òưºî¤µ¤»¤ë¥æ¡¼¥¶¡¼¤È¥°¥ë¡¼¥×
use constant RSYNCD_UID => 'root';
use constant RSYNCD_GID => 'root';

my $hosts_allow = "rsync: ALL\n";

sub main();
sub conf_global($);
sub conf_share($$$);
sub url_encode($);

main();
exit;

sub main() {
	my @conf = ();
	my @secret = ();
	my $info;

	# ¥°¥í¡¼¥Ð¥ë¥»¥¯¥·¥ç¥ó
	conf_global(\@conf);

	# shareinfo ¤òÆÉ¤ó¤ÇÀßÄê¥Õ¥¡¥¤¥ë¤ÎÆâÍÆ¤òºîÀ®
	open(FILE, '<', SHAREINFO) || die SHAREINFO . ": $!\n";
	while($info = ) {
		conf_share($info, \@conf, \@secret);
	}
	close(FILE);

	# Generate our shares
	open(FILE, '<', SHAREINFO) || die SHAREINFO . ": $!\n";
	while($info = ) {
		conf_our_shares($info, \@conf, \@secret);
	}
	close(FILE);

	# rsyncd.conf ¤Î½ñ¤­¹þ¤ß
	open(FILE, '>', RSYNCD_CONF) || die RSYNCD_CONF . ": $!\n";
	foreach(@conf) {
		print FILE "$_\n";
	}
	#print FILE "\n" . $rsync_conf;
	close(FILE);
	chmod(0644, RSYNCD_CONF);

	#  So that rsync works we need to configure the hosts.allow
	open(FILE, '>', HOSTS_ALLOW) || die HOSTS_ALLOW . ": $!\n";
	print FILE $hosts_allow;
	close(FILE);
	chmod(0644, HOSTS_ALLOW);


	# rsyncd.secret ¤Î½ñ¤­¹þ¤ß
	open(FILE, '>', RSYNCD_SECRET) || die RSYNCD_SECRET . ": $!\n";
	foreach(@secret) {
		print FILE "$_\n";
	}
	close(FILE);
	chmod(0600, RSYNCD_SECRET);
}

sub conf_global($) {
	# rsyncd.conf ¤Î¥°¥í¡¼¥Ð¥ë¥»¥¯¥·¥ç¥ó¤òÀßÄê
	my $conf = shift;

	# rsync¤¬Æ°ºî¤¹¤ë¥æ¡¼¥¶¡¼¤È¥°¥ë¡¼¥×
	push(@$conf, "uid = " . RSYNCD_UID);
	push(@$conf, "gid = " . RSYNCD_GID);

	# chroot ¤ò»ÈÍѤ¹¤ë
	push(@$conf, "use chroot = yes");
}

sub conf_share($$$) {
	# shareinfo ¤Î¥¨¥ó¥È¥ê¤Ò¤È¤Ä¤ò½èÍý
	my ($info, $conf, $secret) = @_;

	# ʸ»úÎó¤ò¼¡¤Î16¹àÌܤËʬ²ò
	# name<>drive<>comment<>rwuser<>rwgroup<>rouser<>rogroup<>
	# win<>mac<>ftp<>backup<>backuppwd<>readonly<>trash<>quota<>quotasize
	my @data = split(/<>/, $info);
	return if @data != 16;

	# ¥Ð¥Ã¥¯¥¢¥Ã¥×¥Õ¥é¥°¤¬Î©¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð²¿¤â¤·¤Ê¤¤
	my ($name, $drive, $backup, $backuppwd) = ($data[0], $data[1], $data[10], $data[11]);
	return if $backup ne '1';

	# rsync¤Î¥â¥¸¥å¡¼¥ë̾: ¥É¥é¥¤¥Ö¤È¶¦Í­Ì¾¤òURL¥¨¥ó¥³¡¼¥É¤·¤¿¤â¤Î¤ò·ë¹ç
	my $module = $drive . '_' . url_encode($name);

	push(@$conf, '');

	# rsync¤Î¥â¥¸¥å¡¼¥ë̾
	push(@$conf, "[$module]");

	# ¥Ñ¥¹Ì¾: SJIS¤Ç½ñ¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤¬¡¢2¥Ð¥¤¥ÈÌܤ¬ '\' ¤Ë¤Ê¤Ã¤Æ¤¤¤ë
	# SJISʸ»ú¤¬ºÇ¸å¤Ë¤¢¤ë¤È¡¢¼¡¤Î¹Ô¤Ë¤Ä¤Ê¤¬¤Ã¤Æ¤¤¤ë¤È²ò¼á¤µ¤ì¤Æ¤·¤Þ¤¦¤Î¤Ç¡¢
	# "/." ¤òÄɲ䷤Ƥ½¤ì¤òËɤ°
	push(@$conf, "  path = /mnt/$drive/$name/.");

	# ¥ê¡¼¥É¥ª¥ó¥ê¡¼¥Õ¥é¥°: ½ñ¤­¹þ¤ß²Äǽ¤Ë¤¹¤ë
	push(@$conf, "  read only = no");

	# ¥Ñ¥¹¥ï¡¼¥É¤¬ÀßÄꤵ¤ì¤Æ¤¤¤¿¤é°Ê²¼¤Î¹àÌܤòÄɲÃ
	# ¤³¤ì¤é¤¬¤Ê¤±¤ì¤Ð¡¢rsync¤Ïǧ¾Ú¤Ê¤·¤ÇÀܳ²Äǽ¤È¤·¤Æ½èÍý¤¹¤ë
	if($backuppwd ne '') {
		# »ÈÍѵö²Ä¥æ¡¼¥¶¡¼: ¥â¥¸¥å¡¼¥ë̾¤ÈƱ¤¸Ê¸»úÎó¤ò¥æ¡¼¥¶¡¼Ì¾¤È¤·¤Æ»ÈÍѤ¹¤ë
		push(@$conf, "  auth users = $module");

		# ¥Ñ¥¹¥ï¡¼¥É¥Õ¥¡¥¤¥ë: ¤¹¤Ù¤Æ¤Î¥â¥¸¥å¡¼¥ë¤ÇƱ¤¸¥Õ¥¡¥¤¥ë¤ò»ÈÍѤ¹¤ë¤¬
		# auth users ¤ÎÀßÄê¤È¤ÎÁȤ߹ç¤ï¤»¤Ç¡¢¥â¥¸¥å¡¼¥ë̾¤ÈƱ¤¸¥æ¡¼¥¶¡¼Ì¾¤ò
		# »È¤Ã¤¿¤È¤­¤À¤±»ÈÍѤǤ­¤ë¤³¤È¤Ë¤Ê¤ë
		push(@$conf, "  secrets file = " . RSYNCD_SECRET);

		# ¥Ñ¥¹¥ï¡¼¥É¥Õ¥¡¥¤¥ë¤Ø¤Î¥¨¥ó¥È¥ê¤ÎÄɲÃ
		push(@$secret, "$module:$backuppwd");
	}
}

# a little different from the original since it wont check the backup flags
# will add all shares to the conf array
sub conf_our_shares($$$) {
	# shareinfo ¤Î¥¨¥ó¥È¥ê¤Ò¤È¤Ä¤ò½èÍý
	my ($info, $conf, $secret) = @_;

	# name<>drive<>comment<>rwuser<>rwgroup<>rouser<>rogroup<>
	# win<>mac<>ftp<>backup<>backuppwd<>readonly<>trash<>quota<>quotasize
	my @data = split(/<>/, $info);
	return if @data != 16;

	# ¥Ð¥Ã¥¯¥¢¥Ã¥×¥Õ¥é¥°¤¬Î©¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð²¿¤â¤·¤Ê¤¤
	my ($name, $drive, $comment,$backuppwd, $read_only) = ($data[0], $data[1], $data[2], $data[11], $data[12]);

	# rsync¤Î¥â¥¸¥å¡¼¥ë̾: ¥É¥é¥¤¥Ö¤È¶¦Í­Ì¾¤òURL¥¨¥ó¥³¡¼¥É¤·¤¿¤â¤Î¤ò·ë¹ç
	my $module = url_encode($name);

	push(@$conf, '');

	# rsync¤Î¥â¥¸¥å¡¼¥ë̾
	push(@$conf, "[$module]");

	# mount point	
	push(@$conf, "  path = /mnt/$drive/$name/.");
	push(@$conf, "  comment = $comment");
	if( $read_only ) {
		push(@$conf, "  read only = yes");
	} else {
		push(@$conf, "  read only = no");
	}

	# Set back passwords if they are configured
	if($backuppwd ne '') {

		push(@$conf, "  auth users = $module");
		push(@$conf, "  secrets file = " . RSYNCD_SECRET);

		# Store backup password for this module
		push(@$secret, "$module:$backuppwd");
	}
}


sub url_encode($) {
	# URL¥¨¥ó¥³¡¼¥É¤â¤É¤­
	# ±Ñ¿ô»ú°Ê³°¤Îʸ»ú¤Ï "%xx" ¤Î¤è¤¦¤Êʸ»ú¥³¡¼¥É¤Ç¤Îɽµ­¤ËÊÑ´¹
	# ËÜÍè¤ÎURL¥¨¥ó¥³¡¼¥É¤Ç¤ÏÊÑ´¹¤·¤Ê¤¯¤Æ¤â¤è¤¤µ­¹æ¤â¤¢¤ë¤¬¡¢
	# ¤³¤³¤Ç¤Ï°ÂÁ´À­½Å»ë¤Çµ­¹æÁ´¤Æ¤òÊÑ´¹
	my $str = shift;
	$str =~ s/([^0-9A-Za-z])/sprintf("%%%02x",ord($1))/ge;
	return $str;
}