xref: /illumos-gate/usr/src/lib/krb5/ss/execute_cmd.c (revision 6d084746)
17c478bd9Sstevel@tonic-gate /*
2*6d084746S  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * For copyright info, see copyright.h.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "ss_internal.h"
137c478bd9Sstevel@tonic-gate #include "copyright.h"
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate /*
187c478bd9Sstevel@tonic-gate  * get_request(tbl, idx)
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Function:
217c478bd9Sstevel@tonic-gate  *      Gets the idx'th request from the request table pointed to
227c478bd9Sstevel@tonic-gate  *      by tbl.
237c478bd9Sstevel@tonic-gate  * Arguments:
247c478bd9Sstevel@tonic-gate  *      tbl (ss_request_table *)
257c478bd9Sstevel@tonic-gate  *              pointer to request table
267c478bd9Sstevel@tonic-gate  *      idx (int)
277c478bd9Sstevel@tonic-gate  *              index into table
287c478bd9Sstevel@tonic-gate  * Returns:
297c478bd9Sstevel@tonic-gate  *      (ss_request_entry *)
307c478bd9Sstevel@tonic-gate  *              pointer to request table entry
317c478bd9Sstevel@tonic-gate  * Notes:
327c478bd9Sstevel@tonic-gate  *      Has been replaced by a macro.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef __SABER__
367c478bd9Sstevel@tonic-gate /* sigh.  saber won't deal with pointer-to-const-struct */
get_request(tbl,idx)377c478bd9Sstevel@tonic-gate static struct _ss_request_entry * get_request (tbl, idx)
387c478bd9Sstevel@tonic-gate     ss_request_table * tbl;
397c478bd9Sstevel@tonic-gate     int idx;
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate     struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
427c478bd9Sstevel@tonic-gate     struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
437c478bd9Sstevel@tonic-gate     return e + idx;
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate #else
467c478bd9Sstevel@tonic-gate #define get_request(tbl,idx)    ((tbl) -> requests + (idx))
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * check_request_table(rqtbl, argc, argv, sci_idx)
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Function:
537c478bd9Sstevel@tonic-gate  *      If the command string in argv[0] is in the request table, execute
547c478bd9Sstevel@tonic-gate  *      the commands and return error code 0.  Otherwise, return error
557c478bd9Sstevel@tonic-gate  *      code ss_et_command_not_found.
567c478bd9Sstevel@tonic-gate  * Arguments:
577c478bd9Sstevel@tonic-gate  *      rqtbl (ss_request_table *)
587c478bd9Sstevel@tonic-gate  *              pointer to request table
597c478bd9Sstevel@tonic-gate  *      argc (int)
607c478bd9Sstevel@tonic-gate  *              number of elements in argv[]
617c478bd9Sstevel@tonic-gate  *      argv (char *[])
627c478bd9Sstevel@tonic-gate  *              argument string array
637c478bd9Sstevel@tonic-gate  *      sci_idx (int)
647c478bd9Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
657c478bd9Sstevel@tonic-gate  * Returns:
667c478bd9Sstevel@tonic-gate  *      (int)
677c478bd9Sstevel@tonic-gate  *              zero if command found, ss_et_command_not_found otherwise
687c478bd9Sstevel@tonic-gate  * Notes:
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate 
check_request_table(rqtbl,argc,argv,sci_idx)717c478bd9Sstevel@tonic-gate static int check_request_table (rqtbl, argc, argv, sci_idx)
727c478bd9Sstevel@tonic-gate     register ss_request_table *rqtbl;
737c478bd9Sstevel@tonic-gate     int argc;
747c478bd9Sstevel@tonic-gate     char *argv[];
757c478bd9Sstevel@tonic-gate     int sci_idx;
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate #ifdef __SABER__
787c478bd9Sstevel@tonic-gate     struct _ss_request_entry *request;
797c478bd9Sstevel@tonic-gate #else
807c478bd9Sstevel@tonic-gate     register ss_request_entry *request;
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate     register ss_data *info;
837c478bd9Sstevel@tonic-gate     register char const * const * name;
847c478bd9Sstevel@tonic-gate     char *string = argv[0];
857c478bd9Sstevel@tonic-gate     int i;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
887c478bd9Sstevel@tonic-gate     info->argc = argc;
897c478bd9Sstevel@tonic-gate     info->argv = argv;
907c478bd9Sstevel@tonic-gate     for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
917c478bd9Sstevel@tonic-gate 	for (name = request->command_names; *name; name++)
927c478bd9Sstevel@tonic-gate 	    if (!strcmp(*name, string)) {
937c478bd9Sstevel@tonic-gate 		info->current_request = request->command_names[0];
947c478bd9Sstevel@tonic-gate 		(request->function)(argc, (const char *const *) argv,
957c478bd9Sstevel@tonic-gate 				    sci_idx,info->info_ptr);
967c478bd9Sstevel@tonic-gate 		info->current_request = (char *)NULL;
977c478bd9Sstevel@tonic-gate 		return(0);
987c478bd9Sstevel@tonic-gate 	    }
997c478bd9Sstevel@tonic-gate     }
1007c478bd9Sstevel@tonic-gate     return(SS_ET_COMMAND_NOT_FOUND);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * really_execute_command(sci_idx, argc, argv)
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * Function:
1077c478bd9Sstevel@tonic-gate  *      Fills in the argc, argv values in the subsystem entry and
1087c478bd9Sstevel@tonic-gate  *      call the appropriate routine.
1097c478bd9Sstevel@tonic-gate  * Arguments:
1107c478bd9Sstevel@tonic-gate  *      sci_idx (int)
1117c478bd9Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
1127c478bd9Sstevel@tonic-gate  *      argc (int)
1137c478bd9Sstevel@tonic-gate  *              number of arguments in argument list
1147c478bd9Sstevel@tonic-gate  *      argv (char **[])
1157c478bd9Sstevel@tonic-gate  *              pointer to parsed argument list (may be reallocated
1167c478bd9Sstevel@tonic-gate  *              on abbrev expansion)
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * Returns:
1197c478bd9Sstevel@tonic-gate  *      (int)
1207c478bd9Sstevel@tonic-gate  *              Zero if successful, ss_et_command_not_found otherwise.
1217c478bd9Sstevel@tonic-gate  * Notes:
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate 
really_execute_command(sci_idx,argc,argv)1247c478bd9Sstevel@tonic-gate static int really_execute_command (sci_idx, argc, argv)
1257c478bd9Sstevel@tonic-gate     int sci_idx;
1267c478bd9Sstevel@tonic-gate     int argc;
1277c478bd9Sstevel@tonic-gate     char **argv[];
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate     register ss_request_table **rqtbl;
1307c478bd9Sstevel@tonic-gate     register ss_data *info;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate     for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
1357c478bd9Sstevel@tonic-gate         if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
1367c478bd9Sstevel@tonic-gate             return(0);
1377c478bd9Sstevel@tonic-gate     }
1387c478bd9Sstevel@tonic-gate     return(SS_ET_COMMAND_NOT_FOUND);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * ss_execute_command(sci_idx, argv)
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * Function:
1457c478bd9Sstevel@tonic-gate  *	Executes a parsed command list within the subsystem.
1467c478bd9Sstevel@tonic-gate  * Arguments:
1477c478bd9Sstevel@tonic-gate  *	sci_idx (int)
1487c478bd9Sstevel@tonic-gate  *		ss-internal index for subsystem control info structure
1497c478bd9Sstevel@tonic-gate  *	argv (char *[])
1507c478bd9Sstevel@tonic-gate  *		parsed argument list
1517c478bd9Sstevel@tonic-gate  * Returns:
1527c478bd9Sstevel@tonic-gate  *	(int)
1537c478bd9Sstevel@tonic-gate  *		Zero if successful, ss_et_command_not_found otherwise.
1547c478bd9Sstevel@tonic-gate  * Notes:
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
ss_execute_command(sci_idx,argv)1587c478bd9Sstevel@tonic-gate ss_execute_command(sci_idx, argv)
1597c478bd9Sstevel@tonic-gate 	int sci_idx;
1607c478bd9Sstevel@tonic-gate 	register char *argv[];
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	register int i, argc;
1637c478bd9Sstevel@tonic-gate 	char **argp;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	argc = 0;
1667c478bd9Sstevel@tonic-gate 	for (argp = argv; *argp; argp++)
1677c478bd9Sstevel@tonic-gate 		argc++;
1687c478bd9Sstevel@tonic-gate 	argp = (char **)malloc((argc+1)*sizeof(char *));
1697c478bd9Sstevel@tonic-gate 	for (i = 0; i <= argc; i++)
1707c478bd9Sstevel@tonic-gate 		argp[i] = argv[i];
1717c478bd9Sstevel@tonic-gate 	i = really_execute_command(sci_idx, argc, &argp);
1727c478bd9Sstevel@tonic-gate 	free(argp);
1737c478bd9Sstevel@tonic-gate 	return(i);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * ss_execute_line(sci_idx, line_ptr)
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  * Function:
1807c478bd9Sstevel@tonic-gate  *      Parses and executes a command line within a subsystem.
1817c478bd9Sstevel@tonic-gate  * Arguments:
1827c478bd9Sstevel@tonic-gate  *      sci_idx (int)
1837c478bd9Sstevel@tonic-gate  *              ss-internal index for subsystem control info structure
1847c478bd9Sstevel@tonic-gate  *      line_ptr (char *)
1857c478bd9Sstevel@tonic-gate  *              Pointer to command line to be parsed.
1867c478bd9Sstevel@tonic-gate  * Returns:
1877c478bd9Sstevel@tonic-gate  *      (int)
1887c478bd9Sstevel@tonic-gate  *      	Error code.
1897c478bd9Sstevel@tonic-gate  * Notes:
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate 
ss_execute_line(sci_idx,line_ptr)1927c478bd9Sstevel@tonic-gate int ss_execute_line (sci_idx, line_ptr)
1937c478bd9Sstevel@tonic-gate     int sci_idx;
1947c478bd9Sstevel@tonic-gate     char *line_ptr;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate     char **argv;
19756a424ccSmp     int argc, ret;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate     /* flush leading whitespace */
2007c478bd9Sstevel@tonic-gate     while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
2017c478bd9Sstevel@tonic-gate         line_ptr++;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate     /* check if it should be sent to operating system for execution */
2047c478bd9Sstevel@tonic-gate     if (*line_ptr == '!') {
2057c478bd9Sstevel@tonic-gate         if (ss_info(sci_idx)->flags.escape_disabled)
2067c478bd9Sstevel@tonic-gate             return SS_ET_ESCAPE_DISABLED;
2077c478bd9Sstevel@tonic-gate         else {
2087c478bd9Sstevel@tonic-gate             line_ptr++;
2097c478bd9Sstevel@tonic-gate             system(line_ptr);
2107c478bd9Sstevel@tonic-gate 	    return 0;
2117c478bd9Sstevel@tonic-gate         }
2127c478bd9Sstevel@tonic-gate     }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate     /* parse it */
215*6d084746S     /* Solaris Kerberos */
216*6d084746S     (void) ss_parse(sci_idx, line_ptr, &argc, &argv, 0);
2177c478bd9Sstevel@tonic-gate     if (argc == 0)
2187c478bd9Sstevel@tonic-gate         return 0;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate     /* look it up in the request tables, execute if found */
22156a424ccSmp     ret = really_execute_command (sci_idx, argc, &argv);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate     free(argv);
2247c478bd9Sstevel@tonic-gate 
22556a424ccSmp     return(ret);
2267c478bd9Sstevel@tonic-gate }
227