Notes Api SECKFMGetCertifierCtx let crash Client

By migrating an application from R5.0.12 to R6.53 FP1 a notes api function crash my client.

The NSD file indicate that there is an Access_Violation but i can’t find out where to look.

Agent is signed by id to run unristricted agent

ACL of certlog database is modified

ECL on workstation have all option available

Any help is welcome

regards

Declare Private Function SECKFMGetCertifierCtx Lib “nnotes” (Byval pCertFile As String, _

pKfmPW As KFM_PASSWORD, Byval pLogFile As String, pExpDate As TIMEDATE, retCertName As String, _

rethKfmCertCtx As Long, retflsHierarchical As Integer, retwFileVersion As Long) As Integer

############################################################

thread 1/8: [ NLNOTES:0494:0570]

FP=0012d4cc, PC=77f82870, SP=0012d4a8, stkbase=00030000, stksize=49152

############################################################

[ 1] 0x77f82870 ntdll.RtlDeleteElementGenericTable+49 (18,36ee80,0,601a6b33)

[ 2] 0x7c57b3db KERNEL32.CreateThread+599 (12dc64,200,0,0)

@[ 3] 0x601a5ac4 nnotes._OSFaultCleanup@12+1060 (0,0,0,4021a0)

@[ 4] 0x601ae6d7 nnotes._OSNTUnhandledExceptionFilter@4+183 (12e3e0,4021a0,5b1e90,0)

[ 5] 0x7c59bd62 KERNEL32.SetTimeZoneInformation+257 (12e3e0,0,12ffb0,c0000005)

[ 6] 0x78010362 MSVCRT._fcloseall+2041 (0,12e3e0,78010003,12e3e8)

@[ 7] 0x00401e15 NLNOTES._WinMainCRTStartup+335 (12f2c4,1010052,7ffdf000,3)

[ 8] 0x7c59893d KERNEL32.LocalFlags+120 (401cc6,0,c8,100)

############################################################

FATAL THREAD 1/8 [ NLNOTES:0494:0570]

FP=0x0012e794, PC=0x60069ba6, SP=0x0012e794, stksize=0

EAX=0x00000000, EBX=0x04d60758, ECX=0x45443d4f, EDX=0x00000000

ESI=0x45443d4f, EDI=0xffffffff, CS=0x0000001b, SS=0x00000023

DS=0x00000023, ES=0x00000023, FS=0x0000003b, GS=0x00000000 Flags=0x00010246

Exception code: c0000005 (ACCESS_VIOLATION)

############################################################

Subject: Notes Api SECKFMGetCertifierCtx let crash Client

nobody will be able to help without code.

most time such crashes occure because of missing or wrong error handling, which is vital when calling c-api functions from lscript.

Markus Seitz

markus.seitz@icodex.com

Subject: Here’s the code

This is working code on a R5.0.12.

'=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=

’ UserRegistration Class

'=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=

'----- API Constants ----

Private Const MAILSYSTEM_NOTES = 0

Private Const MAILSYSTEM_CCMAIL = 1

Private Const MAILSYSTEM_VIMMAIL = 2

Private Const MAILSYSTEM_NONE = 99

Private Const REG_MAIL_OWNER_ACL_MANAGER = 0

Private Const REG_MAIL_OWNER_ACL_DESIGNER = 1

Private Const REG_MAIL_OWNER_ACL_EDITOR = 2

Private Const KFM_IDFILE_TYPE_DERIVED = 4

Private Const fREGCreateIDFileNow = &H0001

Private Const fREGUSARequested = &H0002

Private Const fREGCreateMailFileNow = &H0004

Private Const fREGCreateAddrBookEntry = &H0008

Private Const fREGOkayToModifyID = &H0010

Private Const fREGOkayToModifyAddrbook = &H0020

Private Const fREGSaveIDInFile = &H0040

Private Const fREGCreateLimitedClient = &H0080

Private Const fREGCreateDesktopClient = &H0100

Private Const fREGSaveIDInAddrBook = &H0200

Private Const fREGCreateMailFileUsingAdminp = &H0800

Private Const fREGSetInternetPassword = &H1000

Private Const fREGExtCreateMailFTIndex = &H00000001

Private Const fREGExtReturnPersonNote = &H00000002

Private Const fREGExtEnforceUniqueShortName = &H00000004

Private Const bufferSize = 1024

