📄 win32install.cpp
字号:
batch_logon_name = SE_BATCH_LOGON_NAME; if (error = WarAddUserRightToAccount(lsa, user_name.GetValue().c_str(), service_logon_name.GetValue().c_str())) { WarError err(WAR_ERR_SYSTEM_ERROR, error); err_log << "Failed to set the SE_SERVICE_LOGON_NAME LSA policy" << err << war_endl; } if (error = WarAddUserRightToAccount(lsa, user_name.GetValue().c_str(), tcb_name.GetValue().c_str())) { WarError err(WAR_ERR_SYSTEM_ERROR, error); err_log << "Failed to set the SE_TCB_NAME LSA policy" << err << war_endl; } if (error = WarAddUserRightToAccount(lsa, _L("FTP-Guest"), batch_logon_name.GetValue().c_str())) { WarError err(WAR_ERR_SYSTEM_ERROR, error); err_log << "Failed to set the SE_BATCH_LOGON_NAME LSA policy on FTP-Guest" << err << war_endl; } WarCloseLsa(&lsa); } } catch(WarException& ex) { if (WAR_ERR_OBJECT_EXIST != ex.LocalError()) throw ex; } }}void DoInstall(){ WarLog info_log(WARLOG_INFO, "DoInstall"); info_log << "Installing the server in the current environment." << war_endl; WarLog err_log(WARLOG_ERROR, "DoInstall()"); ConfigFlag = 1; if (ServiceName.empty()) ServiceName = "WARSVR"; WarFtpdLiteWin32NtService my_service(WarCollector<TCHAR>(ServiceName.c_str()).GetValue().c_str()); if (my_service.IsInstalled()) my_service.Uninstall(); // Set initial registry values war_registrypath_t root_reg_path; root_reg_path << "SOFTWARE" << WAR_SYSSLASH << WAR_VENDOR << WAR_SYSSLASH << WAR_DEBUG_PREFIX _T("Servers") << WAR_SYSSLASH << ServiceName; // Open the Win32 registry WarWin32Registry reg_root, reg_defaults, reg_log, reg_log_cout, reg_log_logfile, reg_log_eventlog, reg_modules, reg_modules_fsys, reg_modules_fsys_file, reg_modules_fsys_file_styles, reg_modules_fsys_file_styles_denied, reg_modules_fsys_file_styles_home, reg_modules_fsys_file_styles_public, reg_modules_fsys_file_styles_upload, reg_webadm; reg_root.Create(HKEY_LOCAL_MACHINE,root_reg_path); if (InstallFlag > 0) { // Installation TCHAR current_path[MAX_PATH]; current_path[0] = 0; ::GetCurrentDirectory(sizeof(current_path), current_path); // Set startup path if (RootPath.empty()) reg_root.SetValue(WAR_WINNT_STARTUP_PATH, war_regstr_t(current_path)); else reg_root.SetValue(WAR_WINNT_STARTUP_PATH, war_regstr_t(WarCollector<TCHAR>(RootPath).GetValue().c_str())); reg_root.SetValue(WAR_WINNT_REG_CLASSID, _T(WAR_CLSID_ROOT)); reg_root.SetValue(WAR_WINNT_REG_NAME, _T(PROGRAM)); { TCHAR str_path[MAX_PATH]; if (::GetModuleFileName(NULL, str_path, sizeof(str_path) / sizeof(TCHAR))) { war_syspath_t path = str_path; reg_root.SetValue(WAR_WINNT_PROGRAM_NAME, war_regstr_t(path.GetFilename().GetPath())); } }#if WAR_CRYPTO war_syspath_t key_file; bool do_make_encryption_key = false; key_file << current_path << WAR_SYSSLASH << "server_key.pam"; if (GetFileAttributes(key_file.GetPath()) == 0xffffffff) { info_log << "Generating RSA key for encryption of private date." << war_endl; // Create a new private key for the server RSA *rsa; rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL); if (NULL == rsa) { cerr << "Failed to create a RSA key." << endl; WarThrow(WarError(WAR_ERR_SYSTEM_CALL_FAILED), "RSA_generate_key()"); } FILE *fp = _tfopen(key_file.GetPath(), _T("wt")); if (NULL == fp) { WAR_CERR << _T("Failed to open ") << key_file.GetPath() << _T(" for write.") << endl; WarThrow(WarError(WAR_ERR_SYSTEM_CALL_FAILED), "_tfopen()"); } // Write a non-encrypted key for now. The user can encrypt this later on. if (PEM_write_RSAPrivateKey(fp, rsa, NULL,/*EVP_des_ede3_cbc()*/ NULL, 0, NULL, NULL) == 0) { RSA_free(rsa); fclose(fp); cerr << "Failed to write to the RSA key file." << endl; WarThrow(WarError(WAR_ERR_SYSTEM_CALL_FAILED), "PEM_write_RSAPrivateKey()"); } RSA_free(rsa); fclose(fp); do_make_encryption_key = true; } if (do_make_encryption_key || !reg_root.HaveValue(WAR_WINNT_REG_CRYPTKEY)) { FILE *fp = _tfopen(key_file.GetPath(), _T("rt")); if (NULL == fp) { WAR_CERR << _T("Failed to reopen ") << key_file.GetPath() << _T(" for read.") << endl; WarThrow(WarError(WAR_ERR_SYSTEM_CALL_FAILED), "_tfopen()"); } EVP_PKEY *key = PEM_read_PrivateKey( fp, NULL, NULL, NULL); fclose(fp); if (NULL == key) { WarOpenSslError sys_err; err_log << "PEM_read_PrivateKey() failed when reading pam file " << key_file.GetPath() << sys_err << war_endl; WarThrow(sys_err, "PEM_read_PrivateKey()"); } reg_root.SetValue(WAR_WINNT_REG_PKEYFILE, key_file.GetPath()); char *strkey = BN_bn2hex(key->pkey.dh->priv_key); WarCryptText key_crypt(strkey); WarCollector<TCHAR> data_key(WarCollector<TCHAR>::SM_ERASE); data_key = WarUserAuth::GeneratePasswd(64); war_regstr_t encrypded_data_key; key_crypt.Encrypt(encrypded_data_key, data_key.GetValue()); reg_root.SetValue(WAR_WINNT_REG_CRYPTKEY, encrypded_data_key); //reg_root.SetValue(_T("DEBUG Data Encryption Key"), data_key.GetValue()); memset(strkey, 0, strlen(strkey)); EVP_PKEY_free(key); } // Load the encryption password reg_root.LoadPamFile(); #endif // WAR_CRYPTO#ifdef WAR_WINNT // Install the NT performance-monitor info_log << "Integrating with NT Performance Monitoir." << war_endl; WarPath<WCHAR> dll_path; dll_path << current_path << WAR_SYSSLASH << "wfde-perfmon.dll"; CPrfData::Uninstall(); CPrfData::Install(dll_path.GetPath());#endif // WAR_WINNT // General options info_log << "Setting default options." << war_endl; WarInstallServerOptionsWin32(reg_root); // Logging reg_log.Create(KEY(reg_root, WAR_WINNT_REG_LOG_MODULES)); reg_log_cout.Create(KEY(reg_log, _T("cout"))); reg_log_cout.SetValue(WAR_WINNT_REG_EVENTS, VAL(_T("WARLOG_ERROR;WARLOG_SECURITY;WARLOG_INOUT;WARLOG_SECURITY;WARLOG_WARNINGS;WARLOG_SYSTEM;WARLOG_SNDFILE;WARLOG_RCVFILE;WARLOG_CREDIR;WARLOG_DELDIR;WARLOG_DELFILE;WARLOG_DELFILE;WARLOG_LOGIN;WARLOG_LOGOUT;WARLOG_CREACC;WARLOG_DELACC;WARLOG_INFO"))); reg_log_cout.SetValue(WAR_WINNT_REG_CLASSID, _T(WAR_CLSID_LOG_COUT)); reg_log_cout.SetValue(WAR_WINNT_REG_ENABLE, true); reg_log_logfile.Create(KEY(reg_log, _T("logfile"))); reg_log_logfile.SetValue(WAR_WINNT_REG_EVENTS, VAL(_T("WARLOG_ERROR;WARLOG_SECURITY;WARLOG_INOUT;WARLOG_SECURITY;WARLOG_WARNINGS;WARLOG_SYSTEM;WARLOG_SNDFILE;WARLOG_RCVFILE;WARLOG_CREDIR;WARLOG_DELDIR;WARLOG_DELFILE;WARLOG_DELFILE;WARLOG_LOGIN;WARLOG_LOGOUT;WARLOG_CREACC;WARLOG_DELACC;WARLOG_INFO"))); reg_log_logfile.SetValue(WAR_WINNT_REG_FILENAME, VAL(_T("server.log"))); reg_log_logfile.SetValue(WAR_WINNT_REG_CLASSID, _T(WAR_CLSID_LOG_FILE)); reg_log_logfile.SetValue(WAR_WINNT_REG_ENABLE, true); reg_log_eventlog.Create(KEY(reg_log, WAR_WINNT_REG_LOG_EVENTLOG)); reg_log_eventlog.SetValue(WAR_WINNT_REG_EVENTS, VAL(_T("WARLOG_ERROR;WARLOG_SECURITY;WARLOG_WARNINGS;WARLOG_SYSTEM;WARLOG_INFO"))); reg_log_eventlog.SetValue(WAR_WINNT_REG_TAGNAME, VAL(_T(NT_EVENT_TAG))); reg_log_eventlog.SetValue(WAR_WINNT_REG_CLASSID, _T(WAR_CLSID_LOG_NTEVLOG)); reg_log_eventlog.SetValue(WAR_WINNT_REG_ENABLE, true); // Modules reg_modules.Create(KEY(reg_root, WAR_WINNT_REG_MODULES)); reg_modules_fsys.Create(KEY(reg_modules, WAR_WINNT_REG_FILE_SYSTEMS)); reg_modules_fsys_file.Create(KEY(reg_modules_fsys, _T("file"))); reg_modules_fsys_file.SetValue(WAR_WINNT_REG_ENABLE, 1); reg_modules_fsys_file_styles.Create(KEY(reg_modules_fsys_file, WAR_WINNT_REG_STYLES)); reg_modules_fsys_file_styles_denied.Create(KEY(reg_modules_fsys_file_styles, _T("Denied"))); reg_modules_fsys_file_styles_denied.SetValue(WAR_WINNT_REG_PERMISSIONS, 0x3800); reg_modules_fsys_file_styles_home.Create(KEY(reg_modules_fsys_file_styles, _T("Home"))); reg_modules_fsys_file_styles_home.SetValue(WAR_WINNT_REG_PERMISSIONS, 0x1d3b9); reg_modules_fsys_file_styles_public.Create(KEY(reg_modules_fsys_file_styles, _T("Public"))); reg_modules_fsys_file_styles_public.SetValue(WAR_WINNT_REG_PERMISSIONS, 0x528a); reg_modules_fsys_file_styles_upload.Create(KEY(reg_modules_fsys_file_styles, _T("Upload"))); reg_modules_fsys_file_styles_upload.SetValue(WAR_WINNT_REG_PERMISSIONS, 0x3d3b0);#if WAR_USE_WEBADM // Webadm info_log << "Setting up web interface for administration." << war_endl; reg_webadm.Create(KEY(reg_root, WAR_WINNT_REG_WEBADM)); reg_webadm.SetValue(WAR_WINNT_AUTH_GROUP, VAL(_T("Administrators"))); reg_webadm.SetValue(WAR_WINNT_REG_ENABLE, true);#ifdef DEBUG reg_webadm.SetValue(WAR_WINNT_REG_ADDRESS, VAL(_T("localhost:10010")));#else reg_webadm.SetValue(WAR_WINNT_REG_ADDRESS, VAL(_T("localhost:10021")));#endif#endif // #if WAR_USE_WEBADM // Sites CreateSite(reg_root, _T("Default site"), _T(DEFAULT_FTP_SVR_NAME), ST_DEFAULT); CreateSite(reg_root, _T("Administration"), _T(DEFAULT_ADMIN_SVR_NAME), ST_ADMIN); // Create the NT servic // Update the properties for the NT user account we are // using! WarCollector<TCHAR> service_args, user_name = UserName, user_passwd(UserPasswd.GetValue(), WarCollector<TCHAR>::SM_ERASE), visual_name; info_log << "The service will use the name \"" << ServiceName << "\"." << war_endl; service_args << "--daemon " << "--name=" << ServiceName; visual_name << PROGRAM << " [" << ServiceName << ']'; my_service.Install(visual_name.GetValue().c_str(), service_args.GetValue().c_str(), AutostartFlag, user_name.GetValue().c_str(), user_passwd.GetValue().c_str()); // Erase the password buffers UserPasswd.Reset(); user_passwd.Reset(); } else { // Uninstall info_log << "Uninstalling the service, NT users and groups created by this installation." << war_endl; // The NT service is already removed.#ifdef WAR_WINNT // remove the performance-monitor CPrfData::Uninstall();#endif // Remove users and groups WarWin32Registry reg_modules, reg_modules_auth, reg_modules_auth_winnt, reg_modules_auth_winnt_users, reg_modules_auth_winnt_groups; reg_modules.Create(KEY(reg_root, WAR_WINNT_REG_MODULES)); reg_modules_auth.Create(KEY(reg_modules, WAR_WINNT_REG_AUTH_MODULES)); reg_modules_auth_winnt.Create(KEY(reg_modules_auth, WAR_WINNT_REG_WINNT)); reg_modules_auth_winnt_users.Create(KEY(reg_modules_auth_winnt, WAR_WINNT_REG_USERS)); reg_modules_auth_winnt_groups.Create(KEY(reg_modules_auth_winnt, WAR_WINNT_REG_GROUPS)); war_user_auth_win32_nt_ptr_t auth_ptr = new WarUserAuthWin32Nt; try { WarWin32Registry::keyname_list_t user_list, group_list; reg_modules_auth_winnt_users.EnumKey(user_list); while(!user_list.empty()) { info_log << "Deleting user \"" << user_list.front() << "\"." << war_endl; auth_ptr->DeleteUser(user_list.front().c_str()); user_list.pop_front(); } reg_modules_auth_winnt_groups.EnumKey(group_list); while(!group_list.empty()) { info_log << "Deleting group \"" << group_list.front() << "\"." << war_endl; auth_ptr->DeleteGroup(group_list.front().c_str()); group_list.pop_front(); } } catch(WarException) { } // Remove registry settings reg_root.DeleteKey(); AutostartFlag = -1; // Schedule removal of autostart }}void DoAutostart(){ ConfigFlag = 1; if (AutostartFlag > 0) { // Set up autostart } else { // Remove autostart }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -