ADImport.c
/*
Josh santomieri
santomieri systems
joshs@santsys.com
*/
/* file format: UserName, Password, LastName, FirstName */
/* this program creates a file for import into a windows 2000 active directory server */
/* compile it and run it to try it out */
#ifndef _INC_STDIO
	#include 
#endif
#ifndef _INC_STRING
	#include 
#endif

#define READ_FILE "Import.csv"
#define WRITE_FILE "ADImport.ldf"
#define PROF_BAT "prof_bat.bat"
#define USR_BAT "usr_bat.bat"
#define PWD_BAT "pwd_bat.bat"
#define ADIMPORT_INI "adimport.ini"

#define VERSION "1.0.0.1"

#ifndef MAX_PATH
	#define MAX_PATH 260
#endif

void Usage()
{
	printf("\n\n");
	printf("ADImport.exe { OPTIONS }\n\n");
	printf("Options:\n");
	printf("\t-f [DataFile] - Path to file to read.\n");
	printf("\t-p [DN_Params] - Dn Params, OU=, CN=, modifications... etc...\n");
	printf("\t-d [Prompt_for_Description]\n");
	printf("\t-s [S_Path] - Logon Script Path.\n");
	printf("\t-pp [Prof_Path] - Profile Path.\n");
	printf("\t-hp [Home_Path] - Home Directory Path.\n");
	printf("\t-hd [Home_Drive] - Home Drive Letter.\n");
	printf("\t-v - Displays Version\n\n");
	printf("Example:\n\tADImport.exe Import.csv CN=Users -pp \\\\mysrv\\prof$\\ -hp \\\\mysrv\\Users$\\ -hd H:\n\n");
	printf("File Format (CSV):\n");
	printf("\tUserName, Password, LastName, FirstName\n\n");
}

void Version()
{
	printf("\nActive Directory Import\n");
	printf("Version: %s\n\n", VERSION);
	printf("Josh Santomieri\n");
	printf("Santomieri Systems\n\n");
}

// function for removing whitespace at the beginning of the values
int remspace(char *cstr)
{
	int iLen = 0, i;
	int iTmp = 0;

	iLen = (int)strlen(cstr) + 1;
	if(iLen <= 1)
		return 0;

	if(cstr[0] == ' ')
	{
		for(i = 0; i< iLen; i++)
		{
			cstr[i] = cstr[i+1];
		}
		cstr[iLen - 1] = '\0';
	}

	return iLen - 1;
}

