コード例 #1
0
ファイル: Groups.cs プロジェクト: VulpesSARL/Fox-SDC
        public RESTStatus CreateGroup(SQLLib sql, CreateGroup GroupName, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            if (GroupName.Name == null)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (GroupName.Name.Trim() == "")
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (GroupName.ToParent != null)
            {
                lock (ni.sqllock)
                {
                    if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM Grouping WHERE ID=@id",
                                                          new SQLParam("@id", GroupName.ToParent))) == 0)
                    {
                        ni.Error   = "Group does not exist";
                        ni.ErrorID = ErrorFlags.InvalidData;
                        return(RESTStatus.Fail);
                    }
                }
            }

            lock (ni.sqllock)
            {
                if (Convert.ToInt32(sql.ExecSQLScalar("SELECT COUNT(*) FROM Grouping WHERE ParentID=@pid AND Name=@n",
                                                      new SQLParam("@pid", GroupName.ToParent),
                                                      new SQLParam("@n", GroupName.Name.Trim()))) > 0)
                {
                    ni.Error   = "Duplicate name";
                    ni.ErrorID = ErrorFlags.DuplicateElement;
                    return(RESTStatus.Fail);
                }
            }

            NewGroupID = new NetInt64();

            Int64?nid = null;

            lock (ni.sqllock)
            {
                nid = sql.InsertMultiDataID("Grouping",
                                            new SQLData("Name", GroupName.Name.Trim()),
                                            new SQLData("ParentID", GroupName.ToParent));
            }

            if (nid == null)
            {
                ni.Error   = "SQL Error";
                ni.ErrorID = ErrorFlags.SQLError;
                return(RESTStatus.ServerError);
            }
            NewGroupID.Data = nid.Value;

            return(RESTStatus.Success);
        }
コード例 #2
0
        public RESTStatus CreatePolicy(SQLLib sql, NewPolicyReq request, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            if (request.Name == null || request.Name.Trim() == "")
            {
                ni.Error   = "Invalid name";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            request.Name = request.Name.Trim();

            if (request.Grouping != null && request.MachineID != null)
            {
                ni.Error   = "Either Grouping OR MachineID should be set";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            if (request.Grouping != null)
            {
                lock (ni.sqllock)
                {
                    if (Groups.GroupExsits(sql, request.Grouping.Value) == false)
                    {
                        ni.Error   = "Group does not exists";
                        ni.ErrorID = ErrorFlags.InvalidData;
                        return(RESTStatus.Fail);
                    }
                }
            }

            if (request.MachineID != null)
            {
                lock (ni.sqllock)
                {
                    if (Computers.MachineExists(sql, request.MachineID) == false)
                    {
                        ni.Error   = "MachineID does not exists";
                        ni.ErrorID = ErrorFlags.InvalidData;
                        return(RESTStatus.Fail);
                    }
                }
            }

            try
            {
                JsonConvert.DeserializeObject(request.Data);
            }
            catch
            {
                ni.Error   = "JSON error";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.Fail);
            }

            NewPolicyID = new NetInt64();
            Int64?res;

            lock (ni.sqllock)
            {
                res = sql.InsertMultiDataID("Policies",
                                            new SQLData("Type", request.Type),
                                            new SQLData("Name", request.Name),
                                            new SQLData("Grouping", request.Grouping),
                                            new SQLData("MachineID", request.MachineID),
                                            new SQLData("DataBlob", request.Data),
                                            new SQLData("DT", DateTime.Now),
                                            new SQLData("Version", 1),
                                            new SQLData("Enabled", 0));
            }
            if (res == null)
            {
                ni.Error   = "SQL Error";
                ni.ErrorID = ErrorFlags.SQLError;
                return(RESTStatus.ServerError);
            }
            NewPolicyID.Data = res.Value;

            return(RESTStatus.Created);
        }
コード例 #3
0
ファイル: SimpleTasks.cs プロジェクト: VulpesSARL/Fox-SDC
        public RESTStatus SetSTasks(SQLLib sql, SimpleTask NewTask, NetworkConnectionInfo ni)
        {
            if (ni.HasAcl(ACLFlags.ChangeServerSettings) == false)
            {
                ni.Error   = "Access denied";
                ni.ErrorID = ErrorFlags.AccessDenied;
                return(RESTStatus.Denied);
            }

            if (NewTask == null)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.NotFound);
            }

            if (string.IsNullOrWhiteSpace(NewTask.Name) == true || string.IsNullOrWhiteSpace(NewTask.Data) == true || NewTask.Type < 1)
            {
                ni.Error   = "Invalid data";
                ni.ErrorID = ErrorFlags.InvalidData;
                return(RESTStatus.NotFound);
            }

            if (NewTask.ExecAfter != null)
            {
                if (NewTask.ExecAfter.Value.Year < 2010)
                {
                    ni.Error   = "ExecAfter is invalid";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.NotFound);
                }
            }

            lock (ni.sqllock)
            {
                if (Computers.MachineExists(sql, NewTask.MachineID) == false)
                {
                    ni.Error   = "Invalid data";
                    ni.ErrorID = ErrorFlags.InvalidData;
                    return(RESTStatus.NotFound);
                }

                Int64?ID = sql.InsertMultiDataID("SimpleTasks",
                                                 new SQLData("MachineID", NewTask.MachineID),
                                                 new SQLData("Type", NewTask.Type),
                                                 new SQLData("Name", NewTask.Name),
                                                 new SQLData("ExecAfter", NewTask.ExecAfter),
                                                 new SQLData("Data", NewTask.Data));

                if (ID == null)
                {
                    ni.Error   = "Server error";
                    ni.ErrorID = ErrorFlags.SQLError;
                    return(RESTStatus.ServerError);
                }

                NewTaskID      = new NetInt64();
                NewTaskID.Data = ID.Value;
            }

            return(RESTStatus.Success);
        }