xref: /titanic_41/usr/src/cmd/rexd/rex.c (revision 7a286c471efbab8562f7655a82931904703fffe0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * rex_xdr - remote execution external data representations
24  *
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /* XXX - Bad, Bad, Bad.... Fix this. This isn't allowed in the base */
32 #define	BSD_COMP
33 
34 #include <stdio.h>
35 #include <rpc/rpc.h>
36 #include <sys/errno.h>
37 #include <sys/ttold.h>
38 #include <stropts.h>
39 #include <sys/stream.h>
40 #include <sys/tty.h>
41 #include <sys/ptyvar.h>
42 
43 #include "rex.h"
44 
45 /*
46  * xdr_rex_start - process the command start structure
47  */
48 int
49 xdr_rex_start(XDR *xdrs,  struct rex_start *rst)
50 {
51 	return
52 		xdr_argv(xdrs, &rst->rst_cmd) &&
53 		xdr_string(xdrs, &rst->rst_host, 1024) &&
54 		xdr_string(xdrs, &rst->rst_fsname, 1024) &&
55 		xdr_string(xdrs, &rst->rst_dirwithin, 1024) &&
56 		xdr_argv(xdrs, &rst->rst_env) &&
57 		xdr_u_short(xdrs, &rst->rst_port0) &&
58 		xdr_u_short(xdrs, &rst->rst_port1) &&
59 		xdr_u_short(xdrs, &rst->rst_port2) &&
60 		xdr_u_long(xdrs, &rst->rst_flags);
61 }
62 
63 int
64 xdr_argv(XDR *xdrs, char ***argvp)
65 {
66 	register char **argv = *argvp;
67 	register char **ap;
68 	int i, count;
69 
70 	/*
71 	 * find the number of args to encode or free
72 	 */
73 	if ((xdrs->x_op) != XDR_DECODE)
74 		for (count = 0, ap = argv; *ap != 0; ap++)
75 			count++;
76 	/* XDR the count */
77 	if (!xdr_u_int(xdrs, (unsigned *) &count))
78 		return (FALSE);
79 
80 	/*
81 	 * now deal with the strings
82 	 */
83 	if (xdrs->x_op == XDR_DECODE) {
84 		*argvp = argv = (char **)
85 			mem_alloc((unsigned)(count+1)*sizeof (char **));
86 		for (i = 0; i <= count; i++)	/* Note: <=, not < */
87 			argv[i] = 0;
88 	}
89 
90 	for (i = 0, ap = argv; i < count; i++, ap++)
91 		if (!xdr_string(xdrs, ap, 10240))
92 			return (FALSE);
93 
94 	if (xdrs->x_op == XDR_FREE && argv != NULL) {
95 		mem_free((char *) argv, (count+1)*sizeof (char **));
96 		*argvp = NULL;
97 	}
98 	return (TRUE);
99 }
100 
101 /*
102  * xdr_rex_result - process the result of a start or wait operation
103  */
104 int
105 xdr_rex_result(XDR *xdrs, struct rex_result *result)
106 {
107 	return
108 		xdr_int(xdrs, &result->rlt_stat) &&
109 		xdr_string(xdrs, &result->rlt_message, 1024);
110 
111 }
112 
113 /*
114  * xdr_rex_ttymode - process the tty mode information
115  */
116 int
117 xdr_rex_ttymode(XDR *xdrs, struct rex_ttymode *mode)
118 {
119 	u_int six = 6;
120 	u_int four = 4;
121 	char *speedp = NULL;
122 	char *morep = NULL;
123 	char *yetmorep = NULL;
124 
125 	if (xdrs->x_op != XDR_FREE) {
126 		speedp = &mode->basic.sg_ispeed;
127 		morep = (char *)&mode->more;
128 		yetmorep = (char *)&mode->yetmore;
129 	}
130 	return
131 		xdr_bytes(xdrs, (char **) &speedp, (u_int *)&four, 4) &&
132 		xdr_short(xdrs, (short *) &mode->basic.sg_flags) &&
133 		xdr_bytes(xdrs, (char **) &morep, (u_int *)&six, 6) &&
134 		xdr_bytes(xdrs, (char **) &yetmorep, (u_int *)&six, 6) &&
135 		xdr_u_long(xdrs, &mode->andmore);
136 }
137 
138 
139 /*
140  * xdr_rex_ttysize - process the tty size information
141  */
142 int
143 xdr_rex_ttysize(XDR *xdrs, struct ttysize *size)
144 {
145 	return
146 		xdr_int(xdrs, &size->ts_lines) &&
147 		xdr_int(xdrs, &size->ts_cols);
148 }
149