Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap.pm	(revision 2475)
@@ -102,5 +102,5 @@
     my ($self, $id) = @_;
 
-    my $ssid = $self->config('sambaSID') || 'S-2016-01-07';
+    my $ssid = $self->config('sambaSID') || 'S-1-5-21-2016-01-07';
     if (defined($id)) {
         $ssid .= '-' . $id;
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/Group.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/Group.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/Group.pm	(revision 2475)
@@ -40,5 +40,5 @@
 sub _key_attr { 'cn' } 
 
-sub _my_ldap_classes { qw(top posixGroup) }
+sub _my_ldap_classes { qw(top posixGroup sambaGroupMapping) }
 
 sub _get_attr_schema {
@@ -56,5 +56,16 @@
         dn => { ro => 1 },
         objectClass => { ro => 1 },
+        sambaSID => { },
     }
+    );
+}
+
+sub _update_class {
+    my ($self) = @_;
+
+    $self->SUPER::_update_class(
+        sambaSID => $self->base->sambaSID(
+            _computeSSID($self->_get_attributes('gidNumber'))
+        )
     );
 }
@@ -64,4 +75,6 @@
 
     my $entry = Net::LDAP::Entry->new();
+
+    $data{sambaSID} ||= $base->sambaSID(_computeSSID($data{gidNumber}));
 
     $entry->dn(join(',',
@@ -87,4 +100,9 @@
     my $res = $base->get_object('group', $id)->set_fields(%delayed);
     return defined($res) ? 1 : 0;
+}
+
+sub _computeSSID {
+    my ($value) = @_;
+    $value * 2 + 1000
 }
 
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/User.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/User.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ldap/User.pm	(revision 2475)
@@ -136,5 +136,5 @@
         sambaLMPassword => { readable => 0, },
         sambaNTPassword => { readable => 0, },
-        sambaSID => { ro => 1 },
+        sambaSID => { },
     }
     );
@@ -168,5 +168,5 @@
     $data{sn} ||= $id; # sn is mandatory
     $data{uid} ||= $id; # uid is mandatory
-    $data{sambaSID} = $base->sambaSID(_computeSSID($data{uidNumber}));
+    $data{sambaSID} ||= $base->sambaSID(_computeSSID($data{uidNumber}));
     $data{homeDirectory} ||= '/dev/null'; # homeDirectory is mandatory
     $data{$class->_key_attribute($base)} = $id;
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2475)
@@ -94,4 +94,9 @@
         $self->{_db}->do(q{set DATESTYLE to 'DMY'});
         $self->log(LA_DEBUG, 'New connection to DB');
+
+        $self->get_global_value('sambaSID') or do {
+            $self->_setSambaSID;
+            $self->commit;
+        };
 
         return $self->{_db};
@@ -776,4 +781,37 @@
 }
 
+=head2 sambaSID($id)
+
+Return the base samba SID set in the config or a default one if none is set.
+
+If C<$id> is given return the full SID suitable for an object.
+
+=cut
+
+sub sambaSID {
+    my ($self, $id) = @_;
+
+    my $ssid = $self->get_global_value('sambaSID');
+    if (defined($id)) {
+        $ssid .= '-' . $id;
+    }
+    return $ssid;
+}
+
+sub _setSambaSID {
+    my ( $self ) = @_;
+
+    my $sid = sprintf(
+        'S-1-5-21-%d-%d',
+        int(rand 1000000000),
+        int(rand 1000000000),
+    );
+
+    $self->set_global_value( 'sambaSID', $sid );
+    return $sid;
+}
+
+
+
 =head2 find_expired_users ($expire)
 
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Group.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Group.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Group.pm	(revision 2475)
@@ -194,4 +194,23 @@
                 label => l('Member count'),
             },
+
+            # Samba:
+
+            sambaSID => {
+                ro => 1,
+                auto => 1,
+            },
+            _sambaSID => {
+                ro => 1,
+                hide => 1,
+                managed => 1,
+                get => sub {
+                    my ($self) = @_;
+                    my $obj = $self->object;
+                    my $uid = $obj->get_field('gidnumber');
+                    return $obj->base->sambaSID( $uid * 2 + 1001 );
+                }
+            },
+
         }
     )
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm	(revision 2474)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm	(revision 2475)
@@ -1012,4 +1012,7 @@
                 }
             },
+
+            # Shadow:
+
             shadowExpire => {
                 ro => 1,
@@ -1041,4 +1044,46 @@
                 }
             },
+            shadowWarning => { },
+            shadowMin => { },
+            shadowMax => { },
+            shadowLastChange => {
+                ro => 1,
+                auto => 1,
+            },
+            _shadowLastChange => {
+                ro => 1,
+                hide => 1,
+                managed => 1,
+                get => sub {
+                    my ($self) = @_;
+                    my $obj = $self->object;
+                    my $ts = $obj->get_field('passwordLastSet');
+                    $ts or return;
+
+                    my $Dt = DateTime::Format::Pg->parse_datetime( $ts );
+                    return int( $Dt->epoch / 86400);
+                }
+            },
+            shadowInactive => { },
+            shadowFlag => { },
+
+            # Samba:
+
+            sambaSID => {
+                ro => 1,
+                auto => 1,
+            },
+            _sambaSID => {
+                ro => 1,
+                hide => 1,
+                managed => 1,
+                get => sub {
+                    my ($self) = @_;
+                    my $obj = $self->object;
+                    my $uid = $obj->get_field('uidnumber');
+                    return $obj->base->sambaSID( $uid * 2 + 1000 );
+                }
+            },
+
             directReports => {
                 auto => 1,
@@ -1321,27 +1366,4 @@
                 label => l('Name'),
             },
-            shadowWarning => { },
-            shadowMin => { },
-            shadowMax => { },
-            shadowLastChange => {
-                ro => 1,
-                auto => 1,
-            },
-            _shadowLastChange => {
-                ro => 1,
-                hide => 1,
-                managed => 1,
-                get => sub {
-                    my ($self) = @_;
-                    my $obj = $self->object;
-                    my $ts = $obj->get_field('passwordLastSet');
-                    $ts or return;
-
-                    my $Dt = DateTime::Format::Pg->parse_datetime( $ts );
-                    return int( $Dt->epoch / 86400);
-                }
-            },
-            shadowInactive => { },
-            shadowFlag => { },
             otherTelephone => { },
             nickname => {
