Lotus Domino encrypt and sign all NSF in Domino folder
With the info on this page you will be able to make a Notes application that will encrypt and sign all Notes databases in a certain folder on your Domino server.
Make 2 forms:
Make 2 views:
Scheduled agent
And make a script library with the name slEncrypt with below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
Option Declare %Include "lsConst.lss" Const FORM_CONFIGURATION = "(fmConfiguration)" Const FIELD_ENCRYPTFOLDER = "fEncryptFolder" Const FIELD_MANAGERS = "fManagers" Const DBCREATE_ENCRYPT_NONE = 0 Const DBCREATE_ENCRYPT_SIMPLE = 1 Const DBCREATE_ENCRYPT_MEDIUM = 2 Const DBCREATE_ENCRYPT_STRONG = 3 Const DBENCRYPT = 0 Const DBDECRYPT = 2 Const NOERROR = 0 Const ERR_LOCAL_ENCRYPT_MUST_COMPACT = 902 Const ERR_LOCAL_DECRYPT_MUST_COMPACT = 903 Const ERR_LOCALSEC_ALREADYSET = 762 Const W32_LIB = {nnotes.dll} Declare Function W32_NSFDbCompactExtended Lib W32_LIB Alias {NSFDbCompactExtended} (ByVal Pathname As String, Options As Long, retStats As Long) As Integer Declare Function W32_NSFDbLocalSecInfoSet Lib W32_LIB Alias {NSFDbLocalSecInfoSet} (ByVal hDb As Long, ByVal wOptions As Integer, ByVal EncryptStrength As Integer, ByVal Username As String) As Integer Declare Function W32_NSFDbLocalSecInfoGet Lib W32_LIB Alias {NSFDbLocalSecInfoGet} (ByVal hDb As Long, wOptions As Integer, EncryptStrength As Integer, ByVal data As String) As Integer Declare Function W32_NSFDbIsLocallyEncrypted Lib W32_LIB Alias {NSFDbIsLocallyEncrypted} ( ByVal hDB As Long, V As Integer) As Integer Declare Sub W32_OSLoadString Lib W32_LIB Alias {OSLoadString} (ByVal null1 As Long, ByVal sError As Integer, ByVal errstr As String, ByVal lenstr As Integer) Declare Function W32_NSFDbOpen Lib W32_LIB Alias {NSFDbOpen}(ByVal dbName As String, hDb As Long) As Integer Declare Function W32_NSFDbClose Lib W32_LIB Alias {NSFDbClose} (ByVal hDb As Long) As Integer Declare Function W32_OSPathNetConstruct Lib W32_LIB Alias {OSPathNetConstruct} (ByVal portName As Integer, ByVal ServerName As String, ByVal FileName As String, ByVal retPathName As String) As Integer Private Class Configuration Private isEncryptFolder As String Private ivManagers As Variant Public Sub New Dim lssnCurrent As New NotesSession Dim ldbCurrent As NotesDatabase Dim ldocProfile As NotesDocument Set ldbCurrent = lssnCurrent.CurrentDatabase Set ldocProfile = ldbCurrent.getProfileDocument(FORM_CONFIGURATION) isEncryptFolder = LCase(ldocProfile.getItemValue(FIELD_ENCRYPTFOLDER)(0)) ivManagers = ldocProfile.getItemValue(FIELD_MANAGERS) End Sub Public Property Get EncryptFolder As String EncryptFolder = isEncryptFolder End Property Public Function getFirstManager() As String If IsArray(ivManagers) Then getFirstManager = ivManagers(LBound(ivManagers)) Else getFirstManager = ivManagers End If End Function Public Function getNextManager(lsManager As String) As String Dim lvPosition As Variant If IsArray(ivManagers) Then lvPosition = ArrayGetIndex(ivManagers, lsManager, 5) If IsNull(lvPosition) Then getNextManager = "" ElseIf lvPosition >= UBound(ivManagers) Then getNextManager = "" Else getNextManager = ivManagers(lvPosition + 1) End If Else getNextManager = "" End If End Function End Class Private Class LiaLog Private issnCurrent As NotesSession Private idbCurrent As NotesDatabase Private ivwLogLookup As NotesView Sub New Set issnCurrent = New NotesSession Set idbCurrent = issnCurrent.currentDatabase Set ivwLogLookup = idbCurrent.getView("(vwLookupLog)") End Sub Public Sub LogMsg(psFilepath As String, psTitle As String, psMessage As String) Dim ldocLog As NotesDocument Dim litLog As NotesItem Set ldocLog = getLogDocument(psFilepath, psTitle) 'add datetime stamp to log message If ldocLog.hasItem("fLog") Then Set litLog = ldocLog.getFirstItem("fLog") litLog.Appendtotextlist(psMessage) Else ldocLog.replaceItemValue "fLog", psMessage End If ldocLog.save True, True End Sub Public Sub LogSuccessEncrypt(psFilepath As String, psTitle As String) Dim ldocLog As NotesDocument Set ldocLog = getLogDocument(psFilepath, psTitle) ldocLog.replaceItemValue "fEncrypted", "1" ldocLog.save True, True End Sub Public Sub LogSuccessDecrypt(psFilepath As String, psTitle As String) Dim ldocLog As NotesDocument Set ldocLog = getLogDocument(psFilepath, psTitle) ldocLog.replaceItemValue "fEncrypted", "2" ldocLog.save True, True End Sub Private Function getLogDocument(psFilepath As String, psTitle As String) As NotesDocument Dim ldocLog As NotesDocument ivwLogLookup.refresh Set ldocLog = ivwLogLookup.getDocumentByKey(psFilepath) If ldocLog Is Nothing Then Set ldocLog = idbCurrent.createDocument ldocLog.replaceItemValue "Form", "fmLog" ldocLog.replaceItemValue "fDate", Now ldocLog.replaceItemValue "fTitle", psTitle ldocLog.replaceItemValue "fFilepath", psFilepath ldocLog.replaceItemValue "fEncrypted", "0" End If Set getLogDocument = ldocLog End Function End Class Private Sub fixupDatabase(pdbProcess As NotesDatabase) Dim lssnCurrent As New NotesSession Dim lsCommand As String lsCommand = "load fixup -F -U " & pdbProcess.filepath lssnCurrent.Sendconsolecommand pdbProcess.server, lsCommand End Sub Private Function ResetEncryptFlag(pobjLog As LiaLog, psFilepath As String, psTitle As String, psNetPath As String, plDbHandle As Long) As Boolean Dim liError As Integer ' set encryptionlevel liError = W32_NSFDbLocalSecInfoSet(plDbHandle, DBDECRYPT, DBCREATE_ENCRYPT_NONE, "") ResetEncryptFlag = (ERR_LOCAL_ENCRYPT_MUST_COMPACT) Or (liError = ERR_LOCALSEC_ALREADYSET) End Function Private Sub clearACL(pACL As NotesACL) Dim lACLEntry As NotesACLEntry Dim lACLNextEntry As NotesACLEntry Set laclEntry = pACL.Getfirstentry() Do Until laclEntry Is Nothing Set laclNextEntry = pACL.GetNextentry(laclEntry) If laclEntry.Name <> "" And laclEntry.name = "-Default" Then laclEntry.Remove End If Set laclEntry = laclNextEntry Loop End Sub Private Sub setACL(pACL As NotesACL, psName As String, piLevel As Integer, pbEnableRoles As Boolean) Dim laclEntry As NotesACLEntry Set laclEntry = pACL.getEntry(psNAme) If laclEntry Is Nothing Then Set laclEntry = pACL.createACLEntry(psName, piLevel) Else laclEntry.Level = piLevel End If if laclEntry.level = ACLLEVEL_MANAGER Then laclEntry.Cancreatedocuments = True End If setRoles pacl, laclEntry End Sub Private Sub signDatabase(pdbProcess As NotesDatabase) Dim lap As NotesAdministrationProcess Set lap = pdbProcess.parent.Createadministrationprocess(pdbProcess.server) lap.Signdatabasewithserverid pdbProcess.server, pdbProcess.Filepath, false End Sub Function GetError (errnum As Integer) As String Dim s As String*256 If IsDefined("WINDOWS") Then W32_OSLoadString 0, errnum And &h03FFFFFFF, s, 256 Else 'TUX_OSLoadString 0, errnum And &h03FFFFFFF, s, 256 End If getError = StrLeft(s, Chr(0)) End Function Private Sub updateACL(pdbProcess As NotesDatabase, pobjConfig As Configuration) Dim lACL As NotesACL Dim lsManager As String Set lACL = pdbProcess.Acl CLearACL lACL setACL lACL, "-Default-", ACLLEVEL_NOACCESS, False setACL lACL, "Anonymous", ACLLEVEL_NOACCESS, False setACL lACL, "OtherDomainServers", ACLLEVEL_NOACCESS, False setACL lACL, pdbProcess.server, ACLLEVEL_MANAGER, True setACL lACL, "LocalDomainServers", ACLLEVEL_MANAGER, True lsManager = pobjConfig.getFirstManager() Do Until Len(lsManager) = 0 setACL lACL, lsManager, ACLLEVEL_MANAGER, True lsManager = pobjConfig.getNextManager(lsManager) Loop lACL.Save End Sub Private Function SetEncryptFlag(pobjLog As LiaLog, psFilepath As String, psTitle As String, psNetPath As String, plDbHandle As Long) As Boolean Dim liError As Integer ' set encryptionlevel liError = W32_NSFDbLocalSecInfoSet(plDbHandle, DBENCRYPT, DBCREATE_ENCRYPT_MEDIUM, "") SetEncryptFlag = (ERR_LOCAL_ENCRYPT_MUST_COMPACT) Or (liError = ERR_LOCALSEC_ALREADYSET) End Function Private Function isLocallyEncrypted(plHandle As Long) As Boolean Dim liReturn As Integer Dim liError As Integer If plHandle <> 0 Then 'check if db is encrypted liError = W32_NSFDbIsLocallyEncrypted(plHandle, liReturn) isLocallyEncrypted = (liError = NOERROR) And (liReturn <> DBCREATE_ENCRYPT_NONE) Else isLocallyEncrypted = False End If End Function Private Sub processDatabase(pobjLog As LiaLog, pdbProcess As NotesDatabase, pobjConfig As Configuration, pbEncrypt As Boolean) Dim lsNetPath As String Dim liError As Integer Dim llDbHandle As Long Dim lsServername As String Dim lbEncrypted As Boolean Dim llRetStats As Long If pdbProcess.parent.isonserver Then lsServerName = "" Else lsServerName = pdbProcess.server End If lsNetPath = PathNetConstruct(lsServerName, pdbProcess.Filepath) If Len(lsNetPath) = 0 Then Exit Sub End If lbEncrypted = False On Error GoTo catch1 ' Open the Database with API call liError = W32_NSFDbOpen(lsNetPath, llDbHandle) If liError = NOERROR Then If isLocallyEncrypted(llDbHandle) <> pbEncrypt Then If pbEncrypt Then pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Encryption started on database" lbEncrypted = setEncryptFlag(pobjLog, pdbProcess.Filepath, pdbProcess.Title, lsNetPath, llDbHandle) Else pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Decryption started on database" lbEncrypted = ResetEncryptFlag(pobjLog, pdbProcess.Filepath, pdbProcess.Title, lsNetPath, llDbHandle) End If If Not pdbProcess.isOpen Then pdbProcess.open "","" End If pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Updating ACL" updateACL pdbProcess, pobjConfig pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Disabling scheduled agents" disableAgents pdbProcess End If End If GoTo finally1 catch1: pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Failed to open database" Resume finally1 finally1: On Error GoTo 0 'if db is open in API close it. If llDbHandle <> 0 Then liError = W32_NSFDbClose(llDbHandle) End If On Error GoTo catch2 If lbEncrypted Then Print "Compacting" pdbProcess.compactwithoptions CMPC_COPYSTYLE 'compact the database 'liError = W32_NSFDbCompactExtended (lsNetPath, 0, llRetStats) 'Print liError, llRetStats, GetError(liError) If pbEncrypt Then pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Database sucessfully encrypted" Else pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Database sucessfully decrypted" End If signDatabase pdbProcess pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Signing database" If pbEncrypt Then pobjLog.LogSuccessEncrypt pdbProcess.Filepath, pdbProcess.Title Else pobjLog.LogSuccessDecrypt pdbProcess.Filepath, pdbProcess.Title End If 'Else ' pobjLog.LogMsg pdbProcess.Filepath, pdbProcess.Title, "Unable to enable encryption on database" ' Print liError, GetError(liError) End If GoTo finally2 catch2: fixupDatabase pdbProcess Print "Error compacting database " & Error$ Resume finally2 finally2: On Error GoTo 0 End Sub Public Sub Encrypt_Start(pbEncrypt As Boolean) Dim lssnCurrent As New NotesSession Dim ldbCurrent As NotesDatabase Dim ldbProcess As NotesDatabase Dim lobjLog As LiaLog Dim ldbdDirectory As NotesDbDirectory Dim lobjConfig As Configuration Dim liPathLength As Integer Dim lsServername As String Set lobjConfig = New Configuration liPathLength = Len(lobjConfig.EncryptFolder) Set lobjLog = New LiaLog Set ldbCurrent = lssnCurrent.CurrentDatabase If lssnCurrent.isOnServer Then lsServername = "" Else lsServername = ldbCurrent.server End If Set ldbdDirectory = lssnCurrent.Getdbdirectory(lsServername) If ldbdDirectory Is Nothing Then Print "Cannot find Database directory" Exit Sub End If Set ldbProcess = ldbdDirectory.Getfirstdatabase(DATABASE) Do Until ldbProcess Is Nothing If Left$(LCase(ldbProcess.Filepath), liPathLength) = lobjConfig.EncryptFolder Then processDatabase lobjLog, ldbProcess, lobjConfig, pbEncrypt End If Set ldbProcess = ldbdDirectory.Getnextdatabase() Loop End Sub Function PathNetConstruct(psServer As String, psFileName As String) As String Dim lsNetPath As String Dim liReturn As Integer lsNetPath=" " liReturn = W32_OSPathNetConstruct(0, psServer, psFileName, lsNetPath) If liReturn <> 0 Then MsgBox "Error" lsNetPath = "" End If PathNetConstruct = lsNetPath End Function Private Sub setRoles(pACL As NotesACL, paclEntry As NotesACLEntry) Dim lvRoles As Variant lvRoles = pACL.Roles If IsArray(lvRoles) Then ForAll lsRole In lvRoles If Left$(lsRole,1) = "[" Then ' Make sure it's not R2 privilege If pacLEntry.level = ACLLEVEL_MANAGER Then paclEntry.enableRole lsRole Else paclEntry.disableRole lsRole End If End If End ForAll End If End Sub Private Sub disableAgents(pdbProcess As NotesDatabase) Dim lvAgents As Variant On Error GoTo fail lvAgents = pdbProcess.agents If Not IsEmpty(lvAgents) Then ForAll lagAgent In LvAgents If lagAgent.trigger <> TRIGGER_MANUAL Then Print "Disabling: "& lagagent.name lagAgent.IsEnabled = False lagAgent.save Else Print "Manual: "& lagagent.name End If End ForAll End If Exit Sub fail: Print "Error: " & Error$ & " at line: " & CStr(Erl) Exit Sub End Sub |