17a0395d0Smrg/*
27a0395d0Smrg *
37a0395d0Smrg * xauth - manipulate authorization file
47a0395d0Smrg *
596402570Smrg *
67a0395d0SmrgCopyright 1989,1998  The Open Group
77a0395d0Smrg
87a0395d0SmrgPermission to use, copy, modify, distribute, and sell this software and its
97a0395d0Smrgdocumentation for any purpose is hereby granted without fee, provided that
107a0395d0Smrgthe above copyright notice appear in all copies and that both that
117a0395d0Smrgcopyright notice and this permission notice appear in supporting
127a0395d0Smrgdocumentation.
137a0395d0Smrg
147a0395d0SmrgThe above copyright notice and this permission notice shall be included in
157a0395d0Smrgall copies or substantial portions of the Software.
167a0395d0Smrg
177a0395d0SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187a0395d0SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197a0395d0SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
207a0395d0SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
217a0395d0SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
227a0395d0SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237a0395d0Smrg
247a0395d0SmrgExcept as contained in this notice, the name of The Open Group shall not be
257a0395d0Smrgused in advertising or otherwise to promote the sale, use or other dealings
267a0395d0Smrgin this Software without prior written authorization from The Open Group.
277a0395d0Smrg * *
287a0395d0Smrg * Author:  Jim Fulton, MIT X Consortium
297a0395d0Smrg */
307a0395d0Smrg
317a0395d0Smrg#ifdef HAVE_CONFIG_H
327a0395d0Smrg#include "config.h"
337a0395d0Smrg#endif
347a0395d0Smrg
357a0395d0Smrg#include "xauth.h"
367a0395d0Smrg
377a0395d0Smrg
387a0395d0Smrg/*
397a0395d0Smrg * global data
407a0395d0Smrg */
4196402570Smrgconst char *ProgramName;		/* argv[0], set at top of main() */
427a0395d0Smrgint verbose = -1;			/* print certain messages */
437a0395d0SmrgBool ignore_locks = False;		/* for error recovery */
447a0395d0SmrgBool break_locks = False;		/* for error recovery */
457a0395d0SmrgBool no_name_lookups = False;		/* show addresses instead of names */
467a0395d0Smrg
477a0395d0Smrg/*
487a0395d0Smrg * local data
497a0395d0Smrg */
507a0395d0Smrg
5196402570Smrgstatic const char *authfilename = NULL;	/* filename of cookie file */
5296402570Smrgstatic const char *defcmds[] = { "source", "-", NULL };  /* default command */
537a0395d0Smrgstatic int ndefcmds = 2;
5496402570Smrgstatic const char *defsource = "(stdin)";
557a0395d0Smrg
567a0395d0Smrg/*
577a0395d0Smrg * utility routines
587a0395d0Smrg */
598abc0ccfSmrg_X_NORETURN
6096402570Smrgstatic void
617a0395d0Smrgusage(void)
627a0395d0Smrg{
6396402570Smrg    static const char *prefixmsg[] = {
647a0395d0Smrg"",
657a0395d0Smrg"where options include:",
667a0395d0Smrg"    -f authfilename                name of authority file to use",
677a0395d0Smrg"    -v                             turn on extra messages",
687a0395d0Smrg"    -q                             turn off extra messages",
697a0395d0Smrg"    -i                             ignore locks on authority file",
707a0395d0Smrg"    -b                             break locks on authority file",
7140c5344fSmrg"    -n                             do not resolve host names in authority file",
728abc0ccfSmrg"    -V                             show version number of xauth",
737a0395d0Smrg"",
747a0395d0Smrg"and commands have the following syntax:",
757a0395d0Smrg"",
767a0395d0SmrgNULL };
7796402570Smrg    static const char *suffixmsg[] = {
787a0395d0Smrg"A dash may be used with the \"merge\" and \"source\" to read from the",
797a0395d0Smrg"standard input.  Commands beginning with \"n\" use numeric format.",
807a0395d0Smrg"",
817a0395d0SmrgNULL };
827a0395d0Smrg
837a0395d0Smrg    fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
847a0395d0Smrg	     ProgramName);
85273c00b8Smrg    for (const char **msg = prefixmsg; *msg; msg++) {
867a0395d0Smrg	fprintf (stderr, "%s\n", *msg);
877a0395d0Smrg    }
887a0395d0Smrg    print_help (stderr, NULL, "    ");	/* match prefix indentation */
897a0395d0Smrg    fprintf (stderr, "\n");
90273c00b8Smrg    for (const char **msg = suffixmsg; *msg; msg++) {
917a0395d0Smrg	fprintf (stderr, "%s\n", *msg);
927a0395d0Smrg    }
937a0395d0Smrg    exit (1);
947a0395d0Smrg}
957a0395d0Smrg
967a0395d0Smrg
977a0395d0Smrg/*
987a0395d0Smrg * The main routine - parses command line and calls action procedures
997a0395d0Smrg */
1007a0395d0Smrgint
10196402570Smrgmain(int argc, const char *argv[])
1027a0395d0Smrg{
10396402570Smrg    const char *sourcename = defsource;
10496402570Smrg    const char **arglist = defcmds;
1057a0395d0Smrg    int nargs = ndefcmds;
1067a0395d0Smrg    int status;
1077a0395d0Smrg
1087a0395d0Smrg    ProgramName = argv[0];
1097a0395d0Smrg
110273c00b8Smrg    for (int i = 1; i < argc; i++) {
11196402570Smrg	const char *arg = argv[i];
1127a0395d0Smrg
1137a0395d0Smrg	if (arg[0] == '-') {
114273c00b8Smrg	    for (const char *flag = (arg + 1); *flag; flag++) {
1158abc0ccfSmrg	        switch (*flag) {
1167a0395d0Smrg		  case 'f':		/* -f authfilename */
1177a0395d0Smrg		    if (++i >= argc) usage ();
1187a0395d0Smrg		    authfilename = argv[i];
1197a0395d0Smrg		    continue;
1207a0395d0Smrg		  case 'v':		/* -v */
1217a0395d0Smrg		    verbose = 1;
1227a0395d0Smrg		    continue;
1237a0395d0Smrg		  case 'q':		/* -q */
1247a0395d0Smrg		    verbose = 0;
1257a0395d0Smrg		    continue;
1267a0395d0Smrg		  case 'b':		/* -b */
1277a0395d0Smrg		    break_locks = True;
1287a0395d0Smrg		    continue;
1297a0395d0Smrg		  case 'i':		/* -i */
1307a0395d0Smrg		    ignore_locks = True;
1317a0395d0Smrg		    continue;
1327a0395d0Smrg		  case 'n':		/* -n */
1337a0395d0Smrg		    no_name_lookups = True;
1347a0395d0Smrg		    continue;
1358abc0ccfSmrg		  case 'V':		/* -V */
1368abc0ccfSmrg		    puts(PACKAGE_VERSION);
1378abc0ccfSmrg	   	    exit(0);
1387a0395d0Smrg		  default:
1397a0395d0Smrg		    usage ();
1408abc0ccfSmrg	        }
1417a0395d0Smrg	    }
1427a0395d0Smrg	} else {
1437a0395d0Smrg	    sourcename = "(argv)";
1447a0395d0Smrg	    nargs = argc - i;
1457a0395d0Smrg	    arglist = argv + i;
1467a0395d0Smrg	    if (verbose == -1) verbose = 0;
1477a0395d0Smrg	    break;
1487a0395d0Smrg	}
1497a0395d0Smrg    }
1507a0395d0Smrg
1517a0395d0Smrg    if (verbose == -1) {		/* set default, don't junk stdout */
1527a0395d0Smrg	verbose = (isatty(fileno(stdout)) != 0);
1537a0395d0Smrg    }
1547a0395d0Smrg
1557a0395d0Smrg    if (!authfilename) {
1567a0395d0Smrg	authfilename = XauFileName ();	/* static name, do not free */
1577a0395d0Smrg	if (!authfilename) {
1587a0395d0Smrg	    fprintf (stderr,
1597a0395d0Smrg		     "%s:  unable to generate an authority file name\n",
1607a0395d0Smrg		     ProgramName);
1617a0395d0Smrg	    exit (1);
1627a0395d0Smrg	}
1637a0395d0Smrg    }
1647a0395d0Smrg    if (auth_initialize (authfilename) != 0) {
1657a0395d0Smrg	/* error message printed in auth_initialize */
1667a0395d0Smrg	exit (1);
1677a0395d0Smrg    }
1687a0395d0Smrg
1697a0395d0Smrg    status = process_command (sourcename, 1, nargs, arglist);
1707a0395d0Smrg
1717a0395d0Smrg    (void) auth_finalize ();
1727a0395d0Smrg    exit ((status != 0) ? 1 : 0);
1737a0395d0Smrg}
1747a0395d0Smrg
1757a0395d0Smrg
176