Sql Server grant permission for sp_grantdbaccess on a newly restored DB - sql

I am unable to grant access to a newly restored database using sp_grantdbaccess. I am trying to do this via dynamic sql like below.
DECLARE #grant_access nvarchar(500)
SET #grant_access = 'EXEC ' + #new_db_name + '.dbo.sp_grantdbaccess ''IIS APPPOOL\myApp'''
EXEC sp_executesql #grant_access
I get the below error back trying to run this from a sproc. Any ideas on how I can grant permission for the app to call sp_grantdbaccess etc? I guess I am needing permission to give permission...
Error restore_backup restore_new_configDBThe server principal "IIS
APPPOOL\myApp" is not able to access the database "new_db_name" under
the current security context

Based on the error message it looks like you are trying to grant access to IIS APPPOOL\myApp using the security context IIS APPPOOL\myApp, but IIS APPPOOL\myApp doesn't have the access rights to the db to grant access rights to itself. I think this is akin to me trying to grant myself access to my neighbors house, but I don't have any authority to do so.
Depending on what type of security policies you are working under, I would either run the app pool under an account that has the greater privileges through integrated auth (but this might negate your need to grant privileges), or execute these SQL statements using a local SQL account with the necessary privileges to the database. In these cases, it would be like asking my neighbor (elevated access) to let me (IIS APPPOOL\myApp) in.
If you think it should work because IIS APPPOOL\myApp had access to the DB prior to being backed up, could this be a problem with IIS APPPOOL\myApp being an orphaned user? See http://msdn.microsoft.com/en-us/library/ms175475.aspx But with this case, you still might run into the above scenario trying to fix the orphaned user.

Related

Create login with execute

I am working on a project and I have access to SQL Server as external user with limited privileges.
When I want to create a login for example with this command, I get permission denied:
CREATE LOGIN [login] WITH PASSWORD=N'test', DEFAULT_DATABASE=[master],
DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF)
However when I try to create a login with this command I can make it and also I have privileges now to enable xp_cmd shell as well:
EXECUTE('EXECUTE(''CREATE LOGIN [test5] WITH PASSWORD=N''''test'''',
DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'') AT "hostname\domain"')
EXECUTE('EXECUTE(''ALTER SERVER ROLE [sysadmin] ADD MEMBER [test5]'')
EXECUTE('EXECUTE(''ALTER SERVER ROLE [db_owner] ADD MEMBER [test5]'')
Can someone please explain why is that?
EXECUTE('string sql statement') AT "hostname\domain" == the 'string sql statement' is a pass-through command executed at the linked server "hostname/domain".
Has someone created a loop-back linked server (a linked server that points to the sql instance itself)?
Linked servers have their own security configuration/settings. If the security of the linked server is configured (for any login) to be made under the security context of a privileged login(eg. sa) then exec('') at linkedserver will be executed with way more/elevated permissions (than expected). This is a major security issue/hole.
Check the security of the linked server and change accordingly (and do you really need a loopback linked server?)

Cannot access the specified path or file on the server, verify that you have the necessary security privileges and that the path or file exists

I am using SQL Server 2012 and just logged in using SQL Server authentication. When I try to create a database in the SQL Server authentication mode, I get this error:
I tried to fix it by allowing privileges to it but all gone in waste. It also doesn't allow me alter privileges of User in the form of granting the access of dbcreator but also went in waste. Please help me anyone here.
Thanks in advance.
cannot access the specified path or file on the server, verify that
you have the necessary security privileges and that the path or file
exists
This issue related to a service account of the data engine. It has nothing to do with your SQL login and related permissions, like dbcreator.
Such a situation can be caused by changing the default data folder to a directory where the SQL Server service account does not enough permissions
So, make sure that the service account has FULL permissions on a folder where the database files to be created.
The service account can be checked/changed using SQL Server Configuration Manager:
go to SQL server configuration --> right click on MSSQLSERVER --> Properties --> built-in --> system
and then restart the instance.
this has worked for me.

Trying to add image in SQL Server

I am trying add a photo in the SQL Management Studio 2018. But it tells to use bulk load statement. On the Stackoverflow I saw some steps that may help me. It was recommended to go to the login->security->properties and activate 'bulkadmin'. This operation failed saying that it is not enough permission.
If you are trying to grant the bulkadmin Server Role to a Login, the login you are using to make this change will need to either have CONTROL permissions on the Login, or the ALTER ANY LOGIN permission on the SQL Server itself.
The easiest solution if possible would be for your own Login to be granted the sysadmin Server Role by another Login with the necessary permissions to do this.
More information about Server-Level Roles from Microsoft here.

Can't connect to SQL Server 2008 database

We have a SQL Server SSIS ETL package. It simply looks to take data from a source db & synchronises it into a target database. Connection parameters are inserted into a XML config file. Been advised we only require SELECT/View permissions on the source but whenever we run the package we get errors stating the service account login has failed & can't open database requested by login.
On a basic SQL Server Management Studio level I can see the account has public access at the instance level but has no permission assigned at the database level. From database properties/permissions I can see the grant box has been ticked for Select access.
I'm not familiar really with how Select/View access grants work, but surely it needs to have some permission defined at the database level; should it not need db_datareader as a minimum?

Unable to connect to Linked Server (Excel file) when using SQL account

The Plan
Have HR personnel dump an Excel file of payroll Job Titles to a network share, add that file as a Linked Server in SSMS, use those titles in my ASP.Net web forms. Using a Linked Server instead of importing data to allow HR to update the Job Titles file at their discretion.
The Problem
I can create the Linked Server and query it without issue, so long as I'm in SSMS with Windows Authentication. But, if I try to access the linked server using a SQL account, I first get the error:
Access to the remote server is denied because no login-mapping exists (Error 7416)
The SQL account being used is the same account my web forms use for everything else. The fact that this SQL account doesn't have file permissions isn't surprising though, so an adjustment to the Linked Server's security should do the trick.
The Problem (part 2)
To reduce the number of variables, I moved the Excel file so it's on the same machine as my SQL Server. I'm logged into the machine and logged into SSMS with my domain admin account.
I access the Linked Server's security tab and, to try and cast the widest net possible, I leave the local login (top part) blank and head straight to "Be made with this security context:" and proceed to provide my domain admin credentials. I hit OK, and I get the following:
Not a valid account name or password (Error 7399)
I know the account name and password are good, so what gives?
Other Things
Folks who've ran into similar things have been instructed to change the logon account being used for the SQL Server service, which I've done using my domain admin account (for troubleshooting, atm). This did not fix the problem.
This is what the Linked Server code looks like, although I created it via the GUI (included for sake of completeness):
EXEC master.dbo.sp_addlinkedserver #server = N'ADP_TITLES', #srvproduct=N'', #provider=N'Microsoft.ACE.OLEDB.12.0', #datasrc=N'C:\JOB_TITLE_EXPORT.xlsx', #provstr=N'Excel 12.0 Xml'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'ADP_TITLES',#useself=N'False',#locallogin=NULL,#rmtuser=N'DOMAIN\username',#rmtpassword='########'
The solution appears to be entering "admin" for the remote login, and leave the password blank.
This only works, however, if the file is stored locally. If it's on the network, I still get a 7399 error, but instead of saying the issue is the account name/password, it now says the issue is unspecified.
I can make my project work with a local file though. Since I'll be moving forward with that, I have no need to solicit additional answers.