Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement agreement initialization.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Loading branch information...
commit a1711d44117b7549f5e728fc77ad5924aeab4f99 1 parent 121eae4
Edward Z. Yang authored November 13, 2011

Showing 1 changed file with 40 additions and 0 deletions. Show diff stats Hide diff stats

  1. 40  mmr-hammer.hs
40  mmr-hammer.hs
@@ -12,6 +12,7 @@ import Data.Time
12 12
 import Control.Monad
13 13
 import Control.Exception
14 14
 import Control.Concurrent
  15
+import Control.Applicative
15 16
 
16 17
 import System.IO
17 18
 import System.IO.Unsafe
@@ -25,12 +26,18 @@ import System.Locale
25 26
 import Text.Printf
26 27
 
27 28
 import Network.URI
  29
+import Network.BSD
28 30
 
29 31
 import LDAP.Init (ldapInitialize, ldapSimpleBind)
30 32
 import LDAP.Search (SearchAttributes(LDAPAllUserAttrs), LDAPEntry(..),
31 33
     LDAPScope(..), ldapSearch)
32 34
 import LDAP.Modify (LDAPModOp(..), LDAPMod(..), ldapAdd, ldapDelete,
33 35
     ldapModify, list2ldm)
  36
+import LDAP.Data (LDAPReturnCode(..))
  37
+import LDAP.Exceptions (LDAPException(..), catchLDAP, throwLDAP)
  38
+
  39
+newtype Canonical = Canonical { canonical :: HostName }
  40
+canonicalize h = Canonical . map toLower . hostName <$> getHostByName h
34 41
 
35 42
 scriptsBase = "dc=scripts,dc=mit,dc=edu"
36 43
 configBase  = "cn=config"
@@ -82,6 +89,11 @@ getBinds ldap = do
82 89
         [] -> error "getBinds: No binds found"
83 90
         bs -> return bs
84 91
 getConflicts ldap = searchScripts ldap "nsds5ReplConflict=*"
  92
+getLocalhost ldap = do
  93
+    (LDAPEntry _ attrs) <- getConfig ldap
  94
+    case lookupKey1 "nsslapd-localhost" attrs of
  95
+        Nothing -> error "getLocalhost: No localhost name in config found"
  96
+        Just x -> return x
85 97
 
86 98
 -- what goes in when you create a replication agreement
87 99
 replicaConfig = constructKeySet
@@ -151,6 +163,32 @@ reinitAgreements ldap statefile = do
151 163
     enableReplication ldap
152 164
     putStrLn "Done!"
153 165
 
  166
+initAgreements ldap targets = do
  167
+    master <- getLocalhost ldap
  168
+    forM_ targets $ \target -> do
  169
+        host <- canonicalize target
  170
+        initAgreement ldap master host `catchLDAP` \e ->
  171
+            if code e == LdapAlreadyExists
  172
+                then putStrLn ("Agreement already exists for " ++ canonical host)
  173
+                else throwLDAP e
  174
+
  175
+initAgreement ldap master (Canonical target) = do
  176
+    putStrLn ("Initializing agreement to " ++ target)
  177
+    let cn = "GSSAPI Replication to " ++ target
  178
+    let agreementDn = "cn=\"" ++ cn ++ "\"," ++ replicaBase
  179
+    ldapAdd ldap agreementDn $ list2ldm LdapModAdd
  180
+        [ ("objectClass", ["top", "nsDS5ReplicationAgreement"])
  181
+        , ("cn", [cn])
  182
+        , ("nsDS5ReplicaHost", [target])
  183
+        , ("nsDS5ReplicaRoot", ["dc=scripts,dc=mit,dc=edu"])
  184
+        , ("nsDS5ReplicaPort", ["389"])
  185
+        , ("nsDS5ReplicaTransportInfo", ["LDAP"])
  186
+        , ("nsDS5ReplicaBindDN", ["uid=ldap/"++master++",ou=People,dc=scripts,dc=mit,dc=edu"])
  187
+        , ("nsDS5ReplicaBindMethod", ["SASL/GSSAPI"])
  188
+        , ("nsDS5ReplicaUpdateSchedule", ["0000-2359 0123456"])
  189
+        , ("nsDS5ReplicaTimeout", ["120"])
  190
+        ]
  191
+
154 192
 serializeEntries = show . map (\(LDAPEntry dn attrs) -> (dn, attrs))
155 193
 unserializeEntries = map (\(dn, attrs) -> LDAPEntry dn attrs) . read
156 194
 
@@ -462,9 +500,11 @@ main = do
462 500
         ["recover", "user", uid] -> recoverUser ldap uid
463 501
         ["cleanruv", target, replicaid] -> cleanRUV ldap target replicaid
464 502
         ["conflicts"] -> printConflicts ldap
  503
+        ("init": "agreements": targets) -> initAgreements ldap targets
465 504
         ("suspend": _) -> usage "suspend [agreements|binds]"
466 505
         ("set":     _) -> usage "set [binds] VALUES..."
467 506
         ("restore": _) -> usage "restore [agreements|binds]"
  507
+        ("init":    _) -> usage "init [agreements]"
468 508
         ("reinit":  _) -> usage "reinit [agreements]"
469 509
         ("disable": _) -> usage "disable [replication|syntaxcheck]"
470 510
         ("enable":  _) -> usage "enable [replication|syntaxcheck]"

0 notes on commit a1711d4

Please sign in to comment.
Something went wrong with that request. Please try again.