Private Const BUFFER_SIZE = 256

Private Const STS_REMOTE = &H4000

Private Const STS_DISPLAYED = &H8000

'----- Type definitions ----

Private Type REG_USERNAME_INFO

LastName As String

FirstName As String

MidInitial As String

OrgUnit As String

ShortName As String

AlternateName As String

AltOrgUnit As String

AltLanguage As String

Spare As String*8

End Type

Private Type REG_MAIL_INFO

MailSystem As Integer

MailOwnerAccess As Integer

DbQuotaSizeLimit As Long

DbQuotaWarningThreshold As Long

MailServerName As String

MailFileName As String

MailTemplateName As String

MailForwardAddress As String

MailACLManager As String

Spare As String*10

End Type

Private Type KFM_PASSWORD

type As Integer 

HashedPassword As String * 48 

End Type

Private Type TIMEDATE

Innards( 1) As Long 

End Type

'----- W32 api declarations ----

Declare Private Sub TimeConstruct Lib “nnotes” (Byval aDate As Long, Byval aTime As Long, TIMEDATE As TIMEDATE)

Declare Private Sub SECKFMCreatePassword Lib “nnotes” (Byval pPassword As String, retHashedPassword As KFM_PASSWORD)

Declare Private Sub SECKFMFreeCertifierCtx Lib “nnotes” (Byval hCertCtx As Long)

Declare Private Function SECKFMGetCertifierCtx Lib “nnotes” (Byval pCertFile As String, _

pKfmPW As KFM_PASSWORD, Byval pLogFile As String, pExpDate As TIMEDATE, retCertName As String, _

rethKfmCertCtx As Long, retflsHierarchical As Integer, retwFileVersion As Long) As Integer

Declare Private Function REGNewUser Lib “nnotes” (Byval hCertCtx As Long, Byval MakeIDType As Integer, _

Byval RegServer As Lmbcs String, RegUserNameInfo As Lmbcs REG_USERNAME_INFO, _

RegMailInfo As Lmbcs REG_MAIL_INFO, Byval Password As Lmbcs String, Byval pGroupList As Long, _

Byval IDFileName As Lmbcs String, Byval Location As Lmbcs String, Byval Comment As Lmbcs String, _

Byval InternetAddress As Lmbcs String, Byval ProfileName As Lmbcs String, Byval LocalAdminName As Lmbcs String, _

Byval Flags As Integer, Byval FlagsExt As Long, Byval MinPasswordLength As Integer, phUserNote As Long, _

phUserNoteNAB As Long, Byval signalstatus As Long, Byval ErrorPathName As Lmbcs String, _

Byval Reserved As Long, Byval Spare As Long ) As Integer

Declare Private Function OSMemAlloc Lib “nnotes” ( Byval T As Integer, Byval N As Long, hM As Long) As Integer

Declare Private Function OSMemFree Lib “nnotes” ( Byval Handle As Long) As Integer

Declare Private Function OSLockObject Lib “nnotes” ( Byval H As Long) As Long

Declare Private Sub OSUnlockObject Lib “nnotes” ( Byval H As Long)

Declare Private Function ListAllocate Lib “nnotes” ( Byval N As Integer, Byval S As Integer, Byval T As Integer, hL As Long, pL As Long, pLSize As Integer) As Integer

Declare Private Function ListAddEntry Lib “nnotes” (Byval hList As Long, Byval fPrefix As Integer, pListSize As Integer, Byval EntryNumber As Integer, Byval text As Lmbcs String, Byval textSize As Integer) As Integer

Declare Private Function OSLoadString Lib “nnotes” (Byval hModule As Long, Byval StringCode As Integer, Byval retBuffer As Lmbcs String, Byval BufferLength As Integer) As Integer

Declare Private Function NSFItemSetTextSummary Lib “nnotes” (Byval hNote As Long, Byval ItemName As String, Byval ItemText As String, Byval TextLength As Integer, Byval summary As Integer) As Integer

Declare Private Function NSFNoteUpdate Lib “nnotes” (Byval hNote As Long, Byval flags As Integer) As Integer

Declare Private Function NSFNoteClose Lib “nnotes” (Byval hNote As Long) As Integer

'----- userRegistration Class ---- v0.2 by Davy Vanherbergen

Public Class userRegistration

Private RegUserNameInfo As REG_USERNAME_INFO

