示例#1
0
static int
passive_session (int xserver, int fd, kx_context *kc)
{
    if (replace_cookie (xserver, fd, XauFileName(), 1))
	return 1;
    else
	return copy_encrypted (kc, xserver, fd);
}
示例#2
0
/* This just gets a cookie of MIT-MAGIC-COOKIE-1 type */
char *
mdmcomm_get_a_cookie (gboolean binary)
{
	FILE *fp;
	char *number;
	char *cookie = NULL;
	Xauth *xau;

	VE_IGNORE_EINTR (fp = fopen (XauFileName (), "r"));
	if (fp == NULL) {
		return NULL;
	}

	number = get_dispnum ();

	cookie = NULL;

	while ((xau = XauReadAuth (fp)) != NULL) {
		/* Just find the FIRST magic cookie, that's what mdm uses */
		if (xau->number_length != strlen (number) ||
		    strncmp (xau->number, number, xau->number_length) != 0 ||
		    /* mdm sends MIT-MAGIC-COOKIE-1 cookies of length 16,
		     * so just do those */
		    xau->data_length != 16 ||
		    xau->name_length != strlen ("MIT-MAGIC-COOKIE-1") ||
		    strncmp (xau->name, "MIT-MAGIC-COOKIE-1",
			     xau->name_length) != 0) {
			XauDisposeAuth (xau);
			continue;
		}

		if (binary) {
			cookie = g_new0 (char, 16);
			memcpy (cookie, xau->data, 16);
		} else {
			int i;
			GString *str;

			str = g_string_new (NULL);

			for (i = 0; i < xau->data_length; i++) {
				g_string_append_printf
					(str, "%02x",
					 (guint)(guchar)xau->data[i]);
			}
			cookie = g_string_free (str, FALSE);
		}

		XauDisposeAuth (xau);

		break;
	}
示例#3
0
void DM::GDMAuthenticate()
{
    FILE *fp;
    const char *dpy, *dnum, *dne;
    int dnl;
    Xauth *xau;

    dpy = DisplayString(QPaintDevice::x11AppDisplay());
    if(!dpy)
    {
        dpy = ::getenv("DISPLAY");
        if(!dpy)
            return;
    }
    dnum = strchr(dpy, ':') + 1;
    dne = strchr(dpy, '.');
    dnl = dne ? dne - dnum : strlen(dnum);

    /* XXX should do locking */
    if(!(fp = fopen(XauFileName(), "r")))
        return;

    while((xau = XauReadAuth(fp)))
    {
        if(xau->family == FamilyLocal && xau->number_length == dnl && !memcmp(xau->number, dnum, dnl) && xau->data_length == 16
           && xau->name_length == 18 && !memcmp(xau->name, "MIT-MAGIC-COOKIE-1", 18))
        {
            QString cmd("AUTH_LOCAL ");
            for(int i = 0; i < 16; i++)
                cmd += QString::number((uchar)xau->data[i], 16).rightJustify(2, '0');
            cmd += "\n";
            if(exec(cmd.latin1()))
            {
                XauDisposeAuth(xau);
                break;
            }
        }
        XauDisposeAuth(xau);
    }

    fclose(fp);
}
示例#4
0
文件: AuGetBest.c 项目: csulmone/X11
Xauth *
XauGetBestAuthByAddr (
#if NeedWidePrototypes
    unsigned int	family,
    unsigned int	address_length,
#else
    unsigned short	family,
    unsigned short	address_length,
#endif
    _Xconst char*	address,
#if NeedWidePrototypes
    unsigned int	number_length,
#else
    unsigned short	number_length,
#endif
    _Xconst char*	number,
    int			types_length,
    char**		types,
    _Xconst int*	type_lengths)
{
    FILE    *auth_file;
    char    *auth_name;
    Xauth   *entry;
    Xauth   *best;
    int	    best_type;
    int	    type;
#ifdef hpux
    char		*fully_qual_address;
    unsigned short	fully_qual_address_length;
#endif

    auth_name = XauFileName ();
    if (!auth_name)
	return NULL;
    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
	return NULL;
    auth_file = fopen (auth_name, "rb");
    if (!auth_file)
	return NULL;

#ifdef hpux
    if (family == FamilyLocal) {
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
	_Xgethostbynameparams hparams;
#endif
	struct hostent *hostp;

	/* make sure we try fully-qualified hostname */
	if ((hostp = _XGethostbyname(address,hparams)) != NULL) {
	    fully_qual_address = hostp->h_name;
	    fully_qual_address_length = strlen(fully_qual_address);
	}
	else
	{
	    fully_qual_address = NULL;
	    fully_qual_address_length = 0;
	}
    }
#endif /* hpux */

    best = NULL;
    best_type = types_length;
    for (;;) {
	entry = XauReadAuth (auth_file);
	if (!entry)
	    break;
	/*
	 * Match when:
	 *   either family or entry->family are FamilyWild or
	 *    family and entry->family are the same and
	 *     address and entry->address are the same
	 *  and
	 *   either number or entry->number are empty or
	 *    number and entry->number are the same
	 *  and
	 *   either name or entry->name are empty or
	 *    name and entry->name are the same
	 */

	if ((family == FamilyWild || entry->family == FamilyWild ||
	     (entry->family == family &&
	     ((address_length == entry->address_length &&
	      binaryEqual (entry->address, address, (int)address_length))
#ifdef hpux
	     || (family == FamilyLocal &&
		fully_qual_address_length == entry->address_length &&
	     	binaryEqual (entry->address, fully_qual_address,
		    (int) fully_qual_address_length))
#endif
	    ))) &&
	    (number_length == 0 || entry->number_length == 0 ||
	     (number_length == entry->number_length &&
	      binaryEqual (entry->number, number, (int)number_length))))
	{
	    if (best_type == 0)
	    {
		best = entry;
		break;
	    }
	    for (type = 0; type < best_type; type++)
		if (type_lengths[type] == entry->name_length &&
		    !(strncmp (types[type], entry->name, entry->name_length)))
		{
		    break;
		}
	    if (type < best_type)
	    {
		if (best)
		    XauDisposeAuth (best);
		best = entry;
		best_type = type;
		if (type == 0)
		    break;
		continue;
	    }
	}
	XauDisposeAuth (entry);
    }
    (void) fclose (auth_file);
    return best;
}
示例#5
0
Xauth *
XauGetAuthByAddr (
#if NeedWidePrototypes
    unsigned int	family,
    unsigned int	address_length,
#else
    unsigned short	family,
    unsigned short	address_length,
#endif
    _Xconst char*	address,
#if NeedWidePrototypes
    unsigned int	number_length,
#else
    unsigned short	number_length,
#endif
    _Xconst char*	number,
#if NeedWidePrototypes
    unsigned int	name_length,
#else
    unsigned short	name_length,
#endif
    _Xconst char*	name)
{
    FILE    *auth_file;
    char    *auth_name;
    Xauth   *entry;

    auth_name = XauFileName ();
    if (!auth_name)
        return NULL;
    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
        return NULL;
    auth_file = fopen (auth_name, "rb");
    if (!auth_file)
        return NULL;
    for (;;) {
        entry = XauReadAuth (auth_file);
        if (!entry)
            break;
        /*
         * Match when:
         *   either family or entry->family are FamilyWild or
         *    family and entry->family are the same and
         *     address and entry->address are the same
         *  and
         *   either number or entry->number are empty or
         *    number and entry->number are the same
         *  and
         *   either name or entry->name are empty or
         *    name and entry->name are the same
         */

        if ((family == FamilyWild || entry->family == FamilyWild ||
                (entry->family == family &&
                 address_length == entry->address_length &&
                 binaryEqual (entry->address, address, address_length))) &&
                (number_length == 0 || entry->number_length == 0 ||
                 (number_length == entry->number_length &&
                  binaryEqual (entry->number, number, number_length))) &&
                (name_length == 0 || entry->name_length == 0 ||
                 (entry->name_length == name_length &&
                  binaryEqual (entry->name, name, name_length))))
            break;
        XauDisposeAuth (entry);
    }
    (void) fclose (auth_file);
    return entry;
}
示例#6
0
void
handle_auth(int sockfd, XDR *in)
{
  int authfd;
  char hostname[BUFSIZ];
  char displayname[BUFSIZ];
  char *auth_file;
  Display* displ;
  size_t auth_size;
  int found = 0, i;

  /*
   * Get our auth size.
   */

  if (!xdr_u_int(in, &auth_size)) {		/* Get the size */
    eacces(sockfd);
    return;
  }
  
  /*
   * Allocate a buffer.
   */

  if ((auth_file = malloc(auth_size)) == NULL) {
    /* errno is supposed to be set by malloc to ENOMEM */
    status_return(sockfd, FAIL);
    exit(OK);
  }

  readn(sockfd, auth_file, auth_size);	/* read packet */

  if (!noauth) {

    /* 
     * write out the file in one gulp.
     */

    authfd = open("/tmp/.tmpxauth", O_CREAT | O_RDWR, S_IRWXU);
  
    if (authfd < 0) {
      /* errno should still be set from the open */
      status_return(sockfd, FAIL);
      exit(OK);
    }
  
    if (write(authfd, auth_file, auth_size) < 0) {
      /* errno should still be set from the write */
      status_return(sockfd, FAIL);
      exit(OK);
    }

    close(authfd);

    gethostname(hostname, BUFSIZ);			/* get our hostname */
    setenv("XAUTHORITY", "/tmp/.tmpxauth", 1);		/* for XOpenDisplay */

    for (i = 0; i < 12; i++) {
      sprintf(displayname, "%s:%d", hostname, i);		/* displayify it */

      /*
       * Test our authentication.
       * our XAUTHORITY environment variable is pointing to the xauth record,
       * so if we've got a match, we the open should succeed.
       */

      displ = XOpenDisplay(displayname);

      if (displ) {
        found++;
        XCloseDisplay(displ);				/* close display */
        break;
      }
    }

    unlink(XauFileName());				/* delete file */

    if (!found) {
      errno = EACCES;
      status_return(sockfd, FAIL);
      exit(OK);
    }
  
  }

  status_return(sockfd, OK);				/* Acknowledge auth */
  authenticated++;					/* Set auth state */
}
示例#7
0
int
main (int argc, char *argv[])
{
	GtkWidget *dialog;
	char *command;
	char *version;
	char *ret;
	const char *message;
	GOptionContext *ctx;

	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	/* Option parsing */
	ctx = g_option_context_new ("- New mdm login");
	g_option_context_add_main_entries (ctx, options, _("main options"));
	g_option_context_parse (ctx, &argc, &argv, NULL);
	g_option_context_free (ctx);

	if (monte_carlo_pi) {
		calc_pi ();
		return 0;
	}

	mdm_log_init ();
	mdm_log_set_debug (debug_in);

	if (args_remaining != NULL && args_remaining[0] != NULL)
		server = args_remaining[0];

	if (send_command != NULL) {
		if ( ! mdmcomm_check (FALSE)) {
			mdm_common_error (_("Error: MDM (MDM Display Manager) is not running."));
			mdm_common_error (_("You might be using a different display manager."));
			return 1;
		}
	} else {
		/*
		 * The --command argument does not display anything, so avoid
		 * running gtk_init until it finishes.  Sometimes the
		 * --command argument is used when there is no display so it
		 * will fail and cause the program to exit, complaining about
		 * "no display".
		 */
		gtk_init (&argc, &argv);

		if ( ! mdmcomm_check (TRUE)) {
			return 1;
		}
	}

	/* Start reading config data in bulk */
	mdmcomm_comm_bulk_start ();

	/* Process --command option */

	g_type_init ();

	if (send_command != NULL) {

		/* gdk_init is needed for cookie code to get display */
		gdk_init (&argc, &argv);
		if (authenticate)
			auth_cookie = mdmcomm_get_auth_cookie ();

		/*
		 * If asking for a translatable config value, then try to get
		 * the translated value first.  If this fails, then go ahead
		 * and call the normal sockets command.
		 */
		if (strncmp (send_command, MDM_SUP_GET_CONFIG " ",
		    strlen (MDM_SUP_GET_CONFIG " ")) == 0) {
			gchar *value = NULL;
			const char *key = &send_command[strlen (MDM_SUP_GET_CONFIG " ")];

			if (is_key (MDM_KEY_WELCOME, key) ||
			    is_key (MDM_KEY_REMOTE_WELCOME, key)) {
				value = mdm_config_get_translated_string ((gchar *)key);
				if (value != NULL) {
					ret = g_strdup_printf ("OK %s", value);
				}
			}

			/*
			 * If the above didn't return a value, then must be a
			 * different key, so call mdmcomm_call_mdm.
			 */
			if (value == NULL)
				ret = mdmcomm_call_mdm (send_command, auth_cookie,
							"1.0.0.0", 5);
		} else {
			ret = mdmcomm_call_mdm (send_command, auth_cookie,
						"1.0.0.0", 5);
		}

		/* At this point we are done using the socket, so close it */
		mdmcomm_comm_bulk_stop ();

		if (ret != NULL) {
			g_print ("%s\n", ret);
			return 0;
		} else {
			dialog = hig_dialog_new (NULL /* parent */,
						 GTK_DIALOG_MODAL /* flags */,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_OK,
						 _("Cannot communicate with MDM "
						   "(The MDM Display Manager)"),
						 _("Perhaps you have an old version "
						   "of MDM running."));
			gtk_widget_show_all (dialog);
			gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
			return 1;
		}
	}

	/*
	 * Now process what mdmflexiserver is more frequently used to
	 * do, start VT (Virtual Terminal) sesions - at least on
	 * systems where it is supported.  On systems where it is not
	 * supporteed VT stands for "Very Tight" and will mess up your
	 * display if you use it.  Tight!  So do not use it.
	 *
	 * I would accept a patch to disable it on such systems, but it
	 * is easy to avoid not using it as long as your distro does not
	 * put the menu choice in the application launch button on the
	 * panel (don't ship the desktop file).
	 */

	/*
	 * Always attempt to get cookie and authenticate.  On remote
	 * servers
	 */
	auth_cookie = mdmcomm_get_auth_cookie ();

	if (use_xnest) {
		char *cookie = mdmcomm_get_a_cookie (FALSE /* binary */);

		if (cookie == NULL) {

			/* At this point we are done using the socket, so close it */
			mdmcomm_comm_bulk_stop ();

			dialog = hig_dialog_new (NULL /* parent */,
						 GTK_DIALOG_MODAL /* flags */,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_OK,
						 _("You do not seem to have the "
						   "authentication needed for this "
						   "operation"),
						 _("Perhaps your .Xauthority "
						   "file is not set up correctly."));
			gtk_widget_show_all (dialog);
			gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
			return 1;
		}
		command = g_strdup_printf (MDM_SUP_FLEXI_XNEST " %s %d %s %s",
					   mdmcomm_get_display (),
					   (int)getuid (),
					   cookie,
					   XauFileName ());
		g_free (cookie);
		version = "1.0.0.0";
		auth_cookie = NULL;
	} else {

		/* check for other displays/logged in users */
		check_for_users ();

		if (auth_cookie == NULL) {

			/* At this point we are done using the socket, so close it */
			mdmcomm_comm_bulk_stop ();

			dialog = hig_dialog_new (NULL /* parent */,
						 GTK_DIALOG_MODAL /* flags */,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_OK,
						 _("You do not seem to be logged in on the "
						   "console"),
						 _("Starting a new login only "
						   "works correctly on the console."));
			gtk_dialog_set_has_separator (GTK_DIALOG (dialog),
						      FALSE);
			gtk_widget_show_all (dialog);
			gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
			return 1;
		}

		read_servers ();
		server = choose_server ();
		if (server == NULL)
			command = g_strdup (MDM_SUP_FLEXI_XSERVER);
		else
			command = g_strdup_printf (MDM_SUP_FLEXI_XSERVER " %s",
						   server);
		version = "1.0.0.0";
	}

	ret = mdmcomm_call_mdm (command, auth_cookie, version, 5);
	g_free (command);
	g_free (auth_cookie);
	g_strfreev (args_remaining);

	/* At this point we are done using the socket, so close it */
	mdmcomm_comm_bulk_stop ();

	if (ret != NULL &&
	    strncmp (ret, "OK ", 3) == 0) {

		/* if we switched to a different screen as a result of this,
		 * lock the current screen */
		if ( ! no_lock && ! use_xnest) {
			maybe_lock_screen ();
		}

		/* all fine and dandy */
		g_free (ret);
		return 0;
	}

	message = mdmcomm_get_error_message (ret, use_xnest);

	dialog = hig_dialog_new (NULL /* parent */,
				 GTK_DIALOG_MODAL /* flags */,
				 GTK_MESSAGE_ERROR,
				 GTK_BUTTONS_OK,
				 _("Cannot start new display"),
				 message);

	gtk_widget_show_all (dialog);
	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	g_free (ret);

	return 1;
}
示例#8
0
/********************************************************************
 *
 * fork/exec a child pdm after setting up a message pipe. 
 */
void mgr_launch_pdm( XpPdmServiceRec *rec )
{
    int       i;
    struct sigaction svec;
    char      buf[1024];
    int       original_umask;
    char      *existing_name;
    FILE      *existing_file;
    Xauth     *entry;
    char      *envstr;


    /*
     * Setup message pipe.
     */
    if ( pipe(rec->message_pipe) == -1 ) {
	rec->pdm_exec_errorcode = g.pdm_start_error;
	sprintf( buf, PDMD_MSG_8, g.prog_name );
	rec->pdm_exec_errormessage = xpstrdup( buf );
	return;
    }

    rec->message_xtid = XtAppAddInput( g.context, rec->message_pipe[0],
			  (XtPointer) XtInputReadMask,
			  message_pipe_handler, (XtPointer) NULL );

    /*
     * See if a cookie file is needed.
     */
    if (rec->cookie_cnt) {
	/*
	 * Create new .Xauthority file.
	 */
	original_umask = umask (0077);      /* disallow non-owner access */
	tmpnam( rec->auth_filename );
	rec->auth_file = fopen( rec->auth_filename, "w" );

	if (rec->auth_file) {
	    /*
	     * Copy existing .Xauthority entries.
	     */
	    existing_name = XauFileName ();

	    if (existing_name) {
		if (access (existing_name, R_OK) == 0) {     /* checks REAL id */
		    existing_file = fopen (existing_name, "r");
		    if (existing_file) {
			for (;;) {
			    entry = XauReadAuth (existing_file);
			    if (!entry)
				break;

			    XauWriteAuth( rec->auth_file, entry );
			    XauDisposeAuth (entry);
			}
			fclose (existing_file);
		    }
		}
	    }

	    /*
	     * Merge in cookies recently sent.
	     */
	    for ( i = 0; i < rec->cookie_cnt; i++ ) {
		XauWriteAuth( rec->auth_file, rec->cookies[i] );
	    }

	    fclose( rec->auth_file );
	}
	original_umask = umask (original_umask);
    }


    rec->pid = fork();

    if ( rec->pid < 0 ) {
	rec->pdm_exec_errorcode = g.pdm_start_error;
	sprintf( buf, PDMD_MSG_9, g.prog_name );
	rec->pdm_exec_errormessage = xpstrdup( buf );
	return;
    }
    else if ( rec->pid == 0) {
	/*
	 * Child process.
	 */

	/*
	 * Hook stderr back to parent via message pipe.
	 */
	dup2(rec->message_pipe[1], 2);
	close(rec->message_pipe[0]);

	/*
	 * The child should have default behavior for all signals.
	 */
	sigemptyset(&svec.sa_mask);
	svec.sa_flags   = 0;
	svec.sa_handler = SIG_DFL;
	(void) sigaction(SIGCHLD, &svec, (struct sigaction *) NULL);

	for (i=3; i < FOPEN_MAX; i++) {
	    if ((i != rec->message_pipe[1]) && 
		(rec->auth_file && (i != fileno(rec->auth_file))))
	    {
		(void) fcntl (i, F_SETFD, 1);
	    }
	}

	/*
	 * Set the new locale for the child.
	 *
	 * note: the locale hint will be of the form:
	 *
	 *    name_spec[;registry_spec[;ver_spec[;encoding_spec]]]
	 *
	 * for now, just pull out the name_spec (e.g. 'C')
	 * and use it.   With a little work, a more complex
	 * syntax could be understood and the appropriate
	 * actions taken here rather than just wedging
	 * name_spec into setlocale() and hoping.
	 */
	if ( !(rec->locale_hint) ) {
	    /*
	     * Leave current locale alone.
	     */
	}
	else if ( strcmp( rec->locale_hint, "" ) ) {
	    /*
	     * Leave current locale alone.  Note that "" into
	     * setlocale says to go with default vs leave it alone.
	     */
	}
	else {
	    char *tptr1, *tptr2;

	    tptr1 = xpstrdup( rec->locale_hint );
            tptr2 = strchr( tptr1, ';' );
	    if (tptr2) *tptr2 = '\0';
	
	    setlocale( LC_ALL, tptr1 );
	    XFree( tptr1 );
	}

	/*
	 * Set XAUTHORITY env var if needed.
	 */
	if ((rec->cookie_cnt) && (rec->auth_filename) && (rec->auth_file)) {
	    envstr = Xmalloc( strlen(rec->auth_filename) + 12 );
	    sprintf( envstr, "XAUTHORITY=%s", rec->auth_filename );
	    putenv( envstr );
	}

	/*
	 * Start the child for real.
	 */
	(void) execvp(rec->pdm_exec_argvs[0], rec->pdm_exec_argvs);

	(void) fprintf (stderr, PDMD_MSG_10, g.prog_name, rec->pdm_exec_argvs[0]);

	/*
	 * tomg - need to deal with failed child start.
	 */
	exit(PDM_EXIT_ERROR);
    }
    else {
	/*
	 * Parent process.
	 */

	/*
	 * Close the write end of the pipe - only the child needs it.
	 */
	close(rec->message_pipe[1]);
	rec->message_pipe[1] = -1;
    }
}
示例#9
0
文件: gdm.c 项目: Asher256/gshutdown
static gboolean
gdm_authenticate_connection (GdmProtocolData *data)
{
#define GDM_MIT_MAGIC_COOKIE_LEN 16

        const char *xau_path;
        FILE       *f;
        Xauth      *xau;
        char       *display_number;
        gboolean    retval;

        if (data->auth_cookie) {
                char *msg;
                char *response;

                msg = g_strdup_printf (GDM_PROTOCOL_MSG_AUTHENTICATE " %s",
                                       data->auth_cookie);
                response = gdm_send_protocol_msg (data, msg);
                g_free (msg);
 
                if (response && !strcmp (response, "OK")) {
                        g_free (response);
                        return TRUE;
                } else {
                        g_free (response);
                        g_free (data->auth_cookie);
                        data->auth_cookie = NULL;
                }
        }

        if (!(xau_path = XauFileName ()))
                return FALSE;

        if (!(f = fopen (xau_path, "r")))
                return FALSE;

        retval = FALSE;
        display_number = get_display_number ();

        while ((xau = XauReadAuth (f))) {
                char  buffer[40]; /* 2*16 == 32, so 40 is enough */
                char *msg;
                char *response;
                int   i;

                if (xau->family != FamilyLocal ||
                    strncmp (xau->number, display_number, xau->number_length) ||
                    strncmp (xau->name, "MIT-MAGIC-COOKIE-1", xau->name_length) ||
                    xau->data_length != GDM_MIT_MAGIC_COOKIE_LEN) {
                        XauDisposeAuth (xau);
                        continue;
                }
                
                for (i = 0; i < GDM_MIT_MAGIC_COOKIE_LEN; i++)
                        g_snprintf (buffer + 2*i, 3, "%02x", (guint)(guchar)xau->data[i]);
                
                XauDisposeAuth (xau);
                
                msg = g_strdup_printf (GDM_PROTOCOL_MSG_AUTHENTICATE " %s", buffer);
                response = gdm_send_protocol_msg (data, msg);
                g_free (msg);
 
                if (response && !strcmp (response, "OK")) {
			data->auth_cookie = g_strdup (buffer);
                        g_free (response);
                        retval = TRUE;
                        break;
                }

                g_free (response);
        }

        g_free (display_number);
        
        fclose (f);

        return retval;

#undef GDM_MIT_MAGIC_COOKIE_LEN
}
示例#10
0
int
main (int argc, char **argv)
{
    char **xargv;
    int i, j;
    int fd;
	
    xargv = alloca (sizeof (char *) * (argc + 32));
	
    if (!read_boolean_pref (CFSTR ("no_auth"), FALSE))
		auth_file = XauFileName ();
	
    /* The standard X11 behaviour is for the server to quit when the first
	 client exits. But it can be useful for debugging (and to mimic our
	 behaviour in the beta releases) to not do that. */
	
    xinit_kills_server = read_boolean_pref (CFSTR ("xinit_kills_server"), TRUE);
	
    for (i = 1; i < argc; i++)
    {
		if (argv[i][0] == ':')
			server_name = argv[i];
    }
	
    if (server_name == NULL)
    {
		static char name[8];
		
		/* No display number specified, so search for the first unused.
		 
		 There's a big old race condition here if two servers start at
		 the same time, but that's fairly unlikely. We could create
		 lockfiles or something, but that's seems more likely to cause
		 problems than the race condition itself.. */
		
		for (i = 0; i < MAX_DISPLAYS; i++)
		{
			if (!display_exists_p (i))
				break;
		}
		
		if (i == MAX_DISPLAYS)
		{
			fprintf (stderr, "%s: couldn't allocate a display number", argv[0]);
			exit (1);
		}
		
		sprintf (name, ":%d", i);
		server_name = name;
    }
	
    if (auth_file != NULL)
    {
		/* Create new Xauth keys and add them to the .Xauthority file */
		
		make_auth_keys (server_name);
		write_auth_file (TRUE);
    }
	
    /* Construct our new argv */
	
    i = j = 0;
	
    xargv[i++] = argv[j++];
	
    if (auth_file != NULL)
    {
		xargv[i++] = "-auth";
		xargv[i++] = auth_file;
    }
	
    /* By default, don't listen on tcp sockets if Xauth is disabled. */
	
    if (read_boolean_pref (CFSTR ("nolisten_tcp"), auth_file == NULL))
    {
		xargv[i++] = "-nolisten";
		xargv[i++] = "tcp";
    }
	
    while (j < argc)
    {
		if (argv[j++][0] != ':')
			xargv[i++] = argv[j-1];
    }
	
    xargv[i++] = (char *) server_name;
    xargv[i++] = NULL;
	
    /* Detach from any controlling terminal and connect stdin to /dev/null */
	
#ifdef TIOCNOTTY
    fd = open ("/dev/tty", O_RDONLY);
    if (fd != -1)
    {
		ioctl (fd, TIOCNOTTY, 0);
		close (fd);
    }
#endif
	
    fd = open ("/dev/null", O_RDWR, 0);
    if (fd >= 0)
    {
		dup2 (fd, 0);
		if (fd > 0)
			close (fd);
    }
	
    if (!start_server (xargv))
		return 1;
	
    if (!wait_for_server ())
    {
		kill (server_pid, SIGTERM);
		return 1;
    }
	
    if (!start_client ())
    {
		kill (server_pid, SIGTERM);
		return 1;
    }
	
    signal (SIGCHLD, sigchld_handler);
	
    signal (SIGTERM, termination_signal_handler);
    signal (SIGHUP, termination_signal_handler);
    signal (SIGINT, termination_signal_handler);
    signal (SIGQUIT, termination_signal_handler);

    if (setjmp (exit_continuation) == 0)
    {
		if (install_ipaddr_source ())
			CFRunLoopRun ();
		else
			while (1) pause ();
    }
	
    signal (SIGCHLD, SIG_IGN);

    if (client_pid >= 0) kill (client_pid, SIGTERM);
    if (server_pid >= 0) kill (server_pid, SIGTERM);
	
    if (auth_file != NULL)
    {
		/* Remove our Xauth keys */
		
		write_auth_file (FALSE);
    }
	
    free_auth_items ();
	
    return 0;
}
示例#11
0
Xauth *
XauGetBestAuthByAddr (
#if NeedWidePrototypes
    unsigned int	family,
    unsigned int	address_length,
#else
    unsigned short	family,
    unsigned short	address_length,
#endif
    _Xconst char*	address,
#if NeedWidePrototypes
    unsigned int	number_length,
#else
    unsigned short	number_length,
#endif
    _Xconst char*	number,
    int			types_length,
    char**		types,
    _Xconst int*	type_lengths)
{
    FILE    *auth_file;
    char    *auth_name;
    Xauth   *entry;
    Xauth   *best;
    int	    best_type;
    int	    type;

    auth_name = XauFileName ();
    if (!auth_name)
	return 0;
    if (access (auth_name, R_OK) != 0)		/* checks REAL id */
	return 0;
    auth_file = fopen (auth_name, "rb");
    if (!auth_file)
	return 0;


    best = 0;
    best_type = types_length;
    for (;;) {
	entry = XauReadAuth (auth_file);
	if (!entry)
	    break;
	/*
	 * Match when:
	 *   either family or entry->family are FamilyWild or
	 *    family and entry->family are the same and
	 *     address and entry->address are the same
	 *  and
	 *   either number or entry->number are empty or
	 *    number and entry->number are the same
	 *  and
	 *   either name or entry->name are empty or
	 *    name and entry->name are the same
	 */

	if ((family == FamilyWild || entry->family == FamilyWild ||
	     (entry->family == family &&
	     ((address_length == entry->address_length &&
	      binaryEqual (entry->address, address, (int)address_length))
	    ))) &&
	    (number_length == 0 || entry->number_length == 0 ||
	     (number_length == entry->number_length &&
	      binaryEqual (entry->number, number, (int)number_length))))
	{
	    if (best_type == 0)
	    {
		best = entry;
		break;
	    }
	    for (type = 0; type < best_type; type++)
		if (type_lengths[type] == entry->name_length &&
		    !(strncmp (types[type], entry->name, entry->name_length)))
		{
		    break;
		}
	    if (type < best_type)
	    {
		if (best)
		    XauDisposeAuth (best);
		best = entry;
		best_type = type;
		if (type == 0)
		    break;
		continue;
	    }
	}
	XauDisposeAuth (entry);
    }
    (void) fclose (auth_file);
    return best;
}