Add signing and overwrite fixes
[webmin.git] / makemodulerpm.pl
1 #!/usr/bin/perl
2 # makemodulerpm.pl
3 # Create an RPM for a webmin or usermin module or theme
4
5 $target_dir = "/tmp";   # where to copy the RPM to
6
7 if (-d "/usr/src/OpenLinux") {
8         $basedir = "/usr/src/OpenLinux";
9         }
10 else {
11         $basedir = "/usr/src/redhat";
12         }
13 $licence = "Freeware";
14 $release = 1;
15 $< = $>;                # If running setuid
16 $ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin";
17 $allow_overwrite = 0;
18
19 # Parse command-line args
20 while(@ARGV) {
21         local $a = &untaint(shift(@ARGV));
22         if ($a eq "--force-theme") {
23                 $force_theme = 1;
24                 }
25         elsif ($a eq "--rpm-dir") {
26                 $basedir = &untaint(shift(@ARGV));
27                 }
28         elsif ($a eq "--licence" || $a eq "--license") {
29                 $licence = &untaint(shift(@ARGV));
30                 }
31         elsif ($a eq "--rpm-depends") {
32                 $rpmdepends = 1;
33                 }
34         elsif ($a eq "--no-prefix") {
35                 $no_prefix = 1;
36                 }
37         elsif ($a eq "--vendor") {
38                 $vendor = &untaint(shift(@ARGV));
39                 }
40         elsif ($a eq "--provides") {
41                 $provides = &untaint(shift(@ARGV));
42                 }
43         elsif ($a eq "--url") {
44                 $url = shift(@ARGV);
45                 }
46         elsif ($a eq "--release") {
47                 $release = &untaint(shift(@ARGV));
48                 }
49         elsif ($a eq "--usermin") {
50                 $force_usermin = 1;
51                 }
52         elsif ($a eq "--target-dir") {
53                 $target_dir = &untaint(shift(@ARGV));
54                 }
55         elsif ($a eq "--dir") {
56                 $final_mod = &untaint(shift(@ARGV));
57                 }
58         elsif ($a eq "--requires") {
59                 push(@extrareqs, shift(@ARGV));
60                 }
61         elsif ($a eq "--allow-overwrite") {
62                 $allow_overwrite = 1;
63                 }
64         elsif ($a eq "--sign") {
65                 $sign = 1;
66                 }
67         elsif ($a =~ /^\-\-/) {
68                 print STDERR "Unknown option $a\n";
69                 exit(1);
70                 }
71         else {
72                 if (!defined($dir)) {
73                         $dir = $a;
74                         }
75                 else {
76                         $ver = $a;
77                         }
78                 }
79         }
80
81 # Validate args
82 if (!$dir) {
83         print STDERR "usage: makemodulerpm.pl [--force-theme]\n";
84         print STDERR "                        [--rpm-dir directory]\n";
85         print STDERR "                        [--rpm-depends]\n";
86         print STDERR "                        [--no-prefix]\n";
87         print STDERR "                        [--vendor name]\n";
88         print STDERR "                        [--licence name]\n";
89         print STDERR "                        [--url url]\n";
90         print STDERR "                        [--provides provides]\n";
91         print STDERR "                        [--usermin]\n";
92         print STDERR "                        [--release number]\n";
93         print STDERR "                        [--target-dir directory]\n";
94         print STDERR "                        [--dir directory-in-package]\n";
95         print STDERR "                        [--allow-overwrite]\n";
96         print STDERR "                        <module> [version]\n";
97         exit(1);
98         }
99 chop($par = `/usr/bin/dirname $dir`);
100 $par = &untaint($par);
101 chop($source_mod = `/bin/basename $dir`);
102 $source_mod = &untaint($source_mod);
103 $source_dir = "$par/$source_mod";
104 $mod = $final_mod || $source_mod;
105 if (!-d $basedir) {
106         die "RPM directory $basedir does not exist";
107         }
108 if ($mod eq "." || $mod eq "..") {
109         die "directory must be an actual directory (module) name, not \"$mod\"";
110         }
111 $spec_dir = "$basedir/SPECS";
112 $rpm_source_dir = "$basedir/SOURCES";
113 $rpm_dir = "$basedir/RPMS/noarch";
114 $source_rpm_dir = "$basedir/SRPMS";
115 if (!-d $spec_dir || !-d $rpm_source_dir || !-d $rpm_dir) {
116         die "RPM directory $basedir is not valid";
117         }
118
119 # Is this actually a module or theme directory?
120 -d $source_dir || die "$dir is not a directory";
121 if (&read_file("$source_dir/module.info", \%minfo) && $minfo{'desc'}) {
122         $depends = join(" ", map { s/\/[0-9\.]+//; $_ }
123                                 grep { !/^[0-9\.]+$/ }
124                                   split(/\s+/, $minfo{'depends'}));
125         if ($minfo{'usermin'} && (!$minfo{'webmin'} || $force_usermin)) {
126                 $prefix = "usm-";
127                 $desc = "Usermin module for '$minfo{'desc'}'";
128                 $prog = "usermin";
129                 }
130         else {
131                 $prefix = "wbm-";
132                 $desc = "Webmin module for '$minfo{'desc'}'";
133                 $prog = "webmin";
134                 }
135         $iver = $minfo{'version'};
136         $post_config = 1;
137         }
138 elsif (&read_file("$source_dir/theme.info", \%tinfo) && $tinfo{'desc'}) {
139         if ($tinfo{'usermin'} && (!$tinfo{'usermin'} || $force_usermin)) {
140                 $prefix = "ust-";
141                 $desc = "Usermin theme '$tinfo{'desc'}'";
142                 $prog = "usermin";
143                 }
144         else {
145                 $prefix = "wbt-";
146                 $desc = "Webmin theme '$tinfo{'desc'}'";
147                 $prog = "webmin";
148                 }
149         $iver = $tinfo{'version'};
150         $istheme = 1;
151         $post_config = 0;
152         }
153 else {
154         die "$source_dir does not appear to be a webmin module or theme";
155         }
156 $prefix = "" if ($no_prefix);
157 $ucprog = ucfirst($prog);
158 $ver ||= $iver;         # Use module.info version, or 1
159 $ver ||= 1;
160
161 # Copy the directory to a temp location for tarring
162 system("/bin/mkdir -p /tmp/makemodulerpm");
163 system("cd $par && /bin/cp -rpL $source_mod /tmp/makemodulerpm/$mod");
164 system("/usr/bin/find /tmp/makemodulerpm -name .svn | xargs rm -rf");
165 system("/usr/bin/find /tmp/makemodulerpm -name .xvpics | xargs rm -rf");
166 system("/usr/bin/find /tmp/makemodulerpm -name '*.bak' | xargs rm -rf");
167 system("/usr/bin/find /tmp/makemodulerpm -name '*~' | xargs rm -rf");
168 system("/usr/bin/find /tmp/makemodulerpm -name '*.rej' | xargs rm -rf");
169 system("/usr/bin/find /tmp/makemodulerpm -name core | xargs rm -rf");
170 system("/bin/chown -R root:bin /tmp/makemodulerpm/$mod");
171
172 # Tar up the directory
173 system("cd /tmp/makemodulerpm && tar czhf $rpm_source_dir/$mod.tar.gz $mod");
174 system("/bin/rm -rf /tmp/makemodulerpm");
175
176 # Build list of dependencies on other RPMs, for inclusion as an RPM
177 # Requires: header
178 if ($rpmdepends) {
179         foreach $d (split(/\s+/, $minfo{'depends'})) {
180                 local ($dwebmin, $dmod, $dver);
181                 if ($d =~ /^[0-9\.]+$/) {
182                         # Depends on a version of Webmin
183                         $dwebmin = $d;
184                         }
185                 elsif ($d =~ /^(\S+)\/([0-9\.]+)$/) {
186                         # Depends on some version of a module
187                         $dmod = $1;
188                         $dver = $2;
189                         }
190                 else {
191                         # Depends on any version of a module
192                         $dmod = $d;
193                         }
194
195                 # If the module is part of Webmin, we don't need to depend on it
196                 if ($dmod) {
197                         local %dinfo;
198                         &read_file("$dmod/module.info", \%dinfo);
199                         next if ($dinfo{'longdesc'});
200                         }
201                 push(@rdeps, $dwebmin ? ("webmin", ">=", $dwebmin) :
202                              $dver ? ($prefix.$dmod, ">=", $dver) :
203                                      ($prefix.$dmod));
204                 }
205         }
206 $rdeps = join(" ", @rdeps, @extrareqs);
207
208 # Create the SPEC file
209 $providesheader = $provides ? "Provides: $provides" : undef;
210 $vendorheader = $vendor ? "Vendor: $vendor" : undef;
211 $urlheader = $url ? "URL: $url" : undef;
212 open(SPEC, ">$spec_dir/$prefix$mod.spec");
213 print SPEC <<EOF;
214 %define __spec_install_post %{nil}
215
216 Summary: $desc
217 Name: $prefix$mod
218 Version: $ver
219 Release: $release
220 PreReq: /bin/sh /usr/bin/perl /usr/libexec/$prog
221 Requires: /bin/sh /usr/bin/perl /usr/libexec/$prog $rdeps
222 AutoReq: 0
223 License: $licence
224 Group: System/Tools
225 Source: $mod.tar.gz
226 Vendor: Jamie Cameron
227 BuildRoot: /tmp/%{name}-%{version}
228 BuildArchitectures: noarch
229 $providesheader
230 $vendorheader
231 $urlheader
232 %description
233 $desc in RPM format
234
235 %prep
236 %setup -n $mod
237
238 %build
239 (find . -name '*.cgi' ; find . -name '*.pl') | perl -ne 'chop; open(F,\$_); \@l=<F>; close(F); \$l[0] = "#\!/usr/bin/perl\$1\n" if (\$l[0] =~ /#\!\\S*perl\\S*(.*)/); open(F,">\$_"); print F \@l; close(F)'
240 (find . -name '*.cgi' ; find . -name '*.pl') | xargs chmod +x
241
242 %install
243 mkdir -p %{buildroot}/usr/libexec/$prog/$mod
244 cp -rp * %{buildroot}/usr/libexec/$prog/$mod
245 echo rpm >%{buildroot}/usr/libexec/$prog/$mod/install-type
246
247 %clean
248 [ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
249
250 %files
251 /usr/libexec/$prog/$mod
252
253 %pre
254 # Check if webmin/usermin is installed
255 if [ ! -r /etc/$prog/config -o ! -d /usr/libexec/$prog ]; then
256         echo "$ucprog does not appear to be installed on your system."
257         echo "This RPM cannot be installed unless the RPM version of $ucprog"
258         echo "is installed first."
259         exit 1
260 fi
261 if [ "$depends" != "" -a "$rpmdepends" != 1 ]; then
262         # Check if depended webmin/usermin modules are installed
263         for d in $depends; do
264                 if [ ! -r /usr/libexec/$prog/\$d/module.info ]; then
265                         echo "This $ucprog module depends on the module \$d, which is"
266                         echo "not installed on your system."
267                         exit 1
268                 fi
269         done
270 fi
271 # Check if this module is already installed
272 if [ -d /usr/libexec/$prog/$mod -a "\$1" = "1" -a "$allow_overwrite" != "1" ]; then
273         echo "This $ucprog module is already installed on your system."
274         exit 1
275 fi
276
277 %post
278 if [ "$post_config" = "1" ]; then
279         # Copy config file to /etc/webmin or /etc/usermin
280         os_type=`grep "^os_type=" /etc/$prog/config | sed -e 's/os_type=//g'`
281         os_version=`grep "^os_version=" /etc/$prog/config | sed -e 's/os_version=//g'`
282         /usr/bin/perl /usr/libexec/$prog/copyconfig.pl \$os_type \$os_version /usr/libexec/$prog /etc/$prog $mod
283
284         # Update the ACL for the root user, or the first user in the ACL
285         grep "^root:" /etc/$prog/webmin.acl >/dev/null
286         if [ "\$?" = "0" ]; then
287                 user=root
288         else
289                 user=`head -1 /etc/$prog/webmin.acl | cut -f 1 -d :`
290         fi
291         mods=`grep \$user: /etc/$prog/webmin.acl | cut -f 2 -d :`
292         echo \$mods | grep " $mod" >/dev/null
293         if [ "\$?" != "0" ]; then
294                 grep -v ^\$user: /etc/$prog/webmin.acl > /tmp/webmin.acl.tmp
295                 echo \$user: \$mods $mod > /etc/$prog/webmin.acl
296                 cat /tmp/webmin.acl.tmp >> /etc/$prog/webmin.acl
297                 rm -f /tmp/webmin.acl.tmp
298         fi
299 fi
300 if [ "$force_theme" != "" -a "$istheme" = "1" ]; then
301         # Activate this theme
302         grep -v "^preroot=" /etc/$prog/miniserv.conf >/etc/$prog/miniserv.conf.tmp
303         (cat /etc/$prog/miniserv.conf.tmp ; echo preroot=$mod) > /etc/$prog/miniserv.conf
304         rm -f /etc/$prog/miniserv.conf.tmp
305         grep -v "^theme=" /etc/$prog/config >/etc/$prog/config.tmp
306         (cat /etc/$prog/config.tmp ; echo theme=$mod) > /etc/$prog/config
307         rm -f /etc/$prog/config.tmp
308         (/etc/$prog/stop && /etc/$prog/start) >/dev/null 2>&1
309 fi
310 rm -f /etc/$prog/module.infos.cache
311
312 # Run post-install function
313 if [ "$prog" = "webmin" ]; then
314         cd /usr/libexec/$prog
315         WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/run-postinstalls.pl $mod
316 fi
317
318 # Run post-install shell script
319 if [ -r "/usr/libexec/$prog/$mod/postinstall.sh" ]; then
320         cd /usr/libexec/$prog
321         WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/$mod/postinstall.sh
322 fi
323
324 %preun
325 # De-activate this theme, if in use and if we are not upgrading
326 if [ "$istheme" = "1" -a "\$1" = "0" ]; then
327         grep "^preroot=$mod" /etc/$prog/miniserv.conf >/dev/null
328         if [ "\$?" = "0" ]; then
329                 grep -v "^preroot=$mod" /etc/$prog/miniserv.conf >/etc/$prog/miniserv.conf.tmp
330                 (cat /etc/$prog/miniserv.conf.tmp) > /etc/$prog/miniserv.conf
331                 rm -f /etc/$prog/miniserv.conf.tmp
332                 grep -v "^theme=$mod" /etc/$prog/config >/etc/$prog/config.tmp
333                 (cat /etc/$prog/config.tmp) > /etc/$prog/config
334                 rm -f /etc/$prog/config.tmp
335                 (/etc/$prog/stop && /etc/$prog/start) >/dev/null 2>&1
336         fi
337 fi
338 # Run the pre-uninstall script, if we are not upgrading
339 if [ "$prog" = "webmin" -a "\$1" = "0" -a -r "/usr/libexec/$prog/$mod/uninstall.pl" ]; then
340         cd /usr/libexec/$prog
341         WEBMIN_CONFIG=/etc/$prog WEBMIN_VAR=/var/$prog /usr/libexec/$prog/run-uninstalls.pl $mod
342 fi
343 /bin/true
344
345 %postun
346 EOF
347 close(SPEC);
348
349 # Build the actual RPM
350 $cmd = -x "/usr/bin/rpmbuild" ? "/usr/bin/rpmbuild" : "/bin/rpm";
351 system("$cmd -ba $spec_dir/$prefix$mod.spec") && exit;
352 unlink("$rpm_source_dir/$mod.tar.gz");
353
354 # Sign if requested
355 if ($sign) {
356         system("rpm --resign $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $source_rpm_dir/$prefix$mod-$ver-$release.src.rpm");
357         }
358
359 if ($target_dir =~ /:/) {
360         # scp to dest
361         system("scp $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $target_dir/$prefix$mod-$ver-$release.noarch.rpm");
362         }
363 else {
364         # Just copy
365         system("/bin/cp $rpm_dir/$prefix$mod-$ver-$release.noarch.rpm $target_dir/$prefix$mod-$ver-$release.noarch.rpm");
366         }
367
368 # read_file(file, &assoc, [&order], [lowercase])
369 # Fill an associative array with name=value pairs from a file
370 sub read_file
371 {
372 open(ARFILE, $_[0]) || return 0;
373 while(<ARFILE>) {
374         s/\r|\n//g;
375         if (!/^#/ && /^([^=]+)=(.*)$/) {
376                 $_[1]->{$_[3] ? lc($1) : $1} = $2;
377                 push(@{$_[2]}, $1) if ($_[2]);
378                 }
379         }
380 close(ARFILE);
381 return 1;
382 }
383  
384 sub untaint
385 {
386 $_[0] =~ /^(.*)$/;
387 return $1;
388 }
389