Example #1
0
        public override void ProcessDeploy(String cacheId, PluginConnectorBaseDeployPackage package, Dictionary <String, Object> config, List <PluginConnectorBaseDeployPackageMapping> fieldMapping)
        {
            if (!CheckInputConfig(config, true, Log))
            {
                return;
            }


            String connectionstring = "Data Source=" + config["server"].ToString() + ";Initial Catalog=master;User Id=" + config["username"].ToString() + ";Password='******';";

            MSSQLDB       db         = null;
            StringBuilder processLog = new StringBuilder();
            PluginLogType logType    = PluginLogType.Information;

            try
            {
                if (!String.IsNullOrEmpty(package.password))
                {
                    processLog.AppendLine("Package contains password");
                }
                else
                {
                    processLog.AppendLine("Package not contains password");
                }

                db = new MSSQLDB(connectionstring);
                db.openDB();

                LogEvent dbExecLog = new LogEvent(delegate(Object sender, PluginLogType type, String text)
                {
                    processLog.AppendLine(text);
                });

                db.OnLog += dbExecLog;

                //Verifica se o registro existe
                DataTable dtReg = db.ExecuteDataTable("SELECT name AS Login_Name FROM sys.server_principals  WHERE TYPE IN ('S') and name = '" + package.login + "'", CommandType.Text, null);
                if (dtReg == null)
                {
                    logType = PluginLogType.Error;
                    processLog.AppendLine("Error on select data: " + db.LastError);
                    Log2(this, PluginLogType.Error, package.entityId, package.identityId, "Error on select data: " + db.LastError, "");
                    return;
                }


                //Preenche a tabela de parâmetros com os campos que serão inseridos/atualizados
                if (dtReg.Rows.Count == 0)
                {
                    //Não existe, cria
                    String tmpPwd = IAM.Password.RandomPassword.Generate(20);
                    tmpPwd = tmpPwd.Replace("'", "");
                    tmpPwd = tmpPwd.Replace(".", "");
                    tmpPwd = tmpPwd.Replace("\\", "");
                    tmpPwd = tmpPwd.Replace("[", "");
                    tmpPwd = tmpPwd.Replace("]", "");

                    if (package.password == "")
                    {
                        processLog.AppendLine("User not found in AD and IAM Password not found in properties list, creating a random password (" + tmpPwd + ")");
                    }

                    String insert = "CREATE LOGIN [" + package.login + "] WITH PASSWORD=N'" + tmpPwd + "', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF";

                    StringBuilder tmpText = new StringBuilder();
                    try
                    {
                        tmpText.AppendLine("ExecuteNonQuery.SQL = " + insert);

                        db.ExecuteNonQuery(insert, CommandType.Text, null);
                    }
                    catch (Exception ex2)
                    {
                        processLog.AppendLine(tmpText.ToString());

                        throw new Exception("Error adding user", ex2);
                    }
                    finally
                    {
                        tmpText.Clear();
                        tmpText = null;
                    }

                    NotityChangeUser(this, package.entityId);

                    processLog.AppendLine("");
                    processLog.AppendLine("User added");
                }


                if (package.password != "")
                {
                    String insert = "ALTER LOGIN [" + package.login + "] WITH PASSWORD=N'" + package.password + "'";

                    StringBuilder tmpText = new StringBuilder();
                    try
                    {
                        tmpText.AppendLine("ExecuteNonQuery.SQL = " + insert);

                        db.ExecuteNonQuery(insert, CommandType.Text, null);
                    }
                    catch (Exception ex2)
                    {
                        String sPs = "";
                        try
                        {
                            PasswordStrength ps = CheckPasswordStrength(package.password, package.fullName.fullName);

                            sPs += "Length = " + package.password.Length + Environment.NewLine;
                            sPs += "Contains Uppercase? " + ps.HasUpperCase + Environment.NewLine;
                            sPs += "Contains Lowercase? " + ps.HasLowerCase + Environment.NewLine;
                            sPs += "Contains Symbol? " + ps.HasSymbol + Environment.NewLine;
                            sPs += "Contains Number? " + ps.HasDigit + Environment.NewLine;
                            sPs += "Contains part of the name/username? " + ps.HasNamePart + Environment.NewLine;
                        }
                        catch { }

                        Log2(this, PluginLogType.Error, package.entityId, package.identityId, "Error on set user password, check the password complexity rules", ex2.Message + (ex2.InnerException != null ? " " + ex2.InnerException.Message : "") + Environment.NewLine + sPs);
                        return;
                    }
                    finally
                    {
                        tmpText.Clear();
                        tmpText = null;
                    }
                }

                NotityChangeUser(this, package.entityId);

                db.OnLog -= dbExecLog;


                //Executa as ações do RBAC
                if ((package.pluginAction != null) && (package.pluginAction.Count > 0))
                {
                    processLog.AppendLine("");
                    foreach (PluginConnectorBaseDeployPackageAction act in package.pluginAction)
                    {
                        try
                        {
                            switch (act.actionKey.ToLower())
                            {
                            case "sql":
                                String sql2 = act.actionValue.Replace("{login}", package.login).Replace("{full_name}", package.fullName.fullName);
                                db.ExecuteNonQuery(sql2, CommandType.Text, null);
                                break;

                            default:
                                processLog.AppendLine("Action not recognized: " + act.actionKey);
                                Log2(this, PluginLogType.Warning, package.entityId, package.identityId, "Action not recognized: " + act.actionKey, "");
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            processLog.AppendLine("Error on execute action (" + act.actionKey + "): " + ex.Message);
                            Log2(this, PluginLogType.Error, package.entityId, package.identityId, "Error on execute action (" + act.actionKey + "): " + ex.Message, "");
                        }
                    }
                }


                if (package.password != "")
                {
                    Log2(this, PluginLogType.Information, package.entityId, package.identityId, "User updated with password", "");
                }
                else
                {
                    Log2(this, PluginLogType.Information, package.entityId, package.identityId, "User updated without password", "");
                }
            }
            catch (Exception ex)
            {
                logType = PluginLogType.Error;
                processLog.AppendLine("Error on process deploy: " + ex.Message + (ex.InnerException != null ? " --> " + ex.InnerException.Message : ""));

#if DEBUG
                String debugInfo = JSON.Serialize2(new { package = package, fieldMapping = fieldMapping });
                if (package.password != "")
                {
                    debugInfo = debugInfo.Replace(package.password, "Replaced for user security");
                }

                processLog.AppendLine(debugInfo);
#endif

                Log2(this, PluginLogType.Error, package.entityId, package.identityId, "Error on process deploy: " + ex.Message, processLog.ToString());
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }

                Log2(this, logType, package.entityId, package.identityId, "Deploy executed", processLog.ToString());
                processLog.Clear();
                processLog = null;
            }
        }
