public IEnumerable <User> Get() { List <User> users = new List <User>(); //users.Add(new User(){Name = "test", Ids = "", Info = "Read Only"}); string username = ""; if (HttpContext != null && HttpContext.Request != null) { username = ProductsController.getUsername(HttpContext.Request.Headers["Authorization"]); } if (username != Startup.Configuration["adminUser"]) { users.Add(new User() { Name = username, Ids = "", Info = getType(username) }); return(users); } try { using (StreamReader sr = System.IO.File.OpenText("/etc/apache2/.htpasswd")) { while (!sr.EndOfStream) { string s = sr.ReadLine(); if (s.IndexOf(':') != -1) { s = s.Substring(0, s.IndexOf(':')); } users.Add(new User() { Name = s, Ids = "", Info = getType(s) }); } } } catch (Exception ex) { Console.WriteLine("exception: " + ex.Message); } return(users); }
public IActionResult Put(int id, [FromBody] User u) { string username = ""; if (HttpContext != null && HttpContext.Request != null) { username = ProductsController.getUsername(HttpContext.Request.Headers["Authorization"]); } if (u.Name.IndexOf('"') != -1 || u.Ids.IndexOf('"') != -1) { return(Ok(-2)); } try { Process p = Process.Start("htpasswd", "-bv /etc/apache2/.htpasswd " + username + " \"" + u.Name + "\""); p.WaitForExit(); if (p.ExitCode != 0) { return(Ok(-1)); } } catch (Exception ex) { Console.WriteLine("Put exception: " + ex.Message); } try { Process p = Process.Start("htpasswd", "-b /etc/apache2/.htpasswd " + username + " \"" + u.Ids + "\""); p.WaitForExit(); } catch (Exception ex) { Console.WriteLine("Put exception: " + ex.Message); } return(Ok(0)); }
public async Task <System.Net.Http.HttpResponseMessage> PutFile(int id, string resource) { string username = ""; if (HttpContext != null && HttpContext.Request != null) { username = ProductsController.getUsername(HttpContext.Request.Headers["Authorization"]); } if (string.IsNullOrEmpty(username)) { username = ProductsController.getUsername2(HttpContext.Request.Headers["Cookie"]); } if (username != Startup.Configuration["adminUser"]) { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } int index = resource.IndexOf('/'); string profileName = resource.Substring(index + 1); string filename = String.Format("log-{0}.txt", profileName); if (Request != null && Request.Query.ContainsKey("filename")) { filename = Request.Query["filename"].ToString(); } bool append = false; if (Request != null && Request.Query.ContainsKey("append")) { append = Request.Query["append"].ToString() == "true"; } ProductsRepo repo = new ProductsRepo(); if (repo.GetOne(id) == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } bool history = repo.GetOne(id).UploadHistory; if (filename.EndsWith(".zip")) { history = false; } try { string body = ""; using (var reader = new StreamReader(Request.Body)) { body = await reader.ReadToEndAsync(); } StringReader sr = new StringReader(body); System.Net.Mail.MailMessage mm = Amende.Snorre.MailMessageMimeParser.ParseMessage(sr); string datedir = DateTime.Now.ToString("MMddyyyy"); System.IO.Directory.CreateDirectory("tmp/" + id); if (history) { System.IO.Directory.CreateDirectory("tmp/" + id + "/" + datedir); } Console.WriteLine("count=" + mm.Attachments.Count); if (mm.Attachments.Count > 0) { using (var fileStream = System.IO.File.Open(String.Format(@"{0}/{1}/{2}", "tmp", id, filename), append ? FileMode.Append : FileMode.Create)) { using (MemoryStream ms = new MemoryStream()) { mm.Attachments[0].ContentStream.CopyTo(ms); ms.Position = 0; ms.CopyTo(fileStream); if (append || !checkHash(ms, id, filename)) { history = false; //System.Console.WriteLine("dup hash for " + filename); } } } if (history) { System.IO.File.Copy(String.Format(@"{0}/{1}/{2}", "tmp", id, filename), String.Format(@"{0}/{1}/{2}/{3}_{4}", "tmp", id, datedir, DateTime.Now.Ticks, filename)); } System.IO.Directory.CreateDirectory("wwwroot/WebApp/logs/" + id); System.IO.File.WriteAllText("wwwroot/WebApp/logs/" + id + "/" + filename + "_date.txt", DateTime.Now.ToString()); } else { using (StreamWriter sw = System.IO.File.CreateText("tmp/" + id + "/" + filename)) { sw.Write(mm.Body); } } } catch (Exception ex) { Console.WriteLine("exception" + ex.Message); } return(new HttpResponseMessage(HttpStatusCode.Created)); }