Pat O
Cognex, Corp.
If you want access to the SMB Server without any login/user
authentication I'm pretty sure you need to disable user authentication
for the SMB Server. Theoretically this is done by setting
"UseAuthentication"=dword:0 under [HKEY_LOCAL_MACHINE\Services
\SMBServer\Shares]. However, this setting has been known not to work
properly in CE5 and I don't recall having seen any fixes for it yet.
As you can imagine, dropping the user auth also has security
implications since you leave the device pretty much wide open.
Henrik Viklund
http://www.addlogic.se
On Jul 30, 11:54 pm, "patrick.oh...@cognex.com" <pdoh...@gmail.com>
wrote:
Pat O
Cognex, Corp.
There is one other thing that you could think about, if you are the device
OEM and are the ones building the operating systme for it: create your own
custom NETUI and, rather than having it show the password dialog when
requested, have it grab the credentials from a file or via some other non-UI
method.
Paul T.
"patric...@cognex.com" <pdo...@gmail.com> wrote in message
news:1186155205.0...@i13g2000prf.googlegroups.com...
Paul T.
-----
// SetSMSUser.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <Security.h>
#include <Credmgr.h>
#include <lmcons.h>
HINSTANCE hSecurity = NULL;
PSecurityFunctionTableW pSecFuncTableW = NULL;
PFNCECREDREAD pCeCredRead = NULL;
PFNCECREDWRITE pCeCredWrite = NULL;
ACQUIRE_CREDENTIALS_HANDLE_FN_W pAcquireCredentialsHandleW = NULL;
FREE_CREDENTIALS_HANDLE_FN pFreeCredentialsHandle = NULL;
void LoadSecurityLib()
{
if ( !hSecurity )
{
hSecurity = LoadLibraryW(L"secur32.dll");
if (hSecurity)
{
INIT_SECURITY_INTERFACE_W pInitSecurityInterface =
(INIT_SECURITY_INTERFACE_W)GetProcAddressW(hSecurity,
L"InitSecurityInterfaceW");
if (pInitSecurityInterface && (pSecFuncTableW =
pInitSecurityInterface()))
{
pCeCredRead = (PFNCECREDREAD)pSecFuncTableW->Reserved5;
pCeCredWrite = (PFNCECREDWRITE) pSecFuncTableW->Reserved6;
pAcquireCredentialsHandleW = pSecFuncTableW->AcquireCredentialsHandleW;
pFreeCredentialsHandle = pSecFuncTableW->FreeCredentialsHandle;
}
}
}
}
void FreeSecurityLib()
{
if ( hSecurity )
{
FreeLibrary( hSecurity );
hSecurity = NULL;
}
}
static void SplitDomainUserName(PCTSTR pszDomainUser, PTSTR pszDomain,
PDWORD pcchDomain, PTSTR pszUser, PDWORD pcchUser)
{
PCTSTR pch;
DWORD cch;
for (pch= pszDomainUser; *pch; pch++ )
{
if (*pch == '\\')
{
cch = (pch-pszDomainUser);
if (pszDomain && *pcchDomain > cch )
{
memcpy(pszDomain, pszDomainUser, cch*sizeof(TCHAR));
pszDomain[cch] = '\0';
}
*pcchDomain = cch + 1;
cch = _tcslen(pch+1);
if (pszUser && *pcchUser > cch)
{
memcpy(pszUser, pch+1, (cch+1)*sizeof(TCHAR));
}
*pcchUser = cch + 1;
return;
}
}
if (pszDomain)
*pszDomain = '\0';
*pcchDomain = 1;
cch = pch - pszDomainUser;
if (pszUser && *pcchUser > cch)
memcpy(pszUser, pszDomainUser, (cch+1)*sizeof(TCHAR));
*pcchUser = cch+1;
return;
}
bool ReadCredentials()
{
PCECREDENTIAL pCredential = NULL;
bool fPasswordSaved = false;
if (pCeCredRead && pCeCredRead(NULL,CRED_TYPE_DOMAIN_PASSWORD, 0,
&pCredential))
{
fPasswordSaved = !(pCredential->Flags & CRED_FLAGS_PROMPT_NOW);
TCHAR domain[ 256 ];
TCHAR user[ 256 ];
DWORD cchDomain = sizeof( domain ) / sizeof( domain[0] );
DWORD cchUser = sizeof( user ) / sizeof( user[0] );
SplitDomainUserName(pCredential->UserName, domain, &cchDomain, user,
&cchUser);
LocalFree(pCredential);
return true;
}
else
{
DEBUGMSG(1, (TEXT("CeCredRead error\r\n")));
return false;
}
}
BOOL SaveSspCredentials(SEC_WINNT_AUTH_IDENTITY_W *pAuthIdentity)
{
PWSTR Packages[] = {L"NTLM", L"KERBEROS"};
BOOL fRet = TRUE;
int i;
if (!pAcquireCredentialsHandleW || !pFreeCredentialsHandle)
return FALSE;
__try
{
for (i = 0; i < sizeof( Packages ) / sizeof( Packages[0] ); i++)
{
CredHandle hCred;
TimeStamp Lifetime;
if (pAcquireCredentialsHandleW(
NULL, // principal
Packages[i],
SECPKG_CRED_OUTBOUND,
NULL, // LOGON id
pAuthIdentity,
NULL, // get key fn
NULL, // get key arg
&hCred,
&Lifetime
) == NO_ERROR)
{
pFreeCredentialsHandle(&hCred);
}
}
}
__except(1)
{
fRet = FALSE;
DEBUGMSG(1, (TEXT("AcquireCredentialsHandle faulted")));
}
return fRet;
}
bool WriteCredentials( TCHAR *domain, TCHAR *user, TCHAR *password )
{
bool ret = true;
BOOL fSuccess = FALSE;
CECREDENTIAL credential;
int cchUser = _tcslen( user );
int cchDomain = _tcslen( domain );
int cchPassword = _tcslen( password );
// If the password is empty, set a flag telling the security software
// to prompt for the password, when it's needed.
if ( password[0] )
credential.Flags = 0;
else
credential.Flags = CRED_FLAGS_PROMPT_NOW; // prompt for password
credential.Type = CRED_TYPE_DOMAIN_PASSWORD;
credential.CredentialBlobSize = 0;
credential.CredentialBlob = NULL;
credential.Persist = CRED_PERSIST_LOCAL_MACHINE;
credential.UserName = (PWCHAR)LocalAlloc(0, (cchUser+ cchDomain +
2)*sizeof(TCHAR));
if ( credential.UserName )
{
PTCHAR pch = credential.UserName;
// The user name contains the domain and user name, if there is
// a domain. Check here for that and, if there is a domain,
// copy it to the buffer.
if ( cchDomain )
{
memcpy( credential.UserName, domain, cchDomain*sizeof(TCHAR) );
credential.UserName[cchDomain] = '\\';
pch = credential.UserName + cchDomain+1;
}
memcpy(pch, user, cchUser*sizeof(TCHAR));
pch[cchUser] = '\0';
// Write the credential to the registry.
fSuccess = pCeCredWrite(NULL, &credential);
if (!fSuccess)
{
DEBUGMSG(1, (TEXT("CeCredWrite error writing
%s\r\n"),credential.UserName));
ret = false;
}
else
{
DEBUGMSG(1, (TEXT("CeCredWrite success writing %s\r\n"),
credential.UserName));
}
LocalFree( credential.UserName );
}
if ( cchUser )
{
SEC_WINNT_AUTH_IDENTITY_W authIdentity =
{ user, cchUser, domain, cchDomain, password, cchPassword,
SEC_WINNT_AUTH_IDENTITY_UNICODE};
SaveSspCredentials( &authIdentity );
}
// set the device 'default' domain to the same as the user domain
// these domains can be different if desired
HKEY key;
if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T( "\\Ident" ), 0, 0, &key ) ==
ERROR_SUCCESS )
{
RegSetValueEx( key, _T( "ComputerDomain" ), 0, REG_SZ,
(LPBYTE)domain, ( DNLEN + 1 ) * sizeof( TCHAR ) );
RegCloseKey( key );
}
return ret;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
LoadSecurityLib();
// Make up some credentials that we'll use. I'm setting this up for
// no domain.
TCHAR *domain = _T( "" );
TCHAR *user = _T( "paul-t" );
TCHAR *password = _T( "password" );
WriteCredentials( domain, user, password );
FreeSecurityLib();
return 0;
}
I was not able to compile this. I am looking into where the include
files come from. I assume that I do not have something in my image
that I need. Thanks for the code.
Pat O
Paul T.
"patric...@cognex.com" <pdo...@gmail.com> wrote in message
news:1187011025.1...@q4g2000prc.googlegroups.com...