$OpenBSD: patch-Users_Users_pm,v 1.4 2011/01/18 13:07:21 ajacoutot Exp $
--- Users/Users.pm.orig	Sat Aug 21 15:18:12 2010
+++ Users/Users.pm	Tue Jan 18 13:48:11 2011
@@ -58,7 +58,7 @@ use Utils::Replace;
 # They are tried in array order. First found = used.
 @passwd_names =     ( "/etc/passwd" );
 @shadow_names =     ( "/etc/shadow", "/etc/master.passwd" );
-@login_defs_names = ( "/etc/login.defs", "/etc/adduser.conf" );
+@login_defs_names = ( "/etc/login.defs", ,"/etc/usermgmt.conf", "/etc/adduser.conf" );
 @shell_names =      ( "/etc/shells" );
 @skel_dir =         ( "/usr/share/skel", "/etc/skel" );
 
@@ -72,6 +72,7 @@ $cmd_deluser  = &Utils::File::locate_tool ("deluser");
 
 $cmd_chfn     = &Utils::File::locate_tool ("chfn");
 $cmd_pw       = &Utils::File::locate_tool ("pw");
+$cmd_user     = &Utils::File::locate_tool ("user");
 
 $cmd_passwd   = &Utils::File::locate_tool ("passwd");
 $cmd_chpasswd = &Utils::File::locate_tool ("chpasswd");
@@ -236,6 +237,12 @@ my $freebsd_logindefs_defaults = {
   'skel_dir' => '/etc/skel/',
 };
 
+my $openbsd_logindefs_defaults = {
+  'shell'    => '/bin/ksh',
+  'group'    => -1,
+  'skel_dir' => '/etc/skel/',
+};
+
 my $logindefs_dist_map = {
   'redhat-6.2'      => $rh_logindefs_defaults,
   'redhat-7.0'      => $rh_logindefs_defaults,
@@ -252,6 +259,7 @@ my $logindefs_dist_map = {
   'archlinux'       => $gentoo_logindefs_defaults,
   'slackware-9.1.0' => $gentoo_logindefs_defaults,
   'freebsd-5'       => $freebsd_logindefs_defaults,
+  'openbsd-4'       => $openbsd_logindefs_defaults,
   'suse-9.0'        => $gentoo_logindefs_defaults,
   'solaris-2.11'    => $gentoo_logindefs_defaults,
 };
@@ -384,9 +392,17 @@ sub get
     # Detect lock status of password
     # We run 'passwd' instead of reading /etc/shadow directly
     # to avoid leaving sensitive data in memory (hard to clear in perl)
-    $fd = &Utils::File::run_pipe_read ("passwd -S $login");
-    @passwd_status = split ' ', <$fd>;
-    &Utils::File::close_file ($fd);
+    # On OpenBSD, the '-S' switch for passwd(1) does not exist
+    if ($Utils::Backend::tool{"system"} eq "OpenBSD")
+    {
+      @passwd_status = "P";
+    }
+    else
+    {
+      $fd = &Utils::File::run_pipe_read ("passwd -S $login");
+      @passwd_status = split ' ', <$fd>;
+      &Utils::File::close_file ($fd);
+    }
 
     if ($passwd_status[1] eq "P")
     {
@@ -441,6 +457,17 @@ sub del_user
         @command = ($cmd_pw, "userdel", "-n", $$user[$LOGIN]);
       }
   }
+  elsif ($Utils::Backend::tool{"system"} eq "OpenBSD")
+  {
+    if ($remove_home)
+      {
+        @command = ($cmd_userdel, "-r", $$user[$LOGIN]);
+      }
+    else
+      {
+        @command = ($cmd_userdel, $$user[$LOGIN]);
+      }
+  }
   elsif ($cmd_deluser) # use deluser (preferred method)
     {
       if ($remove_home)
@@ -497,11 +524,15 @@ sub set_passwd
   my ($login, $password, $passwd_status) = @_;
   my ($pwdpipe);
 
-  # handle empty password via passwd, as all tools don't support it
-  if ($passwd_status & 1)
+  # OpenBSD passwd(1) has no '-d' switch
+  if ($Utils::Backend::tool{"system"} ne "OpenBSD")
   {
-    &Utils::File::run ("passwd", "-d", $login);
-    return;
+    # handle empty password via passwd, as all tools don't support it
+    if ($passwd_status & 1)
+    {
+      &Utils::File::run ("passwd", "-d", $login);
+      return;
+    }
   }
 
   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
@@ -520,7 +551,7 @@ sub set_passwd
     print $pwdpipe $password;
     &Utils::File::close_file ($pwdpipe);
   }
-  else
+  elsif ($Utils::Backend::tool{"system"} ne "OpenBSD")
   {
     $pwdpipe = &Utils::File::run_pipe_write ($cmd_chpasswd);
     print $pwdpipe "$login:$password";
@@ -534,14 +565,17 @@ sub set_lock
   my ($login, $passwd_status) = @_;
   my ($pwdpipe);
 
-  if ($passwd_status & (1 << 1))
+  if ($Utils::Backend::tool{"system"} ne "OpenBSD")
   {
-    &Utils::File::run ("passwd", "-l", $login);
+    if ($passwd_status & (1 << 1))
+    {
+      &Utils::File::run ("passwd", "-l", $login);
+    }
+    else
+    {
+      &Utils::File::run ("passwd", "-u", $login);
+    }
   }
-  else
-  {
-    &Utils::File::run ("passwd", "-u", $login);
-  }
 }
 
 # This function allows empty values to be passed, in which cas
@@ -620,7 +654,8 @@ sub add_user
         $Utils::Backend::tool{"platform"} !~ /^slackware/ &&
         $Utils::Backend::tool{"platform"} !~ /^archlinux/ &&
         $Utils::Backend::tool{"platform"} !~ /^redhat/ &&
-        $Utils::Backend::tool{"platform"} !~ /^gentoo/)
+        $Utils::Backend::tool{"platform"} !~ /^gentoo/ &&
+        $Utils::Backend::tool{"platform"} !~ /^openbsd/)
     {
       # use adduser if available and valid (slackware one is b0rk)
       # set empty gecos fields and password, they will be filled out later
@@ -650,7 +685,16 @@ sub add_user
       push (@command, ("-d", $$user[$HOME])) if ($$user[$HOME]);
       push (@command, ("-s", $$user[$SHELL])) if ($$user[$SHELL]);
       push (@command, ("-u", $$user[$UID])) if ($real_uid);
-      push (@command, ("-g", $$user[$GID])) if ($real_gid);
+
+      if ($Utils::Backend::tool{"platform"} =~ /^openbsd/)
+      {
+        push (@command, "-g", "=uid");
+      }
+      else
+      {
+        push (@command, ("-g", $$user[$GID])) if ($real_gid);
+      }
+
       push (@command, $$user[$LOGIN]);
 
       &Utils::File::run (@command);
@@ -691,6 +735,18 @@ sub change_user
                          "-d", $$new_user[$HOME],
                          "-g", $$new_user[$GID],
                          "-s", $$new_user[$SHELL]);
+
+    &Utils::File::run (@command);
+  }
+  elsif ($Utils::Backend::tool{"system"} eq "OpenBSD")
+  {
+    @command = ($cmd_usermod, "-o",
+                              "-d", $$new_user[$HOME],
+                              "-g", $$new_user[$GID],
+                              "-l", $$new_user[$LOGIN],
+                              "-s", $$new_user[$SHELL],
+                              "-u", $$new_user[$UID],
+                                    $$old_user[$LOGIN]);
 
     &Utils::File::run (@command);
   }
