xref: /titanic_50/usr/src/lib/krb5/ss/execute_cmd.c (revision 91d7f85e02991954d1e1bd44673df567ad8dcc87)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright 1987, 1988, 1989 by Massachusetts Institute of Technology
8  *
9  * For copyright info, see copyright.h.
10  */
11 
12 #include "ss_internal.h"
13 #include "copyright.h"
14 #include <stdio.h>
15 
16 
17 /*
18  * get_request(tbl, idx)
19  *
20  * Function:
21  *      Gets the idx'th request from the request table pointed to
22  *      by tbl.
23  * Arguments:
24  *      tbl (ss_request_table *)
25  *              pointer to request table
26  *      idx (int)
27  *              index into table
28  * Returns:
29  *      (ss_request_entry *)
30  *              pointer to request table entry
31  * Notes:
32  *      Has been replaced by a macro.
33  */
34 
35 #ifdef __SABER__
36 /* sigh.  saber won't deal with pointer-to-const-struct */
37 static struct _ss_request_entry * get_request (tbl, idx)
38     ss_request_table * tbl;
39     int idx;
40 {
41     struct _ss_request_table *tbl1 = (struct _ss_request_table *) tbl;
42     struct _ss_request_entry *e = (struct _ss_request_entry *) tbl1->requests;
43     return e + idx;
44 }
45 #else
46 #define get_request(tbl,idx)    ((tbl) -> requests + (idx))
47 #endif
48 
49 /*
50  * check_request_table(rqtbl, argc, argv, sci_idx)
51  *
52  * Function:
53  *      If the command string in argv[0] is in the request table, execute
54  *      the commands and return error code 0.  Otherwise, return error
55  *      code ss_et_command_not_found.
56  * Arguments:
57  *      rqtbl (ss_request_table *)
58  *              pointer to request table
59  *      argc (int)
60  *              number of elements in argv[]
61  *      argv (char *[])
62  *              argument string array
63  *      sci_idx (int)
64  *              ss-internal index for subsystem control info structure
65  * Returns:
66  *      (int)
67  *              zero if command found, ss_et_command_not_found otherwise
68  * Notes:
69  */
70 
71 static int check_request_table (rqtbl, argc, argv, sci_idx)
72     register ss_request_table *rqtbl;
73     int argc;
74     char *argv[];
75     int sci_idx;
76 {
77 #ifdef __SABER__
78     struct _ss_request_entry *request;
79 #else
80     register ss_request_entry *request;
81 #endif
82     register ss_data *info;
83     register char const * const * name;
84     char *string = argv[0];
85     int i;
86 
87     info = ss_info(sci_idx);
88     info->argc = argc;
89     info->argv = argv;
90     for (i = 0; (request = get_request(rqtbl, i))->command_names; i++) {
91 	for (name = request->command_names; *name; name++)
92 	    if (!strcmp(*name, string)) {
93 		info->current_request = request->command_names[0];
94 		(request->function)(argc, (const char *const *) argv,
95 				    sci_idx,info->info_ptr);
96 		info->current_request = (char *)NULL;
97 		return(0);
98 	    }
99     }
100     return(SS_ET_COMMAND_NOT_FOUND);
101 }
102 
103 /*
104  * really_execute_command(sci_idx, argc, argv)
105  *
106  * Function:
107  *      Fills in the argc, argv values in the subsystem entry and
108  *      call the appropriate routine.
109  * Arguments:
110  *      sci_idx (int)
111  *              ss-internal index for subsystem control info structure
112  *      argc (int)
113  *              number of arguments in argument list
114  *      argv (char **[])
115  *              pointer to parsed argument list (may be reallocated
116  *              on abbrev expansion)
117  *
118  * Returns:
119  *      (int)
120  *              Zero if successful, ss_et_command_not_found otherwise.
121  * Notes:
122  */
123 
124 static int really_execute_command (sci_idx, argc, argv)
125     int sci_idx;
126     int argc;
127     char **argv[];
128 {
129     register ss_request_table **rqtbl;
130     register ss_data *info;
131 
132     info = ss_info(sci_idx);
133 
134     for (rqtbl = info->rqt_tables; *rqtbl; rqtbl++) {
135         if (check_request_table (*rqtbl, argc, *argv, sci_idx) == 0)
136             return(0);
137     }
138     return(SS_ET_COMMAND_NOT_FOUND);
139 }
140 
141 /*
142  * ss_execute_command(sci_idx, argv)
143  *
144  * Function:
145  *	Executes a parsed command list within the subsystem.
146  * Arguments:
147  *	sci_idx (int)
148  *		ss-internal index for subsystem control info structure
149  *	argv (char *[])
150  *		parsed argument list
151  * Returns:
152  *	(int)
153  *		Zero if successful, ss_et_command_not_found otherwise.
154  * Notes:
155  */
156 
157 int
158 ss_execute_command(sci_idx, argv)
159 	int sci_idx;
160 	register char *argv[];
161 {
162 	register int i, argc;
163 	char **argp;
164 
165 	argc = 0;
166 	for (argp = argv; *argp; argp++)
167 		argc++;
168 	argp = (char **)malloc((argc+1)*sizeof(char *));
169 	for (i = 0; i <= argc; i++)
170 		argp[i] = argv[i];
171 	i = really_execute_command(sci_idx, argc, &argp);
172 	free(argp);
173 	return(i);
174 }
175 
176 /*
177  * ss_execute_line(sci_idx, line_ptr)
178  *
179  * Function:
180  *      Parses and executes a command line within a subsystem.
181  * Arguments:
182  *      sci_idx (int)
183  *              ss-internal index for subsystem control info structure
184  *      line_ptr (char *)
185  *              Pointer to command line to be parsed.
186  * Returns:
187  *      (int)
188  *      	Error code.
189  * Notes:
190  */
191 
192 int ss_execute_line (sci_idx, line_ptr)
193     int sci_idx;
194     char *line_ptr;
195 {
196     char **argv;
197     int argc, ret;
198 
199     /* flush leading whitespace */
200     while (line_ptr[0] == ' ' || line_ptr[0] == '\t')
201         line_ptr++;
202 
203     /* check if it should be sent to operating system for execution */
204     if (*line_ptr == '!') {
205         if (ss_info(sci_idx)->flags.escape_disabled)
206             return SS_ET_ESCAPE_DISABLED;
207         else {
208             line_ptr++;
209             system(line_ptr);
210 	    return 0;
211         }
212     }
213 
214     /* parse it */
215     /* Solaris Kerberos */
216     (void) ss_parse(sci_idx, line_ptr, &argc, &argv, 0);
217     if (argc == 0)
218         return 0;
219 
220     /* look it up in the request tables, execute if found */
221     ret = really_execute_command (sci_idx, argc, &argv);
222 
223     free(argv);
224 
225     return(ret);
226 }
227