Example #1
0
File: SmAuth.c Project: juddy/edcde
static int
writeIceauth (
	int			nEntries,
	IceAuthDataEntry	*entries,
	int			restore)
{
    FILE *fp = NULL;
    char *path;
    char *extraPath;
    int oldUmask;
    int i;
    IceAuthDataEntry *dataEntry;
    IceAuthFileEntry *fileEntry;
    IceAuthFileEntry newEntry;
    IceAuthFileEntryList *fileEntryList = (IceAuthFileEntryList *)NULL;
    IceAuthFileEntryList *fileEntryP;

    if ((path = IceAuthFileName()) == (char *)NULL)
	return 0;

    if (IceLockAuthFile(path, AUTH_RETRIES, AUTH_TIMEOUT, AUTH_DEADTIME)
	!= IceAuthLockSuccess) {
	/*
	 * Let's try another PATH, in case IceLockAuthFile's call to
	 * link() fails.  This workaround code was taken from 
	 * dtlogin/auth.c.
	 */
        IceUnlockAuthFile(path);
	extraPath = XtMalloc (MAXPATHLEN);
	(void) strcpy (extraPath, CDE_CONFIGURATION_TOP ".ICEauthority");
	if (IceLockAuthFile(extraPath, AUTH_RETRIES, AUTH_TIMEOUT, AUTH_DEADTIME)
		!= IceAuthLockSuccess) {
	    IceUnlockAuthFile (extraPath);
	    return 0;
	 }
	 path = extraPath;
    }

    /* If file exists, read entries into memory. */
    if (access(path, F_OK) == 0)
    {
	if ((fp = fopen(path, "rb")) == (FILE *)NULL)
	{
	    IceUnlockAuthFile(path);
	    return 0;
	}

	/* For each file entry: if matches something in entries, discard. */
	/* Otherwise, hold onto it - we'll be writing it back to file. */
	while ((fileEntry = IceReadAuthFileEntry(fp))
	       != (IceAuthFileEntry *)NULL)
	{
	    if (!fileEntryInDataEntries(nEntries, entries, fileEntry) &&
		!addToEntryList(&fileEntryList, fileEntry))
	    {
		freeEntryList(fileEntryList);
		IceUnlockAuthFile(path);
		fclose(fp);
		return 0;
	    }
	}

	fclose(fp);
    }

    /* Set umask to disallow non-owner access. */
    oldUmask = umask(0077);

    /* Write entries and fileEntryList to file. */
    if ((fp = fopen(path, "wb")) == (FILE *)NULL)
    {
	freeEntryList(fileEntryList);
	IceUnlockAuthFile(path);
	umask(oldUmask);
	return 0;
    }

    for (fileEntryP = fileEntryList;
	 fileEntryP != (IceAuthFileEntryList *)NULL;
	 fileEntryP = fileEntryP->next)
    {
	if (IceWriteAuthFileEntry(fp, fileEntryP->fileEntry) == 0)
	{
	    fclose(fp);
	    umask(oldUmask);
	    freeEntryList(fileEntryList);
	    IceUnlockAuthFile(path);
	    return 0;
	}
    }

    /* Done with fileEntryList - free it up. */
    freeEntryList(fileEntryList);

    if (!restore)
    {
	for (i = 0; i < nEntries; i++)
	{
	    dataEntry = &(entries[i]);
	    newEntry.protocol_name = dataEntry->protocol_name;
	    newEntry.protocol_data_length = 0;
	    newEntry.protocol_data = "";
	    newEntry.network_id = dataEntry->network_id;
	    newEntry.auth_name = dataEntry->auth_name;
	    newEntry.auth_data_length = dataEntry->auth_data_length;
	    newEntry.auth_data = dataEntry->auth_data;

	    if (IceWriteAuthFileEntry(fp, &newEntry) == 0)
	    {
		fclose(fp);
		umask(oldUmask);
		IceUnlockAuthFile(path);
		return 0;
	    }
	}
    }

    /* Success! */
    fclose(fp);
    umask(oldUmask);
    IceUnlockAuthFile(path);
    return 1;
}
Example #2
0
/*
 * The main routine - parses command line and calls action procedures
 */
int
main (int argc, char *argv[])
{
    int i;
    const char *sourcename = defsource;
    char **arglist = defcmds;
    int nargs = ndefcmds;
    int status;

    ProgramName = argv[0];

    for (i = 1; i < argc; i++) {
	const char *arg = argv[i];

	if (arg[0] == '-') {
	    const char *flag;

	    for (flag = (arg + 1); *flag; flag++) {
		switch (*flag) {
		  case 'f':		/* -f authfilename */
		    if (++i >= argc) usage ();
		    authfilename = argv[i];
		    continue;
		  case 'v':		/* -v */
		    verbose = 1;
		    continue;
		  case 'q':		/* -q */
		    verbose = 0;
		    continue;
		  case 'b':		/* -b */
		    break_locks = True;
		    continue;
		  case 'i':		/* -i */
		    ignore_locks = True;
		    continue;
		  default:
		    usage ();
		}
	    }
	} else {
	    sourcename = "(argv)";
	    nargs = argc - i;
	    arglist = argv + i;
	    if (verbose == -1) verbose = 0;
	    break;
	}
    }

    if (verbose == -1) {		/* set default, don't junk stdout */
	verbose = (isatty(fileno(stdout)) != 0);
    }

    if (!authfilename) {
	authfilename = IceAuthFileName ();	/* static name, do not free */
	if (!authfilename) {
	    fprintf (stderr,
		     "%s:  unable to generate an authority file name\n",
		     ProgramName);
	    exit (1);
	}
    }
    if (auth_initialize (authfilename) != 0) {
	/* error message printed in auth_initialize */
	exit (1);
    }

    status = process_command (sourcename, 1, nargs, arglist);

    (void) auth_finalize ();
    exit ((status != 0) ? 1 : 0);
}