/*
** Checksvrsuids.
**	This script determines the need to update the suids in a server.
**	It determines whether suid 2 is in use or not, and if so,
**	whether it is occupied by the 'probe' login. If suid 2 is
**	occupied by any login other than 'probe', it will be necessary to
**	run the scripts which update the suids in all databases.
*/

/* Declarations */
declare @login_at_2	varchar(30)	/* Usename at suid 2 */
declare @probe_exists	int		/* Whether or not 'probe' exists */
declare @max_suid	smallint	/* Maximum suid in syslogins */

/* Ensure that a user with 'sa_role' is running this script. */
if (proc_role('sa_role') = 0)
begin
	print ""
	print "ERROR: 'sa_role' is required to execute this script."
	goto exit_script
end

/* Initialisations */
set nocount on

/* Determine the login name at suid 2, and the max suid */
select @login_at_2 = name from master.dbo.syslogins where suid = 2
select @max_suid = max(suid) from master.dbo.syslogins
if (exists (select * from master.dbo.syslogins where name = 'probe'))
	select @probe_exists = 1
else
	select @probe_exists = 0

/*
** Case 1: The 'probe' login is installed with an suid of 2. Nothing more
**         needs to be done.
*/
if (@login_at_2 = 'probe')
begin
	print ""
	print "The 'probe' login is correctly installed."
	print ""
	print "There is no need to do anything further."
	goto exit_script
end

/*
** Case 2: The only login is 'sa'.  Provided the new version of 'sp_addlogin'
**	   is used in future, then future user logins will be allocated an
**	   suid > 2, while 'probe' will be installed with an suid of 2.
*/
if (@max_suid = 1)
begin
	print ""
	print "The only login installed is 'sa'."
	print ""
	print "All future logins should be added with the new 'sp_addlogin'"
	print "    system stored procedure that will be installed as part of"
	print "    the current release/installation."
	print ""
	print "There is no need to do anything further."
	goto exit_script
end

/*
** Case 3: The 'probe' login is installed, but with an incorrect suid.
**	   The suids will need to be updated.
*/
if (@probe_exists = 1)
begin
	print ""
	print "The 'probe' login is installed, but it does not have a suid of 2."
	print ""
	print "Contact Sybase Technical Support for help in updating the suids."
	goto exit_script
end


/*
** Case 3: The 'probe' login does not exist. There is a "gap" at suid = 2, 
**	   where the 'probe' login was at some point in time. Nothing more 
**	   needs to be done.
*/
if (@login_at_2 = NULL)
begin
	print ""
	print "The 'probe' login is not installed, but suid 2 is not in"
	print "    use, and may be used for the 'probe' login in future."
	print ""
	print "All future logins should be added with the new 'sp_addlogin'"
	print "    system stored procedure that will be installed as part of"
	print "    the current release/installation."
	print ""
	print "There is no need to do anything further."
	goto exit_script
end

/*
** Case 5: The 'probe' login does not exist, and another login has
**         an suid of 2.
*/
print ""
print "The 'probe' login does not exist, and another user has an suid of 2."
print ""
print "Contact Sybase Technical Support for help in updating the suids."

exit_script:

go
