예제 #1
0
OSStatus DoPrivilegedExec(const char *pathToTool, char *arg1, char *arg2, char *arg3, char *arg4, char *arg5, char *arg6) {
    short               i;
    char                *args[8];
    OSStatus            err;
    FILE                *ioPipe = NULL;
    char                *p, junk[256];

    err = GetAuthorization();
    if (err != noErr) {
        if (err == errAuthorizationCanceled)
            return err;
        ShowSecurityError("GetAuthorization returned error %d", err);
    } else {
        for (i=0; i<5; i++) {       // Retry 5 times if error
            args[0] = arg1;
            args[1] = arg2;
            args[2] = arg3;
            args[3] = arg4;
            args[4] = arg5;
            args[5] = arg6;
            args[6] = NULL;

            err = AuthorizationExecuteWithPrivileges (gOurAuthRef, pathToTool, 0, args, &ioPipe);
            if (ioPipe) {
                // We use the pipe to signal us when the command has completed
                do {
                    p = fgets(junk, sizeof(junk), ioPipe);
                } while (p);
                
                fclose (ioPipe);
            }

            // AuthorizationExecuteWithPrivileges() does a fork() and so 
            // leaves a zombie process.  Clear these so we don't exceed 
            // the system-imposed limit of processes per user (MAXUPRC).
            while (waitpid(-1, 0, WNOHANG) > 0);
#if 0
            if (strcmp(arg2, "-R") == 0)
                SleepTicks(DELAY_TICKS_R);
            else
                SleepTicks(DELAY_TICKS);
#endif
            if (err == noErr)
                break;
        }
    }
    if (err != noErr)
        ShowSecurityError("\"%s %s %s %s %s %s\" returned error %d", pathToTool, 
                            arg1 ? arg1 : "", arg2 ? arg2 : "", arg3 ? arg3 : "", 
                            arg4 ? arg4 : "", arg5 ? arg5 : "", err);

       return err;
}
/*
 * Attempts to connect to server, given display name. Returns file descriptor
 * (network socket) or -1 if connection fails.  Display names may be of the
 * following format:
 *
 *     [protocol/] [hostname] : [:] displaynumber [.screennumber]
 *
 * A string with exactly two colons seperating hostname from the display
 * indicates a DECnet style name.  Colons in the hostname may occur if an
 * IPv6 numeric address is used as the hostname.  An IPv6 numeric address
 * may also end in a double colon, so three colons in a row indicates an
 * IPv6 address ending in :: followed by :display.  To make it easier for
 * people to read, an IPv6 numeric address hostname may be surrounded by
 * [ ] in a similar fashion to the IPv6 numeric address URL syntax defined
 * by IETF RFC 2732.
 *
 * If no hostname and no protocol is specified, the string is interpreted
 * as the most efficient local connection to a server on the same machine.
 * This is usually:
 *
 *     o  shared memory
 *     o  local stream
 *     o  UNIX domain socket
 *     o  TCP to local host
 *
 * This function will eventually call the X Transport Interface functions
 * which expects the hostname in the format:
 *
 *	[protocol/] [hostname] : [:] displaynumber
 *
 */
