Ejemplo n.º 1
0
        public static int CheckForGroupGID(string group)
        {
            int retint = -1;

            using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
            {
                dbCon.Username = "******";
                dbCon.Password = "******";
                dbCon.Connect();

                string command = string.Format("SELECT `groupid` FROM `groupDB` WHERE `groupname`='{0}'", group);
                retint = dbCon.SearchForInt(command, "groupid");

                command = string.Empty;
                dbCon.Close();
            }
            return(retint);
        }
        public static void DeleteUser(Packet packet)
        {
            Packet sendPacket;

            try
            {
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Delete user to the server [{1}]", packet.ClientIP, packet.Username));
                string username = packet.Argument;
                if (username == "root" || username == Environment.UserName)
                {
                    ConsoleUtils.Print(string.Format("[{0}]'s Denied: User requesting to remove unallowed name: {1} [{2}]", packet.ClientIP, username, packet.Username));
                    sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                    packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                    return;
                }

                UserInfoInserter.DeleteUser(username);
                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("DELETE FROM `userDB` WHERE (`username`= '{0}')", username);
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }
                UserCleaner.RemoveUserInEveryGroup(username);
                UserCleaner.RemoveUserInEveryProject(username);

                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Successfully deleting user {1} [{2}]", packet.ClientIP, username, packet.Username));
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Delete User Error: {0}", e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
        public static void CreateUser(Packet packet)
        {
            Packet sendPacket;

            try
            {
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Create new user to the server [{1}]", packet.ClientIP, packet.Username));
                string[] store    = packet.Argument.Split(char.Parse("|"));
                string   name     = store[0];
                string   username = store[1];
                if (username.ToLower().Contains("root") || username == Environment.UserName)
                {
                    ConsoleUtils.Print(string.Format("Adding User Error: User requesting to create unallowed name [{0}]", username));
                    sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                    packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                    return;
                }
                string password = store[2];
                UserInfoInserter.AddUser(username, password);
                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("INSERT INTO `exceeddb`.`userDB` (`name`, `username`, `password`, `dateCreated`," +
                                                   " `token`, `tokenExpired`) VALUES ('{0}', '{1}', '{2}', '{3}', '', '')", name, username, CryptSharp.Crypter.Sha512.Crypt(password), DateTime.Now.ToString());
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                ConsoleUtils.Print(string.Format("[{0}]'s Success: Successfully creating user {1} [{2}]", packet.ClientIP, username, packet.Username));
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Adding User Error: {0}", e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
        public static void ChangePassword(Packet packet)
        {
            Packet sendPacket;

            try
            {
                string[] store    = packet.Argument.Split(char.Parse("|"));
                string   username = store[0];
                string   password = store[1];
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Changing password of user [{1}] in the server [{2}]", packet.ClientIP, username, packet.Username));
                if (username == "root" || username == Environment.UserName)
                {
                    ConsoleUtils.Print(string.Format("Change Password Error: User requesting to change password of unallowed name [{0}]", username));
                    sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                    packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                    return;
                }
                UserInfoInserter.ChangeUserPassword(username, password);
                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("UPDATE `userDB` SET `password`='{0}' WHERE `username`='{1}'", CryptSharp.Crypter.Sha512.Crypt(password), username);
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Change Password Error: {0}", e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
Ejemplo n.º 5
0
        public static void RemoveUserInEveryGroup(string username)
        {
            string[] groupstack = new string[0];
            using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
            {
                dbCon.Username = "******";
                dbCon.Password = "******";
                dbCon.Connect();

                string command = "SELECT `groupname` FROM `groupDB`";
                groupstack = dbCon.GetListCollumnValue(command, "groupname");

                command = string.Empty;
                dbCon.Close();
            }

            if (groupstack.Length == 0)
            {
                return;
            }

            foreach (string group in groupstack)
            {
                try
                {
                    MySQLAccountParsing.RemoveUserFromGroup(username, group);
                }
                catch (Exception ex)
                {
                    if (!(ex is MissingMemberException))
                    {
                        throw;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public bool HasValidToken()
        {
            try
            {
                string convertedToken;
                string result;
                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command  = string.Format("SELECT * FROM `adminDB` WHERE `username`='{0}'", Username);
                    string expToken = dbCon.SearchForString(command, "tokenExpired");
                    if (string.IsNullOrEmpty(expToken))
                    {
                        return(false);
                    }

                    DateTime validate = DateTime.Parse(expToken);
                    if (DateTime.Now > validate)
                    {
                        return(false);
                    }

                    result         = dbCon.SearchForString(command, "token");
                    convertedToken = Token.GetString();
                    command        = string.Empty;

                    dbCon.Close();
                }
                return(convertedToken == result);
            }
            catch (Exception e)
            {
                ConsoleUtils.Print("HasValidToken(): " + e.Message);
                return(false);
            }
        }
        public static void RemoveGroup(Packet packet)
        {
            Packet sendPacket;

            try
            {
                string group = packet.Argument;
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Remove group from the server [{2}]", packet.ClientIP, group, packet.Username));

                UserInfoInserter.RemoveGroup(group);

                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("DELETE FROM `groupDB` WHERE (`groupname`= '{0}')", group);
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }

                GroupCleaner.RemoveGroupFromEveryProject(group);

                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Removing group success: {1} [{2}]", packet.ClientIP, group, packet.Username));
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Remove Group Error: [{0}] {1}", e.GetType().ToString(), e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
        public static void CreateProject(Packet packet)
        {
            Packet sendPacket;

            try
            {
                string[] split       = packet.Argument.Split(char.Parse("|"));
                string   projectName = split[0];
                string   activate    = split[1];
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Create new project to the server [{1}]", packet.ClientIP, packet.Username));
                Project newProj = ProjectBrowserStore.CreateProject(projectName);
                ProjectBrowserStore.ActivateProject(newProj);

                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("INSERT INTO `exceeddb`.`projectDB` (`projectname`, `group`, `user`)" +
                                                   " VALUES ('{0}', '', '')", newProj.ProjectName);
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }

                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Create Project Error: [{0}] {1}", e.GetType().ToString(), e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
        public static void CreateGroup(Packet packet)
        {
            Packet sendPacket;

            try
            {
                string group = packet.Argument;
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Add group to the server [{2}]", packet.ClientIP, group, packet.Username));

                int gid = UserInfoInserter.AddGroup(group);

                using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                {
                    dbCon.Username = "******";
                    dbCon.Password = "******";
                    dbCon.Connect();

                    string command = string.Format("INSERT INTO `exceeddb`.`groupDB` (`groupid`, `groupname`, `dateCreated`, `member`)" +
                                                   " VALUES ('{0}', '{1}', '{2}', '')", gid, group, DateTime.Now.ToString());
                    dbCon.ExecuteCommand(command);
                    command = string.Empty;
                    dbCon.Close();
                }

                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                ConsoleUtils.Print(string.Format("[{0}]'s Request: Adding group success: {1} [{2}]", packet.ClientIP, group, packet.Username));
            }
            catch (Exception e)
            {
                ConsoleUtils.Print(string.Format("Adding Group Error: [{0}] {1}", e.GetType().ToString(), e.Message));
                sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Failure, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                return;
            }
        }
        internal static void RequestGroupList(Packet packet)
        {
            string filepath = string.Format("/tmp/exceedop/grouplist_{0}",
                                            DateTime.Now.ToString().Split(char.Parse(" "))[0].Replace(char.Parse("/"), char.Parse("_")));

            FileMode option = !File.Exists(filepath) ? FileMode.OpenOrCreate : FileMode.Truncate;

            lock (stateGroup)
            {
                using (var fsl = new FileStream(filepath, option, FileAccess.Write))
                {
                    List <string[]> resDb;
                    using (var dbCon = new MySQLDatabaseConnection(System.Net.IPAddress.Parse("127.0.0.1"), "exceeddb"))
                    {
                        dbCon.Username = "******";
                        dbCon.Password = "******";
                        dbCon.Connect();

                        string command = "SELECT * FROM `groupDB`";
                        resDb   = dbCon.GetAllCollumnValue(command);
                        command = string.Empty;
                        dbCon.Close();
                    }

                    if (resDb.Count == 0)
                    {
                        Packet sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Success, packet.Token);
                        packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);
                        return;
                    }

                    StreamWriter writer = new StreamWriter(fsl);
                    writer.WriteLine("=== Group List ===");
                    writer.WriteLine("  ");
                    foreach (string[] list in resDb)
                    {
                        writer.WriteLine(string.Format("[{0}]", list[1]));
                        writer.WriteLine(string.Format("   dateCreated: {0}", list[2]));
                        writer.WriteLine(string.Format("   users: {0}", list[3]));
                        writer.WriteLine("  ");
                    }
                    writer.Close();
                }
            }

            using (var file = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                Packet sendPacket = PacketParser.CreatePacketWithToken(ResponseEnum.Confirm, packet.Token);
                packet.Stream.Write(sendPacket.GetBytes(), 0, sendPacket.Length);

                int curReadBytes = 0;

                do
                {
                    byte[] sendByte = new byte[10000];
                    curReadBytes = file.Read(sendByte, 0, sendByte.Length);
                    if (curReadBytes != 0)
                    {
                        packet.Stream.Write(sendByte, 0, curReadBytes);
                        byte[] recByte = new byte[2];
                        packet.Stream.Read(recByte, 0, 2);
                        if (Utilities.ByteConverter.ToResponse(recByte, 0) != ResponseEnum.Confirm)
                        {
                            packet.Stream.Write(ByteReader.ReadBytes(ResponseEnum.Failure), 0, 2);
                            throw new InvalidAsynchronousStateException("User did not responded properly");
                        }
                    }
                    else
                    {
                        packet.Stream.Write(ByteReader.ReadBytes(ResponseEnum.Success), 0, 2);
                    }
                }while (curReadBytes != 0);
            }
        }