Private RegMailInfo As REG_MAIL_INFO

Private minPasswordLength As Integer

Private flags As Integer

Private flagsExt As Long



'public properties in same sequence as the new registration dialog box



Property Set firstName As String

	RegUserNameInfo.firstName = firstName		

End Property



Property Set midInitial As String

	RegUserNameInfo.midInitial = midInitial		

End Property	



Property Set lastName As String

	RegUserNameInfo.lastName = lastName		

End Property



Property Set shortName As String

	RegUserNameInfo.shortName = shortName		

End Property



Public password As String



Property Set passwordQuality As Integer

	If passwordQuality < 32 Then

		minPasswordLength = passwordQuality

	Else

		minPasswordLength = 31

	End If

End Property



Property Set setInternetPassword As Variant

	If setInternetPassword Then

		flags = flags Or fREGSetInternetPassword

	Else

		flags = flags And (&HFFFF Xor fREGSetInternetPassword)

	End If

End Property



Public internetAddress As String



Property Set mailSystem As String

	Select Case Ucase(mailSystem)

	Case "NONE":

		RegMailInfo.mailSystem = MAILSYSTEM_NONE

	Case "CCMAIL":

		RegMailInfo.mailSystem = MAILSYSTEM_CCMAIL

	Case "VIM"

		RegMailInfo.mailSystem = MAILSYSTEM_VIMMAIL

	Case Else 

		RegMailInfo.mailSystem = MAILSYSTEM_NOTES

	End Select

End Property



Property Set mailFileTemplate As String

	RegMailInfo.MailTemplateName  = mailFileTemplate

End Property



Property Set mailFileName As String

	RegMailInfo.MailFileName  = mailFileName

End Property



Property Set mailServer As String

	RegMailInfo.mailServerName = mailServer

End Property



Property Set mailFileAccessLevel As String

	Select Case Ucase(mailFileAccessLevel)

	Case "EDITOR":

		RegMailInfo.MailOwnerAccess  = REG_MAIL_OWNER_ACL_EDITOR

	Case "DESIGNER":

		RegMailInfo.MailOwnerAccess  = REG_MAIL_OWNER_ACL_DESIGNER

	Case Else 

		RegMailInfo.MailOwnerAccess  = REG_MAIL_OWNER_ACL_MANAGER

	End Select		

End Property



Property Set createMailFileNow As Variant

	If createMailFileNow Then

		flags = flags Or fREGCreateMailFileNow

	Else

		flags = flags And (&HFFFF Xor fREGCreateMailFileNow)

	End If

End Property



Property Set createMailFileUsingAdminp As Variant

	

	If createMailFileUsingAdminp Then

		CreateMailFileNow = False

		flags = flags Or fREGCreateMailFileUsingAdminp

	Else

		flags = flags And (&HFFFF Xor fREGCreateMailFileUsingAdminp)

	End If

	

End Property



Property Set createFTindex As Variant

	If createFTindex Then

		flagsExt = flagsExt Or fREGExtCreateMailFTIndex

	Else

		flagsExt = flagsExt And (&HFFFFFFFF Xor fREGExtCreateMailFTIndex)

	End If

End Property



Property Set mailQuota As Integer

	RegMailInfo.DbQuotaSizeLimit = mailQuota

End Property



Property Set mailThreshold As Integer

	RegMailInfo.DbQuotaWarningThreshold = mailThreshold

End Property



Property Set isNorthAmerican As Variant

	If isNorthAmerican Then

		flags = flags Or fREGUSARequested

	Else

		flags = flags And (&HFFFF Xor fREGUSARequested)

	End If

End Property



Property Set storeIDinNAB As Variant

	If storeIDinNAB Then

		flags = flags Or fREGSaveIDInAddrBook

	Else

		Me.storeIDInFile = True

		flags = flags And (&HFFFF Xor fREGSaveIDInAddrBook)

	End If

End Property



Property Set storeIDInFile As Variant

	If storeIDInFile Then

		flags = flags Or fREGSaveIDInFile

	Else

		Me.storeIDInNAB = True

		flags = flags And (&HFFFF Xor fREGSaveIDInFile)

	End If

End Property



Public idFile As String



Public setupProfile As String



Property Set UniqueOrgUnit As String

	RegUserNameInfo.orgUnit = UniqueOrgUnit

End Property



Public location As String



