Example #1
0
        public IActionResult Delete(int agentid, string dirname)
        {
            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(Unauthorized());
            }

            try
            {
                Directory.Delete("tmp/" + agentid + "/" + dirname, true);
                return(Ok(0));
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return(StatusCode(500));
            }
        }
Example #2
0
        public IActionResult Post(User u)
        {
            string username = "";

            if (HttpContext != null && HttpContext.Request != null)
            {
                username = ProductsController.getUsername(HttpContext.Request.Headers["Authorization"]);
            }
            if (username != Startup.Configuration["adminUser"])
            {
                return(Unauthorized());
            }

            Console.WriteLine("user="******"htpasswd", "-b /etc/apache2/.htpasswd " + u.Name + " " + u.Info);
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Post exception: " + ex.Message);
            }

            return(Ok(u));
        }
Example #3
0
        public IActionResult Get()
        {
            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(Unauthorized());
            }

            List <string> dirs = new List <string>();

            foreach (var v in Directory.EnumerateDirectories("tmp"))
            {
                dirs.Add(v.Substring(1 + v.IndexOf('/')));
            }

            return(Ok(dirs));
        }
Example #4
0
        new public ActionResult File(string id, string agentid, string filename)
        {
            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["readonlyUser"])
            {
                return(Unauthorized());
            }

            try
            {
                //System.Console.WriteLine(id + " " + agentid + " " + filename);
                return(new FileStreamResult(new FileStream("tmp/" + agentid + "/" + filename, FileMode.Open),
                                            filename.EndsWith(".zip") ? "application/zip" : "image/jpeg"));
            }
            catch (Exception)
            {
                return(NotFound());
            }
        }
Example #5
0
        public ActionResult Files(string agentid, string id)
        {
            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(Unauthorized());
            }

            try
            {
                createCSV(agentid, id);
                System.IO.File.Delete("/tmp/test.zip");
                System.IO.Compression.ZipFile.CreateFromDirectory("./tmp/" + agentid + "/" + id + "/", "/tmp/test.zip",
                                                                  System.IO.Compression.CompressionLevel.Optimal, true);

                return(new PhysicalFileResult("/tmp/test.zip", "application/zip"));
            }
            catch (Exception ex)
            {
                return(Ok(ex.Message));
            }

            //return Ok(agentid + " " + id);
        }
Example #6
0
        public IActionResult Get(int id)
        {
            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(Unauthorized());
            }

            List <Dirs> dirs = new List <Dirs>();

            foreach (var v in Directory.EnumerateDirectories("tmp/" + id))
            {
                var files = Directory.EnumerateFiles(v);

                HashSet <string> dict = new HashSet <string>();
                foreach (var file in files)
                {
                    string key   = file.Substring(file.IndexOf('_') + 1);
                    int    index = key.LastIndexOf('/');
                    if (index != -1)
                    {
                        key = key.Substring(index + 1);
                    }

                    if (!dict.Contains(key))
                    {
                        dict.Add(key);
                    }
                }
                string s = "";
                foreach (var dic in dict)
                {
                    s += dic + ' ';
                }

                dirs.Add(
                    new Dirs()
                {
                    Name  = v.Substring(1 + v.LastIndexOf('/')),
                    Size  = files.Count().ToString(),
                    Files = s
                }
                    );
            }

            dirs.Sort(new DirsSorter());

            return(Ok(dirs));
        }
Example #7
0
        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);
        }
Example #8
0
        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));
        }
Example #9
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));
        }