Example #2
0
        public override PluginConnectorBaseFetchResult FetchFields(Dictionary <String, Object> config)
        {
            PluginConnectorBaseFetchResult ret = new PluginConnectorBaseFetchResult();

            LogEvent iLog = new LogEvent(delegate(Object sender, PluginLogType type, string text)
            {
                if (Log != null)
                {
                    Log(sender, type, text);
                }
            });


            if (!CheckInputConfig(config, true, iLog, true, true))
            {
                ret.success = false;
                return(ret);
            }

            List <PluginConfigFields> cfg = new List <PluginConfigFields>();

            PluginConfigFields[] tmpF = this.GetConfigFields();
            foreach (PluginConfigFields cf in tmpF)
            {
                try
                {
                    iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): " + (config.ContainsKey(cf.Key) ? config[cf.Key].ToString() : "empty"));
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): error on get data -> " + ex.Message);
                }
            }


            String connectionstring = "Data Source=" + config["server"].ToString() + ";Initial Catalog=master;User Id=" + config["username"].ToString() + ";Password='******';";

            MSSQLDB db = null;

            try
            {
                db = new MSSQLDB(connectionstring);
                db.openDB();

                String sql = "SELECT name AS Login_Name FROM sys.server_principals  WHERE TYPE IN ('S') and name not like '%##%' ORDER BY name, type_desc";

                DataTable dtSource = db.Select(sql);

                if (dtSource == null)
                {
                    throw new Exception("Erro on select: " + db.LastError);
                }

                try
                {
                    foreach (DataColumn dc in dtSource.Columns)
                    {
                        if (!ret.fields.ContainsKey(dc.ColumnName))
                        {
                            ret.fields.Add(dc.ColumnName, new List <string>());
                        }
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                }

                foreach (DataRow dr in dtSource.Rows)
                {
                    String regId = Guid.NewGuid().ToString();

                    try
                    {
                        foreach (DataColumn dc in dtSource.Columns)
                        {
                            if (!ret.fields.ContainsKey(dc.ColumnName))
                            {
                                ret.fields.Add(dc.ColumnName, new List <string>());
                            }

                            ret.fields[dc.ColumnName].Add(dr[dc.ColumnName].ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                    }
                }

                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
Example #3
0
        public override void ProcessImport(String cacheId, String importId, Dictionary <String, Object> config, List <PluginConnectorBaseDeployPackageMapping> fieldMapping)
        {
            if (!CheckInputConfig(config, true, Log))
            {
                return;
            }

            List <String> prop = new List <String>();


            String connectionstring = "Data Source=" + config["server"].ToString() + ";Initial Catalog=master;User Id=" + config["username"].ToString() + ";Password='******';";

            MSSQLDB db = null;

            try
            {
                db = new MSSQLDB(connectionstring);
                db.openDB();

                String sql = "SELECT name AS Login_Name FROM sys.server_principals  WHERE TYPE IN ('S') and name not like '%##%' ORDER BY name, type_desc";

                DataTable dtSource = db.Select(sql);

                if (dtSource == null)
                {
                    throw new Exception("Erro on select: " + db.LastError);
                }

                foreach (DataRow dr in dtSource.Rows)
                {
                    PluginConnectorBaseImportPackageUser package = new PluginConnectorBaseImportPackageUser(importId);
                    try
                    {
                        foreach (DataColumn dc in dtSource.Columns)
                        {
                            package.AddProperty(dc.ColumnName, dr[dc.ColumnName].ToString(), dc.DataType.ToString());
                        }

                        ImportPackageUser(package);
                    }
                    catch (Exception ex)
                    {
                        Log2(this, PluginLogType.Error, 0, 0, "Erro ao importar o registro: " + ex.Message, "");
                        Log(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                    }
                    finally
                    {
                        package.Dispose();
                        package = null;
                    }
                }
            }
            catch (Exception ex)
            {
                Log2(this, PluginLogType.Error, 0, 0, "Error on process import: " + ex.Message, "");
                Log(this, PluginLogType.Error, ex.Message);
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }