error out if you try to add duplicates; this should lower my annoyance-level
[freeside.git] / FS / bin / freeside-adduser
1 #!/usr/bin/perl -w
2 #
3 # $Id: freeside-adduser,v 1.9 2006-04-09 20:36:06 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 open(MAPSECRETS,"<$FREESIDE_CONF/mapsecrets")
17   or die "can't open $FREESIDE_CONF/mapsecrets: $!";
18 while (<MAPSECRETS>) {
19   /^(\S+) / or die "unparsable line in mapsecrets: $_";
20   die "user $user already exists\n" if $user eq $1;
21 }
22 close MAPSECRETS;
23
24 if ( $opt_h ) {
25   my @args = ( 'htpasswd' );
26   push @args, '-b' if $opt_b;
27   push @args, '-c' if $opt_c;
28   push @args, $opt_h, $user;
29   push @args, shift if $opt_b;
30   system(@args) == 0 or die "htpasswd failed: $?";
31 }
32
33 my $secretfile = $opt_s || 'secrets';
34
35 open(MAPSECRETS,">>$FREESIDE_CONF/mapsecrets")
36   and flock(MAPSECRETS,LOCK_EX)
37     or die "can't open $FREESIDE_CONF/mapsecrets: $!";
38 print MAPSECRETS "$user $secretfile\n";
39 close MAPSECRETS or die "can't close $FREESIDE_CONF/mapsecrets: $!";
40
41 sub usage {
42   die "Usage:\n\n  freeside-adduser [ -h htpasswd_file [ -c ] [ -b ] ] [ -s secretfile ] username"
43 }
44
45 =head1 NAME
46
47 freeside-adduser - Command line interface to add (freeside) users.
48
49 =head1 SYNOPSIS
50
51   freeside-adduser [ -h htpasswd_file [ -c ] ] [ -s secretfile ] username
52
53 =head1 DESCRIPTION
54
55 Adds a user to the Freeside billing system.  This is for adding users (internal
56 sales/tech folks) to the web interface, not for adding customer accounts.
57
58   -h: Also call htpasswd for this user with the given filename
59
60   -c: Passed to htpasswd(1)
61
62   -s: Specify an alternate secret file
63
64   -b: same as htpasswd(1), probably insecure, not recommended
65
66 =head1 SEE ALSO
67
68 L<htpasswd>(1), base Freeside documentation
69
70 =cut
71