int
main(int argc, char *argv[])
{
    static XtAppContext 	appContext;
    IceConn			iceConn;
    IceProtocolSetupStatus	setupstat;
    char			*vendor = NULL;
    char			*release = NULL;
    pmGetProxyAddrMsg		*pMsg;
    char 			*pData;
    int				i;
    size_t			len;
    IceReplyWaitInfo		replyWait;
    GetProxyAddrReply		reply;
    int				majorVersion, minorVersion;
    Bool			gotReply, ioErrorOccured;
    char			errorString[255];
    char			*serviceName = NULL, *serverAddress = NULL;
    char			*hostAddress = NULL, *startOptions = NULL;
    char			*managerAddress = NULL;
    Bool			haveAuth = 0;
    char			authName[40];
    char			authData[128];
    char			*authDataBinary = NULL;
    int				authLen = 0;

    for (i = 1; i < argc; i++)
    {
	if (argv[i][0] == '-')
	{
	    switch (argv[i][1])
	    {
	    case 'a':					/* -auth */
		haveAuth = 1;
		continue;

	    case 'm':					/* -manager */
		if (++i >= argc) goto usage;
		managerAddress = (char *) XtNewString (argv[i]);
		continue;

	    case 's':					/* -server */
		if (++i >= argc) goto usage;
		serverAddress = (char *) XtNewString (argv[i]);
		continue;

	    case 'n':					/* -name */
		if (++i >= argc) goto usage;
		serviceName = XtNewString (argv[i]);
		continue;

	    case 'h':					/* -host */
		if (++i >= argc) goto usage;
		hostAddress = XtNewString (argv[i]);
		continue;

	    case 'o':					/* -options */
		if (++i >= argc) goto usage;
		startOptions = XtNewString (argv[i]);
		continue;

	    case 'v':
		puts(PACKAGE_STRING);
		exit(0);
	    }
	}

    usage:
	if (i >= argc)
	    fprintf (stderr, "%s: %s requires an argument\n",
		     argv[0], argv[i-1]);
	else
	    fprintf (stderr, "%s: unrecognized argument '%s'\n",
		     argv[0], argv[i]);
	usage();
    }

    if (serviceName == NULL) {
	fprintf (stderr, "%s: -name serviceName must be specified\n", argv[0]);
	usage();
    }
    if (serverAddress == NULL) {
	fprintf (stderr, "%s: -server serverAddr must be specified\n", argv[0]);
	usage();
    }

    if (managerAddress == NULL) {
	managerAddress = getenv("PROXY_MANAGER");
	if (managerAddress == NULL) {
	    fprintf (stderr, "Error: -manager option must be specified when PROXY_MANAGER is not in the environment\n");
	    exit (1);
	}
    }

    /*
     * Register support for PROXY_MANAGEMENT.
     */

    if ((PMopcode = IceRegisterForProtocolSetup (
	PM_PROTOCOL_NAME,
	"XC", "1.0",
	PMversionCount, PMversions,
	0, /* authcount */
	NULL, /* authnames */ 
        NULL, /* authprocs */
	NULL  /* IceIOErrorProc */ )) < 0)
    {
	fprintf (stderr,
	    "Could not register PROXY_MANAGEMENT protocol with ICE");
	exit (1);
    }


    appContext = XtCreateApplicationContext ();

    InitWatchProcs (appContext);

    if ((iceConn = IceOpenConnection (
	managerAddress, NULL, 0, 0, 256, errorString)) == NULL)
    {
	fprintf (stderr,
	    "Could not open ICE connection to proxy manager: %s", errorString);
	exit (1);
    }

    setupstat = IceProtocolSetup (iceConn, PMopcode, NULL,
	False /* mustAuthenticate */,
	&majorVersion, &minorVersion,
	&vendor, &release, 256, errorString);

    if (setupstat != IceProtocolSetupSuccess)
    {
	IceCloseConnection (iceConn);
	fprintf (stderr,
	    "Could not initialize proxy management protocol: %s\n",
	    errorString);
	exit (1);
    }


    /*
     * If auth data is supplied, read it from stdin.
     */

    if (haveAuth)
    {
	fgets (authName, sizeof (authName), stdin);
	fgets (authData, sizeof (authData), stdin);

	for (i = 0; i < strlen (authName); i++)
	    if (authName[i] == '\n')
	    {
		authName[i] = '\0';
		break;
	    }
	for (i = 0; i < strlen (authData); i++)
	    if (authData[i] == '\n')
	    {
		authData[i] = '\0';
		break;
	    }

	/*
	 * Convert the hex auth data to binary.
	 */

	authLen = cvthexkey (authData, &authDataBinary);

	if (authLen == -1)
	{
	    fprintf (stderr, "Could not convert hex auth data to binary\n");
	    exit (1);
	}
    }


    /*
     * Now send the GetProxyAddr request.
     */

    len = STRING_BYTES (serviceName) +
	  STRING_BYTES (serverAddress) +
	  STRING_BYTES (hostAddress) +
	  STRING_BYTES (startOptions) +
	  (authLen > 0 ? (STRING_BYTES (authName) + authLen) : 0);

    IceGetHeaderExtra (iceConn, PMopcode, PM_GetProxyAddr,
	SIZEOF (pmGetProxyAddrMsg), WORD64COUNT (len),
	pmGetProxyAddrMsg, pMsg, pData);

    pMsg->authLen = (CARD16) authLen;

    STORE_STRING (pData, serviceName);
    STORE_STRING (pData, serverAddress);
    STORE_STRING (pData, hostAddress);
    STORE_STRING (pData, startOptions);
    if (authLen > 0)
    {
	STORE_STRING (pData, authName);
	memcpy (pData, authDataBinary, authLen);
    }

    IceFlush (iceConn);

    replyWait.sequence_of_request = IceLastSentSequenceNumber (iceConn);
    replyWait.major_opcode_of_request = PMopcode;
    replyWait.minor_opcode_of_request = PM_GetProxyAddr;
    replyWait.reply = (IcePointer) &reply;

    gotReply = False;
    ioErrorOccured = False;

    while (!gotReply && !ioErrorOccured)
    {
	ioErrorOccured = (IceProcessMessages (
	    iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError);

	if (ioErrorOccured)
	{
	    fprintf (stderr, "IO error occured\n");
	    exit (1);
	}
	else if (gotReply)
	{
	    if (reply.status == PM_Success)
	    {
		fprintf (stdout, "%s\n", reply.addr);
		exit (0);
	    }
	    else
	    {
		fprintf (stderr, "Error from proxy manager: %s\n",
		    reply.error);
		exit (1);
	    }
	}
    }
    /*NOTREACHED*/
    exit(0);
}
Пример #2
0
SmcConn
SmcOpenConnection(char *networkIdsList, SmPointer context,
		  int xsmpMajorRev, int xsmpMinorRev,
		  unsigned long mask, SmcCallbacks *callbacks,
		  char *previousId, char **clientIdRet,
		  int errorLength, char *errorStringRet)
{
    SmcConn			smcConn;
    IceConn			iceConn;
    char 			*ids;
    IceProtocolSetupStatus	setupstat;
    int				majorVersion;
    int				minorVersion;
    char			*vendor = NULL;
    char			*release = NULL;
    smRegisterClientMsg 	*pMsg;
    char 			*pData;
    int				extra, len;
    IceReplyWaitInfo		replyWait;
    _SmcRegisterClientReply	reply;
    Bool			gotReply, ioErrorOccured;

    const char *auth_names[] = {"MIT-MAGIC-COOKIE-1"};
    IcePoAuthProc auth_procs[] = {_IcePoMagicCookie1Proc};
    int auth_count = 1;

    IcePoVersionRec versions[] = {
        {SmProtoMajor, SmProtoMinor, _SmcProcessMessage}
    };
    int version_count = 1;


    *clientIdRet = NULL;

    if (errorStringRet && errorLength > 0)
	*errorStringRet = '\0';

    if (!_SmcOpcode)
    {
	/*
	 * For now, there is only one version of XSMP, so we don't
	 * have to check {xsmpMajorRev, xsmpMinorRev}.  In the future,
	 * we will check against versions and generate the list
	 * of versions the application actually supports.
	 */

	if ((_SmcOpcode = IceRegisterForProtocolSetup ("XSMP",
	    SmVendorString, SmReleaseString, version_count, versions,
            auth_count, auth_names, auth_procs, NULL)) < 0)
	{
	    if (errorStringRet && errorLength > 0) {
		strncpy (errorStringRet,
			 "Could not register XSMP protocol with ICE",
			 errorLength);
		errorStringRet[errorLength - 1] = '\0';
	    }

	    return (NULL);
	}
    }

    if (networkIdsList == NULL || *networkIdsList == '\0')
    {
	if ((ids = (char *) getenv ("SESSION_MANAGER")) == NULL)
	{
	    if (errorStringRet && errorLength > 0) {
		strncpy (errorStringRet,
			 "SESSION_MANAGER environment variable not defined",
			 errorLength);
		errorStringRet[errorLength - 1] = '\0';
	    }
	    return (NULL);
	}
    }
    else
    {
	ids = networkIdsList;
    }

    if ((iceConn = IceOpenConnection (
	ids, context, 0, _SmcOpcode, errorLength, errorStringRet)) == NULL)
    {
	return (NULL);
    }

    if ((smcConn = (SmcConn) malloc (sizeof (struct _SmcConn))) == NULL)
    {
	if (errorStringRet && errorLength > 0) {
	    strncpy (errorStringRet, "Can't malloc", errorLength);
	    errorStringRet[errorLength - 1] = '\0';
	}
	IceCloseConnection (iceConn);
	return (NULL);
    }

    setupstat = IceProtocolSetup (iceConn, _SmcOpcode,
	(IcePointer) smcConn,
	False /* mustAuthenticate */,
	&majorVersion, &minorVersion,
	&vendor, &release, errorLength, errorStringRet);

    if (setupstat == IceProtocolSetupFailure ||
	setupstat == IceProtocolSetupIOError)
    {
	IceCloseConnection (iceConn);
	free ((char *) smcConn);
	return (NULL);
    }
    else if (setupstat == IceProtocolAlreadyActive)
    {
	/*
	 * This case should never happen, because when we called
	 * IceOpenConnection, we required that the ICE connection
	 * may not already have XSMP active on it.
	 */

	free ((char *) smcConn);
	if (errorStringRet && errorLength > 0) {
	    strncpy (errorStringRet, "Internal error in IceOpenConnection",
		     errorLength);
	    errorStringRet[errorLength - 1] = '\0';
	}
	return (NULL);
    }

    smcConn->iceConn = iceConn;
    smcConn->proto_major_version = majorVersion;
    smcConn->proto_minor_version = minorVersion;
    smcConn->vendor = vendor;
    smcConn->release = release;
    smcConn->client_id = NULL;

    bzero ((char *) &smcConn->callbacks, sizeof (SmcCallbacks));
    set_callbacks (smcConn, mask, callbacks);

    smcConn->interact_waits = NULL;
    smcConn->phase2_wait = NULL;
    smcConn->prop_reply_waits = NULL;

    smcConn->save_yourself_in_progress = False;
    smcConn->shutdown_in_progress = False;


    /*
     * Now register the client
     */

    if (!previousId)
	previousId = "";
    len = strlen (previousId);
    extra = ARRAY8_BYTES (len);

    IceGetHeaderExtra (iceConn, _SmcOpcode, SM_RegisterClient,
	SIZEOF (smRegisterClientMsg), WORD64COUNT (extra),
	smRegisterClientMsg, pMsg, pData);

    STORE_ARRAY8 (pData, len, previousId);

    IceFlush (iceConn);

    replyWait.sequence_of_request = IceLastSentSequenceNumber (iceConn);
    replyWait.major_opcode_of_request = _SmcOpcode;
    replyWait.minor_opcode_of_request = SM_RegisterClient;
    replyWait.reply = (IcePointer) &reply;

    gotReply = False;
    ioErrorOccured = False;

    while (!gotReply && !ioErrorOccured)
    {
	ioErrorOccured = (IceProcessMessages (
	    iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError);

	if (ioErrorOccured)
	{
	    if (errorStringRet && errorLength > 0) {
		strncpy (errorStringRet, "IO error occured opening connection",
			 errorLength);
		errorStringRet[errorLength - 1] = '\0';
	    }
	    free (smcConn->vendor);
	    free (smcConn->release);
	    free ((char *) smcConn);

	    return (NULL);
	}
	else if (gotReply)
	{
	    if (reply.status == 1)
	    {
		/*
		 * The client successfully registered.
		 */

		*clientIdRet = reply.client_id;

		smcConn->client_id = (char *) malloc (
		    strlen (*clientIdRet) + 1);

		strcpy (smcConn->client_id, *clientIdRet);
	    }
	    else
	    {
		/*
		 * Could not register the client because the previous ID
		 * was bad.  So now we register the client with the
		 * previous ID set to NULL.
		 */

		extra = ARRAY8_BYTES (0);

		IceGetHeaderExtra (iceConn, _SmcOpcode, SM_RegisterClient,
		    SIZEOF (smRegisterClientMsg), WORD64COUNT (extra),
		    smRegisterClientMsg, pMsg, pData);

		STORE_ARRAY8 (pData, 0, "");

		IceFlush (iceConn);

		replyWait.sequence_of_request =
		    IceLastSentSequenceNumber (iceConn);

		gotReply = False;
	    }
	}
    }

    return (smcConn);
}
Пример #3
0
  Bool
dcop_call(
  const char  * appId,
  const char  * remApp,
  const char  * remObjId,
  const char  * remFun,
  const char  * data,
  int           dataLength,
  char       ** replyType,
  char       ** replyData,
  int         * replyDataLength
)
{
  IceReplyWaitInfo          waitInfo;
  IceProcessMessagesStatus  status;
  struct dcop_reply_struct  replyStruct;

  char  * pos               = 0L;
  char  * outputData        = 0L;
  int     outputDataLength  = 0;
  int     temp              = 0;
  Bool    success           = False;
  Bool    readyRet          = False;

  struct DCOPMsg * pMsg;


  fprintf(stderr, "dcop_call() ...\n");

  if (0 == dcop_ice_conn) {
    fprintf(stderr, "Try running dcop_register(), moron\n");
    return False;
  }


  temp += strlen(appId);
  temp += strlen(remApp);
  temp += strlen(remObjId);
  temp += strlen(remFun);
  temp += dataLength;
  temp += 1024; /* Extra space for marshalling overhead */

  outputData = (char *)malloc(temp);

  temp = 0;

  pos = outputData;
  pos = dcop_write_string(pos, appId);
  pos = dcop_write_string(pos, remApp);
  pos = dcop_write_string(pos, remObjId);
  pos = dcop_write_string(pos, remFun);
  pos = dcop_write_int(pos, dataLength);

  outputDataLength = pos - outputData;

  IceGetHeader(
	       dcop_ice_conn,
	       dcop_major_opcode,
	       DCOPCall,
	       sizeof(struct DCOPMsg),
	       struct DCOPMsg,
	       pMsg
	       );

  pMsg->length += outputDataLength + dataLength;

  IceSendData(dcop_ice_conn, outputDataLength, outputData);
  IceSendData(dcop_ice_conn, dataLength, (char *)data);

  IceFlush(dcop_ice_conn);

  free(outputData);
  outputData = NULL;

  if (IceConnectionStatus(dcop_ice_conn) != IceConnectAccepted) {
      fprintf(stderr, "dcop_call(): Connection not accepted\n");
      return False;
  }

  waitInfo.sequence_of_request = IceLastSentSequenceNumber(dcop_ice_conn);
  waitInfo.major_opcode_of_request = dcop_major_opcode;
  waitInfo.minor_opcode_of_request = DCOPCall;

  replyStruct.status = DCOP_REPLY_PENDING;
  replyStruct.replyId = dcop_reply_id++;
  replyStruct.replyType = replyType;
  replyStruct.replyData = replyData;
  replyStruct.replyDataLength = replyDataLength;

  waitInfo.reply = (IcePointer)(&replyStruct);

  readyRet = False;

  do {
      fprintf(stderr, "dcop_call(): Doing IceProcessMessages\n");
      status = IceProcessMessages(dcop_ice_conn, &waitInfo, &readyRet);

      if (status == IceProcessMessagesIOError) {
        fprintf(stderr, "dcop_call(): IceProcessMessagesIOError\n");
        IceCloseConnection(dcop_ice_conn);
        return False;
      }

      fprintf(stderr, "dcop_call(): readyRet == %s\n", readyRet ? "True" : "False");
  } while (!readyRet);

  fprintf(stderr, "dcop_call(): Finished\n");
  return (replyStruct.status == DCOP_REPLY_OK) ? True : False;
}