19fe995a9Smrg/*
29fe995a9Smrg * xauth - manipulate authorization file
39fe995a9Smrg *
49fe995a9Smrg *
59fe995a9SmrgCopyright 1989, 1998  The Open Group
69fe995a9Smrg
79fe995a9SmrgPermission to use, copy, modify, distribute, and sell this software and its
89fe995a9Smrgdocumentation for any purpose is hereby granted without fee, provided that
99fe995a9Smrgthe above copyright notice appear in all copies and that both that
109fe995a9Smrgcopyright notice and this permission notice appear in supporting
119fe995a9Smrgdocumentation.
129fe995a9Smrg
139fe995a9SmrgThe above copyright notice and this permission notice shall be included in
149fe995a9Smrgall copies or substantial portions of the Software.
159fe995a9Smrg
169fe995a9SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
179fe995a9SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
189fe995a9SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
199fe995a9SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
209fe995a9SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
219fe995a9SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
229fe995a9Smrg
239fe995a9SmrgExcept as contained in this notice, the name of The Open Group shall not be
249fe995a9Smrgused in advertising or otherwise to promote the sale, use or other dealings
259fe995a9Smrgin this Software without prior written authorization from The Open Group.
269fe995a9Smrg * *
279fe995a9Smrg * Original Author of "xauth" : Jim Fulton, MIT X Consortium
289fe995a9Smrg * Modified into "iceauth"    : Ralph Mor, X Consortium
299fe995a9Smrg */
309fe995a9Smrg
319fe995a9Smrg#include "iceauth.h"
329fe995a9Smrg
339fe995a9Smrg
349fe995a9Smrg/*
359fe995a9Smrg * global data
369fe995a9Smrg */
379fe995a9Smrgconst char *ProgramName;		/* argv[0], set at top of main() */
389fe995a9Smrgint verbose = -1;			/* print certain messages */
399fe995a9SmrgBool ignore_locks = False;		/* for error recovery */
409fe995a9SmrgBool break_locks = False;		/* for error recovery */
419fe995a9Smrg
429fe995a9Smrg/*
439fe995a9Smrg * local data
449fe995a9Smrg */
459fe995a9Smrg
469fe995a9Smrgstatic char *authfilename = NULL;	/* filename of cookie file */
47e8ac26b0Smrgstatic const char *defcmds[] = { "source", "-", NULL };  /* default command */
489fe995a9Smrgstatic int ndefcmds = 2;
499fe995a9Smrgstatic const char *defsource = "(stdin)";
509fe995a9Smrg
519fe995a9Smrg
529fe995a9Smrg/*
539fe995a9Smrg * utility routines
549fe995a9Smrg */
559d794632Smrgstatic void _X_NORETURN
56e8ac26b0Smrgusage (int exitcode)
579fe995a9Smrg{
589fe995a9Smrg    static const char prefixmsg[] =
599fe995a9Smrg"\n"
609fe995a9Smrg"where options include:\n"
619fe995a9Smrg"    -f authfilename                name of authority file to use\n"
629fe995a9Smrg"    -v                             turn on extra messages\n"
639fe995a9Smrg"    -q                             turn off extra messages\n"
649fe995a9Smrg"    -i                             ignore locks on authority file\n"
659fe995a9Smrg"    -b                             break locks on authority file\n"
66e8ac26b0Smrg"    -V                             print version and exit\n"
679fe995a9Smrg"\n"
689fe995a9Smrg"and commands have the following syntax:\n";
699fe995a9Smrg    static const char suffixmsg[] =
709fe995a9Smrg"A dash may be used with the \"merge\" and \"source\" to read from the\n"
719fe995a9Smrg"standard input.  Commands beginning with \"n\" use numeric format.\n";
729fe995a9Smrg
739fe995a9Smrg    fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
749fe995a9Smrg	     ProgramName);
75e8ac26b0Smrg    fprintf (stderr, "%s", prefixmsg);
76e8ac26b0Smrg    print_help (stderr, NULL);
779fe995a9Smrg    fprintf (stderr, "\n%s\n", suffixmsg);
78e8ac26b0Smrg    exit (exitcode);
799fe995a9Smrg}
809fe995a9Smrg
819fe995a9Smrg
829fe995a9Smrg/*
839fe995a9Smrg * The main routine - parses command line and calls action procedures
849fe995a9Smrg */
859fe995a9Smrgint
869fe995a9Smrgmain (int argc, char *argv[])
879fe995a9Smrg{
889fe995a9Smrg    const char *sourcename = defsource;
89e8ac26b0Smrg    const char **arglist = defcmds;
909fe995a9Smrg    int nargs = ndefcmds;
919fe995a9Smrg    int status;
929fe995a9Smrg
939fe995a9Smrg    ProgramName = argv[0];
949fe995a9Smrg
95772b5186Smrg    for (int i = 1; i < argc; i++) {
969d794632Smrg	const char *arg = argv[i];
979fe995a9Smrg
989fe995a9Smrg	if (arg[0] == '-') {
99772b5186Smrg	    for (const char *flag = (arg + 1); *flag; flag++) {
1009fe995a9Smrg		switch (*flag) {
1019fe995a9Smrg		  case 'f':		/* -f authfilename */
102e8ac26b0Smrg		    if (++i >= argc) {
103e8ac26b0Smrg			fprintf(stderr, "%s: -f requires an argument\n",
104e8ac26b0Smrg				ProgramName);
105e8ac26b0Smrg			usage (1);
106e8ac26b0Smrg		    }
1079fe995a9Smrg		    authfilename = argv[i];
1089fe995a9Smrg		    continue;
109e8ac26b0Smrg		  case 'V':		/* -V */
110e8ac26b0Smrg		    printf("%s\n", PACKAGE_STRING);
111e8ac26b0Smrg		    exit(0);
1129fe995a9Smrg		  case 'v':		/* -v */
1139fe995a9Smrg		    verbose = 1;
1149fe995a9Smrg		    continue;
1159fe995a9Smrg		  case 'q':		/* -q */
1169fe995a9Smrg		    verbose = 0;
1179fe995a9Smrg		    continue;
1189fe995a9Smrg		  case 'b':		/* -b */
1199fe995a9Smrg		    break_locks = True;
1209fe995a9Smrg		    continue;
1219fe995a9Smrg		  case 'i':		/* -i */
1229fe995a9Smrg		    ignore_locks = True;
1239fe995a9Smrg		    continue;
124e8ac26b0Smrg		  case 'u':		/* -u */
125e8ac26b0Smrg		    usage (0);
1269fe995a9Smrg		  default:
127e8ac26b0Smrg		    fprintf(stderr, "%s: unrecognized option '%s'\n",
128e8ac26b0Smrg			    ProgramName, flag);
129e8ac26b0Smrg		    usage (1);
1309fe995a9Smrg		}
1319fe995a9Smrg	    }
1329fe995a9Smrg	} else {
1339fe995a9Smrg	    sourcename = "(argv)";
1349fe995a9Smrg	    nargs = argc - i;
135e8ac26b0Smrg	    arglist = (const char **) argv + i;
1369fe995a9Smrg	    if (verbose == -1) verbose = 0;
1379fe995a9Smrg	    break;
1389fe995a9Smrg	}
1399fe995a9Smrg    }
1409fe995a9Smrg
1419fe995a9Smrg    if (verbose == -1) {		/* set default, don't junk stdout */
1429fe995a9Smrg	verbose = (isatty(fileno(stdout)) != 0);
1439fe995a9Smrg    }
1449fe995a9Smrg
1459fe995a9Smrg    if (!authfilename) {
1469fe995a9Smrg	authfilename = IceAuthFileName ();	/* static name, do not free */
1479fe995a9Smrg	if (!authfilename) {
1489fe995a9Smrg	    fprintf (stderr,
1499fe995a9Smrg		     "%s:  unable to generate an authority file name\n",
1509fe995a9Smrg		     ProgramName);
1519fe995a9Smrg	    exit (1);
1529fe995a9Smrg	}
1539fe995a9Smrg    }
1549fe995a9Smrg    if (auth_initialize (authfilename) != 0) {
1559fe995a9Smrg	/* error message printed in auth_initialize */
1569fe995a9Smrg	exit (1);
1579fe995a9Smrg    }
1589fe995a9Smrg
1599fe995a9Smrg    status = process_command (sourcename, 1, nargs, arglist);
1609fe995a9Smrg
1619fe995a9Smrg    (void) auth_finalize ();
1629fe995a9Smrg    exit ((status != 0) ? 1 : 0);
1639fe995a9Smrg}
164