Public localadmin As String



Public comment As String	



Property Set AltLanguage As String

	RegUserNameInfo.AltLanguage = AltLanguage

End Property



Property Set AlternateName As String

	RegUserNameInfo.AlternateName = AltName

End Property



Property Set altOrgUnit As String

	RegUserNameInfo.AltOrgUnit = altOrgUnit

End Property



Property Set createNABEntry As Variant

	If createNABEntry Then

		flags = flags Or fREGCreateAddrBookEntry

	Else

		flags = flags And (&HFFFF Xor fREGCreateAddrBookEntry)

	End If

End Property



Property Set createIDfile As Variant

	If createIDfile Then

		flags = flags Or fREGCreateIDFileNow

	Else

		flags = flags And (&HFFFF Xor fREGCreateIDFileNow)

	End If

End Property





Public addToGroups As Variant



'other private members

Private hCertCtx As Long 

Private phUserNote As Long

Private phUserNoteNAB As Long

Private errorpathname As String	

Private hashedPassword As KFM_PASSWORD

Private certLog As String

Private certExpires As TIMEDATE

Private certID As String

Private NABServer As String

Private itemsToAdd As Integer

Private itemnames() As String

Private itemvalues() As String



'UserRegistration Class Constructor



Public Sub new(registrationServer As String, certifierID As String, _

certifierpwd As String , expirationDate As notesdatetime )

	

	itemsToAdd = 0

	Redim itemnames(0)

	Redim itemvalues(0)

	

	NABServer = registrationServer

	

	Set certdb = New NotesDatabase( registrationServer, "certlog.nsf") 

	If certdb.IsOpen = False Then 

		Error 666, "Certlog.nsf not found on " + registration

		Exit Sub 

	End If 

	certLog = registrationServer + "!!certlog.nsf" 		

	

	'hash pwd

	Call SECKFMCreatePassword(certifierpwd, hashedPassword) 

	

	'expiration date

	Dim JulianDate As Double  

	JulianDate = GregorianToJulian(Year( expirationDate.LSGMTTime), Month( expirationDate.LSGMTTime),		Day( expirationDate.LSGMTTime)) 

	Call TimeConstruct(JulianDate, &HFFFFFFFF, certExpires) 

	

	certID = certifierID

	

	'Setting some default flags

	CreateIDFile = True	'create ID file

	CreateNABEntry = True 'create NAB entry

	CreateMailFileNow = False 'create mailFile in background

	CreateMailFileUsingAdminP = True

	

	

End Sub





'Register Method



Public Function register(force As Variant)

	

	Dim retflsHierarchical As Integer 

	Dim retCertName As String 

	Dim retwFileVersion As Long 

	Dim result As Integer

	Dim nullvalue As Long

	

	nullvalue = 0

	

	If force Then

		Flags = flags Or fREGOkayToModifyID Or fREGOkayToModifyAddrbook

	Else

		flags = flags And ( &HFFFF Xor ( fREGOkayToModifyID Or fREGOkayToModifyAddrbook ) )

	End If

	

	'always return notehandle

	flagsExt = flagsExt Or fREGExtReturnPersonNote

	

	'Get certifier context

	Stop

	result = SECKFMGetCertifierCtx( certID, HashedPassword, certLog, certExpires, retCertName, hCertCtx, retflsHierarchical, retwFileVersion) 

	If result > 0 Then Call flagError(result,"Could not create certifier context.")

	

	

	Dim pList As Long

	Dim hList As Long

	

	hList = BuildGroupList()

	

	If hList Then

		'refresh pointers

		OSUnlockObject(hList)

		pList = OSLockObject(hList)

	End If

	

	

	'Regiser user		

	result = REGNewUser (hCertCtx, KFM_IDFILE_TYPE_DERIVED, NABServer, RegUserNameInfo, _

	RegMailInfo, Password, pList, IDFile, Location, Comment, InternetAddress, ProfileName, _

	LocalAdmin, Flags, FlagsExt, MinPasswordLength, phUserNote, phUserNoteNAB, nullValue, _

	ErrorPathName, nullValue, nullValue ) 

	

	If hList Then

		OSUnlockObject(hList)

		OSMemFree(hList)

	End If		

	

	If result > 0 Then Call flagError(result,"Could not register user.")

	

	'Free certifier  context

	Call SECKFMFreeCertifierCtx( hCertCtx) 

	

	

	'Update nabnote with extra items

	If itemsToAdd = 0 Then Exit Function

	

	If phUsernote = 0 Then

		Error 69, "note handle to person document is empty"

	Else

		Dim itemname As String

		Dim itemvalue As String

		

		For i = 0 To (itemsToAdd-1)

			itemname = itemnames(i) + Chr$(0)

			itemvalue = itemvalues(i)

			result = NSFItemSetTextSummary (phUserNote, itemname, itemvalue, Len(itemvalue), True)

			If result > 0 Then Call flagError(result,"Could not set item: " + itemnames(i))	

		Next

		

		result = NSFNoteUpdate( phUserNote, 0)

		If result > 0 Then Call flagError(result,"Could not update note")

		result = NSFNoteClose( phUserNote )

		If result > 0 Then Call flagError(result,"Could not close note")

		

	End If

	