int main(int ivar, char **vars)
{
	char cBuff[255];
	char cDN[255], cParam[255], cScript[255], cProf[255], cHPath[255], cHDrive[4];
	char cDesc[255];
	char cFName[25], cLName[25];
	char cPwd[21];
	char cUser[21];
	char *token = NULL;
	int i = 0, iUseParam = 0, iUseFile = 0, iUseScript = 0, iUseProf = 0, iUseHPath = 0, iUseHDrive = 0;
	int iUseDesc = 0;

	FILE *fin, *fout, *fprof, *fusr, *fpwd, *adi;

	char cFileName[5][MAX_PATH];
	char cFileBuff[MAX_PATH];
	/* Null All The Buffers */
	memset(cFileName[0], '\0', sizeof(cFileName[0]));
	memset(cFileName[1], '\0', sizeof(cFileName[1]));
	memset(cFileName[2], '\0', sizeof(cFileName[2]));
	memset(cFileName[3], '\0', sizeof(cFileName[3]));
	memset(cFileName[4], '\0', sizeof(cFileName[4]));
	memset(cScript, '\0', sizeof(cScript));
	memset(cProf, '\0', sizeof(cProf));
	memset(cHPath, '\0', sizeof(cHPath));
	memset(cHDrive, '\0', sizeof(cHDrive));
	memset(cDesc, '\0', sizeof(cDesc));

	i = 0;
	memset(cParam, '\0', sizeof(cParam));
	while(i < ivar)
	{
		if(strcmp(vars[i], "/?") == 0)
		{
			Usage();
			return 0;
		}

		if(strcmp(vars[i], "-v") == 0)
		{
			Version();
			return 0;
		}

		if(strcmp(vars[i], "-f") == 0)
		{
			strcpy(cFileName[0], vars[i+1]);
			strcpy(cFileBuff, vars[i+1]);
			cFileBuff[strlen(cFileBuff) - 4] = '\0';
			sprintf(cFileName[1], "%s.ldf", cFileBuff);
			sprintf(cFileName[2], "prof_%s.bat", cFileBuff);
			sprintf(cFileName[3], "usr_%s.bat", cFileBuff);
			sprintf(cFileName[4], "pwd_%s.bat", cFileBuff);
			printf("File: %s\n", vars[i+1]);
			iUseFile = 1;
		}

		if(strcmp(vars[i], "-p") == 0)
		{
			printf("Params: %s\n", vars[i+1]);
			strcpy(cParam, vars[i+1]);
			iUseParam = 1;
		}

		if(strcmp(vars[i], "-d") == 0)
		{
			printf("Enter Description: ");
			fgets(cDesc, 255, stdin);
			if(strlen(cDesc) > 0)
				cDesc[strlen(cDesc) - 1] = '\0';
			printf("Description: %s\n", cDesc);
			iUseDesc = 1;
		}

		if(strcmp(vars[i], "-s") == 0)
		{
			printf("Script: %s\n", vars[i+1]);
			strcpy(cScript, vars[i+1]);
			iUseScript = 1;
		}

		if(strcmp(vars[i], "-pp") == 0)
		{
			printf("Profile Path: %s\n", vars[i+1]);
			strcpy(cProf, vars[i+1]);
			iUseProf = 1;
		}

		if(strcmp(vars[i], "-hp") == 0)
		{
			printf("Home Path: %s\n", vars[i+1]);
			strcpy(cHPath, vars[i+1]);
			iUseHPath = 1;
		}

		if(strcmp(vars[i], "-hd") == 0)
		{
			printf("Home Drive: %s\n", vars[i+1]);
			strcpy(cHDrive, vars[i+1]);
			iUseHDrive = 1;
		}

		i++;
	}
	
	if(!iUseFile)
	{
		strcpy(cFileName[0], READ_FILE);
		strcpy(cFileName[1], WRITE_FILE);
		strcpy(cFileName[2], PROF_BAT);
		strcpy(cFileName[3], USR_BAT);
		strcpy(cFileName[4], PWD_BAT);
		printf("File: %s\n", READ_FILE);
	}

	puts("\n");

	if((adi = fopen(ADIMPORT_INI, "rb")) == NULL)
	{
		if((adi = fopen(ADIMPORT_INI, "wb")) == NULL)
		{
			printf("\nError Creating INI File!!!\n");
			return 0;
		}
		else
		{
			fprintf(adi, "# INI file for ADImport.exe\r\n");
			fprintf(adi, "# set up 'dn:' entry for ldif file\r\n");
			fprintf(adi, "# Example: DC=lab-38,DC=dhs,DC=dist,DC=dixonusd,DC=org\r\n");
		}

		printf("The file '%s' needs your input (Please Set up the DN entry.).\n\n", ADIMPORT_INI);
		fclose(adi);
		return 0;
	}
	else
	{
		memset(cDN, '\0', sizeof(cDN));
		while((!feof(adi)) && (fgets(cBuff, 255, adi) != NULL))
		{
			if(cBuff[0] != '#' || cBuff[0] != '\r' || cBuff[0] != '\n' || cBuff[0] != ' ')
			{
				strcpy(cDN, cBuff);
			}
		}
		fclose(adi);
	}

	if((fin = fopen(cFileName[0], "rb")) == NULL)
	{
		printf("\nError Opening File for Reading!\n\n");
		Usage();
		return 0;
	}

	if((fout = fopen(cFileName[1], "wb")) == NULL)
	{
		printf("\nError opening file for writing!\n");
		Usage();
		return 0;
	}

	if((fprof = fopen(cFileName[2], "wb")) == NULL)
	{
		printf("\nError opening file for writing!\n%s\n\n", PROF_BAT);
		Usage();
		return 0;
	}

	if((fusr = fopen(cFileName[3], "wb")) == NULL)
	{
		printf("\nError opening file for writing!\n%s\n\n", USR_BAT);
		Usage();
		return 0;
	}

	if((fpwd = fopen(cFileName[4], "wb")) == NULL)
	{
		printf("\nError opening file for writing!\n%s\n\n", PWD_BAT);
		Usage();
		return 0;
	}

	while((!feof(fin)) && (fgets(cBuff, 255, fin) != NULL))
	{
		memset(cFName, '\0', sizeof(cFName));
		memset(cLName, '\0', sizeof(cLName));
		memset(cPwd, '\0', sizeof(cPwd));
		memset(cUser, '\0', sizeof(cUser));

		token = strtok(cBuff, ",\n\r");
		if(token == NULL)
		{
			printf("\nError getting User!\n");
			Usage();
			return 0;
		}
		sprintf(cUser,"%s", token);
		
		token = strtok(NULL, ",\n\r");
		if(token == NULL)
		{
			printf("\nError getting pwd!\n");
			Usage();
			return 0;
		}
		sprintf(cPwd, "%s", token);
		remspace(cPwd);

		token = strtok(NULL, ",\n\r");
		if(token == NULL)
		{
			printf("\nError getting fName!\n");
			Usage();
			return 0;
		}
		sprintf(cLName, "%s", token);
		remspace(cLName);

		token = strtok(NULL, ",\n\r");
		if(token == NULL)
		{
			printf("\nError getting lName!\n");
			Usage();
			return 0;
		}
		sprintf(cFName, "%s", token);
		remspace(cFName);

		puts(cUser);
		puts(cPwd);
		puts(cLName);
		puts(cFName);
		puts("\n");

		/* ldf Info File */
		//fprintf(fout, "dn: CN=%s,CN=Users,DC=lab-38,DC=dhs,DC=dist,DC=dixonusd,DC=org\r\n", cUser);
		if(iUseParam)
		{
			fprintf(fout, "dn: CN=%s,%s,%s\r\n", cUser, cParam, cDN);
		}
		else
		{
			fprintf(fout, "dn: CN=%s,%s\r\n", cUser, cDN);
		}

		fprintf(fout, "changetype: add\r\n");
		fprintf(fout, "cn: %s\r\n", cUser);
		fprintf(fout, "objectClass: user\r\n");
		if(iUseDesc)
		{
			fprintf(fout, "description: %s\r\n", cDesc);
		}
		fprintf(fout, "sn: %s\r\n", cLName);
		fprintf(fout, "givenName: %s\r\n", cFName);
		fprintf(fout, "displayName: %s\r\n", cUser);
		fprintf(fout, "userPassword: %s\r\n", cPwd);
		fprintf(fout, "userPrincipalName: %s\r\n", cUser);
		fprintf(fout, "SAMAccountName: %s\r\n", cUser);
		fprintf(fout, "userAccountControl: %d\r\n", (1 + 512 + 65536));
		if(iUseProf)
			fprintf(fout, "profilePath: %s%s\r\n", cProf, cUser);
		if(iUseScript)
            fprintf(fout, "scriptPath: %s\r\n", cScript);
		if(iUseHPath)
			fprintf(fout, "homeDirectory: %s%s\r\n", cHPath, cUser);
		if(iUseHDrive)
			fprintf(fout, "homeDrive: %s\r\n", cHDrive);

		fprintf(fout, "\r\n");

		/* Password bat file */
		fprintf(fpwd, "net user %s %s\r\n", cUser, cPwd);

		/* profile bat file */
		fprintf(fprof, "md %s\r\n", cUser);
		fprintf(fprof, "md \"%s\\Application Data\"\r\n", cUser);
		fprintf(fprof, "md %s\\Desktop\r\n", cUser);
		fprintf(fprof, "md %s\\Favorites\r\n", cUser);
		fprintf(fprof, "md %s\\Personal\r\n", cUser);
		fprintf(fprof, "md %s\\SendTo\r\n", cUser);
		fprintf(fprof, "md \"%s\\Start Menu\"\r\n", cUser);
		fprintf(fprof, "cacls %s /T /C /G Administrator:F %s:C < yes.txt\r\n", cUser, cUser); 

		/* user bat file */
		fprintf(fusr, "md %s\r\n", cUser);
		fprintf(fusr, "cacls %s /T /C /G Administrator:F %s:C < yes.txt\r\n", cUser, cUser); 
	}

	fclose(fin);
	fclose(fout);
	fclose(fprof);
	fclose(fusr);
	fclose(fpwd);
	return 0;
}