ifms-configpc/config.cpp
2024-09-04 12:04:50 -04:00

210 lines
8.6 KiB
C++

#include "var.h"
int main ()
{
//Cor verde
system("color a");
//strings do nome do computador
string patri = "";
string buffer = "";
//dados do arquivo de configuracao do wifi
string file_wifi_adm_data = "";
file_wifi_adm_data = "<WLANProfile xmlns='http://www.microsoft.com/networking/WLAN/profile/v1'><name>"+SSID_WIFI_ADM+"</name><SSIDConfig><SSID><hex>49464D532D50502D4D4151</hex><name>"+SSID_WIFI_ADM+"</name></SSID><nonBroadcast>true</nonBroadcast></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>"+PASSWORD_WIFI_ADM+"</keyMaterial></sharedKey></security></MSM><MacRandomization xmlns='http://www.microsoft.com/networking/WLAN/profile/v3'><enableRandomization>false</enableRandomization><randomizationSeed>3251268096</randomizationSeed></MacRandomization></WLANProfile>";
string file_wifi_acad_data = "";
file_wifi_acad_data = "<WLANProfile xmlns='http://www.microsoft.com/networking/WLAN/profile/v1'><name>"+SSID_WIFI_ACAD+"</name><SSIDConfig><SSID><hex>49464D532D50502D41434144454D49434F</hex><name>"+SSID_WIFI_ACAD+"</name></SSID><nonBroadcast>true</nonBroadcast></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security><authEncryption><authentication>WPA2PSK</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>"+PASSWORD_WIFI_ACAD+"</keyMaterial></sharedKey></security></MSM><MacRandomization xmlns='http://www.microsoft.com/networking/WLAN/profile/v3'><enableRandomization>false</enableRandomization><randomizationSeed>879094211</randomizationSeed></MacRandomization></WLANProfile>";
//variavel que armazena o diretorio temp
const string path_temp = getenv("TEMP");
//caminho do arquivo de configuracao do wifi adm
string path_wifi_adm = path_temp;
path_wifi_adm += "\\IFMS-PP-MAQ.xml";
//caminho do arquivo de configuracao do wifi acad
string path_wifi_acad = path_temp;
path_wifi_acad += "\\IFMS-PP-ACADEMICO.xml";
//caminho do script
string path_ps1 = path_temp;
path_ps1 += "\\joinDomain.ps1";
int opt, cont;
int connect = 0;
int ping = 0;
//ativando e alterando a senha dos usuarios locais
printf("Ativando o usuario ADMINISTRADOR!\n");
string str_user_adm_local_active = "";
str_user_adm_local_active = "cmd /c net user "+USER_ADM_LOCAL+" /active:yes";
system(str_user_adm_local_active.c_str());
printf("Alterando a senha do usuario ADMINISTRADOR!\n");
string str_pw_adm_local_set = "";
str_pw_adm_local_set = "cmd /c net user "+USER_ADM_LOCAL+" "+PASSWORD_ADM_LOCAL;
system(str_pw_adm_local_set.c_str());
printf("Alterando a senha do usuario SERTI!\n");
string str_pw_serti_local_set = "";
str_pw_serti_local_set = "cmd /c net user "+USER_SERTI_LOCAL+" "+PASSWORD_SERTI_LOCAL;
system(str_pw_serti_local_set.c_str());
printf("\nDeseja ingressar o PC em qual Dominio?\n1-ADMINISTRATIVO\n2-ACADEMICO\n0-NENHUM\nOpcao: ");
scanf("%d", &opt);
if(opt == 1){
printf("Adicionando a rede WI-FI %s\n", SSID_WIFI_ADM.c_str());
//cria o arquivo perfil xml
ofstream outfile (path_wifi_adm.c_str());
outfile << file_wifi_adm_data << endl;
outfile.close();
//conecta na rede wifi
string str_wifi_adm = "";
str_wifi_adm = "cmd /c netsh wlan add profile filename="+path_wifi_adm+" user=all";
system(str_wifi_adm.c_str());
//tenta conectar e testa a conexao com o ping
cont = 0;
string str_connect = "";
string str_ping = "";
do{
str_connect = "cmd /c netsh wlan connect "+SSID_WIFI_ADM;
connect = system(str_connect.c_str());
str_ping = "cmd /c ping -n 1 "+PING_ADM;
ping = system(str_ping.c_str());
if(cont > 0){
printf("\nTentativa %d de 5", cont+1);
Sleep(3000);
}else if(cont == 4){
printf("\nNAO FOI POSSIVEL SE CONECTAR!\n");
system("pause");
return 0;
}
cont++;
}while(connect != 0 && ping != 0);
//exclui o arquivo xml
remove(path_wifi_adm.c_str());
//solicita ao usuario o numero de patrimonio
printf("\nAdicione o Numero do Patrimonio (Sem os 0s A ESQUERDA) ou Nome do Computador: ");
do{
cin >> buffer;
//verifica o tamanho do numero de patrimonio para completar
if(buffer.length() > 8){
printf("\nNo Maximo 8 digitos!!!\n");
}else{
patri = "PP-";
for(int i=0; i < 8-buffer.length(); i++){
patri += "0";
}
}
}while(buffer.length() > 8);
patri += buffer;
//ativa script powershell
system("powershell Set-ExecutionPolicy RemoteSigned");
//cria o script powershell adm
string str_ps_adm = "";
str_ps_adm = "$usr = '"+USER_AD_ADM+"@"+DOMAIN_AD_ADM+"'\n$pw = '"+PASSWORD_AD_ADM+"' | ConvertTo-SecureString -asPlainText -Force\n$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)\nif ( $hostname -eq '"+patri+"' ){\nAdd-Computer -DomainName "+DOMAIN_AD_ADM+" -Credential $creds -Force -Options JoinWithNewName,AccountCreate -OUPath 'OU=PP-Administrativo,OU=PP-Computadores,OU=PP-Outros,OU=IFMS-Outros,DC=ifms,DC=edu,DC=br'\n}else {\nRename-Computer -NewName '"+patri+"'\nSleep 5\nAdd-Computer -DomainName "+DOMAIN_AD_ADM+" -Credential $creds -Force -Options JoinWithNewName,AccountCreate -OUPath 'OU=PP-Administrativo,OU=PP-Computadores,OU=PP-Outros,OU=IFMS-Outros,DC=ifms,DC=edu,DC=br'}";
//cria arquivo powershell
ofstream outfile1 (path_ps1.c_str());
outfile1 << str_ps_adm << endl;
outfile1.close();
//executa powershell
// string execPs = "powershell " + path_ps1;
// system(execPs.c_str());
//deleta arquivo powershell
// remove(path_ps1.c_str());
}else if(opt == 2){
printf("Adicionando a rede WI-FI %s\n", SSID_WIFI_ACAD.c_str());
//cria o arquivo perfil xml
ofstream outfile (path_wifi_acad.c_str());
outfile << file_wifi_acad_data << endl;
outfile.close();
//conecta na rede wifi
string str_wifi_acad = "";
str_wifi_acad = "cmd /c netsh wlan add profile filename="+path_wifi_acad+" user=all";
system(str_wifi_acad.c_str());
//tenta conectar e testa a conexao com o ping
cont = 0;
string str_connect = "";
string str_ping = "";
do{
str_connect = "cmd /c netsh wlan connect "+SSID_WIFI_ACAD;
connect = system(str_connect.c_str());
str_ping = "cmd /c ping -n 1 "+PING_ACAD;
ping = system(str_ping.c_str());
if(cont > 0){
printf("\nTentativa %d de 5", cont+1);
Sleep(3000);
}else if(cont == 4){
printf("\nNAO FOI POSSIVEL SE CONECTAR!\n");
system("pause");
return 0;
}
cont++;
}while(connect != 0 && ping != 0);
//exclui o arquivo xml
remove(path_wifi_acad.c_str());
//solicita ao usuario o numero de patrimonio
printf("\nAdicione o Numero do Patrimonio (SEM OS 0s A ESQUERDA e COM NO MAXIMO 8 DIGITOS): ");
do{
cin >> buffer;
//verifica o tamanho do numero de patrimonio para completar
if(buffer.length() > 8){
printf("\nNo Maximo 8 digitos!!!\n");
}else{
patri = "PP-";
for(int i=0; i < 8-buffer.length(); i++){
patri += "0";
}
}
}while(buffer.length() > 8);
patri += buffer;
//ativa script powershell
system("powershell Set-ExecutionPolicy RemoteSigned");
//cria o script powershell adm
string str_ps_acad = "";
str_ps_acad = "$hostname = hostname\n$usr = '"+USER_AD_ACAD+"@"+DOMAIN_AD_ACAD+"'\n$pw = '"+PASSWORD_AD_ACAD+"' | ConvertTo-SecureString -asPlainText -Force\n$creds = New-Object System.Management.Automation.PSCredential($usr,$pw)\nif ( $hostname -eq '"+patri+"' ){\nAdd-Computer -DomainName "+DOMAIN_AD_ACAD+" -Credential $creds -Force -Options JoinWithNewName,AccountCreate -OUPath 'OU=Computadores,OU=IFMS-PP,DC=acad,DC=pp,DC=ifms,DC=edu,DC=br'\n}else {\nRename-Computer -NewName '"+patri+"'\nSleep 5\nAdd-Computer -DomainName "+DOMAIN_AD_ACAD+" -Credential $creds -Force -Options JoinWithNewName,AccountCreate -OUPath 'OU=Computadores,OU=IFMS-PP,DC=acad,DC=pp,DC=ifms,DC=edu,DC=br'}";
//cria arquivo powershell
ofstream outfile1 (path_ps1.c_str());
outfile1 << str_ps_acad << endl;
outfile1.close();
//executa powershell
string execPs = "powershell " + path_ps1;
system(execPs.c_str());
//deleta arquivo powershell
remove(path_ps1.c_str());
}
system("pause");
return 0;
}