Imported Upstream version 2.3
[ossec-hids.git] / src / init / darwin-addusers.pl
1 #!/usr/bin/env perl
2 #######################################
3 # Name:    ossec-add-ung.pl
4 # Desc:    Add ossec users and groups on OSX using the NetInfo cmds.
5 # Author:  Chuck L.
6 # License: GPL
7 ###
8 # for vi: set tabstop=4
9 ###
10 # Rev history:
11 # v0.1 - Initial coding. 
12 # v0.2 - Modified script to use subroutines. It gets the job done, 
13 #        but more work required to add further functionality. -CL
14 # v0.2.1 - Modified the user add lines to have the users disabled
15 #          from the start ('*' was missing in passwd field). -CL
16 #######################################
17
18 # Variables and whatnot
19 my ($debug, $oUid, $oGid, @inUseUids, @inUseGids, $rev, $revDate);
20 $rev     = '0.2-1';
21 $revDate = '30-Aug-2006';
22 $debug   = '0';
23 $fName   = "/tmp/niusers.tmp";
24
25 # Commands
26 $NILOAD  = "/usr/bin/niload";
27 $NIRPT   = "/usr/bin/nireport";
28 $NIUTIL  = "/usr/bin/niutil";
29 $SORT    = "/usr/bin/sort";
30 $GREP    = "/usr/bin/grep";
31 $SUDO    = "/usr/bin/sudo";
32
33 # Subroutine calls
34 findUsersGroups();
35 createUsersGroups();
36
37 #######################################
38 #######################################
39 # Subroutines
40 #######################################
41 sub findUsersGroups {
42         @inUseUids = `$NIRPT . /users uid | $GREP "^5[0-9][0-9]" | $SORT -ru`;
43         @inUseGids = `$NIRPT . /groups gid | $GREP "^5[0-9][0-9]" | $SORT -ru`;
44
45         foreach (@inUseUids) {
46                 chomp();
47                 print "In use UID: $_\n" if $debug;
48         if ($oUid < $_) {
49                         $oUid = $_;
50         }
51         }
52         $oUid++;
53         print "Next available UID: $oUid\n" if $debug;
54
55         foreach (@inUseGids) {
56                 chomp();
57                 print "In use GID: $_\n" if $debug;
58         if ($oGid < $_) {
59                         $oGid = $_;
60         }
61         }
62         $oGid++;
63         print "Next available GID: $oGid\n" if $debug;
64 } # end sub
65
66 sub createUsersGroups {
67         print "Sub - UID is: $oUid\n" if $debug;
68         print "Sub - GID is: $oGid\n" if $debug;
69
70     my $oUidM = $oUid + 1;
71     my $oUidE = $oUid + 2;
72     my $oUidR = $oUid + 3;
73
74         $niPid = open (NIFH, "| $SUDO $NILOAD -v group /");
75         print "Adding ossec group\n" if $debug;
76     print NIFH "ossec:*:" . $oGid . ":ossec,ossecm,ossecr\n";
77         close (NIFH);
78
79     $fh = open (NITMP, ">$fName") or die "Unable to create temp file: $!\n";
80
81         print "Adding ossec users\n" if $debug;
82     print NITMP "ossec:*:" . $oUid . ":" . $oGid . "::0:0:ossec acct:/var/ossec:/sbin/nologin\n";
83     print NITMP "ossecm:*:" . $oUidM . ":" . $oGid . "::0:0:ossecm acct:/var/ossec:/sbin/nologin\n";
84     print NITMP "ossecr:*:" . $oUidR . ":" . $oGid . "::0:0:ossecr acct:/var/ossec:/sbin/nologin\n";
85
86         close ($fh);
87         $rtnVal = system("$SUDO $NILOAD -v passwd / < $fName");
88     print "Return value from syscmd: $rtnVal\n" if $debug;
89         unlink ($fName);
90
91 } # end sub
92
93 #################
94 # End program
95 #################
96