вторник, 9 августа 2011 г.

Check windows user credentials

C++ realisation

#include "stdafx.h"
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#define ERROR_CODE 1;

int main(int argc, char* argv[])
{
if(argc != 3){
return ERROR_CODE;
}

char acUserName[100];
DWORD nUserName = sizeof(acUserName);
GetUserName(acUserName, &nUserName);

if(strcmp(argv[1], acUserName)){
return ERROR_CODE;
}

LPCSTR lpszUsername = argv[1];
LPCSTR lpszPassword = argv[2];
LPCSTR lpszDomain = NULL;

HANDLE phToken;

if (!LogonUser(lpszUsername, lpszDomain, lpszPassword,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &phToken)) {
return ERROR_CODE;
}

return 0;
}

понедельник, 8 августа 2011 г.

How to validate user credentials in windows?

Validating with user name and password:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace ConsoleApplication1
{
class Program
{
[DllImport("ADVAPI32.DLL")]
public static extern bool LogonUser(String lpszUsername,
String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);

const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1;

private static bool isValidUser(string userName, string domainName, string passsWord) {
bool Validateuser; int token1;
Validateuser = LogonUser(userName, domainName,
passsWord, LOGON32_LOGON_NETWORK, 0, out token1);

if (Validateuser) {
return true;
} else {
return false;
}
}

static void Main(string[] args) {
if (args.Length != 2) {
Environment.Exit(1);
return;
}

String fullName = WindowsIdentity.GetCurrent().Name;
String un = fullName.Split('\\')[1]; //getting user name
String dom = fullName.Split('\\')[0];//getting domain name
String userName = args[0];

if (!userName.Equals(un)) {
Environment.Exit(1);
return;
}

bool isValid = isValidUser(userName, dom, args[1]);
if (!isValid) {
Environment.Exit(1);
return;
}
}
}
}

суббота, 6 августа 2011 г.

How to check user credentials?

linux version:
/**============================================================================

File......: linux_user_validator.c
Author....: Vyacheslav Kovalyov
Created...: 8/6/11

Description: Linux user validator.
How to make: g++ linux_user_validator.c -lpam -lpam_misc
Dependence : libpam-dev

============================================================================*/

#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>
#include <dlfcn.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define ERR_CODE 1;
#define COPY_STRING(s) (s) ? strdup(s) : NULL;

static char* user;
static char* pwd;
static int PAM_conv (int, const struct pam_message**,
struct pam_response**, void*);
static struct pam_conv PAM_converse = {
PAM_conv,
NULL
};

static int PAM_conv (int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr) {
int replies = 0;
struct pam_response *reply = NULL;

reply = (struct pam_response *) malloc(sizeof(struct pam_response) * num_msg);
if (!reply) return PAM_CONV_ERR;

for (replies = 0; replies < num_msg; replies++) {

//SecurId requires this syntax.
if (! strcmp(msg[replies]->msg,"Enter PASSCODE: ")) {
reply[replies].resp = COPY_STRING(pwd);
}

if (! strcmp(msg[replies]->msg,"Password: ")) {
reply[replies].resp = COPY_STRING(pwd);
}

//Mac OS X
if (! strcmp(msg[replies]->msg,"Password:")) {
reply[replies].resp = COPY_STRING(pwd);
}

// HP-UX
if (! strcmp(msg[replies]->msg,"System Password:")) {
reply[replies].resp = COPY_STRING(pwd);
}
}
*resp = reply;
return PAM_SUCCESS;
}

int main(int argc, char** argv)
{
if (argc < 3) {
return ERR_CODE;
}

user = argv[1];
pwd = argv[2];

static struct pam_conv pam_conversation = { misc_conv, NULL };
pam_handle_t* pamh;

int res = pam_start(argv[0], user, &PAM_converse, &pamh);

if (res == PAM_SUCCESS) {
res = pam_authenticate(pamh, 0);
}

if (res == PAM_SUCCESS) {
res = pam_acct_mgmt(pamh, 0);
}

pam_end(pamh, res);

return res == PAM_SUCCESS ? 0 : ERR_CODE;
}