XtransConnInfo
_X11TransConnectDisplay (
    char *display_name,
    char **fullnamep,			/* RETURN */
    int *dpynump,			/* RETURN */
    int *screenp,			/* RETURN */
    char **auth_namep,			/* RETURN */
    int *auth_namelenp,			/* RETURN */
    char **auth_datap,			/* RETURN */
    int *auth_datalenp)			/* RETURN */
{
    int family;
    int saddrlen;
    Xtransaddr *saddr;
    char *lastp, *lastc, *p;		/* char pointers */
    char *pprotocol = NULL;		/* start of protocol name */
    char *phostname = NULL;		/* start of host of display */
    char *pdpynum = NULL;		/* start of dpynum of display */
    char *pscrnum = NULL;		/* start of screen of display */
    Bool dnet = False;			/* if true, then DECnet format */
    int idisplay = 0;			/* required display number */
    int iscreen = 0;			/* optional screen number */
    /*  int (*connfunc)(); */		/* method to create connection */
    int len, hostlen;			/* length tmp variable */
    int retry;				/* retry counter */
    char addrbuf[128];			/* final address passed to
					   X Transport Interface */
    char* address = addrbuf;
    XtransConnInfo trans_conn = NULL;	/* transport connection object */
    int connect_stat;
#if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
    Bool reset_hostname = False;	/* Reset hostname? */
    char *original_hostname = NULL;
    int local_transport_index = -1;
    const char *local_transport[] = { LOCAL_TRANSPORT_LIST, NULL };
#endif

    p = display_name;

    saddrlen = 0;			/* set so that we can clear later */
    saddr = NULL;

    /*
     * Step 0, find the protocol.  This is delimited by the optional
     * slash ('/').
     */
    for (lastp = p; *p && *p != ':' && *p != '/'; p++) ;
    if (!*p) return NULL;		/* must have a colon */

    if (p != lastp && *p != ':') {	/* protocol given? */
	pprotocol = copystring (lastp, p - lastp);
	if (!pprotocol) goto bad;	/* no memory */
	p++;				/* skip the '/' */
    } else
	p = display_name;		/* reset the pointer in
					   case no protocol was given */

    /*
     * Step 1, find the hostname.  This is delimited by either one colon,
     * or two colons in the case of DECnet (DECnet Phase V allows a single
     * colon in the hostname).  (See note above regarding IPv6 numeric
     * addresses with triple colons or [] brackets.)
     */

    lastp = p;
    lastc = NULL;
    for (; *p; p++)
	if (*p == ':')
	    lastc = p;

    if (!lastc) return NULL;		/* must have a colon */

    if ((lastp != lastc) && (*(lastc - 1) == ':')
#if defined(IPv6) && defined(AF_INET6)
      && ( ((lastc - 1) == lastp) || (*(lastc - 2) != ':'))
#endif
	) {
	/* DECnet display specified */

#ifndef DNETCONN
	goto bad;
#else
	dnet = True;
	/* override the protocol specified */
	if (pprotocol)
	    Xfree (pprotocol);
	pprotocol = copystring ("dnet", 4);
	hostlen = lastc - 1 - lastp;
#endif
    }
    else
	hostlen = lastc - lastp;

    if (hostlen > 0) {		/* hostname given? */
	phostname = copystring (lastp, hostlen);
	if (!phostname) goto bad;	/* no memory */
    }

    p = lastc;

#if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
    /* check if phostname == localnodename AND protocol not specified */
    if (!pprotocol && phostname) {
	char localhostname[256];

	if ((_XGetHostname (localhostname, sizeof localhostname) > 0)
	    && (strcmp(phostname, localhostname) == 0)) {
	    original_hostname = phostname;
	    phostname = NULL;
	    reset_hostname = True;
	}
    }
#endif


    /*
     * Step 2, find the display number.  This field is required and is
     * delimited either by a nul or a period, depending on whether or not
     * a screen number is present.
     */

    for (lastp = ++p; *p && isascii(*p) && isdigit(*p); p++) ;
    if ((p == lastp) ||			/* required field */
	(*p != '\0' && *p != '.') ||	/* invalid non-digit terminator */
	!(pdpynum = copystring (lastp, p - lastp)))  /* no memory */
      goto bad;
    idisplay = atoi (pdpynum);


    /*
     * Step 3, find the screen number.  This field is optional.  It is
     * present only if the display number was followed by a period (which
     * we've already verified is the only non-nul character).
     */

    if (*p) {
	for (lastp = ++p; *p && isascii(*p) && isdigit (*p); p++) ;
	if (p != lastp) {
	    if (*p ||			/* non-digits */
		!(pscrnum = copystring (lastp, p - lastp))) /* no memory */
		goto bad;
	    iscreen = atoi (lastp);
	}
    }

    /*
     * At this point, we know the following information:
     *
     *     pprotocol                protocol string or NULL
     *     phostname                hostname string or NULL
     *     idisplay                 display number
     *     iscreen                  screen number
     *     dnet                     DECnet boolean
     *
     * We can now decide which transport to use based on the defined
     * connection types and the hostname string.
     * If phostname & pprotocol are NULL, then choose the best transport.
     * If phostname is "unix" & pprotocol is NULL, then choose UNIX domain
     * sockets (if configured).
     */

#if defined(TCPCONN) || defined(UNIXCONN) || defined(LOCALCONN) || defined(MNX_TCPCONN) || defined(OS2PIPECONN)
    if (!pprotocol) {
#if defined(UNIXCONN)
	if (phostname && (strcmp (phostname, "unix") == 0)) {
	    Xfree(pprotocol);
	    pprotocol = copystring ("unix", 4);
	} else
#endif
#ifdef HAVE_LAUNCHD
	if (phostname && phostname[0]=='/') {
		pprotocol = copystring ("local", 5);
	}
#endif
	if (!phostname)
	{
	    if (local_transport[0] != NULL) {
		pprotocol = Xstrdup(local_transport[0]);
		local_transport_index = 0;
	    }
	}

	if (!pprotocol) { /* if still not found one, tcp is our last resort */
	    pprotocol = copystring ("tcp", 3);
	}
    }
#endif


  connect:
    /*
     * This seems kind of backwards, but we need to put the protocol,
     * host, and port back together to pass to _X11TransOpenCOTSClient().
     */

    {
	int olen = 3 + (pprotocol ? strlen(pprotocol) : 0) +
		       (phostname ? strlen(phostname) : 0) +
		       (pdpynum   ? strlen(pdpynum)   : 0);
	if (olen > sizeof addrbuf) address = Xmalloc (olen);
    }
    if (!address) goto bad;

    sprintf(address,"%s/%s:%d",
	pprotocol ? pprotocol : "",
	phostname ? phostname : "",
	idisplay );

    /*
     * Make the connection, also need to get the auth address info for
     * the connection.  Do retries in case server host has hit its
     * backlog (which, unfortunately, isn't distinguishable from there not
     * being a server listening at all, which is why we have to not retry
     * too many times).
     */
    for(retry=X_CONNECTION_RETRIES; retry>=0; retry-- )
    {
	if ( (trans_conn = _X11TransOpenCOTSClient(address)) == NULL )
	{
	    break;
	}
	if ((connect_stat = _X11TransConnect(trans_conn,address)) < 0 )
	{
	    _X11TransClose(trans_conn);
	    trans_conn = NULL;

	    if (connect_stat == TRANS_TRY_CONNECT_AGAIN)
		continue;
	    else
		break;
	}

	_X11TransGetPeerAddr(trans_conn, &family, &saddrlen, &saddr);

	/*
	 * The family is given in a socket format (ie AF_INET). This
	 * will convert it to the format used by the authorization and
	 * X protocol (ie FamilyInternet).
	 */

	if( _X11TransConvertAddress(&family, &saddrlen, &saddr) < 0 )
	{
	    _X11TransClose(trans_conn);
	    trans_conn = NULL;
	    if (saddr)
	    {
		free ((char *) saddr);
		saddr = NULL;
	    }
	    continue;
	}

	break;
    }

    if (address != addrbuf) Xfree (address);
    address = addrbuf;

    if( trans_conn == NULL )
      goto bad;

    /*
     * Set close-on-exec so that programs that fork() doesn't get confused.
     */

    _X11TransSetOption(trans_conn,TRANS_CLOSEONEXEC,1);

    /*
     * Build the expanded display name:
     *
     *     [host] : [:] dpy . scr \0
     */
#if defined(LOCALCONN) || defined(TCPCONN) || defined(UNIXCONN)
    /*
     *  If we computed the host name, get rid of it so that
     *  XDisplayString() and XDisplayName() agree.
     */
    if (reset_hostname && (phostname != original_hostname)) {
	Xfree (phostname);
	phostname = original_hostname;
	original_hostname = NULL;
    }
#endif
    len = ((phostname ? strlen(phostname) : 0) + 1 + (dnet ? 1 : 0) +
	   strlen(pdpynum) + 1 + (pscrnum ? strlen(pscrnum) : 1) + 1);
    *fullnamep = (char *) Xmalloc (len);
    if (!*fullnamep) goto bad;

#ifdef HAVE_LAUNCHD
    if (phostname && strlen(phostname) > 11 && !strncmp(phostname, "/tmp/launch", 11))
    	sprintf (*fullnamep, "%s%s%d",
	     (phostname ? phostname : ""),
	     (dnet ? "::" : ":"),
	     idisplay);
    else
#endif
    sprintf (*fullnamep, "%s%s%d.%d",
	     (phostname ? phostname : ""),
	     (dnet ? "::" : ":"),
	     idisplay, iscreen);

    *dpynump = idisplay;
    *screenp = iscreen;
    if (pprotocol) Xfree (pprotocol);
    if (phostname) Xfree (phostname);
    if (pdpynum) Xfree (pdpynum);
    if (pscrnum) Xfree (pscrnum);
#if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
    if (original_hostname) Xfree (original_hostname);
#endif

    GetAuthorization(trans_conn, family, (char *) saddr, saddrlen, idisplay,
		     auth_namep, auth_namelenp, auth_datap, auth_datalenp);
    return trans_conn;


    /*
     * error return; make sure everything is cleaned up.
     */
  bad:
    if (trans_conn) (void)_X11TransClose(trans_conn);
    if (saddr) free ((char *) saddr);
    if (pprotocol) Xfree (pprotocol);
    if (phostname) Xfree (phostname);
    if (address && address != addrbuf) { Xfree(address); address = addrbuf; }

#if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
    /* If connecting to the local machine, and we failed, try again with
     * the next transport type available, if there is one.
     */
    if (local_transport_index >= 0) {
	if (local_transport[++local_transport_index] != NULL) {
	    pprotocol = Xstrdup(local_transport[local_transport_index]);
#ifdef TCPCONN
	    if (strcmp(pprotocol, "tcp") == 0) {
		if (original_hostname != NULL) {
		    phostname = original_hostname;
		    original_hostname = NULL;
		} else {
		    phostname = copystring("localhost", 9);
		}
	    } else
#endif /* TCPCONN */
	    {
		if ((phostname != NULL) && (original_hostname == NULL)) {
		    original_hostname = phostname;
		}
		phostname = NULL;
	    }
	    goto connect;
	}
    }

    /* No more to try, we've failed all available local transports */
    if (original_hostname) Xfree(original_hostname);
#endif /* LOCALCONN || UNIXCONN || TCPCONN */

    if (pdpynum) Xfree (pdpynum);
    if (pscrnum) Xfree (pscrnum);
    return NULL;

}
예제 #3
0
bool CheckForLicense(DWORD mask)
  {
  //CWaitCursor Wait;
  char Buff[_MAX_PATH];
  char Msg[2048];
  CString sLicenseLoc;
  //sLicenseLoc = "c:\\test\\"; //probably want to get this from registry or ini file or let user select or ???
  sLicenseLoc = "C:\\Program Files\\HYPROD 2k\\"; //probably want to get this from registry or ini file or let user select or ???
  sprintf(Buff, "%s%s", (const char*)sLicenseLoc, CK_KeyFile);
  //todo:check that this file exists, if not, give an error message
  int err = InitCrypkey( Buff, CK_MASTER_KEY, CK_USER_KEY, FALSE, CK_NetworkChecktime);

  if (err==0)
    {
    DWORD dwOpLevel = 0;
    err = GetAuthorization(&dwOpLevel, 0); //check authorization, use up 0 runs
    EndCrypkey();

    if (err==0) //check this only if we think we are authorized
      {
      if ( ((dwOpLevel & mask) != 0) )
        {//legal license found
        if (0)
          {//examples to get some info
          char Buff[1024];
          int authopt = 0;
          int num_allowed = 0;
          int num_used = 0;
          ULONG start_date = 0;
          int ret = GetRestrictionInfo(&authopt, &start_date, &num_allowed, &num_used);
          if (ret==0)
            sprintf(Buff, "Date and time license last updated : %d", start_date);
          else
            sprintf(Buff, "Date and time license last updated : Unknown");
          AfxMessageBox(Buff);
          sprintf(Buff, "Number of Copies allowed from this site : %d", GetNumCopies());
          AfxMessageBox(Buff);
          sprintf(Buff, "Number of multi-users that the site has been granted : %d", GetNumMultiUsers());
          AfxMessageBox(Buff);
          if (Get1RestInfo(1)==0)
            sprintf(Buff, "Time Restrictions : None");
          else
            sprintf(Buff, "Time Restrictions : No of days allowed:%d  No of days used:%d", Get1RestInfo(2), Get1RestInfo(3));
          AfxMessageBox(Buff);
          int Ver = CrypkeyVersion();
          if (Ver>0)
            sprintf(Buff, "Using CrypKey Version : %d.%d", (int)floor(Ver/10.0), (int)(Ver - (floor(Ver/10.0)*10.0)));
          else
            sprintf(Buff, "CrypKey Version : Unknown");
          AfxMessageBox(Buff);
          }
        return(true);
        }
      else
        {//report error:
        AfxMessageBox("Alcan License:\nValid license not found!");
        }
      }
    else
      {//report error:
      if (err==-4)
        {//report error:
        AfxMessageBox("Alcan License:\nValid license not found!");
        }
      else
        {
        sprintf(Msg, "Alcan License:\nGetAuthorization Failure %d\n%s for %s", err, ExplainErr(EXP_INIT_ERR, err), Buff);
        AfxMessageBox(Msg);
        }
      }

    }
  else
    {//report error:
    sprintf(Msg, "Alcan License:\nInitialization Failure %d\n%s for %s", err, ExplainErr(EXP_INIT_ERR, err), Buff);
    AfxMessageBox(Msg);
    EndCrypkey();
    }

  return(false);
  }
