don't do the duplicate check unless there's a file already; fixes problem with first...
[freeside.git] / FS / bin / freeside-adduser
1 #!/usr/bin/perl -w
2 #
3 # $Id: freeside-adduser,v 1.10 2006-04-13 21:29:01 ivan Exp $
4
5 use strict;
6 use vars qw($opt_h $opt_b $opt_c $opt_s);
7 use Fcntl qw(:flock);
8 use Getopt::Std;
9
10 my $FREESIDE_CONF = "/usr/local/etc/freeside";
11
12 getopts("bch:s:");
13 die &usage if $opt_c && ! $opt_h;
14 my $user = shift or die &usage;
15
16 if ( -e "$FREESIDE_CONF/mapsecrets" ) {
17   open(MAPSECRETS,"<$FREESIDE_CONF/mapsecrets")
18     or die "can't open $FREESIDE_CONF/mapsecrets: $!";
19   while (<MAPSECRETS>) {
20     /^(\S+) / or die "unparsable line in mapsecrets: $_";
21     die "user $user already exists\n" if $user eq $1;
22   }
23   close MAPSECRETS;
24 }
25
26 if ( $opt_h ) {
27   my @args = ( 'htpasswd' );
28   push @args, '-b' if $opt_b;
29   push @args, '-c' if $opt_c;
30   push @args, $opt_h, $user;
31   push @args, shift if $opt_b;
32   system(@args) == 0 or die "htpasswd failed: $?";
33 }
34
35 my $secretfile = $opt_s || 'secrets';
36
37 open(MAPSECRETS,">>$FREESIDE_CONF/mapsecrets")
38   and flock(MAPSECRETS,LOCK_EX)
39     or die "can't open $FREESIDE_CONF/mapsecrets: $!";
40 print MAPSECRETS "$user $secretfile\n";
41 close MAPSECRETS or die "can't close $FREESIDE_CONF/mapsecrets: $!";
42
43 sub usage {
44   die "Usage:\n\n  freeside-adduser [ -h htpasswd_file [ -c ] [ -b ] ] [ -s secretfile ] username"
45 }
46
47 =head1 NAME
48
49 freeside-adduser - Command line interface to add (freeside) users.
50
51 =head1 SYNOPSIS
52
53   freeside-adduser [ -h htpasswd_file [ -c ] ] [ -s secretfile ] username
54
55 =head1 DESCRIPTION
56
57 Adds a user to the Freeside billing system.  This is for adding users (internal
58 sales/tech folks) to the web interface, not for adding customer accounts.
59
60   -h: Also call htpasswd for this user with the given filename
61
62   -c: Passed to htpasswd(1)
63
64   -s: Specify an alternate secret file
65
66   -b: same as htpasswd(1), probably insecure, not recommended
67
68 =head1 SEE ALSO
69
70 L<htpasswd>(1), base Freeside documentation
71
72 =cut
73