Like most companies you will have the need to automate specific database tasks directly in the database.
As an example, frequent database backups.
Within SAP ASE databases, if you have an attached ABAP stack, then DB13 can be used to schedule database level jobs, with the caveat that the ABAP stack must be running for jobs to run.
Another alternative, is to use the SAP ASE JobScheduler to automate tasks.
The JobScheduler comes with the SAP ASE system and runs as a separate process.
Using the JobScheduler you can configure jobs/tasks to be executed even if there is no ABAP stack.
However, it means that any configuration must be repeated across all databases and you will lack any central job scheduling control.
As a good catch-all and an alternative to the above options, there is another way.
Use an enterprise job scheduler (e.g. SAP Business Process Automation) combined with a centrally accessible server-side script to call the database routine/utility.
One central schedule and one script. Simple.
The added advantage being that the central job scheduler will also be responsible for controlling your SAP business processes. This allows an operator to tightly control and monitor the backup window around the critical business processes.
There’s generally one problem with the above, and that is where to put the password for the SAP ASE database login.
You can hardcode it into the script, and restrict permissions as much as possible, but this means you will have just one password (unless you put ALL the passwords for all the databases the script will be executed against).
A possible solution to this, is to use a little known binary of the SAP Kernel for ASE databases, called sybxctrl.
Within your usual DIR_CT_RUN location, there will exist somewhere, a binary called sybctrl and its twin, sybxctrl.
They are the same binary code but separate files.
What’s the difference? Well, it’s purely about Linux permissions.
After you install an ABAP Kernel (on Linux), you’re supposed to run the saproot.sh script, which prepares certain SAP binaries, adjusts ownerships and configures executable permissions.
With regards to sybctrl, it is owned by the Linux root user, and it also has the SUID permission set (numerical: 4750, character: swxr-x—).
When this binary is executed by any member of the sapsys Linux group (<sid>adm or syb<sid> users are members of this group), then it will switch executable context to that of the root Linux user. In essence, it executes almost as if you were the root user executing it.
This is a useful Linux feature, to be able to execute binaries in this way; however, it can also leave a security hole.
To provide a level of protection to the O/S during the standard Linux binary execution process, when an SUID enabled binary is executed the Linux kernel puts a restriction around the contents of the user’s environment by arbitrarily setting vairables like PATH and LD_LIBRARY_PATH.
This means that execution of sybctrl will limit the ability for loading of additional shared libraries in non-standard directory locations, which may be required for certain functions.
Why does sybctrl need root? This is fairly easy to explain, it’s because you normally log onto a Linux SAP system as the <sid>adm Linux user then start all components including the SAP ASE database. Except the SAP ASE database runs as Linux user syb<sid>. So, to be generic, the binary sybctrl is used and executed from <sid>adm via the root user (probably so that shared memory can be freed), before it switches to syb<sid> to start or stop the database.
So how does sybxctrl differ to its more powerful sybctrl? Simple, it doesn’t have the SUID permission set on it.
It doesn’t need to run via root for what it does, but it does need access to the SYBASE_OCS and other shared library locations. So, to ensure that those environment variables are maintained throughout execution, this separate copy of sybctrl was required.
How can we use sybxctrl?
As the <sid>adm Linux user, you can execute sybxctrl with no parameters to return the usage information (the parameters that it does accept).
Of interest to us for our password-less database script, are the parameters:
Here’s how we can use sybxctrl to embed a script into the database, then execute it without needing a password.
Before we can use the sybxctrl binary, you need to first ensure:
– You have Kernel 7.22_EXT pl500 and above (or 7.49+).
– You run the saproot.sh to correctly set up permissions on the SAP Kernel binaries.
– You run a “stopdb” as <sid>adm. This will actually copy across the sybxctrl from sybctrl.
– You run sybxctrl to load in a script.
During the execution of sybxctrl with the “load_script” parameter, a new database table called SYBSISQL is created in the DBO schema (not the usual SAPSR3[DB] schema!) of the <SID> ASE database. This table is where the script that you load into the DB, is stored.
Let’s now create a test script as <sid>adm:
> echo “select user_name()ngo” > /tmp/myscript.sql
All this script will do is print out the current database username.
Now we load the script into the database using the sybxctrl binary and the “load_script” parameter as follows:
> sybxctrl load_script /tmp/myscript.sql -exe $SYBASE/$SYBASE_OCS/bin/isql -auth sapsa
You are prompted for the sapsa database user password.
Enter the password and the load should succeed.
What just happened?
You have just populated that new database table (<SID>.DBO.SYBSISQL) with the text of the script that you created, plus the details of how to run it (which binary and the full path, plus parameters).
We have told sybxctrl that we want to use the isql binary (SPA ASE command line SQL utility), and that we want it to execute as the sapsa database user (the “auth” parameter).
Now we’ve loaded the script, we can unload it and check it as follows:
> rm /tmp/myscript.sql
> sybxctrl unload_script myscript.sql /tmp/
NOTE: You remove the path from the name of the script.
You are prompted again for the sapsa database user password.
Enter the password and the unload should succeed.
We can now see the script contents:
> cat /tmp/myscript.sql
Finally, we can now try and execute the script.
The execution happens as though it was a batch operation, so we need to provide an output file for any output from the script.
Execute the script as follows:
> sybxctrl exec_script myscript.sql /tmp/myscript.out -auth sapsa
Did you notice, you didn’t need to enter the password for sapsa!
Check the output file that we specified:
> cat /tmp/myscript.out
1 row affected
To remove the script from the database:
> sybxctrl delete_script myscript.sql
Enter the sapsa password and the deletion should succeed.
What are the implications of what we have just done?
We can now embed a pre-configured script into the SAP ASE database, which can be executed at the O/S level as any Linux user with permissions to run sybxctrl, without requiring a password and it will execute as the sapsa database user.
There are many possibilities for this setup when you understand the full parameter list of both sybxctrl and isql.
As an example, you could pipe in parameters to the embedded script…
Couple the script with some standard code and configure the ASE database to use ASE backup configurations and you could have an automated backup routine that is passwordless.
Are there any problems in using sybxctrl?
Yes, a few.
1, sybctrl will be patched as part of the kernel patching, but synxctrl is not. So you must ensure that you either run a “stopdb” at some point to pickup the new script, or manually copy it into place after Kernel binary patching is completed.
2, isql will be patched as part of the ASE patching. This changes the checksum value of the binary file which is recorded during the “load_script” in the new SYBSISQL table.
Therefore, after ASE binary patching, you should unload and re-load the script to re-create the table entry.
3, Any embedded scripts will be carried along with any SAP system copies if you use the database backup/restore method. But not if you use the R3load tools.
4, If you use SAP Replication Server (SRS), this table and it’s contents will be replicated across to the secondary database (as the table is in the <SID> database).
So bear this in mind for the contents of the backup script, because they may need to be generic if you want to run it on secondary or tertiary databases in an SRS setup.