1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include<windows.h>
#include<iostream>
#include<string>
#include <Sddl.h>
#include <stdio.h>
#include <stdlib.h>
#include <UserEnv.h>
using namespace std;
DWORD WINAPI RemoveInactive(char* path, const FILETIME threshold);
FILETIME deleteThreshold(int months);
SYSTEMTIME getLocTime();
int main()
{
//Get the system time
FILETIME threshold = deleteThreshold(10);
RemoveInactive("C:\\Users", threshold);
cout << endl;
system("pause");
}
//Function calculates the threshold to delete profiles after based on a passed months ago int
FILETIME deleteThreshold(int months)
{
SYSTEMTIME time = getLocTime();
//Loop back in time the number of months required
for (int i = 0; i < months; ++i){
if (time.wMonth == 1){
time.wMonth = 12;
time.wYear--;
}
else
time.wMonth--;
}
printf(" The day is: %02d,%02d,%02d\n", time.wDay, time.wMonth, time.wYear);
//Return the fileTime of this time
FILETIME currentTime;
SystemTimeToFileTime(&time, ¤tTime);
return currentTime;
}
//Returns the current time of this system
SYSTEMTIME getLocTime()
{
//Get the current time
SYSTEMTIME time;
GetLocalTime(&time);
return time;
}
DWORD WINAPI RemoveInactive(char* path, const FILETIME threshold)
{
char *tmpPath = path;
string sPath(tmpPath);
WIN32_FIND_DATA FindFileData;
FILETIME lastWriteTime;
string sTmpPath = sPath;
sTmpPath += "\\*.*";
string currFile = "";
HANDLE hFind = FindFirstFile(sTmpPath.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
cout << "Not a valid path\n";
return 0;
}
else
{
do
{
//Get the time the file was last modified
GetFileTime(hFind, NULL, NULL, &lastWriteTime);
//Calculate the months in between the file write and the current time
int del = CompareFileTime(&threshold, &lastWriteTime);
if (del == 0){
cout << "THIS USER BELOW THRESHOLD";
}
else cout << "THIS USER ABOVE THRESHOLD";
//TODO REMOVE THIS WHEN DONE TESTING
del = 0;
//check if its a directory and the time is past threshold
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)&& del == 1)
{
string filePath = FindFileData.cFileName;
//ignore '.' and '..'
if (strcmp(".", filePath.c_str()) && strcmp("..", filePath.c_str()))
{
//Get the SID of this person
string userName = FindFileData.cFileName;
cout <<"Trying: "<< userName << "\n";
//Buffers
LPTSTR DomainName = NULL;
DWORD dwSid = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
PSID userSID = NULL;
LPSTR sidName = NULL;
// First call to LookupAccountSid to get the buffer sizes.
LookupAccountName(NULL, LPSTR(userName.c_str()), userSID, (LPDWORD)&dwSid, DomainName, (LPDWORD)&dwDomainName, &eUse);
// Reallocate memory for the buffers.
userSID = (PSID)GlobalAlloc(
GMEM_FIXED,
dwSid);
DomainName = (LPTSTR)GlobalAlloc(
GMEM_FIXED,
dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (userSID == NULL){
cout << "No space allocated. Cannot retrieve SID.\n";
return -1;
}
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL){
cout << "No space allocated for domain. Cannot retrieve SID\n";
return -1;
}
int success;
success = LookupAccountName(NULL, LPSTR(userName.c_str()), userSID, (LPDWORD)&dwSid, DomainName, (LPDWORD)&dwDomainName, &eUse);
if (success != 0 && IsValidSid(userSID))
{
cout << "Found this user's SID.\n\n";
//Remove this account.
//Convert SID to string
LPTSTR stringedSID = NULL;
ConvertSidToStringSid(userSID, &stringedSID);
DeleteProfile(stringedSID,NULL,NULL);
}
else
cout << "NO SID FOUND.\n\n";
//Free the allocated space
GlobalFree((LPTSTR)userSID);
GlobalFree((LPTSTR)dwDomainName);
}
}
//If it is not a directory, do nothing
//Check next directory
} while (FindNextFile(hFind, &FindFileData) != 0);
//Close this file
FindClose(hFind);
}
return 0;
}
|