Link with C++ linker
[oweals/cde.git] / cde / programs / dtspcd / main.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * File:         main.c $TOG: main.c /main/8 1999/09/30 15:31:41 mgreess $
25  * Language:     C
26  *
27  * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
28  *
29  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
30  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
31  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
32  * (c) Copyright 1993, 1994 Novell, Inc.                                *
33  */
34
35 #include <bms/sbport.h>
36
37 #include <time.h>                       /* ctime() */
38 #include <pwd.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <unistd.h>                     /* initgroups() */
42 #include <string.h>                     /* basename() on DEC */
43 #ifndef __osf__
44 # include <libgen.h>                    /* basename() everywhere else. */
45 #endif
46 #include <sys/param.h>                  /* MAXPATHLEN */
47
48 #include <bms/bms.h>
49 #include <bms/MemoryMgr.h>
50 #include <bms/XeUserMsg.h>
51 #include <bms/usersig.h>
52 #include <bms/spc.h>
53
54 #include <SPC/spcP.h>           
55 #include <SPC/spc-proto.h>
56 #include <XlationSvc.h>
57 #include <LocaleXlate.h>
58
59 #include <Tt/tt_c.h>
60
61 #include "spcd_event.h"
62
63 static char *MOUNT_POINT = "DTMOUNTPOINT";
64
65 /* Forward declarations */
66 /** ------------------ **/
67 static void Merge_Lang_Var(SPC_Channel_Ptr channel);
68
69 void SPCD_Handle_Client_Data(void *channel, int *source, SbInputId *id );
70 int SPCD_Initialize(void);
71 void SPCD_Exit(int exitval);
72 void SPCD_Handle_Application_Data(void *chn, XeString text, int size, int conn_type );
73 void SPCD_Termination_Handler(SPC_Channel_Ptr channel, int pid, int type, int cause, void *data );
74 int Client_Abort(protocol_request_ptr prot);
75 int Client_Register(protocol_request_ptr prot);
76 int Client_Unregister(protocol_request_ptr prot);
77 int Client_Channel_Open(protocol_request_ptr prot);
78 int Client_Channel_Close(protocol_request_ptr prot);
79 int Client_Channel_Reset(protocol_request_ptr prot);
80 int Client_Channel_Attach(protocol_request_ptr prot);
81 int Client_Application_Spawn(protocol_request_ptr prot);
82 int Client_Application_Signal(protocol_request_ptr prot);
83 int Client_Application_Data(protocol_request_ptr prot);
84 int Client_Server_Debug(protocol_request_ptr prot);
85 int Client_Environ_Reset(protocol_request_ptr prot);
86 int Client_Reply_Devices(protocol_request_ptr prot);
87 int Client_Reply_Logfile(protocol_request_ptr prot);
88 int Client_Delete_Logfile(protocol_request_ptr prot);
89 int Client_Reset_Termio(protocol_request_ptr prot);
90 int Client_Reset_Termios(protocol_request_ptr prot);
91 int Client_Protocol_Version(protocol_request_ptr prot);
92 int Client_Reply_Protocol(protocol_request_ptr prot);
93 int Client_Reply_Hostinfo(protocol_request_ptr prot);
94
95 /* New B.00 methods */
96
97 int Client_Send_EOF(protocol_request_ptr prot);
98 int Client_Channel_Termios(protocol_request_ptr prot);
99 int Client_Enhanced_Spawn(protocol_request_ptr prot);
100
101 #if defined(_AIX) || defined(DEC)
102 # define SA_HANDLER_INT_ARG
103 #endif /* _AIX || DEC */
104
105 void conditional_putenv(XeString env_str);
106 void SPCD_Reply(SPC_Connection_Ptr connection, protocol_request_ptr prot, int retval, int errval );
107 #ifdef SA_HANDLER_INT_ARG
108 void SPCD_Alarm_Handler (int);
109 #else
110 void SPCD_Alarm_Handler (void);
111 #endif /* SA_HANDLER_INT_ARG */
112
113 /* Global data */
114
115 protocol_request_handler spcd_protocol_dispatch_table[NREQS]={
116   Client_Application_Data,   /* APPLICATION_DATA */
117   NULL,                      /* APPLICATION_STDOUT */
118   NULL,                      /* APPLICATION_STDERR */
119   Client_Abort,              /* ABORT */
120   Client_Register,           /* REGISTER */
121   Client_Unregister,         /* UNREGISTER */
122   Client_Channel_Open,       /* CHANNEL_OPEN */
123   Client_Channel_Close,      /* CHANNEL_CLOSE */
124   Client_Channel_Reset,      /* CHANNEL_RESET */
125   Client_Channel_Attach,     /* CHANNEL_ATTACH */
126   Client_Application_Spawn,  /* APPLICATION_SPAWN */
127   Client_Application_Signal, /* APPLICATION_SIGNAL */
128   NULL,                      /* APPLICATION_DIED */
129   NULL,                      /* SERVER_ERROR */
130   NULL,                      /* REPLY */
131   Client_Server_Debug,       /* SERVER_DEBUG */
132   Client_Environ_Reset,      /* ENVIRON_RESET */
133   Client_Reply_Devices,      /* QUERY_DEVICES */
134   NULL,                      /* DEVICE_REPLY */
135   Client_Reply_Logfile,      /* QUERY_LOGFILE */
136   NULL,                      /* LOGFILE_REPLY */
137   Client_Delete_Logfile,     /* DELETE_LOGFILE */
138   Client_Reset_Termio,       /* RESET_TERMIO (obsolete) */
139   Client_Reset_Termios,      /* RESET_TERMIOS */
140   Client_Send_EOF,           /* CHANNEL_SEND_EOF */
141   Client_Channel_Termios,    /* CHANNEL_TERMIOS */
142   Client_Enhanced_Spawn,     /* APP_B00_SPAWN */
143 };
144
145 /*
146  * HomeDir will be set to "HOME=pwent->pw_dir".
147  *
148  * ShellDir will be set to "SHELL=pwent->pw_shell".
149  */
150 XeChar HomeDir[MAXPATHLEN + 6];
151 XeChar ShellDir[MAXPATHLEN + 7];
152 XeString *default_environment;
153
154 int client_validated=NULL;
155 int SPCD_Abort_Okay = FALSE;
156
157 SPC_Connection_Ptr client_connection;
158
159 SPC_Channel client_channel;
160
161 /*
162  * This variable contains the number of minutes in the daemon's
163  * exit timer.  If the daemon has no activity within this period 
164  * of time and there are no sub-processes running, the daemon
165  * will exit.
166  */
167 static int exit_timeout = SPCD_DEFAULT_TIMEOUT;
168
169 /*
170  * The following variable is used by the timer code to indicate
171  * if a request is currently being serviced.  This is necessary
172  * because of the following scenario:
173  *
174  *   t0 - a request is made
175  *   t1 - the request is serviced
176  *   t2 - the timer expires
177  *   t3 - the timer handler sees no sub-processes running so it exits
178  *   t4 - request is done, reply to client
179  *
180  * This variable is set to SPCD_REQUEST_PENDING at t0 and to
181  * SPCD_NO_REQUST_PENDING at t4.  If at t3 the variable is set
182  * to SPCD_REQUEST_PENDING, the alarm will be reset and the
183  * daemon will continue.
184  */
185 static int request_pending = SPCD_NO_REQUEST_PENDING;
186
187 /*----------------------------------------------------------------------+*/
188 int main(unsigned int argc, XeString *argv)
189 /*----------------------------------------------------------------------+*/
190 {
191   /* Parse the command line and set globals accordingly. */
192   XeString log_path = NULL;
193   Boolean terminate_flag = FALSE;
194   int i;
195   struct sigaction alarm_vector;
196   char tmp[200];
197   char *pch;
198   
199   /*
200    * The SPC library needs to know this is a SPC 'daemon' process
201    * to ensure 'SPC_Initialize()' installs a SIGCLD handler.
202    */
203   SPC_who_am_i = SPC_I_AM_A_DAEMON;
204   SPCD_Authentication_Dir = NULL;
205
206   /* set up log file path */
207   log_path = XeSBTempPath((XeString)"DTSPCD.log");
208
209   freopen("/dev/null", "w", stderr);
210   
211   /* Process arguments and set flags.  */
212   for (i=1; i < argc; i++) {
213
214     if (!strcmp ("-log", argv[i])) {
215       /* Log mode.  Print information to a log file */
216       /* Open an error log with whatever name the library wants to use */
217       SPC_Open_Log(log_path, FALSE);
218     }
219
220     else if (!strcmp ("-debug", argv[i])) {
221       /* Debug mode.  Print protocol information to a log file */
222       /* Open an error log with whatever name the library wants to use */
223       SPC_Open_Log(log_path, FALSE);
224       SPC_Print_Protocol = spc_logF;
225       freopen(log_path, "a", stderr);
226       setbuf(stderr, NULL);
227     }
228
229     else if (!strcmp ("-auth_dir", argv[i])) {
230       /* 
231        * Used to override the default directory for authentication file 
232        */
233       i++;
234       if (i != argc) {
235         SPC_Format_Log((XeString)"Authentication directory set to '%s'.", 
236                        argv[i]);
237         SPCD_Authentication_Dir = strdup (argv[i]);
238       }
239     }
240
241     else if (!strcmp ("-mount_point", argv[i])) {
242       /* 
243        * Mount point for the filename mapping system.
244        */
245       i++;
246       if (i != argc) {
247         (void) sprintf (tmp, "%s=%s", MOUNT_POINT, argv[i]);
248         if (putenv (tmp) == 0) {
249           SPC_Format_Log((XeString)"Mount point set to '%s'.", argv[i]);
250           SPC_mount_point_env_var = (char *) malloc (strlen (tmp) + 1);
251           (void) strcpy (SPC_mount_point_env_var, tmp);
252         }
253         else
254           SPC_Format_Log((XeString)"Failed to add the mount point '%s' to the environment.", tmp);
255       }
256     }
257
258     else if (!strcmp ("-timeout", argv[i])) {
259       /* 
260        * The timeout is specified, so use it instead of the default.
261        */
262       i++;
263       if (i != argc) {
264         exit_timeout = atoi(argv[i]);
265         SPC_Format_Log((XeString)"Setting the exit timer to '%s' minutes.", 
266                        argv[i]);
267       }
268     }
269
270     else {
271       /* 
272        * Unknown command option 
273        */
274       SPC_Format_Log((XeString)"Command line option '%s' unrecognized.", 
275                      argv[i]);
276     }
277   }
278
279   /* free strings allocated for path */
280   XeFree(log_path);
281
282   /*
283    * Initialize the i/o function pointers.
284    */
285   SbAddInput_hookfn        = SPCD_AddInput;
286   SbAddException_hookfn    = SPCD_AddException;
287   SbRemoveInput_hookfn     = SPCD_RemoveInput;
288   SbRemoveException_hookfn = SPCD_RemoveException;
289   SbMainLoopUntil_hookfn   = SPCD_MainLoopUntil;
290   SbBreakMainLoop_hookfn   = SPCD_BreakMainLoop;
291
292   /* Initialization Commands */
293   if (SPCD_Initialize()==SPC_ERROR)
294     SPCD_Exit(1);
295
296   client_connection=SPC_Start_Daemon(FALSE);
297   if(client_connection==SPC_ERROR)
298     SPCD_Exit(1);
299
300   client_channel.connection = client_connection;
301   
302   SPC_XtAddInput(&client_channel,
303                  &client_connection->termination_id,
304                  client_connection->sid,
305                  SPCD_Handle_Client_Data,
306                  SPC_Client);
307   
308   if (exit_timeout != SPCD_NO_TIMER) {
309     alarm_vector.sa_handler = SPCD_Alarm_Handler;
310     alarm_vector.sa_flags = 0;
311     (void) sigaction (SIGALRM, &alarm_vector, (struct sigaction *)NULL);
312     (void) alarm (exit_timeout * 60);
313   }
314
315   /*
316    * The daemon's mount point environment variable needs to be 
317    * saved.  It will be used to override the client's mount point
318    * setting or the mount point specified in any of the environment 
319    * files.
320    */
321   if (SPC_mount_point_env_var == NULL)
322     if ((pch = getenv (MOUNT_POINT)) != NULL) {
323       SPC_mount_point_env_var = (char *) malloc (strlen (pch) + 
324                                                  strlen (MOUNT_POINT) + 2);
325       (void) sprintf (SPC_mount_point_env_var, "%s=%s", MOUNT_POINT, pch);
326     }
327
328   XeCall_SbMainLoopUntil(&terminate_flag);
329   SPCD_Exit(0);
330 }
331
332 /*----------------------------------------------------------------------+*/
333 void SPCD_Handle_Client_Data(void      *channel,
334                              int       *UNUSED_PARM(source),
335                              SbInputId *UNUSED_PARM(id))
336 /*----------------------------------------------------------------------+*/
337 {
338   protocol_request_ptr prot;
339   int retval;
340   SPC_Connection_Ptr connection=((SPC_Channel_Ptr)channel)->connection;
341
342   request_pending = SPCD_REQUEST_PENDING;
343
344   prot=SPC_Read_Protocol(connection);
345   if(!prot)
346     SPCD_Exit(0);
347
348   /* Check for valid client (in other words, that we have registered
349      this client). */
350
351   if((!client_validated) && (prot->request_type != REGISTER)) {
352     SPC_Error(SPC_Client_Not_Valid);
353     if (exit_timeout != SPCD_NO_TIMER)
354       (void) alarm (exit_timeout * 60);
355     request_pending = SPCD_REQUEST_PENDING;
356     return;
357   }
358
359   SPCD_Abort_Okay = TRUE;
360   retval=SPC_Dispatch_Protocol(prot, spcd_protocol_dispatch_table);
361   SPCD_Abort_Okay = FALSE;
362   if(REPLY_EXPECTED(prot->request_type, TRUE) != NO_REPLY_VAL)
363     SPCD_Reply(connection, prot, retval, errno);
364   SPC_Free_Protocol_Ptr(prot);
365
366   /* 
367    * Reset the alarm and go back to select.
368    */
369   if (exit_timeout != SPCD_NO_TIMER)
370     (void) alarm (exit_timeout * 60);
371
372   request_pending = SPCD_NO_REQUEST_PENDING;
373 }
374
375 /*----------------------------------------------------------------------+*/
376 int SPCD_Initialize(void)
377 /*----------------------------------------------------------------------+*/
378 {
379   XeString sys_env_file = NULL;
380   
381   /* Do initialization for SPC */
382   if(SPC_Initialize()==SPC_ERROR)
383     return(SPC_ERROR);
384
385   /* Do Daemon specific initialization */
386
387   /* Error handling */
388
389   XeProgName=SPCD_PROG_NAME;
390
391   default_environment=
392     (XeString *) malloc(DEFAULT_ENVP_SIZE * sizeof(XeString));
393   default_environment[0]=NULL;
394
395   /*
396    * First add the installed environment file.
397    */
398   sys_env_file = (XeString) malloc (strlen(SPCD_ENV_INSTALL_DIRECTORY) +
399                                     strlen(SPCD_ENV_FILE) + 3);
400   (void) sprintf (sys_env_file, "%s/%s", 
401                   SPCD_ENV_INSTALL_DIRECTORY, 
402                   SPCD_ENV_FILE);
403   default_environment=SPC_Add_Env_File(sys_env_file,default_environment);
404
405   /*
406    * Now add the configured environment file.
407    */
408   sys_env_file = (XeString) realloc (sys_env_file,
409                                      strlen(SPCD_ENV_CONFIG_DIRECTORY) +
410                                      strlen(SPCD_ENV_FILE) + 3);
411   (void) sprintf (sys_env_file, "%s/%s", 
412                   SPCD_ENV_CONFIG_DIRECTORY, 
413                   SPCD_ENV_FILE);
414
415   default_environment=SPC_Add_Env_File(sys_env_file,default_environment);
416
417   free (sys_env_file);
418
419   return(TRUE);
420 }
421
422 /*----------------------------------------------------------------------+*/
423 void SPCD_Exit(int exitval)
424 /*----------------------------------------------------------------------+*/
425 {
426   int i;
427
428   if (SPC_logfile_list != NULL)
429     for (i = 0; SPC_logfile_list[i] != NULL; i++)
430       (void) unlink (SPC_logfile_list[i]);
431
432   SPC_Format_Log((XeString)"Exiting server.  Retval: %d", exitval);
433   SPC_Close_Log();
434
435   exit(exitval);
436 }
437
438 /*----------------------------------------------------------------------+*/
439 void SPCD_Handle_Application_Data(void * chn,
440                                   XeString text,
441                                   int size,
442                                   int conn_type )
443 /*----------------------------------------------------------------------+*/
444 {
445   SPC_Channel_Ptr channel= (SPC_Channel_Ptr) chn;
446   SPC_Write_Protocol_Request(client_connection, channel,
447                              CONNECTOR_TO_PROT(conn_type), text, size);
448 }
449
450
451 /*----------------------------------------------------------------------+*/
452 void SPCD_Termination_Handler(SPC_Channel_Ptr channel,
453                               int UNUSED_PARM(pid),
454                               int UNUSED_PARM(type),
455                               int UNUSED_PARM(cause),
456                               void * UNUSED_PARM(data))
457 /*----------------------------------------------------------------------+*/
458 {
459   /* Write a termination protocol request */
460
461   SPC_Write_Protocol_Request(client_connection, channel,
462                              APPLICATION_DIED, channel->status);
463
464   /* That's all, folks! */
465 }
466
467 /*
468  ***
469  *** Protocol request handlers
470  ***
471 */
472
473 /*----------------------------------------------------------------------+*/
474 int Client_Abort(protocol_request_ptr prot)
475 /*----------------------------------------------------------------------+*/
476 {
477   return(print_protocol_request((XeString)"--> ABORT", prot));
478 }
479
480 #define FREE_USER_PASS(a, b) free(a); free(b);
481
482 /*----------------------------------------------------------------------+*/
483 int Client_Register(protocol_request_ptr prot)
484 /*----------------------------------------------------------------------+*/
485 {
486   XeString username;
487   XeString passwd;
488   XeString proto_ver;
489   XeString hostinfo;
490   struct passwd *pwent;
491   XeString tmpfile, tmppath;
492   XeChar  buffer[MAXPATHLEN * 2];
493   struct stat buf;
494   XeString netfile;
495   int free_netfile = 0;
496   char *spc_prefix = "/.SPC_";
497   char *spc_suffix;
498   char tmpnam_buf[L_tmpnam + 1];
499
500   print_protocol_request((XeString)"--> REGISTER", prot);
501   prot->channel=0;
502
503   READ_REGISTER(prot->dataptr, username, passwd, proto_ver, hostinfo);
504   
505   if(strcmp(username, "") != 0) {
506     SPC_Format_Log ( "+++> Starting authentication for user '%s'\n     from host '%s'.", 
507             username, hostinfo);
508     /*  
509      * We have a username so generate a temp filename and send it 
510      * back to the client after creating the proper path for the file.
511      */
512     if(SPCD_Authentication_Dir)
513       /*
514        * Use the directory specified on the command line.
515        */
516       tmppath = strdup(SPCD_Authentication_Dir);
517     else {
518       /*
519        * Use the $HOME directory if it can be retrieved from the
520        * password file.
521        */
522       if(!(pwent=getpwnam(username))) {
523         SPC_Format_Log ("+++> FAILURE: username '%s' is unknown.", username);
524         SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
525                                    FAILED_FILE_NAME, NULL, NULL);
526         FREE_USER_PASS(username, passwd);
527         SPC_Error(SPC_Bad_Username);
528         return(SPC_ERROR);
529       }
530       else {
531         tmppath = (XeString)(pwent->pw_dir);
532            
533         if ((lstat (tmppath, &buf) != 0) || 
534             (!S_ISDIR(buf.st_mode))     || 
535             (!(buf.st_mode & S_IRUSR)))
536           /*
537            * Use the default directory.
538            */
539           tmppath = XeSBTempPath(XeString_NULL);
540       }
541     }
542
543     /*
544      * tempnam(3) has side effects caused by permissions of the directory
545      * given or the TMPDIR envirnoment variable.  Because of these side
546      * effects, the function may return "/tmp/.SPC_xxxxxx" and ignore
547      * tmppath.  The protocol will fail when this occurs.  The fix is
548      * to construct the tmpfile name.
549      */
550     tmpnam(tmpnam_buf);
551     spc_suffix = basename(tmpnam_buf); /* Don't free result - not alloc'd! */
552
553     /* Allocate space for tmppath, spc_prefix, and spc_suffix. */
554     tmpfile = (char *)malloc((strlen(tmppath) + strlen(spc_prefix) +
555                               strlen(spc_suffix) + 1) * sizeof(char));
556     sprintf(tmpfile, "%s%s%s", tmppath, spc_prefix, spc_suffix);
557   }
558   else {
559 #if 0
560     /*
561      * No username was supplied (this could happen with pre-CDE
562      * clients) so generate a temp filename.
563      */
564     if(SPCD_Authentication_Dir)
565       tmppath = strdup(SPCD_Authentication_Dir);
566     else
567       tmppath = XeSBTempPath(XeString_NULL);
568     tmpfile = tempnam(tmppath, ".SPC_" );
569     XeFree(tmppath);
570 #endif
571
572     SPC_Format_Log ("+++> FAILURE: NULL username.");
573     SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
574                                FAILED_FILE_NAME, NULL, NULL);
575     FREE_USER_PASS(username, passwd);
576     SPC_Error(SPC_Bad_Username);
577     return(SPC_ERROR);
578
579   }
580
581   if(!tmpfile) {
582     SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
583                                FAILED_FILE_NAME, NULL, NULL);
584     SPC_Format_Log("+++> FAILURE: cannot malloc.");
585     SPC_Error(SPC_Out_Of_Memory);
586     return(SPC_ERROR);
587   }
588
589   /* 
590    * Store info about the client (protocol version & host type) 
591    * For Pre A.01, this will be defaulted to (hpux 7.0 s300) 
592    */
593   client_connection->hostinfo = hostinfo;
594
595   if (proto_ver) {
596       (void) sscanf(proto_ver, "%d", &client_connection->protocol_version);
597       SPC_client_version_number = client_connection->protocol_version;
598       XeFree(proto_ver);
599   }
600   else
601       /* The client didn't send a protocol_version so set it to "1". */
602       SPC_client_version_number = 1;
603
604   SPC_Format_Log("     Client protocol version is '%d'.", 
605                  SPC_client_version_number);
606   SPC_Format_Log("+++> Authentication file is '%s'.", tmpfile);
607
608   /*
609    * For non-CDE clients, the clients expect the daemon to send
610    * a "real" file name.  Beginning with 'SPC_PROTOCOL_VERSION_CDE_BASE'
611    * the clients are expecting a "netfile" name.
612    */
613   netfile = tmpfile;
614
615   if (SPC_client_version_number >= SPC_PROTOCOL_VERSION_CDE_BASE) {
616      netfile = tt_file_netfile (tmpfile);
617
618      if (tt_ptr_error (netfile) != TT_OK) {
619        SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
620                                   FAILED_FILE_NAME, NULL, NULL);
621        SPC_Format_Log("+++> FAILURE: cannot create a cannonical file name for the authentication file.\n     (%s)", 
622                       tt_status_message(tt_pointer_error(netfile)));
623        XeFree(tmpfile);
624        SPC_Error(SPC_Bad_Authentication);
625        return(SPC_ERROR);
626      }
627      free_netfile = 1;
628      SPC_Format_Log("     Authentication 'netfile' is '%s'.", netfile);
629   }
630
631   /* As we send the next packet, include protocol and host info about */
632   /* us (the server).  If going to a pre A.02 client, it will be      */
633   /* ignored by it.                                                   */
634
635   SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY, 
636                              netfile, SPC_PROTOCOL_VERSION_STR, 
637                              SPC_LocalHostinfo());
638
639   prot=SPC_Filter_Connection(client_connection, NULL, REGISTER, TRUE);
640   if(prot==SPC_ERROR)
641     return(SPC_ERROR);
642   sprintf(buffer, (XeString)"--> REGISTER (%s)", netfile);
643   print_protocol_request(buffer, prot);
644   SPC_Free_Protocol_Ptr(prot);
645
646   /* Was the client able to create the authentication */
647   /* file in the temp directory?                        */
648
649   if(lstat(tmpfile, &buf)==ERROR) {
650     SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
651                                FAILED_FILE_NAME, NULL, NULL);
652     SPC_Format_Log("+++> FAILURE: stat authentication file '%s'.", tmpfile);
653     if (free_netfile)
654       tt_free(netfile);
655     XeFree(tmpfile);
656     SPC_Error(SPC_Bad_Authentication);
657     return(SPC_ERROR);
658   }
659
660   /*
661    * If the file does not have the setuid bit set then return failure.
662    *
663    * Note that if the protocol_version is < 2, this bit will
664    * not be set and the client will not be able to connect.
665    */
666   if(!(buf.st_mode & S_ISUID)) {
667     SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
668                                FAILED_FILE_NAME, NULL, NULL);
669     SPC_Format_Log("+++> FAILURE: authentication file '%s' does not have the setuid bit set.",
670                    tmpfile);
671     if (free_netfile)
672       tt_free(netfile);
673     XeFree(tmpfile);
674     SPC_Error(SPC_Bad_Permission);
675     return(SPC_ERROR);
676   }
677     
678   unlink(tmpfile);
679   XeFree(tmpfile);
680     
681   /* 
682    * Is uid associated with the authentication file created by the 
683    * client present in our password file?    
684    */
685   if(!(pwent=getpwuid(buf.st_uid))) {
686     SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
687                                FAILED_FILE_NAME, NULL, NULL);
688     SPC_Format_Log("+++> FAILURE: the authentication file created by the client has a uid '%d'\n     and this uid is not in the password file.",
689                      buf.st_uid);
690     SPC_Error(SPC_Bad_Username);
691     if (free_netfile)
692       tt_free(netfile);
693     return(SPC_ERROR);
694   }
695
696   /*
697    * Comapre the user name in the request with the user name in 
698    * the passwd file.  They must be the same to continue.
699    */
700   if (strcmp (pwent->pw_name, username) != 0) {
701       SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
702                                  FAILED_FILE_NAME, NULL, NULL);
703       SPC_Format_Log("+++> FAILURE: the request is for username '%s' with uid '%d'\n     but this uid has name '%s' in the password file.",
704                      username,
705                      buf.st_uid,
706                      pwent->pw_name);
707       SPC_Error(SPC_Bad_Password);
708       if (free_netfile)
709         tt_free(netfile);
710       return(SPC_ERROR);
711   }
712
713   client_validated=TRUE;
714
715   /*
716    * Need to initialize the group access list if a username
717    * was supplied in the request.
718    */
719   if ((initgroups(username, pwent->pw_gid)) == -1) {
720         SPC_Format_Log("+++> FAILURE: initgroups ('%s', '%s')",
721                        username, 
722                        pwent->pw_gid);
723   }
724
725   /* We have authenticated ourselves.  Set the process identifiers of
726      this process to the looked up ones. */
727   setgid(pwent->pw_gid);
728   setuid(pwent->pw_uid);
729
730   Xechdir(pwent->pw_dir);
731   sprintf(HomeDir,  (XeString)"HOME=%s",  pwent->pw_dir);
732   sprintf(ShellDir, (XeString)"SHELL=%s", pwent->pw_shell);
733   putenv(HomeDir);
734   putenv(ShellDir);
735
736   spc_user_environment_file=(XeString)XeMalloc(MAXPATHLEN);
737   sprintf(spc_user_environment_file, (XeString)"%s/%s/%s", pwent->pw_dir, 
738           SPCD_ENV_HOME_DIRECTORY, SPCD_ENV_FILE);
739   default_environment=
740     SPC_Add_Env_File(spc_user_environment_file, default_environment);
741
742   SPC_Write_Protocol_Request(client_connection, NULL, LOGFILE_REPLY,
743                              PASSED_FILE_NAME, NULL, NULL);
744
745   return(prot->seqno);
746 }
747
748 /*----------------------------------------------------------------------+*/
749 int Client_Unregister(protocol_request_ptr prot)
750 /*----------------------------------------------------------------------+*/
751 {
752   return(print_protocol_request((XeString)"--> UNREGISTER", prot));
753 }
754
755 /*----------------------------------------------------------------------+*/
756 int Client_Channel_Open(protocol_request_ptr prot)
757 /*----------------------------------------------------------------------+*/
758 {
759   int iomode;
760   SPC_Channel_Ptr channel;
761
762   print_protocol_request((XeString)"--> CHANNEL_OPEN", prot);
763   READ_OPEN(prot->dataptr, iomode);
764
765   /* Don't ever wanna wait, do line-oriented reads,
766      use the toolkit, or execute as a system command */
767
768   /* We don't do system commands because the client side has
769      already converted the channel into the proper form for us */
770
771   iomode &= ~(SPCIO_WAIT | SPCIO_LINEORIENTED |
772               SPCIO_USE_XTOOLKIT | SPCIO_SYSTEM);
773
774   /* However, we do always want our termination to be synchronous */
775
776   iomode |= SPCIO_SYNC_TERMINATOR;
777
778   channel=XeSPCOpen(NULL, iomode);
779   if(channel==SPC_ERROR)
780     return(SPC_ERROR);
781
782   XeSPCAddInput(channel, SPCD_Handle_Application_Data, channel);
783   XeSPCRegisterTerminator(channel, SPCD_Termination_Handler, channel);
784
785   return((int) channel);
786 }
787
788 /*----------------------------------------------------------------------+*/
789 int Client_Channel_Close(protocol_request_ptr prot)
790 /*----------------------------------------------------------------------+*/
791 {
792   SPC_Channel_Ptr channel=prot->channel;
793
794   print_protocol_request((XeString)"--> CHANNEL_CLOSE", prot);
795
796   if(IS_ACTIVE(channel)) {
797
798     /*
799       Uh-oh.  We have recieved a close request, but the channel is
800       active.  We kill the process, and explicitly wait for the
801       process to terminate.
802       */
803
804     XeSPCSignalProcess(channel, SIGKILL);
805     SPC_Wait_For_Termination(channel);
806   }
807   
808   return(XeSPCClose(channel));
809 }
810
811 /*----------------------------------------------------------------------+*/
812 int Client_Channel_Reset(protocol_request_ptr prot)
813 /*----------------------------------------------------------------------+*/
814 {
815   SPC_Channel_Ptr channel=prot->channel;
816
817   print_protocol_request((XeString)"--> CHANNEL_RESET", prot);
818
819   return(XeSPCReset(channel));
820 }
821
822 /*----------------------------------------------------------------------+*/
823 int Client_Channel_Attach(protocol_request_ptr prot)
824 /*----------------------------------------------------------------------+*/
825 {
826   SPC_Channel_Ptr channel=prot->channel;
827   int pid;
828
829   print_protocol_request((XeString)"--> ATTACH", prot);
830
831   READ_ATTACH(prot->dataptr, pid);
832   return(XeSPCAttach(channel, pid));
833 }
834
835 static void
836 Merge_Lang_Var(SPC_Channel_Ptr channel)
837 {
838   XeString stdLang;
839
840   if ((stdLang = SPC_Getenv((XeString)"LANG", channel->envp))
841       != (XeString)NULL)
842   {
843     _DtXlateDb db = NULL;
844     char platform[_DtPLATFORM_MAX_LEN];
845     XeChar *langBuf;
846     int execVer;
847     int compVer;
848     XeString myLang;
849
850     if (_DtLcxOpenAllDbs(&db) == 0)
851     {
852       if ((_DtXlateGetXlateEnv(db, platform, &execVer, &compVer) == 0) &&
853           (_DtLcxXlateStdToOp(db, platform, compVer, DtLCX_OPER_SETLOCALE,
854                               stdLang, NULL, NULL, NULL, &myLang) == 0))
855       {
856         if ((langBuf = (XeChar *)malloc((strlen(myLang) + 6)
857                                         * sizeof(XeChar)))
858             != (XeChar *)NULL)
859         {
860           sprintf(langBuf, "LANG=%s", myLang);
861           channel->envp = SPC_Putenv(langBuf, channel->envp);
862           free(langBuf);
863         }
864
865         free(myLang);
866       }
867
868       _DtLcxCloseDb(&db);
869     }
870   }
871 }
872
873 /*----------------------------------------------------------------------+*/
874 int Client_Application_Spawn(protocol_request_ptr prot)
875 /*----------------------------------------------------------------------+*/
876 {
877   SPC_Channel_Ptr channel=prot->channel;
878   int retval;
879
880   print_protocol_request((XeString)"--> APPLICATION_SPAWN", prot);
881   READ_APPLICATION_SPAWN(prot->dataptr,
882                          channel->path,
883                          channel->context_dir,
884                          channel->argv, channel->envp);
885
886   Merge_Lang_Var(channel);
887
888   channel->IOMode |= SPCIO_DEALLOC_ARGV;
889
890   channel->envp=SPC_Merge_Envp(channel->envp, default_environment);
891
892   retval=XeSPCExecuteProcess(channel);
893   if(retval==SPC_ERROR)
894     return(SPC_ERROR);
895
896   return(channel->pid);
897 }
898
899 /*----------------------------------------------------------------------+*/
900 int Client_Application_Signal(protocol_request_ptr prot)
901 /*----------------------------------------------------------------------+*/
902 {
903   XeString signame;
904   int sig = 0;
905   SPC_Channel_Ptr channel=prot->channel;
906
907   print_protocol_request((XeString)"--> APPLICATION_SIGNAL", prot);
908   READ_STRING_NO_COPY(prot->dataptr, signame);
909
910   if (client_connection->protocol_version >= 2) {
911       if ( (sig = XeNameToSignal( signame )) == XE_SIG_NOT_IN_TABLE ) {
912           SPC_Error(SPC_Bad_Signal_Name, signame);
913           return (SPC_ERROR);
914       }
915   } else {
916       /* Must be the old 1.0, 1.1 protocol, assume its a number.       */
917       /* Note however, that this is not portable as the signal numbers */
918       /* differ from system to system.  This should really be an error */
919
920       /* 1.0 code allowed signal 0 (check if process alive), but its     */
921       /* part of XPG3 so we don't honor it .. nobody ever used it anyway */
922
923       int ok = sscanf(signame, "%x", &sig);  /* WRITE_INT (%x) was used */
924       if ( (ok == EOF) || (sig == 0) ) {
925           SPC_Error(SPC_Bad_Signal_Format, signame);
926           return (SPC_ERROR);
927       }
928   }
929
930   return(XeSPCSignalProcess(channel, sig));
931 }
932
933 /*----------------------------------------------------------------------+*/
934 int Client_Application_Data(protocol_request_ptr prot)
935 /*----------------------------------------------------------------------+*/
936 {
937   SPC_Channel_Ptr channel=prot->channel;
938   buffered_data_ptr pdata=prot->dataptr;
939
940   print_protocol_request((XeString)"--> APPLICATION_DATA", prot);
941
942   return(XeSPCWrite(channel,
943                     pdata->data+REQUEST_HEADER_LENGTH,
944                     pdata->len));
945 }
946
947 /*----------------------------------------------------------------------+*/
948 int Client_Server_Debug(protocol_request_ptr prot)
949 /*----------------------------------------------------------------------+*/
950 {
951   SPC_Channel_Ptr channel=prot->channel;
952   buffered_data_ptr pdata=prot->dataptr;
953   XeChar filename[MAXPATHLEN];
954   time_t timeval;
955
956   print_protocol_request((XeString)"--> SERVER_DEBUG", prot);
957   READ_DEBUG(pdata, filename);
958
959   if(SPC_Print_Protocol)
960     fclose(SPC_Print_Protocol);
961
962   SPC_Print_Protocol=fopen(filename, (XeString)"a+");
963
964   if(!SPC_Print_Protocol)
965     return(ERROR);
966
967   setbuf(SPC_Print_Protocol, NULL);
968
969   time(&timeval);
970   fprintf(SPC_Print_Protocol, (XeString)"Begin protocol filedump: %s", ctime(&timeval));
971   return(TRUE);
972 }
973
974 /*
975  ***
976  *** The purpose of this routine is to filter out environment variables
977  *** which should not get put into the environment.
978  ***
979 */
980
981 /*----------------------------------------------------------------------+*/
982 void conditional_putenv(XeString env_str)
983 /*----------------------------------------------------------------------+*/
984 {
985   if(strncmp(env_str, (XeString)"HOME=", 5) && /* HOME may be different */
986      strncmp(env_str, (XeString)"PWD=", 4)     /* PWD set by chdir, may change */
987                                                /* Should SHELL be on this list? */
988      )
989     {
990       putenv(env_str);
991     }
992 }
993
994 /*----------------------------------------------------------------------+*/
995 int Client_Environ_Reset(protocol_request_ptr prot)
996 /*----------------------------------------------------------------------+*/
997 {
998   int num_vars;
999   XeString envp[100];
1000   char **ret;
1001   int outlen, i;
1002
1003   ret = NULL;
1004   READ_ENVIRON_RESET(prot->dataptr, num_vars);
1005   if(num_vars < 100)
1006     ret = envp;
1007
1008   ret = SPC_Get_Multi_Packet(client_connection, prot,
1009                              ret, &outlen,
1010                              ENVIRON_RESET, "--> ENVIRON_RESET");
1011
1012   if(ret == NULL)
1013     return(SPC_ERROR);
1014
1015   for(i=0; i<outlen; i++)
1016     if(ret[i] && *ret[i])
1017       conditional_putenv(ret[i]);
1018
1019   if(ret != envp)
1020     free((char *)ret);
1021
1022   return(TRUE);
1023 }
1024
1025 /*----------------------------------------------------------------------+*/
1026 int Client_Reply_Devices(protocol_request_ptr prot)
1027 /*----------------------------------------------------------------------+*/
1028 {
1029   SPC_Channel_Ptr channel=prot->channel;
1030
1031   print_protocol_request((XeString)"--> QUERY_DEVICES", prot);
1032
1033   SPC_Write_Protocol_Request(client_connection, channel, DEVICE_REPLY,
1034                              channel->wires[STDIN]->master_name,
1035                              channel->wires[STDIN]->slave_name,
1036                              channel->wires[STDOUT]->master_name,
1037                              channel->wires[STDOUT]->slave_name,
1038                              channel->wires[STDERR]->master_name,
1039                              channel->wires[STDERR]->slave_name);
1040   return(TRUE);
1041 }
1042
1043 /*----------------------------------------------------------------------+*/
1044 int Client_Reply_Logfile(protocol_request_ptr prot)
1045 /*----------------------------------------------------------------------+*/
1046 {
1047   char *netfile;
1048   SPC_Channel_Ptr channel=prot->channel;
1049
1050   print_protocol_request((XeString)"--> QUERY_LOGFILE", prot);
1051
1052   if (SPC_client_version_number >= SPC_PROTOCOL_VERSION_CDE_BASE && 
1053       IS_SPCIO_USE_LOGFILE(channel->IOMode)) {
1054     netfile = tt_file_netfile (channel->logfile);
1055
1056     if (tt_ptr_error (netfile) != TT_OK) {
1057       SPC_Format_Log("+++> FAILURE: cannot create a 'netfile' name for the logfile.\n     (%s)", 
1058                      tt_status_message(tt_pointer_error(netfile)));
1059       return(SPC_ERROR);
1060     }
1061
1062     SPC_Write_Protocol_Request(client_connection, channel, LOGFILE_REPLY,
1063                                netfile, NULL, NULL);
1064     tt_free (netfile);
1065   }
1066   else
1067     SPC_Write_Protocol_Request(client_connection, channel, LOGFILE_REPLY,
1068                                channel->logfile, NULL, NULL);
1069
1070   return(TRUE);
1071 }
1072
1073 /*----------------------------------------------------------------------+*/
1074 int Client_Delete_Logfile(protocol_request_ptr prot)
1075 /*----------------------------------------------------------------------+*/
1076 {
1077   SPC_Channel_Ptr channel=prot->channel;
1078
1079   print_protocol_request((XeString)"--> DELETE_LOGFILE", prot);
1080
1081   return(XeSPCRemoveLogfile(channel));
1082 }
1083
1084 /*----------------------------------------------------------------------+*/
1085 int Client_Reset_Termio(protocol_request_ptr prot)
1086 /*----------------------------------------------------------------------+*/
1087 {
1088   /* This handles old 1.0 versions of the SPC code.  We used to send */
1089   /* an hp-ux version of the termio struct in a non-portable manner  */
1090   /* We need to be able to "eat" such an request if we get one.      */
1091
1092   print_protocol_request((XeString)"--> RESET_TERMIO", prot);
1093
1094   return(SPC_Get_Termio(prot));
1095 }
1096
1097 /*----------------------------------------------------------------------+*/
1098 int Client_Reset_Termios(protocol_request_ptr prot)
1099 /*----------------------------------------------------------------------+*/
1100 {
1101   print_protocol_request((XeString)"--> RESET_TERMIOS", prot);
1102
1103   return(SPC_Get_Termios(prot));
1104 }
1105
1106 /* New B.00 methods */
1107
1108 /*----------------------------------------------------------------------+*/
1109 int Client_Send_EOF(protocol_request_ptr prot)
1110 /*----------------------------------------------------------------------+*/
1111 {
1112   SPC_Channel_Ptr channel=prot->channel;
1113
1114   print_protocol_request((XeString)"--> SEND_EOF", prot);
1115
1116   return(XeSPCSendEOF(channel));
1117 }
1118
1119 int Client_Channel_Termios(protocol_request_ptr prot)
1120 {
1121   SPC_Channel_Ptr channel=prot->channel;
1122
1123   int connection, side;
1124   char buffer[1024];
1125   struct termios t;
1126
1127   print_protocol_request((XeString)"--> CHANNEL_TERMIOS", prot);
1128
1129   READ_TERMIOS(prot->dataptr, connection, side, buffer);
1130   SPC_Encode_Termios(buffer, &t);
1131   
1132   
1133   return(XeSPCSetTermio(channel, connection, side, &t));
1134 }
1135
1136 int Client_Enhanced_Spawn(protocol_request_ptr prot)
1137 {
1138   SPC_Channel_Ptr channel = prot->channel;
1139   int num_vars;
1140   XeString buf[100];
1141   char **ret;
1142   int numenv, numarg;
1143   int return_value, outlen, i;
1144
1145   ret = NULL;
1146   READ_ENVIRON_RESET(prot->dataptr, num_vars);
1147   if(num_vars < 100)
1148     ret = buf;
1149
1150   ret = SPC_Get_Multi_Packet(client_connection, prot,
1151                              ret, &outlen,
1152                              APP_B00_SPAWN, "--> APP_B00_SPAWN");
1153
1154   if(ret == NULL)
1155     return(SPC_ERROR);
1156
1157   channel->path = strdup(ret[0]);
1158   channel->context_dir  = strdup(ret[1]);
1159   
1160   numarg = atoi(ret[2]);
1161   numenv = atoi(ret[3]);
1162
1163   if(numarg == 0)
1164     channel->argv = NULL;
1165   else {
1166     channel->argv = &ret[4];
1167     ret[numarg+3] = NULL;
1168   }
1169
1170   if(numenv == 0)
1171     channel->envp = NULL;
1172   else {
1173     channel->envp = (char **)XeMalloc((numenv+1)*sizeof(char *));
1174     channel->envp[0] = NULL;
1175     channel->envp = SPC_Merge_Envp(channel->envp, &ret[numarg+4]);
1176
1177     Merge_Lang_Var(channel);
1178
1179     channel->envp = SPC_Merge_Envp(channel->envp, default_environment);
1180   }
1181   
1182   return_value = XeSPCExecuteProcess(channel);
1183
1184   /* Make the world safe for freeing the channel */
1185   
1186   channel->argv = NULL;
1187   if(channel->envp == &ret[numarg+4])
1188     channel->envp = NULL;
1189   
1190   for(i=0; i<outlen; i++)
1191     if(ret[i])
1192       free(ret[i]);
1193   
1194   if(ret != buf)
1195     free((char *)ret);
1196
1197   if(return_value==SPC_ERROR)
1198     return(SPC_ERROR);
1199   
1200   return(channel->pid);
1201 }
1202
1203 /*----------------------------------------------------------------------+*/
1204 void SPCD_Reply(SPC_Connection_Ptr connection,
1205                  protocol_request_ptr prot,
1206                  int retval,
1207                  int errval)
1208 /*----------------------------------------------------------------------+*/
1209 {
1210   if(retval==SPC_ERROR)
1211     retval= (-XeSPCErrorNumber);
1212
1213   SPC_Write_Reply(connection, prot, retval, errval);
1214 }
1215
1216 /*----------------------------------------------------------------------+*/
1217   /*
1218    * This function is invoked when the exit timer expires.
1219    * If sub-processes are running, return to select; 
1220    * otherwise exit.
1221    */
1222 #ifdef SA_HANDLER_INT_ARG
1223 void SPCD_Alarm_Handler(int not_used)
1224 #else
1225 void SPCD_Alarm_Handler()
1226 #endif /* SA_HANDLER_INT_ARG */
1227 /*----------------------------------------------------------------------+*/
1228 {
1229   int i;
1230
1231   if (exit_timeout == SPCD_NO_TIMER) 
1232     return;
1233
1234   if (SPC_pid_list != NULL) 
1235     for (i=0; SPC_pid_list[i] != NULL; i++) {
1236       if (SPC_pid_list[i] != SPCD_DEAD_PROCESS) {
1237         /*
1238          * Have at least one sub- process running so reset the 
1239          * alarm and return to select.
1240          */
1241         (void) alarm (exit_timeout * 60);
1242         return;
1243       }
1244     }
1245
1246   /*
1247    * There are no sub-processes running.  Exit if a request is not pending
1248    */
1249   if (request_pending != SPCD_REQUEST_PENDING) {
1250     SPC_Format_Log((XeString)
1251                    "Exit timer expired after '%d' minutes of no activity.", 
1252                    exit_timeout);
1253     SPCD_Exit (0);
1254   }
1255 }