예제 #4
0
/* 
 * Attempts to connect to server, given display name. Returns file descriptor
 * (network socket) or -1 if connection fails.  Display names may be of the
 * following format:
 *
 *     [protocol/] [hostname] : [:] displaynumber [.screennumber]
 *
 * A string with exactly two colons seperating hostname from the display
 * indicates a DECnet style name.  Colons in the hostname may occur if an
 * IPv6 numeric address is used as the hostname.  An IPv6 numeric address
 * may also end in a double colon, so three colons in a row indicates an
 * IPv6 address ending in :: followed by :display.  To make it easier for
 * people to read, an IPv6 numeric address hostname may be surrounded by
 * [ ] in a similar fashion to the IPv6 numeric address URL syntax defined
 * by IETF RFC 2732.
 *
 * If no hostname and no protocol is specified, the string is interpreted
 * as the most efficient local connection to a server on the same machine.  
 * This is usually:
 *
 *     o  shared memory
 *     o  local stream
 *     o  UNIX domain socket
 *     o  TCP to local host
 *
 * This function will eventually call the X Transport Interface functions
 * which expects the hostname in the format:
 *
 *	[protocol/] [hostname] : [:] displaynumber
 *
 */
XtransConnInfo
_X11TransConnectDisplay (
    char *display_name,
    char **fullnamep,			/* RETURN */
    int *dpynump,			/* RETURN */
    int *screenp,			/* RETURN */
    char **auth_namep,			/* RETURN */
    int *auth_namelenp,			/* RETURN */
    char **auth_datap,			/* RETURN */
    int *auth_datalenp)			/* RETURN */
{
    int family;
    int saddrlen;
    Xtransaddr *saddr;
    char *lastp, *lastc, *p;		/* char pointers */
    char *pprotocol = NULL;		/* start of protocol name */
    char *phostname = NULL;		/* start of host of display */
    char *pdpynum = NULL;		/* start of dpynum of display */
    char *pscrnum = NULL;		/* start of screen of display */
    Bool dnet = False;			/* if true, then DECnet format */
    int idisplay = 0;			/* required display number */
    int iscreen = 0;			/* optional screen number */
    /*  int (*connfunc)(); */		/* method to create connection */
    int len, hostlen;			/* length tmp variable */
    int retry;				/* retry counter */
    char addrbuf[128];			/* final address passed to
					   X Transport Interface */
    char* address = addrbuf;
    XtransConnInfo trans_conn = NULL;	/* transport connection object */
    int connect_stat;
#ifdef LOCALCONN
    struct utsname sys;
#endif
#ifdef TCPCONN
    char *tcphostname = NULL;		/* A place to save hostname pointer */
#endif

    p = display_name;

    saddrlen = 0;			/* set so that we can clear later */
    saddr = NULL;

    /*
     * Step 0, find the protocol.  This is delimited by the optional 
     * slash ('/').
     */
    for (lastp = p; *p && *p != ':' && *p != '/'; p++) ;
    if (!*p) return NULL;		/* must have a colon */

    if (p != lastp && *p != ':') {	/* protocol given? */
	pprotocol = copystring (lastp, p - lastp);
	if (!pprotocol) goto bad;	/* no memory */
	p++;				/* skip the '/' */
    } else
	p = display_name;		/* reset the pointer in
					   case no protocol was given */

    /*
     * Step 1, find the hostname.  This is delimited by either one colon,
     * or two colons in the case of DECnet (DECnet Phase V allows a single
     * colon in the hostname).  (See note above regarding IPv6 numeric 
     * addresses with triple colons or [] brackets.)
     */

    lastp = p;
    lastc = NULL;
    for (; *p; p++)
	if (*p == ':')
	    lastc = p;

    if (!lastc) return NULL;		/* must have a colon */

    if ((lastp != lastc) && (*(lastc - 1) == ':') 
#if defined(IPv6) && defined(AF_INET6)
      && ( ((lastc - 1) == lastp) || (*(lastc - 2) != ':'))
#endif
	) {
	/* DECnet display specified */

#ifndef DNETCONN
	goto bad;
#else
	dnet = True;
	/* override the protocol specified */
	if (pprotocol)
	    Xfree (pprotocol);
	pprotocol = copystring ("dnet", 4);
	hostlen = lastc - 1 - lastp;
#endif
    }
    else
	hostlen = lastc - lastp;

    if (hostlen > 0) {		/* hostname given? */
	phostname = copystring (lastp, hostlen);
	if (!phostname) goto bad;	/* no memory */
    }

    p = lastc;

#ifdef LOCALCONN
    /* check if phostname == localnodename AND protocol not specified */
    if (!pprotocol && phostname && uname(&sys) >= 0 &&
	!strncmp(phostname, sys.nodename, 
	(strlen(sys.nodename) < strlen(phostname) ? 
	strlen(phostname) : strlen(sys.nodename))))
    {
#ifdef TCPCONN
	/*
	 * We'll first attempt to connect using the local transport.  If
	 * this fails (which is the case if sshd X protocol forwarding is
	 * being used), retry using tcp and this hostname.
	 */
	tcphostname = copystring(phostname, strlen(phostname));
#endif
	Xfree (phostname);
	phostname = copystring ("unix", 4);
    }
#endif


    /*
     * Step 2, find the display number.  This field is required and is 
     * delimited either by a nul or a period, depending on whether or not
     * a screen number is present.
     */

    for (lastp = ++p; *p && isascii(*p) && isdigit(*p); p++) ;
    if ((p == lastp) ||			/* required field */
	(*p != '\0' && *p != '.') ||	/* invalid non-digit terminator */
	!(pdpynum = copystring (lastp, p - lastp)))  /* no memory */
      goto bad;
    idisplay = atoi (pdpynum);


    /*
     * Step 3, find the screen number.  This field is optional.  It is 
     * present only if the display number was followed by a period (which
     * we've already verified is the only non-nul character).
     */

    if (*p) {
	for (lastp = ++p; *p && isascii(*p) && isdigit (*p); p++) ;
	if (p != lastp) {
	    if (*p ||			/* non-digits */
		!(pscrnum = copystring (lastp, p - lastp))) /* no memory */
		goto bad;
	    iscreen = atoi (lastp);
	}
    }

    /*
     * At this point, we know the following information:
     *
     *     pprotocol                protocol string or NULL
     *     phostname                hostname string or NULL
     *     idisplay                 display number
     *     iscreen                  screen number
     *     dnet                     DECnet boolean
     * 
     * We can now decide which transport to use based on the ConnectionFlags
     * build parameter the hostname string.  If phostname is NULL or equals
     * the string "local", then choose the best transport.  If phostname
     * is "unix", then choose BSD UNIX domain sockets (if configured).
     */

#if defined(TCPCONN) || defined(UNIXCONN) || defined(LOCALCONN) || defined(MNX_TCPCONN) || defined(OS2PIPECONN)
    if (!pprotocol) {
	if (!phostname) {
#if defined(UNIXCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
	    pprotocol = copystring ("local", 5);
#if defined(TCPCONN)
	    tcphostname = copystring("localhost", 9);
#endif
	}
	else
	{
#endif
	    pprotocol = copystring ("tcp", 3);
	}
    }
#endif

#if defined(UNIXCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
    /*
     * Now that the defaults have been established, see if we have any 
     * special names that we have to override:
     *
     *     :N         =>     if UNIXCONN then unix-domain-socket
     *     ::N        =>     if UNIXCONN then unix-domain-socket
     *     unix:N     =>     if UNIXCONN then unix-domain-socket
     *
     * Note that if UNIXCONN isn't defined, then we can use the default
     * transport connection function set above.
     */

    if (!phostname) {
#ifdef apollo
	;   /* Unix domain sockets are *really* bad on apollos */
#else
	if( pprotocol ) Xfree(pprotocol);
	pprotocol = copystring ("local", 5);
#endif
    }
    else if (strcmp (phostname, "unix") == 0) {
	if( pprotocol ) Xfree(pprotocol);
	pprotocol = copystring ("local", 5);
    }
#endif

#if defined(TCPCONN)
  connect:
#endif
    /*
     * This seems kind of backwards, but we need to put the protocol,
     * host, and port back together to pass to _X11TransOpenCOTSClient().
     */

    {
	int olen = 3 + (pprotocol ? strlen(pprotocol) : 0) + 
		       (phostname ? strlen(phostname) : 0) + 
		       (pdpynum   ? strlen(pdpynum)   : 0);
	if (olen > sizeof addrbuf) address = Xmalloc (olen);
    }
    if (!address) goto bad;

    sprintf(address,"%s/%s:%d",
	pprotocol ? pprotocol : "",
	phostname ? phostname : "",
	idisplay );

    /*
     * Make the connection, also need to get the auth address info for
     * the connection.  Do retries in case server host has hit its
     * backlog (which, unfortunately, isn't distinguishable from there not
     * being a server listening at all, which is why we have to not retry
     * too many times).
     */
    for(retry=X_CONNECTION_RETRIES; retry>=0; retry-- )
	{
	if ( (trans_conn = _X11TransOpenCOTSClient(address)) == NULL )
	    {
	    break;
	    }
	if ((connect_stat = _X11TransConnect(trans_conn,address)) < 0 )
	    {
	    _X11TransClose(trans_conn);
	    trans_conn = NULL;

	    if (connect_stat == TRANS_TRY_CONNECT_AGAIN)
	    {
		sleep(1);
		continue;
	    }
	    else
		break;
	    }

	_X11TransGetPeerAddr(trans_conn, &family, &saddrlen, &saddr);

	/*
	 * The family is given in a socket format (ie AF_INET). This
	 * will convert it to the format used by the authorization and
	 * X protocol (ie FamilyInternet).
	 */

	if( _X11TransConvertAddress(&family, &saddrlen, &saddr) < 0 )
	    {
	    _X11TransClose(trans_conn);
	    trans_conn = NULL;
	    sleep(1);
	    if (saddr)
	    {
		free ((char *) saddr);
		saddr = NULL;
	    }
	    continue;
	    }

	break;
	}

    if (address != addrbuf) Xfree (address);
    address = addrbuf;

    if( trans_conn == NULL )
      goto bad;

    /*
     * Set close-on-exec so that programs that fork() doesn't get confused.
     */

    _X11TransSetOption(trans_conn,TRANS_CLOSEONEXEC,1);

    /*
     * Build the expanded display name:
     *
     *     [host] : [:] dpy . scr \0
     */
    len = ((phostname ? strlen(phostname) : 0) + 1 + (dnet ? 1 : 0) +
	   strlen(pdpynum) + 1 + (pscrnum ? strlen(pscrnum) : 1) + 1);
    *fullnamep = (char *) Xmalloc (len);
    if (!*fullnamep) goto bad;

    sprintf (*fullnamep, "%s%s%d.%d",
	     (phostname ? phostname : ""),
	     (dnet ? "::" : ":"),
	     idisplay, iscreen);

    *dpynump = idisplay;
    *screenp = iscreen;
    if (pprotocol) Xfree (pprotocol);
    if (phostname) Xfree (phostname);
    if (pdpynum) Xfree (pdpynum);
    if (pscrnum) Xfree (pscrnum);
#ifdef TCPCONN
    if (tcphostname) Xfree (tcphostname);
#endif

    GetAuthorization(trans_conn, family, (char *) saddr, saddrlen, idisplay,
		     auth_namep, auth_namelenp, auth_datap, auth_datalenp);
    return trans_conn;


    /*
     * error return; make sure everything is cleaned up.
     */
  bad:
    if (trans_conn) (void)_X11TransClose(trans_conn);
    if (saddr) free ((char *) saddr);
    if (pprotocol) Xfree (pprotocol);
    if (phostname) Xfree (phostname);
    if (address && address != addrbuf) { Xfree(address); address = addrbuf; }

#if defined(TCPCONN)
    if (tcphostname) {
	pprotocol = copystring("tcp", 3);
	phostname = tcphostname;
	tcphostname = NULL;
	goto connect;
    }
#endif

    if (pdpynum) Xfree (pdpynum);
    if (pscrnum) Xfree (pscrnum);
    return NULL;

}
예제 #5
0
SPay *CompileCheckPay(char *data, int size, CSocketRW *sock, CPayguideClient *pgc, thread_param *prm)
{
	char unauth[2];
	unauth[0]=2;
	unauth[1]=0;
	unsigned char nettype=0x01;
	unsigned char netversion=0x00;
	SPay *result=NULL;
	if (size<1)
		return NULL;
	const char *pointer=data;
	memcpy(&nettype,data,1);
	if (size>1)
		memcpy(&netversion,(data+1),1);
	
	if (nettype==NETTYPE_GETUSERS)
	{
		if (sock->Authorized()>=AUTH_CONTROL)
		{
			char buff[1024];
			buff[0]=22;
			buff[1]=0;
			char *ptr=buff+2;
			sem_wait(prm->pc_lock);
			prm->sock_list->ResetCursor();
			for (unsigned int i=0; i<prm->sock_list->GetLen(); i++)
			{
				CPayguideClient *pc=prm->sock_list->GetNext();
				if (pc!=NULL)
				{
					unsigned short l=(unsigned short)strlen(pc->GetName());
				
					memcpy(ptr, &l, sizeof(unsigned short));
					ptr+=sizeof(unsigned short);
				
					memcpy(ptr, pc->GetName(), (int)l);
					ptr+=(int)l;
				}
			}
			sem_post(prm->pc_lock);
			sock->Send(buff, ptr-buff);
		}
		else
			sock->Send(unauth, 2);
	}
	
	if (nettype==NETTYPE_PAYCHECK1 || nettype==NETTYPE_PAYCHECK2  || nettype==NETTYPE_PAYCHECK3)
	{
		if (sock->Authorized()>=AUTH_PAYCHECK)
		{
			/* Regular pay */
			long long pay_id=0;
			int provider=0;
			long long terminal_id=0;
			int currency=0;
			char msg[SIZE_DATA+1];
			char stamp[20];
			
			char magic;
			timeval time_now;
			tm *ptm;
			gettimeofday(&time_now, NULL);
			ptm = localtime (&time_now.tv_sec);
			strftime(stamp, 20, "%Y-%m-%d %H:%M:%S", ptm);
	
			pointer+=(SIZE_BODY_TYPE+SIZE_BODY_VERSION);
			
			memcpy(&pay_id, pointer, SIZE_BODY_ID);
			pointer+=SIZE_BODY_ID;
			
			memcpy(&magic, pointer, SIZE_BODY_MAGIC);
			pointer+=SIZE_BODY_MAGIC;
			
			memcpy(&provider, pointer, SIZE_BODY_OPERATOR);
			pointer+=SIZE_BODY_OPERATOR;
			
			memcpy(&terminal_id, pointer, SIZE_BODY_TERMINAL);
			pointer+=SIZE_BODY_TERMINAL;
	
			memcpy(&currency, pointer, SIZE_BODY_CURRENCY);
			pointer+=SIZE_BODY_CURRENCY;
			
			int l=size-SIZE_BODY_CURRENCY-SIZE_BODY_TERMINAL-SIZE_BODY_OPERATOR-SIZE_BODY_ID-SIZE_BODY_TYPE-SIZE_BODY_VERSION-SIZE_BODY_MAGIC;
			if (netversion>=0x01) l-=(SIZE_BODY_SUMMINT+SIZE_BODY_SUMMFLOAT)*2;
			if (netversion>=0x02) l-=(SIZE_BODY_SUMMINT+SIZE_BODY_SUMMFLOAT);

			if (l<0)
				l=0;
			else if (l>SIZE_DATA)
				l=SIZE_DATA;
			if (l>=0)
				memcpy(&msg, pointer, l);
			msg[l]=0;
			
			pointer+=l;
			float real_summ=0; float amount=0; float back_summ=0;
			unsigned int i_summ_buff=0;
			unsigned char f_summ_buff=0;
			if (netversion>=0x01)
			{
				memcpy(&i_summ_buff, pointer, SIZE_BODY_SUMMINT);
				pointer+=SIZE_BODY_SUMMINT;
				
				memcpy(&f_summ_buff, pointer, SIZE_BODY_SUMMFLOAT);
				pointer+=SIZE_BODY_SUMMFLOAT;
				real_summ=(float)i_summ_buff+(unsigned int)f_summ_buff/100.0f;
			}
			if (netversion>=0x02)
			{
				memcpy(&i_summ_buff, pointer, SIZE_BODY_SUMMINT);
				pointer+=SIZE_BODY_SUMMINT;
				
				memcpy(&f_summ_buff, pointer, SIZE_BODY_SUMMFLOAT);
				pointer+=SIZE_BODY_SUMMFLOAT;
				amount=(float)i_summ_buff+(unsigned int)f_summ_buff/100.0f;
			}

			if (netversion>=0x03)
			{
				memcpy(&i_summ_buff, pointer, SIZE_BODY_SUMMINT);
				pointer+=SIZE_BODY_SUMMINT;
				
				memcpy(&f_summ_buff, pointer, SIZE_BODY_SUMMFLOAT);
				pointer+=SIZE_BODY_SUMMFLOAT;
				back_summ=(float)i_summ_buff+(unsigned int)f_summ_buff/100.0f;
			}
			
			if (size!=pointer-data)
			{
				char tmp[256];
				snprintf(tmp, 256, "paycheck: wrong message lenght. l=%i in packet head but %i in real. Packet rejected.", size, (pointer-data));
				LogWrite(LOGMSG_WARNING, tmp);
			}
			else
			{
				result=CompileNewPay(pay_id, 10, provider, stamp, msg, 0, terminal_id, currency, 0, 0, -1,-1,-1);

				if (netversion>=0x01)
					result->summ=real_summ;

				if (netversion>=0x02)
					result->amount=amount;

				if (netversion>=0x03)
					result->back_summ=back_summ;
				
				result->magic=magic;
//				result->nettype=nettype;
				result->netversion=netversion;
			}
		}
		else
			sock->Send(unauth, 2);
	}

	if (nettype==NETTYPE_PING)
	{
		char ping[2];
		ping[0]=5;
		ping[1]=0;
		sock->Send(ping, 2);
	}
		
	if (nettype==NETTYPE_COMMAND)
	{
		//printf("case 4\n");
		/* Command */
		char cmd=0;
		int command=0;
		pointer+=(SIZE_BODY_TYPE+SIZE_BODY_VERSION);
		memcpy(&cmd, pointer, sizeof(cmd));
		command=(int)cmd;
		pointer+=sizeof(cmd);
		
		int l=size-sizeof(cmd);
		if (l>=0)
		{
			int result_size=0;
			ExecCmd(command, pointer, l, &result_size, sock);
		}
		else
		{
//			char tmp[256];
//			snprintf(tmp, 256, "paycheck: wrong message lenght. l=%i in packet head but %i in real. Packet rejected.");
//			LogWrite(LOGMSG_WARNING, tmp);
		}
		
	//	msg[l]=0;
		result=NULL;
	}
	
	if (nettype==NETTYPE_SIGN)
	{
		if (sock->Authorized()>=AUTH_CONTROL)
		{
			pgc->AddToDelivery();
			result=NULL;
		}
		else
			sock->Send(unauth, 2);
	}
	
	if (nettype==NETTYPE_UNSIGN)
	{
		if (sock->Authorized()>=AUTH_STAT)
		{
			pgc->RemoveFromDelivery();
			result=NULL;
		}
		else
			sock->Send(unauth, 2);
	}
	
	if (nettype==NETTYPE_AUTHORIZATION)
	{
		char login[1024];
		char password[1024];
		char *ptr=data+2;
		unsigned short login_size;
		unsigned short password_size;
		char unauth[2];
		unauth[0]=2;
		unauth[1]=0;

		if (size>4)
		{
			memcpy(&login_size,ptr, sizeof(login_size));
			ptr+=sizeof(login_size);

			memcpy(&password_size,ptr, sizeof(password_size));
			ptr+=sizeof(password_size);
			
			if (login_size<1024 && password_size<1024)
			{
				memcpy(login, ptr, login_size);
				login[login_size]=0;
				ptr+=login_size;
				memcpy(password, ptr, password_size);
				password[password_size]=0;
				ptr+=password_size;
				if (ptr-data==size)
				{
					int auth_level=GetAuthorization(login, password);
					if (auth_level==0)
					{
//						printf("Authorization failed\n");
						sock->Send(unauth, 2);
						sock->Down();
					}
					else
					{
						char auth[2];
						auth[0]=3;
						auth[1]=0;
						sock->Authorize(auth_level);
						pgc->Authorize(auth_level, login);
						sock->Send(auth, 2);
//						printf("User %s connected.\n", login);
					}
				}
				else
				{
					sock->Send(unauth, 2);
				}
			}
			else
				sock->Send(unauth, 2);
		}
		else
			sock->Send(unauth, 2);
	
	}
	//printf("returning\n");
	
	return result;
}
예제 #6
0
STRUCT__HEADER__SET	* GetFileSchema(int ifcModel, STRUCT__HEADER__SET * parent)
{
#ifdef	_UNICODE
	STRUCT__HEADER__SET	* headerFileSchema = CreateHeaderSet(parent, L"Set of FileSchemas", 0),
#else
	STRUCT__HEADER__SET	* headerFileSchema = CreateHeaderSet(parent, "Set of FileSchemas", 0),
#endif
						** ppHeader = &headerFileSchema->child;
#ifdef	_UNICODE
	wchar_t	* text = 0;
#else
	char	* text = 0;
#endif
	int		i = 0;

#ifdef	_UNICODE
	if	(!GetSPFFHeaderItem(ifcModel, 9, i, sdaiUNICODE, (char **) &text)) {
		while  (!GetSPFFHeaderItem(ifcModel, 9, i++, sdaiUNICODE, (char **) &text)) {
			(* ppHeader) = CreateHeaderSet(headerFileSchema, L"FileSchema", text);
#else
	if	(!GetSPFFHeaderItem(ifcModel, 9, i, sdaiSTRING, &text)) {
		while  (!GetSPFFHeaderItem(ifcModel, 9, i++, sdaiSTRING, &text)) {
			(* ppHeader) = CreateHeaderSet(headerFileSchema, "FileSchema", text);
#endif
			ppHeader = &(* ppHeader)->next;
			text = 0;
		}
	}

	return	headerFileSchema;
}

STRUCT__HEADER__SET	* GetHeaderInfo(int ifcModel)
{
#ifdef	_UNICODE
	STRUCT__HEADER__SET	* headerFileSchema = CreateHeaderSet(0, L"Header Info", 0),
#else
	STRUCT__HEADER__SET	* headerFileSchema = CreateHeaderSet(0, "Header Info", 0),
#endif
						** ppHeader = &headerFileSchema->child;

	(* ppHeader) = GetHeaderDescription(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetImplementationLevel(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetName(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetTimeStamp(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetAuthor(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetOrganization(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetPreprocessorVersion(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetOriginatingSystem(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetAuthorization(ifcModel, headerFileSchema);
	ppHeader = &(* ppHeader)->next;
	(* ppHeader) = GetFileSchema(ifcModel, headerFileSchema);

	return	headerFileSchema;
}