xref: /onnv-gate/usr/src/lib/krb5/ss/execute_cmd.c (revision 8093:d2fc11f7c4e7)
10Sstevel@tonic-gate /*
2*8093SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * For copyright info, see copyright.h.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
120Sstevel@tonic-gate #include "ss_internal.h"
130Sstevel@tonic-gate #include "copyright.h"
140Sstevel@tonic-gate #include <stdio.h>
150Sstevel@tonic-gate 
160Sstevel@tonic-gate 
170Sstevel@tonic-gate /*
180Sstevel@tonic-gate  * get_request(tbl, idx)
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * Function:
210Sstevel@tonic-gate  *      Gets the idx'th request from the request table pointed to
220Sstevel@tonic-gate  *      by tbl.
230Sstevel@tonic-gate  * Arguments:
240Sstevel@tonic-gate  *      tbl (ss_request_table *)
250Sstevel@tonic-gate  *              pointer to request table
260Sstevel@tonic-gate  *      idx (int)
270Sstevel@tonic-gate  *              index into table
280Sstevel@tonic-gate  * Returns:
290Sstevel@tonic-gate  *      (ss_request_entry *)
300Sstevel@tonic-gate  *              pointer to request table entry
310Sstevel@tonic-gate  * Notes:
320Sstevel@tonic-gate  *      Has been replaced by a macro.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef __SABER__
360Sstevel@tonic-gate /* sigh.  saber won't deal with pointer-to-const-struct */
get_request(tbl,idx)370Sstevel@tonic-gate static struct _ss_request_entry * get_request (tbl, idx)
380Sstevel@tonic-gate     ss_request_table * tbl;
390Sstevel@tonic-gate     int idx;
400Sstevel@tonic-gate {
410Sstevel@tonic-gate     struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
420Sstevel@tonic-gate     struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
430Sstevel@tonic-gate     return e + idx;
440Sstevel@tonic-gate }
450Sstevel@tonic-gate #else
460Sstevel@tonic-gate #define get_request(tbl,idx)    ((tbl) -> requests + (idx))
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * check_request_table(rqtbl, argc, argv, sci_idx)
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Function:
530Sstevel@tonic-gate  *      If the command string in argv[0] is in the request table, execute
540Sstevel@tonic-gate  *      the commands and return error code 0.  Otherwise, return error
550Sstevel@tonic-gate  *      code ss_et_command_not_found.
560Sstevel@tonic-gate  * Arguments:
570Sstevel@tonic-gate  *      rqtbl (ss_request_table *)
580Sstevel@tonic-gate  *              pointer to request table
590Sstevel@tonic-gate  *      argc (int)
600Sstevel@tonic-gate  *              number of elements in argv[]
610Sstevel@tonic-gate  *      argv (char *[])
620Sstevel@tonic-gate  *              argument string array
630Sstevel@tonic-gate  *      sci_idx (int)
640Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
650Sstevel@tonic-gate  * Returns:
660Sstevel@tonic-gate  *      (int)
670Sstevel@tonic-gate  *              zero if command found, ss_et_command_not_found otherwise
680Sstevel@tonic-gate  * Notes:
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate 
check_request_table(rqtbl,argc,argv,sci_idx)710Sstevel@tonic-gate static int check_request_table (rqtbl, argc, argv, sci_idx)
720Sstevel@tonic-gate     register ss_request_table *rqtbl;
730Sstevel@tonic-gate     int argc;
740Sstevel@tonic-gate     char *argv[];
750Sstevel@tonic-gate     int sci_idx;
760Sstevel@tonic-gate {
770Sstevel@tonic-gate #ifdef __SABER__
780Sstevel@tonic-gate     struct _ss_request_entry *request;
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate     register ss_request_entry *request;
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate     register ss_data *info;
830Sstevel@tonic-gate     register char const * const * name;
840Sstevel@tonic-gate     char *string = argv[0];
850Sstevel@tonic-gate     int i;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate     info = ss_info(sci_idx);
880Sstevel@tonic-gate     info->argc = argc;
890Sstevel@tonic-gate     info->argv = argv;
900Sstevel@tonic-gate     for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
910Sstevel@tonic-gate 	for (name = request->command_names; *name; name++)
920Sstevel@tonic-gate 	    if (!strcmp(*name, string)) {
930Sstevel@tonic-gate 		info->current_request = request->command_names[0];
940Sstevel@tonic-gate 		(request->function)(argc, (const char *const *) argv,
950Sstevel@tonic-gate 				    sci_idx,info->info_ptr);
960Sstevel@tonic-gate 		info->current_request = (char *)NULL;
970Sstevel@tonic-gate 		return(0);
980Sstevel@tonic-gate 	    }
990Sstevel@tonic-gate     }
1000Sstevel@tonic-gate     return(SS_ET_COMMAND_NOT_FOUND);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * really_execute_command(sci_idx, argc, argv)
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * Function:
1070Sstevel@tonic-gate  *      Fills in the argc, argv values in the subsystem entry and
1080Sstevel@tonic-gate  *      call the appropriate routine.
1090Sstevel@tonic-gate  * Arguments:
1100Sstevel@tonic-gate  *      sci_idx (int)
1110Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
1120Sstevel@tonic-gate  *      argc (int)
1130Sstevel@tonic-gate  *              number of arguments in argument list
1140Sstevel@tonic-gate  *      argv (char **[])
1150Sstevel@tonic-gate  *              pointer to parsed argument list (may be reallocated
1160Sstevel@tonic-gate  *              on abbrev expansion)
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  * Returns:
1190Sstevel@tonic-gate  *      (int)
1200Sstevel@tonic-gate  *              Zero if successful, ss_et_command_not_found otherwise.
1210Sstevel@tonic-gate  * Notes:
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate 
really_execute_command(sci_idx,argc,argv)1240Sstevel@tonic-gate static int really_execute_command (sci_idx, argc, argv)
1250Sstevel@tonic-gate     int sci_idx;
1260Sstevel@tonic-gate     int argc;
1270Sstevel@tonic-gate     char **argv[];
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate     register ss_request_table **rqtbl;
1300Sstevel@tonic-gate     register ss_data *info;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate     info = ss_info(sci_idx);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate     for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
1350Sstevel@tonic-gate         if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
1360Sstevel@tonic-gate             return(0);
1370Sstevel@tonic-gate     }
1380Sstevel@tonic-gate     return(SS_ET_COMMAND_NOT_FOUND);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * ss_execute_command(sci_idx, argv)
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * Function:
1450Sstevel@tonic-gate  *	Executes a parsed command list within the subsystem.
1460Sstevel@tonic-gate  * Arguments:
1470Sstevel@tonic-gate  *	sci_idx (int)
1480Sstevel@tonic-gate  *		ss-internal index for subsystem control info structure
1490Sstevel@tonic-gate  *	argv (char *[])
1500Sstevel@tonic-gate  *		parsed argument list
1510Sstevel@tonic-gate  * Returns:
1520Sstevel@tonic-gate  *	(int)
1530Sstevel@tonic-gate  *		Zero if successful, ss_et_command_not_found otherwise.
1540Sstevel@tonic-gate  * Notes:
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
ss_execute_command(sci_idx,argv)1580Sstevel@tonic-gate ss_execute_command(sci_idx, argv)
1590Sstevel@tonic-gate 	int sci_idx;
1600Sstevel@tonic-gate 	register char *argv[];
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	register int i, argc;
1630Sstevel@tonic-gate 	char **argp;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	argc = 0;
1660Sstevel@tonic-gate 	for (argp = argv; *argp; argp++)
1670Sstevel@tonic-gate 		argc++;
1680Sstevel@tonic-gate 	argp = (char **)malloc((argc+1)*sizeof(char *));
1690Sstevel@tonic-gate 	for (i = 0; i <= argc; i++)
1700Sstevel@tonic-gate 		argp[i] = argv[i];
1710Sstevel@tonic-gate 	i = really_execute_command(sci_idx, argc, &argp);
1720Sstevel@tonic-gate 	free(argp);
1730Sstevel@tonic-gate 	return(i);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate  * ss_execute_line(sci_idx, line_ptr)
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * Function:
1800Sstevel@tonic-gate  *      Parses and executes a command line within a subsystem.
1810Sstevel@tonic-gate  * Arguments:
1820Sstevel@tonic-gate  *      sci_idx (int)
1830Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
1840Sstevel@tonic-gate  *      line_ptr (char *)
1850Sstevel@tonic-gate  *              Pointer to command line to be parsed.
1860Sstevel@tonic-gate  * Returns:
1870Sstevel@tonic-gate  *      (int)
1880Sstevel@tonic-gate  *      	Error code.
1890Sstevel@tonic-gate  * Notes:
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate 
ss_execute_line(sci_idx,line_ptr)1920Sstevel@tonic-gate int ss_execute_line (sci_idx, line_ptr)
1930Sstevel@tonic-gate     int sci_idx;
1940Sstevel@tonic-gate     char *line_ptr;
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate     char **argv;
1972881Smp153739     int argc, ret;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate     /* flush leading whitespace */
2000Sstevel@tonic-gate     while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
2010Sstevel@tonic-gate         line_ptr++;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate     /* check if it should be sent to operating system for execution */
2040Sstevel@tonic-gate     if (*line_ptr == '!') {
2050Sstevel@tonic-gate         if (ss_info(sci_idx)->flags.escape_disabled)
2060Sstevel@tonic-gate             return SS_ET_ESCAPE_DISABLED;
2070Sstevel@tonic-gate         else {
2080Sstevel@tonic-gate             line_ptr++;
2090Sstevel@tonic-gate             system(line_ptr);
2100Sstevel@tonic-gate 	    return 0;
2110Sstevel@tonic-gate         }
2120Sstevel@tonic-gate     }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate     /* parse it */
215*8093SMark.Phalan@Sun.COM     /* Solaris Kerberos */
216*8093SMark.Phalan@Sun.COM     (void) ss_parse(sci_idx, line_ptr, &argc, &argv, 0);
2170Sstevel@tonic-gate     if (argc == 0)
2180Sstevel@tonic-gate         return 0;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate     /* look it up in the request tables, execute if found */
2212881Smp153739     ret = really_execute_command (sci_idx, argc, &argv);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate     free(argv);
2240Sstevel@tonic-gate 
2252881Smp153739     return(ret);
2260Sstevel@tonic-gate }
227