#include <stdio.h>
#include <assert.h>
#include <windows.h> 
#include <lm.h>

int main(int argc, char *argv[])
{
	LPWKSTA_USER_INFO_1 pBuf = NULL;
	LPWKSTA_USER_INFO_1 pTmpBuf;
	DWORD dwLevel = 1;
	DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
	DWORD dwEntriesRead = 0;
	DWORD dwTotalEntries = 0;
	DWORD dwResumeHandle = 0;
	DWORD i;
	DWORD dwTotalCount = 0;
	NET_API_STATUS nStatus;
	wchar_t pszServerName[100];
	char ascii_server_name[100], *username, *password, temp_server_name[98], file_line[97];
	NETRESOURCE     nr;
	FILE *fp;
	BOOL bFileMode = FALSE;

	if (argc < 4)
	{
	  fprintf(stderr, "Find User usage: \n\nfind_user <username> <password> [<server_name_or_ip> | -f <server_list_file>]\n");
	  exit(1);
	}
	if (!stricmp(argv[3], "-f") && argc == 5)
		bFileMode = TRUE;

	printf("[*] Scanning for logged on users...\n\n");
	printf("Server Name\t\tUsername\n");
	printf("------------------------------------------------------\n");

	if (bFileMode)
	{
		fp = fopen(argv[4], "r");
		while (fgets(file_line, 97, fp))
		{
			sscanf(file_line, "%s\n", temp_server_name);
			username = argv[1];
			password = argv[2];
			strcpy(ascii_server_name, "\\\\");
			strncat(ascii_server_name, temp_server_name, 97);
			ascii_server_name[99] = '\0';
			mbstowcs(pszServerName, ascii_server_name, strlen(ascii_server_name)+1);

			nr.dwType    		 = RESOURCETYPE_DISK;
   			nr.lpLocalName       = NULL;
   			nr.lpProvider        = NULL;
   			nr.lpRemoteName 	 = ascii_server_name;
			WNetAddConnection2A(&nr, password, username, 0);
			
			do // begin do
			{
				nStatus = NetWkstaUserEnum(pszServerName,
										 dwLevel,
										 (LPBYTE*)&pBuf,
										 dwPrefMaxLen,
										 &dwEntriesRead,
										 &dwTotalEntries,
										 &dwResumeHandle);
			  //
			  // If the call succeeds,
			  //
			  if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
			  {
				 if ((pTmpBuf = pBuf) != NULL)
				 {
					//
					// Loop through the entries.
					//
					for (i = 0; (i < dwEntriesRead); i++)
					{
					   assert(pTmpBuf != NULL);

					   if (pTmpBuf == NULL)
					   {
						  //
						  // Only members of the Administrators local group
						  //  can successfully execute NetWkstaUserEnum
						  //  locally and on a remote server.
						  //
						  fprintf(stderr, "An access violation has occurred\n");
						  break;
					   }
					   //
					   // Print the user logged on to the workstation. 
					   //
		               
					   if (!wcschr(pTmpBuf->wkui1_username, L'$'))
					   {
							printf("%s", temp_server_name);
							wprintf(L"\t\t%s\\%s\n", pTmpBuf->wkui1_logon_domain, pTmpBuf->wkui1_username);
					   }
					   pTmpBuf++;
					   dwTotalCount++;
					}
				 }
			  }
			  //
			  // Otherwise, indicate a system error.
			  //
			  else
				 fprintf(stderr, "%s\t\tError: %d\n", temp_server_name, nStatus);
			  //
			  // Free the allocated memory.
			  //
			  if (pBuf != NULL)
			  {
				 NetApiBufferFree(pBuf);
				 pBuf = NULL;
			  }
		   }
		   // 
		   // Continue to call NetWkstaUserEnum while 
		   //  there are more entries. 
		   // 
		   while (nStatus == ERROR_MORE_DATA); // end do
		   //
		   // Check again for allocated memory.
		   //
		   if (pBuf != NULL)
			  NetApiBufferFree(pBuf);

		}
	}
	else 
	{
		username = argv[1];
		password = argv[2];
		strcpy(ascii_server_name, "\\\\");
		strncat(ascii_server_name, argv[3], 97);
		ascii_server_name[99] = '\0';
		mbstowcs(pszServerName, ascii_server_name, strlen(ascii_server_name)+1);

		nr.dwType    		 = RESOURCETYPE_DISK;
   		nr.lpLocalName       = NULL;
   		nr.lpProvider        = NULL;
   		nr.lpRemoteName 	 = ascii_server_name;
		WNetAddConnection2A(&nr, password, username, 0);
		
		do // begin do
		{
			nStatus = NetWkstaUserEnum(pszServerName,
									 dwLevel,
									 (LPBYTE*)&pBuf,
									 dwPrefMaxLen,
									 &dwEntriesRead,
									 &dwTotalEntries,
									 &dwResumeHandle);
		  //
		  // If the call succeeds,
		  //
		  if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
		  {
			 if ((pTmpBuf = pBuf) != NULL)
			 {
				//
				// Loop through the entries.
				//
				for (i = 0; (i < dwEntriesRead); i++)
				{
				   assert(pTmpBuf != NULL);

				   if (pTmpBuf == NULL)
				   {
					  //
					  // Only members of the Administrators local group
					  //  can successfully execute NetWkstaUserEnum
					  //  locally and on a remote server.
					  //
					  fprintf(stderr, "An access violation has occurred\n");
					  break;
				   }
				   //
				   // Print the user logged on to the workstation. 
				   //
	               
				   if (!wcschr(pTmpBuf->wkui1_username, L'$'))
				   {
						printf("%s", argv[3]);
						wprintf(L"\t\t%s\\%s\n", pTmpBuf->wkui1_logon_domain, pTmpBuf->wkui1_username);
				   }
				   pTmpBuf++;
				   dwTotalCount++;
				}
			 }
		  }
		  //
		  // Otherwise, indicate a system error.
		  //
		  else
			 fprintf(stderr, "%s\t\tError: %d\n", argv[3], nStatus);
		  //
		  // Free the allocated memory.
		  //
		  if (pBuf != NULL)
		  {
			 NetApiBufferFree(pBuf);
			 pBuf = NULL;
		  }
	   }
	   // 
	   // Continue to call NetWkstaUserEnum while 
	   //  there are more entries. 
	   // 
	   while (nStatus == ERROR_MORE_DATA); // end do
	   //
	   // Check again for allocated memory.
	   //
	   if (pBuf != NULL)
		  NetApiBufferFree(pBuf);
	}

	return 0;
}