End Function





Public Function setStringItem(itemname As String, itemvalue As String)

	

	If Trim(itemname) = "" Then Exit Function

	

	Redim Preserve itemnames(itemsToAdd)

	Redim Preserve itemvalues(itemsToAdd)

	

	itemnames(itemsToAdd) = itemname

	itemvalues(itemsToAdd) = itemvalue

	

	itemsToAdd = itemsToAdd + 1

	

End Function



Private Function GregorianToJulian(y1 As Long, m1 As Integer, d As Double) As Double 

	

	y& = y1 

	m% = m1 

	If m% < 3 Then 

		m% = m% + 12 

		y& = y& - 1 

	End If 

	GregorianToJulian = d# + ( 153 * m% - 457) / 5 + Int( 365.25# * y&) - Int( y& * .01#) + Int( y& * .0025#) + 1721119# 

	

End Function 



Private Function BuildGroupList() As Long

	

	Dim newlist() As String

	Dim ListEntries As Integer

	Dim rethList As Long

	Dim retpList As Long

	Dim listSize As Integer

	Dim result As Integer

	Dim j As Integer

	

	ListEntries = Ubound(addToGroups)

	

	If Isempty(AddToGroups) Then	

		BuildGroupList = 0

		Exit Function

	Elseif Trim(AddToGroups(0)) = "" Then

		BuildGroupList = 0

		Exit Function

	End If

	

	'Build LIST structure

	result = ListAllocate(0, 0, False, retHList, retpList, listSize)		

	If result > 0 Then Call flagError(result,"Could not allocate list.")

	

	OSUnlockObject(retHList)

	buildGroupList = retHList

	

	Dim grp As String

	Dim grplen As Integer

	

	For j = 0 To ListEntries

		grp = addToGroups(j)

		grplen = Len(addToGroups(j))

		result = ListAddEntry(retHList, False, listSize, j, grp, grplen)

		If result > 0 Then Call flagError(result,"Could not add entry to list.")

	Next

	

End Function



Private Function flagError (errorNumber As Integer, addmsg As String)

	

	Dim bufferSize As Integer

	Dim buffer As String*BUFFER_SIZE

	Dim stringLength As Integer

	Dim msg As String

	

	bufferSize = BUFFER_SIZE

	

	If errornumber = 0 Then Exit Function	

	If errornumber And STS_DISPLAYED Then

		errornumber = errornumber And ( &HFFFF Xor STS_DISPLAYED)

	End If		

	If errornumber And STS_REMOTE Then

		msg = "Remote Error: "

		errornumber = errornumber And ( &HFFFF Xor STS_REMOTE)

	End If

	

	stringLength = OSLoadString(0, errornumber, buffer, bufferSize - 1)

	

	If stringLength > 0 Then

		Error errornumber, addmsg + " - " + msg + Left(buffer, stringLength) + "."

	Else

		Error 69, addmsg + " - Couldn't load Error String for Error " + Cstr(errornumber)

	End If

	

End Function

End Class

Subject: RE: Here’s the code

that’s too complicated for easy reproduction. (some lines are missing cr/lf also)

why don’t you try to debug it, and find out on which exact line it’s crashing?

and post values of variables involved. this info along with the code you posted might really help to find out what’s going wrong.

to be true, code really looks like the one creating it knows what he’s doing. error handling looks fine, api-declarations also.

if you like you could also send me a small db where it’s easy to reproduce (just essential code).

Markus Seitz

markus.seitz@icodex.com