public RESTStatus CancelUploadAgentToServer(SQLLib sql, object dummy, NetworkConnectionInfo ni, Int64 ID) { if (ni.HasAcl(ACLFlags.ComputerLogin) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM FileTransfers WHERE ID=@id AND MachineID=@mid AND Direction in (0,1)", new SQLParam("@mid", ni.Username), new SQLParam("@id", ID))) == 0) { ni.Error = "Invalid ID"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } } string Filename = null; lock (ni.sqllock) { Filename = Settings.Default.DataPath + Convert.ToString(sql.ExecSQLScalar("SELECT ServerFile FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", ni.Username), new SQLParam("@id", ID))); } if (File.Exists(Filename) == true) { try { CommonUtilities.SpecialDeleteFile(Filename); } catch { } } lock (ni.sqllock) { sql.ExecSQLScalar("DELETE FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", ni.Username), new SQLParam("@id", ID)); } return(RESTStatus.Success); }
public RESTStatus DeleteUser(SQLLib sql, NetString User, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Fail); } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM Users WHERE Username=@u", new SQLParam("@u", User.Data))) == 0) { ni.Error = "Invalid User"; ni.ErrorID = ErrorFlags.InvalidID; return(RESTStatus.Fail); } if (User.Data.ToLower().Trim() == "root") { ni.Error = "Invalid User"; ni.ErrorID = ErrorFlags.InvalidID; return(RESTStatus.Fail); } sql.ExecSQL("DELETE FROM Users WHERE Username=@u", new SQLParam("@u", User.Data)); return(RESTStatus.Success); }
public RESTStatus AddUser(SQLLib sql, NetString User, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(User.Data) == true) { ni.Error = "Missing username"; ni.ErrorID = ErrorFlags.InvalidID; return(RESTStatus.Fail); } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM Users WHERE Username=@u", new SQLParam("@u", User.Data.Trim()))) != 0) { ni.Error = "User already exists"; ni.ErrorID = ErrorFlags.InvalidID; return(RESTStatus.Fail); } sql.InsertMultiData("Users", new SQLData("Username", User.Data.Trim()), new SQLData("Name", "new user"), new SQLData("Permissions", 0), new SQLData("Password", "")); return(RESTStatus.Success); }
public RESTStatus ReportUsersList(SQLLib sql, UsersList users, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ComputerLogin) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (users == null) { ni.Error = "Invalid Items"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } users.MachineID = ni.Username; lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE MachineID=@m", new SQLParam("@m", users.MachineID))) == 0) { ni.Error = "Invalid MachineID"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.NotFound); } } lock (ni.sqllock) { sql.ExecSQL("DELETE FROM UsersList WHERE MachineID=@id", new SQLParam("@id", users.MachineID)); } if (users.Users == null) { users.Users = new Dictionary <string, string>(); } foreach (KeyValuePair <string, string> kvp in users.Users) { if (string.IsNullOrWhiteSpace(kvp.Key) == true || string.IsNullOrWhiteSpace(kvp.Value) == true) { continue; } lock (ni.sqllock) { sql.InsertMultiData("UsersList", new SQLData("MachineID", users.MachineID), new SQLData("SID", kvp.Key), new SQLData("Username", kvp.Value)); } } return(RESTStatus.Success); }
static bool FileExistsCompleted(SQLLib sql, Int64 id) { if (Convert.ToInt32(sql.ExecSQLScalar("Select count(*) FROM FileTransfers WHERE ID=@id AND Size=ProgressSize", new SQLParam("@id", id))) == 0) { return(false); } return(true); }
//Telerik Supported output formats: https://docs.telerik.com/reporting/configuring-rendering-extensions public static byte[] GetReportPaperData(SQLLib sql, string Paper, byte[] Default) { object d = sql.ExecSQLScalar("select [data] from ReportPapers where [ID]=@ID", new SQLParam("@id", Paper)); if (d is DBNull || d is null) { return(Default); } return((byte[])d); }
public RESTStatus GetPaperTemplate(SQLLib sql, object dummy, NetworkConnectionInfo ni, string Paper) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (string.IsNullOrWhiteSpace(Paper) == true) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } Paper = Paper.ToUpper().Trim(); if (SupportedNames.Contains(Paper) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } RetPaper = new NetByte(); RetPaper.Data = null; lock (ni.sqllock) { object o = sql.ExecSQLScalar("SELECT [data] from ReportPapers WHERE ID=@id", new SQLParam("@id", Paper)); if (o is DBNull || o is null) { switch (Paper) { case "COMPUTERREPORT": RetPaper.Data = Resources.Computer_Report; break; } } else { RetPaper.Data = (byte[])o; } } return(RESTStatus.Success); }
public RESTStatus ChangeUser(SQLLib sql, UserDetailsPassword User, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Fail); } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM Users WHERE Username=@u", new SQLParam("@u", User.Username))) == 0) { ni.Error = "Invalid User"; ni.ErrorID = ErrorFlags.InvalidID; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(User.NewPassword) == false) { if (MeetPasswordPolicy(User.NewPassword) == false) { ni.Error = "Password policy not met"; ni.ErrorID = ErrorFlags.PWPolicyNotMet; return(RESTStatus.Fail); } string PWMD5REQ = Convert.ToBase64String(Encoding.Unicode.GetBytes(User.NewPassword)); sql.ExecSQL("UPDATE Users SET Password=@p WHERE Username=@u", new SQLParam("@u", User.Username), new SQLParam("@p", PWMD5REQ)); } if (User.Username.ToLower().Trim() == "root") { User.Permissions = SQLTest.AllPermissions; } sql.ExecSQL("UPDATE Users SET Name=@n, Permissions=@p, MustChangePassword=@mchg,EMail=@email,UseLDAP=@UseLDAP,LDAPUsername=@LDAPUsername WHERE Username=@u", new SQLParam("@u", User.Username), new SQLParam("@n", User.Name), new SQLParam("@p", User.Permissions), new SQLParam("@mchg", User.MustChangePassword), new SQLParam("@email", User.EMail), new SQLParam("@useldap", User.UseLDAP), new SQLParam("@LDAPUsername", User.LDAPUsername)); return(RESTStatus.Success); }
public RESTStatus ChangeMyPassword(SQLLib sql, ChangePassword chgpw, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ComputerLogin) == true) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } ni.Error = ""; if (ni.IsLDAP == true) { ni.Error = "Password is LDAP"; ni.ErrorID = ErrorFlags.IsLDAP; return(RESTStatus.Fail); } string PWMD5REQ = Convert.ToBase64String(Encoding.Unicode.GetBytes(chgpw.OldPassword)); if (MeetPasswordPolicy(chgpw.NewPassword) == false) { ni.Error = "Password policy not met"; ni.ErrorID = ErrorFlags.PWPolicyNotMet; return(RESTStatus.Fail); } int Count = Convert.ToInt32(sql.ExecSQLScalar("SELECT Count(*) FROM Users WHERE Username=@u AND Password=@p", new SQLParam("@u", ni.Username), new SQLParam("@p", PWMD5REQ))); if (Count < 1) { ni.Error = "Invalid old password"; ni.ErrorID = ErrorFlags.InvalidPassword; return(RESTStatus.Fail); } string PWMD5New = Convert.ToBase64String(Encoding.Unicode.GetBytes(chgpw.NewPassword)); sql.ExecSQLNQ("UPDATE Users SET Password=@pw, MustChangePassword=0 WHERE Username=@u", new SQLParam("@u", ni.Username), new SQLParam("@pw", PWMD5New)); ni.MustChangePassword = false; return(RESTStatus.NoContent); }
public RESTStatus ComputerLogin(SQLLib sql, RecoveryLogon logon, NetworkConnectionInfo ni, string IPAddress) { RD = new RecoveryData(); if (Fox_LicenseGenerator.SDCLicensing.ValidLicense == false) { RD.Worked = false; return(RESTStatus.Fail); } if (Fox_LicenseGenerator.SDCLicensing.TestExpiry() == false) { RD.Worked = false; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(logon.UCID) == true) { RD.Worked = false; return(RESTStatus.Fail); } if (Settings.Default.UseContract == true) { if (string.IsNullOrWhiteSpace(logon.ContractID) == true || string.IsNullOrWhiteSpace(logon.ContractPassword) == true) { RD.Worked = false; return(RESTStatus.Fail); } } string newID = NetworkConnection.NewSession(); ni = NetworkConnection.GetSession(newID); if (NetworkConnectionProcessor.InitNi(ni) == false) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.ServerError); } sql = ni.sql; if (Settings.Default.UseContract == true) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT Count(*) FROM Contracts WHERE ContractID=@id AND ContractPassword=@pw AND Disabled=0", new SQLParam("@id", logon.ContractID), new SQLParam("@pw", logon.ContractPassword))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE ContractID=@id AND UCID=@u", new SQLParam("@id", logon.ContractID), new SQLParam("@u", logon.UCID))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } } if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE UCID=@u", new SQLParam("@u", logon.UCID))) == 0) { NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } SqlDataReader dr = sql.ExecSQLReader("SELECT * FROM ComputerAccounts WHERE UCID=@u", new SQLParam("@u", logon.UCID)); dr.Read(); string Check = Convert.ToString(dr["CPUName"]).Trim(); Check += Convert.ToString(dr["ComputerModel"]).Trim(); Check += Convert.ToString(dr["BIOS"]).Trim(); string MD5 = MD5Utilities.CalcMD5(Check); if (MD5.ToLower() != logon.MoreMachineHash.ToLower()) { dr.Close(); NetworkConnection.DeleteSession(newID); RD.Worked = false; return(RESTStatus.Fail); } RD.MachineID = Convert.ToString(dr["MachineID"]); RD.MachinePassword = Convert.ToString(dr["Password"]); RD.Worked = true; dr.Close(); NetworkConnection.DeleteSession(newID); return(RESTStatus.Success); }
public RESTStatus ReportNetadapterConfig(SQLLib sql, ListNetworkAdapterConfiguration netadapters, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ComputerLogin) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (netadapters == null) { ni.Error = "Invalid Items"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } netadapters.MachineID = ni.Username; lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM ComputerAccounts WHERE MachineID=@m", new SQLParam("@m", netadapters.MachineID))) == 0) { ni.Error = "Invalid MachineID"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.NotFound); } } lock (ni.sqllock) { sql.ExecSQL("DELETE FROM NetworkConfigSuppl WHERE MachineID=@id", new SQLParam("@id", netadapters.MachineID)); sql.ExecSQL("DELETE FROM NetworkConfig WHERE MachineID=@id", new SQLParam("@id", netadapters.MachineID)); } if (netadapters.Items == null) { netadapters.Items = new List <NetworkAdapterConfiguration>(); } List <int> IFIndex = new List <int>(); foreach (NetworkAdapterConfiguration ncfg in netadapters.Items) { if (IFIndex.Contains(ncfg.InterfaceIndex) == true) { continue; } IFIndex.Add(ncfg.InterfaceIndex); lock (ni.sqllock) { sql.InsertMultiData("NetworkConfig", new SQLData("MachineID", netadapters.MachineID), new SQLData("InterfaceIndex", ncfg.InterfaceIndex), new SQLData("IPEnabled", ncfg.IPEnabled), new SQLData("MACAddress", ncfg.MACAddress == null ? "" : ncfg.MACAddress.Trim()), new SQLData("ServiceName", ncfg.ServiceName == null ? "" : ncfg.ServiceName.Trim()), new SQLData("SettingsID", ncfg.SettingsID == null ? "" : ncfg.SettingsID.Trim()), new SQLData("Description", ncfg.Description == null ? "" : ncfg.Description.Trim()), new SQLData("DHCPEnabled", ncfg.DHCPEnabled), new SQLData("DHCPServer", ncfg.DHCPServer == null ? "" : ncfg.DHCPServer.Trim()), new SQLData("DNSDomain", ncfg.DNSDomain == null ? "" : ncfg.DNSDomain.Trim()), new SQLData("DNSHostName", ncfg.DNSHostName == null ? "" : ncfg.DNSHostName.Trim()), new SQLData("Caption", ncfg.Caption == null ? "" : ncfg.Caption.Trim()), new SQLData("DHCPLeaseExpires", ncfg.DHCPLeaseExpires), new SQLData("DHCPLeaseObtained", ncfg.DHCPLeaseObtained), new SQLData("WINSEnableLMHostsLookup", ncfg.WINSEnableLMHostsLookup), new SQLData("WINSHostLookupFile", ncfg.WINSHostLookupFile == null ? "" : ncfg.WINSHostLookupFile.Trim()), new SQLData("WINSPrimaryServer", ncfg.WINSPrimaryServer == null ? "" : ncfg.WINSPrimaryServer.Trim()), new SQLData("WINSSecondaryServer", ncfg.WINSSecondaryServer == null ? "" : ncfg.WINSSecondaryServer.Trim()), new SQLData("WINSScopeID", ncfg.WINSScopeID == null ? "" : ncfg.WINSScopeID.Trim())); } if (ncfg.IPAddress == null) { ncfg.IPAddress = new List <string>(); } if (ncfg.IPSubnet == null) { ncfg.IPSubnet = new List <string>(); } if (ncfg.DefaultIPGateway == null) { ncfg.DefaultIPGateway = new List <string>(); } if (ncfg.DNSDomainSuffixSearchOrder == null) { ncfg.DNSDomainSuffixSearchOrder = new List <string>(); } if (ncfg.DNSServerSearchOrder == null) { ncfg.DNSServerSearchOrder = new List <string>(); } lock (ni.sqllock) InsertSupplData(sql, netadapters.MachineID, ncfg.InterfaceIndex, 1, ncfg.IPAddress); lock (ni.sqllock) InsertSupplData(sql, netadapters.MachineID, ncfg.InterfaceIndex, 2, ncfg.IPSubnet); lock (ni.sqllock) InsertSupplData(sql, netadapters.MachineID, ncfg.InterfaceIndex, 3, ncfg.DefaultIPGateway); lock (ni.sqllock) InsertSupplData(sql, netadapters.MachineID, ncfg.InterfaceIndex, 4, ncfg.DNSDomainSuffixSearchOrder); lock (ni.sqllock) InsertSupplData(sql, netadapters.MachineID, ncfg.InterfaceIndex, 5, ncfg.DNSServerSearchOrder); } return(RESTStatus.Success); }
public RESTStatus GetPaperData(SQLLib sql, ReportPaperRequest req, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (req == null) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(req.Name) == true) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } if (SupportedNames.Contains(req.Name) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } RetPaper = new NetByte(); switch (req.Name) { case "COMPUTERREPORT": if (req.MachineIDs == null) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } if (req.MachineIDs.Count == 0) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } List <string> MachinesOK = new List <string>(); foreach (string m in req.MachineIDs) { string Query = ""; List <SQLParam> SQLParams = new List <SQLParam>(); lock (ni.sqllock) { if (Computers.MachineExists(sql, m) == false) { continue; } } if (req.From == null && req.To == null) { Query = "Select count(*) from Reporting where machineid=@mid"; SQLParams.Add(new SQLParam("@mid", m)); } if (req.From != null && req.To == null) { Query = "Select count(*) from Reporting where machineid=@mid AND Reported>=@d1"; SQLParams.Add(new SQLParam("@mid", m)); SQLParams.Add(new SQLParam("@d1", req.From.Value)); } if (req.From == null && req.To != null) { Query = "Select count(*) from Reporting where machineid=@mid AND Reported<=@d1"; SQLParams.Add(new SQLParam("@mid", m)); SQLParams.Add(new SQLParam("@d1", req.To.Value)); } if (req.From != null && req.To != null) { Query = "Select count(*) from Reporting where machineid=@mid and Reported between @d1 and @d2"; SQLParams.Add(new SQLParam("@mid", m)); SQLParams.Add(new SQLParam("@d1", req.From.Value)); SQLParams.Add(new SQLParam("@d2", req.To.Value)); } lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar(Query, SQLParams.ToArray())) == 0) { continue; } } MachinesOK.Add(m); } if (MachinesOK.Count == 0) { ni.Error = "Machines has no report"; ni.ErrorID = ErrorFlags.NoData; return(RESTStatus.Fail); } lock (ni.sqllock) { RetPaper.Data = RenderReport.RenderMachineReport(sql, MachinesOK, req.From, req.To, ReportingFlagsPaper.ReReport, "PDF"); } break; } return(RESTStatus.Success); }
public RESTStatus GetFile(SQLLib sql, HttpListenerRequest request, HttpListenerResponse response, object dummy, NetworkConnectionInfo ni, Int64 id) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false && ni.HasAcl(ACLFlags.ComputerLogin) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; response.AddHeader("Content-Type", "text/plain; charset=UTF-8"); response.StatusCode = 403; response.StatusDescription = "Forbidden"; byte[] data = Encoding.UTF8.GetBytes("403 - Forbidden."); response.ContentLength64 = data.LongLength; Stream output = response.OutputStream; output.Write(data, 0, data.Length); return(RESTStatus.Denied); } lock (ni.sqllock) { if (FileExistsCompleted(sql, id) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidID; response.AddHeader("Content-Type", "text/plain; charset=UTF-8"); response.StatusCode = 404; response.StatusDescription = "Not found"; byte[] data = Encoding.UTF8.GetBytes("404 - Not found."); response.ContentLength64 = data.LongLength; Stream output = response.OutputStream; output.Write(data, 0, data.Length); return(RESTStatus.NotFound); } } if (ni.HasAcl(ACLFlags.ComputerLogin) == true) { lock (ni.sqllock) { if (Convert.ToString(sql.ExecSQLScalar("SELECT MachineID FROM FileTransfers WHERE ID=@id AND Size=ProgressSize AND (Direction=0 OR Direction=2)", new SQLParam("@id", id))) != ni.Username) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidID; response.AddHeader("Content-Type", "text/plain; charset=UTF-8"); response.StatusCode = 404; response.StatusDescription = "Not found"; byte[] data = Encoding.UTF8.GetBytes("404 - Not found."); response.ContentLength64 = data.LongLength; Stream output = response.OutputStream; output.Write(data, 0, data.Length); return(RESTStatus.NotFound); } } } string Filename = null; lock (ni.sqllock) { Filename = Convert.ToString(sql.ExecSQLScalar("SELECT ServerFile FROM FileTransfers WHERE ID=@id", new SQLParam("@id", id))); } if (File.Exists(Settings.Default.DataPath + Filename) == false) { ni.Error = "Cannot find local file"; ni.ErrorID = ErrorFlags.FileSystemError; response.AddHeader("Content-Type", "text/plain; charset=UTF-8"); response.StatusCode = 500; response.StatusDescription = "Server Error"; byte[] data = Encoding.UTF8.GetBytes("500 - Server Error."); response.ContentLength64 = data.LongLength; Stream output = response.OutputStream; output.Write(data, 0, data.Length); return(RESTStatus.ServerError); } Downloader.ReadFileChunked(Settings.Default.DataPath + Filename, request, response); return(RESTStatus.Success); }
public RESTStatus AppendUpload(SQLLib sql, FileUploadAppendData upload, NetworkConnectionInfo ni) { if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false) { ni.Error = "Access denied"; ni.ErrorID = ErrorFlags.AccessDenied; return(RESTStatus.Denied); } if (upload == null) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Computers.MachineExists(sql, upload.MachineID) == false) { ni.Error = "Invalid data"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } } if (upload.Data.Length != upload.Size) { ni.Error = "Invalid SZ"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (string.IsNullOrWhiteSpace(upload.MD5) == true) { ni.Error = "No MD5"; ni.ErrorID = ErrorFlags.InvalidValue; return(RESTStatus.Fail); } if (upload.MD5.ToLower() != MD5Utilities.CalcMD5(upload.Data).ToLower()) { ni.Error = "MD5 Error"; ni.ErrorID = ErrorFlags.CheckSumError; return(RESTStatus.Fail); } lock (ni.sqllock) { if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM FileTransfers WHERE ID=@id AND MachineID=@mid AND [Size]!=[ProgressSize] AND Direction=3", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))) == 0) { ni.Error = "Invalid ID"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } } Int64 TotalSZ; Int64 ProgressSize; lock (ni.sqllock) { TotalSZ = Convert.ToInt64(sql.ExecSQLScalar("SELECT [Size] FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } lock (ni.sqllock) { ProgressSize = Convert.ToInt64(sql.ExecSQLScalar("SELECT ProgressSize FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (ProgressSize + upload.Size > TotalSZ) { ni.Error = "Too many data"; ni.ErrorID = ErrorFlags.ChunkTooLarge; return(RESTStatus.Fail); } string Filename = null; lock (ni.sqllock) { Filename = Settings.Default.DataPath + Convert.ToString(sql.ExecSQLScalar("SELECT ServerFile FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (File.Exists(Filename) == false) { if (ProgressSize > 0) { ni.Error = "FS Error - Missing"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } } using (FileStream str = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { str.Seek(0, SeekOrigin.End); str.Write(upload.Data, 0, upload.Size); } FileInfo fi = new FileInfo(Filename); if (ProgressSize + upload.Size != fi.Length) { ni.Error = "FS Error - Final SZ Error"; ni.ErrorID = ErrorFlags.FileSystemError; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET ProgressSize=@psz, DTUpdated=getutcdate() WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@psz", fi.Length), new SQLParam("@id", upload.ID)); } if (fi.Length == TotalSZ) { string MD5 = MD5Utilities.CalcMD5File(Filename); string MD5DB = ""; lock (ni.sqllock) { MD5DB = Convert.ToString(sql.ExecSQLScalar("SELECT MD5Sum FROM FileTransfers WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID))); } if (MD5.ToLower() != MD5DB.ToLower()) { ni.Error = "Final MD5 error"; ni.ErrorID = ErrorFlags.InvalidData; return(RESTStatus.Fail); } lock (ni.sqllock) { sql.ExecSQL("UPDATE FileTransfers SET Direction=0 WHERE ID=@id AND MachineID=@mid", new SQLParam("@mid", upload.MachineID), new SQLParam("@id", upload.ID)); } } return(RESTStatus.Success); }