Skip to content

Commit 3496205

Browse files
committed
Add pasta de logins e linked servers
1 parent 253e743 commit 3496205

5 files changed

Lines changed: 422 additions & 0 deletions

File tree

LinkedServers/AddMappedLogin.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Detalhes
7+
Gera o comando para add logins em um linked server ,mapeado para um login remoto.
8+
Especifica na tabela derivada L a lista de logins locais (name) e a lista de remotos com a respectiva senha.
9+
ATENÇÃO: Não salvar o script com a senha.
10+
11+
12+
*/
13+
14+
IF OBJECT_ID('tempdb..#Users') IS NOT NULL
15+
DROP TABLE #Users;
16+
17+
18+
SELECT
19+
'EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N''Nome'',@useself=N''False'',@locallogin=N'''+L.name+''',@rmtuser=N'''+L.NomeRemoto+''',@rmtpassword='''+L.pass+'''
20+
GO'
21+
FROM
22+
sys.servers S
23+
CROSS JOIN
24+
(
25+
VALUES
26+
('NomeLocal','NomeRemoto','Senha')
27+
) L(name,remotename,pass)
28+
29+
30+
1.37 KB
Binary file not shown.

LoginsUsers/CriarUsersAdvanced.sql

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Detalhes
7+
Cria users associado com logins no banco atual e adiciona no role db_owner.
8+
9+
10+
*/
11+
12+
13+
SET NOCOUNT ON;
14+
15+
IF OBJECT_ID('tempdb..#LoginsAllowed') IS NOT NULL
16+
DROP TABLE #LoginsAllowed;
17+
CREATE TABLE #LoginsAllowed(LoginName sysname);
18+
INSERT INTO #LoginsAllowed VALUES('NomeLogin');
19+
20+
DECLARE
21+
@col_LoginName varchar(1000)
22+
,@userName sysname
23+
,@LoginSID varbinary(100)
24+
,@UserSID varbinary(100)
25+
,@tsql nvarchar(4000)
26+
;
27+
28+
DECLARE curLoginsAllow CURSOR LOCAL FAST_FORWARD
29+
FOR
30+
SELECT * FROM #LoginsAllowed;
31+
32+
33+
OPEN curLoginsAllow;
34+
35+
FETCH NEXT FROM curLoginsAllow INTO @col_LoginName;
36+
37+
While @@FETCH_STATUS = 0
38+
BEGIN
39+
40+
--> Check if login exists
41+
IF SUSER_ID(@col_LoginName) IS NULL
42+
BEGIN
43+
RAISERROR('Login %s não existe. Favor criar e re-executar este script.',0,1,@col_LoginName);
44+
GOTO FETCH_NEXT;
45+
END
46+
47+
SET @LoginSID = SUSER_SID(@col_LoginName);
48+
SET @userName = NULL;
49+
SET @UserSID = NULL;
50+
51+
--> Try get user name and possible mapped login sid...
52+
SELECT
53+
@userName = DP.name
54+
,@UserSID = ISNULL(SP.sid,DP.sid)
55+
FROM
56+
sys.database_principals DP
57+
LEFT JOIN
58+
sys.server_principals SP
59+
ON SP.sid = DP.sid
60+
WHERE
61+
DP.name = @col_LoginName
62+
63+
IF @userName IS NULL --> If user name not found... Try get by login sid...
64+
SELECT
65+
@userName = DP.name
66+
FROM
67+
sys.database_principals DP
68+
WHERE
69+
DP.name = SUSER_SID(@col_LoginName)
70+
71+
-- Possible cenarios
72+
-- User dont exists
73+
-- User exists, different login
74+
-- User exists, no mapping
75+
-- User exists, but mapped with different name
76+
-- User exists
77+
78+
SET @tsql = NULL;
79+
IF @userName IS NULL --> User Dont Exists and Login dont have mapped users...
80+
81+
BEGIN
82+
SET @userName = @col_LoginName;
83+
SET @tsql = 'CREATE USER '+QUOTENAME(@col_LoginName)+' FROM LOGIN '+QUOTENAME(@col_LoginName);
84+
END
85+
86+
ELSE IF @userName IS NOT NULL AND @UserSID IS NULL -- User exists, no mapping
87+
BEGIN
88+
-- Remap user...
89+
SET @tsql = 'ALTER USER '+QUOTENAME(@userName)+' WITH LOGIN = '+QUOTENAME(@col_LoginName,'''');
90+
END
91+
92+
ELSE IF @userName IS NOT NULL AND @UserSID != SUSER_SID(@col_LoginName) AND @UserSID IS NOT NULL --> User exists, different login
93+
BEGIN
94+
--Generate new user name...
95+
SET @userName = @col_LoginName+CONVERT(varchar(25),ABS(CHECKSUM(NEWID())))
96+
SET @tsql = 'CREATE USER '+QUOTENAME(@userName)+' FROM LOGIN '+QUOTENAME(@col_LoginName);
97+
END
98+
99+
100+
101+
-- All other cases, the user already exists and @userName must contain correct username
102+
IF @tsql IS NOT NULL
103+
BEGIN
104+
RAISERROR('Executando (Login: %s): %s',0,1,@col_LoginName,@tsql);
105+
EXEC(@tsql);
106+
END
107+
108+
IF USER_ID(@userName) IS NOT NULL
109+
BEGIN
110+
RAISERROR('Granting role membership: %s',0,1,@userName);
111+
EXEC sp_addrolemember 'db_owner',@userName;
112+
END
113+
114+
FETCH_NEXT:
115+
FETCH NEXT FROM curLoginsAllow INTO @col_LoginName;
116+
END
117+
118+
CLOSE curLoginsAllow;
119+
DEALLOCATE curLoginsAllow;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Detalhes
7+
Gera o exec da proc sp_help_revlogin para todos os logins que existem em algum banco da instância.
8+
9+
10+
*/
11+
12+
IF OBJECT_ID('tempdb..#UserBancos') IS NOT NULL
13+
DROP TABLE #UserBancos;
14+
15+
CREATE TABLE
16+
#UserBancos( banco sysname, userName sysname, sid varbinary(max) );
17+
18+
EXEC sp_MSforeachdb '
19+
USE [?];
20+
21+
INSERT INTO #UserBancos
22+
SELECT
23+
db_name()
24+
,name
25+
,sid
26+
FROM
27+
sys.database_principals DP
28+
'
29+
30+
select distinct SP.NAME,'EXEC sp_help_revlogin '''+SP.name+''' ' from #UserBancos ub
31+
inner join
32+
sys.server_principals SP
33+
ON SP.sid = UB.SID

0 commit comments

Comments
 (0)