summaryrefslogtreecommitdiff
path: root/app/xsm/remote.c
blob: 3fb21ecae3d63a8feb84e8f96ec0a43eb1a1ac9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* $Xorg: remote.c,v 1.5 2001/02/09 02:06:01 xorgcvs Exp $ */
/******************************************************************************

Copyright 1993, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
******************************************************************************/
/* $XFree86: xc/programs/xsm/remote.c,v 1.4 2001/01/17 23:46:30 dawes Exp $ */

/*
 * We use the rstart protocol to restart clients on remote machines.
 */

#include "xsm.h"
#include "log.h"

static char 		*format_rstart_env(char *str);


void
remote_start(const char *restart_protocol, const char *restart_machine,
	     const char *program, char **args, const char *cwd,
	     char **env, const char *non_local_display_env,
	     const char *non_local_session_env)
{
    FILE *fp;
    int	 pipefd[2];
    char msg[256];
    int  i;

    if (strcmp (restart_protocol, "rstart-rsh") != 0)
    {
	if (verbose)
	    printf ("Only rstart-rsh remote execution protocol supported.\n");
	return;
    }

    if (!restart_machine)
    {
	if (verbose)
	    printf ("Bad remote machine specified for remote execute.\n");
	return;
    }

    if (verbose)
	printf ("Attempting to restart remote client on %s\n",
	    restart_machine);

    if (pipe (pipefd) < 0)
    {
	snprintf (msg, sizeof(msg),
		  "%s: pipe() error during remote start of %s",
		  Argv[0], program);
	add_log_text (msg);
	perror (msg);
    }
    else
    {
	switch(fork())
	{
	case -1:

	    snprintf (msg, sizeof(msg),
		      "%s: fork() error during remote start of %s",
		      Argv[0], program);
	    add_log_text (msg);
	    perror (msg);
	    break;

	case 0:		/* kid */

	    close (pipefd[1]);
	    close (0);
	    dup (pipefd[0]);
	    close (pipefd[0]);

	    execlp (RSHCMD, restart_machine, "rstartd", (char *) 0);

	    snprintf (msg, sizeof(msg),
		      "%s: execlp() of rstartd failed for remote start of %s",
		      Argv[0], program);
	    perror (msg);
	    /*
	     * TODO : We would like to send this log information to the
	     * log window in the parent.  This would require using the
	     * pipe between the parent and child.  The child would
	     * set close-on-exec.  If the exec succeeds, the pipe will
	     * be closed.  If it fails, the child can write a message
	     * to the parent.
	     */
	    _exit(255);

	default:		/* parent */

	    close (pipefd[0]);
	    fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
	    fp = fdopen (pipefd[1], "w");

	    fprintf (fp, "CONTEXT X\n");
	    fprintf (fp, "DIR %s\n", cwd);
	    fprintf (fp, "DETACH\n");

	    if (env)
	    {
		/*
		 * The application saved its environment.
		 */

		for (i = 0; env[i]; i++)
		{
		    /*
		     * rstart requires that any spaces, backslashes, or
		     * non-printable characters inside of a string be
		     * represented by octal escape sequences.
		     */

		    char *temp = format_rstart_env (env[i]);
		    fprintf (fp, "MISC X %s\n", temp);
		    if (temp != env[i])
			XtFree (temp);
		}
	    }
	    else
	    {
		/*
		 * The application did not save its environment.
		 * The default PATH set up by rstart may not contain
		 * the program we want to restart.  We play it safe
		 * and pass xsm's PATH.  This will most likely contain
		 * the path we need.
		 */

		char *path = (char *) getenv ("PATH");

		if (path)
		    fprintf (fp, "MISC X PATH=%s\n", path);
	    }

	    fprintf (fp, "MISC X %s\n", non_local_display_env);
	    fprintf (fp, "MISC X %s\n", non_local_session_env);

	    /*
	     * Pass the authentication data.
	     * Each transport has auth data for ICE and XSMP.
	     * Don't pass local auth data.
	     */

	    for (i = 0; i < numTransports * 2; i++)
	    {
		if (Strstr (authDataEntries[i].network_id, "local/"))
		    continue;

		fprintf (fp, "AUTH ICE %s \"\" %s %s ",
		    authDataEntries[i].protocol_name,
		    authDataEntries[i].network_id,
		    authDataEntries[i].auth_name);
		
		fprintfhex (fp,
		    authDataEntries[i].auth_data_length,
		    authDataEntries[i].auth_data);
		
		fprintf (fp, "\n");
	    }

	    /*
	     * And execute the program
	     */

	    fprintf (fp, "EXEC %s %s", program, program);
	    for (i = 1; args[i]; i++)
		fprintf (fp, " %s", args[i]);
	    fprintf (fp, "\n\n");
	    fclose (fp);
	    break;
	}
    }
}



/*
 * rstart requires that any spaces/backslashes/non-printable characters
 * inside of a string be represented by octal escape sequences.
 */

static char *
format_rstart_env(char *str)
{
    int escape_count = 0, i;
    char *temp = str;

    while (*temp != '\0')
    {
	if (!isgraph (*temp) || *temp == '\\')
	    escape_count++;
	temp++;
    }

    if (escape_count == 0)
	return (str);
    else
    {
	int len = strlen (str) + 1 + (escape_count * 3);
	char *ret = (char *) XtMalloc (len);
	char *ptr = ret;

	temp = str;
	while (*temp != '\0')
	{
	    if (!isgraph (*temp) || *temp == '\\')
	    {
		char octal[4];
		snprintf (octal, sizeof(octal), "%o", *temp);
		*(ptr++) = '\\';
		for (i = 0; i < (3 - (int) strlen (octal)); i++)
		    *(ptr++) = '0';
		strcpy (ptr, octal);
		ptr += strlen (octal);
	    }
	    else
		*(ptr++) = *temp;

	    temp++;
	}

	*ptr = '\0';
	return (ret);
    }
}