/************************************************/
/* This checks to see if the ctrlaltdel, or the */
/* STOP-A sequences are disabled in linux or    */
/* Solaris, respectively. These should be 	*/
/* disabled for security reasons.		*/
/* Where?					*/
/* Linux: /etc/inittab				*/
/* Solaris: /etc/default/kbd			*/
/*						*/
/* See lsatmain.c for more details...		*/
/************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int checkkbd(release, kernel, filename, verbose)
char release[50];
char kernel[50];
const char *const filename;
int verbose;
{

    char Sunos[4]="Sun";    /* for comparison	   */
    char Linux[4]="Lin";    /* for comparison	   */
    char *sunptr=Sunos;     /* char pointer		   */
    char *lptr=Linux;	/* char pointer 	   */
    FILE *outfile; 		/* output file pointer     */

    if (verbose == 1)
    {
        printf("In checkkbd module...\n");
    }


    /* open the output file for append. */
    outfile = fopen(filename, "a");
    if (outfile == NULL)
    {
        perror("Error opening outfile.\n");
        perror("Check perms perhaps?\n");
	perror("Error in checkkbd module.\n");
        return(-1);
    }

    fprintf(outfile, "****************************************\n");
    fprintf(outfile, "Checking for keyboard shutdown/halt disable...\n");
    fprintf(outfile, "\n");


    /* ok, we also check here if we are on a Solaris 	*/
    /* machine.  					*/

    if (strstr(release, sunptr) != NULL)
    {
	/* note, I am checking the existance of the line */
	/* and that it is commented out, which is what   */
	/* we DO NOT want. 				 */
        if ((system("grep KEYBOARD_ABORT=disable /etc/default/kbd | grep -v \\# 1>/dev/null 2>/dev/null")) == 0)
	{
		/* its there, but commented out. bad. */
		fprintf(outfile, "Uncomment the following in /etc/default/kbd:\n\n");
		fprintf(outfile, "KEYBOARD_ABORT=disable\n\n");
		fprintf(outfile, "This will disable the STOP-A keyboard function.\n");
	}
    }
    
    /* if not Solaris... must be linux. */
    if (strstr(release, lptr) != NULL)
    {
	/* note here, it is the opposite of SunOS 	*/
	/* in that the line _should_ be commented out	*/
	if ((system("grep ctrlaltdel /etc/inittab | grep -v \\# 1>/dev/null 2>/dev/null")) == 0)
	{
		/* its there, but NOT commented out. bad. */
		fprintf(outfile, "Comment out the following in /etc/inittab:\n\n");
		fprintf(outfile, "ca::ctrlaltdel:/sbin/shutdown -t3 -r now\n\n");
		fprintf(outfile, "This will disable the 3 finger salute.\n");
	}
    }

    fprintf(outfile, "\n");
    fprintf(outfile, "****************************************\n");
 
    /* clean up... */

    if (verbose == 1)
    {
        printf("Finished module checkkbd.\n");
    }
    return(0);
}
