xauth.c revision 8abc0ccf
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",
718abc0ccfSmrg"    -V                             show version number of xauth",
727a0395d0Smrg"",
737a0395d0Smrg"and commands have the following syntax:",
747a0395d0Smrg"",
757a0395d0SmrgNULL };
7696402570Smrg    static const char *suffixmsg[] = {
777a0395d0Smrg"A dash may be used with the \"merge\" and \"source\" to read from the",
787a0395d0Smrg"standard input.  Commands beginning with \"n\" use numeric format.",
797a0395d0Smrg"",
807a0395d0SmrgNULL };
8196402570Smrg    const char **msg;
827a0395d0Smrg
837a0395d0Smrg    fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
847a0395d0Smrg	     ProgramName);
857a0395d0Smrg    for (msg = prefixmsg; *msg; msg++) {
867a0395d0Smrg	fprintf (stderr, "%s\n", *msg);
877a0395d0Smrg    }
887a0395d0Smrg    print_help (stderr, NULL, "    ");	/* match prefix indentation */
897a0395d0Smrg    fprintf (stderr, "\n");
907a0395d0Smrg    for (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{
1037a0395d0Smrg    int i;
10496402570Smrg    const char *sourcename = defsource;
10596402570Smrg    const char **arglist = defcmds;
1067a0395d0Smrg    int nargs = ndefcmds;
1077a0395d0Smrg    int status;
1087a0395d0Smrg
1097a0395d0Smrg    ProgramName = argv[0];
1107a0395d0Smrg
1117a0395d0Smrg    for (i = 1; i < argc; i++) {
11296402570Smrg	const char *arg = argv[i];
1137a0395d0Smrg
1147a0395d0Smrg	if (arg[0] == '-') {
11596402570Smrg	    const char *flag;
1167a0395d0Smrg	    for (flag = (arg + 1); *flag; flag++) {
1178abc0ccfSmrg	        switch (*flag) {
1187a0395d0Smrg		  case 'f':		/* -f authfilename */
1197a0395d0Smrg		    if (++i >= argc) usage ();
1207a0395d0Smrg		    authfilename = argv[i];
1217a0395d0Smrg		    continue;
1227a0395d0Smrg		  case 'v':		/* -v */
1237a0395d0Smrg		    verbose = 1;
1247a0395d0Smrg		    continue;
1257a0395d0Smrg		  case 'q':		/* -q */
1267a0395d0Smrg		    verbose = 0;
1277a0395d0Smrg		    continue;
1287a0395d0Smrg		  case 'b':		/* -b */
1297a0395d0Smrg		    break_locks = True;
1307a0395d0Smrg		    continue;
1317a0395d0Smrg		  case 'i':		/* -i */
1327a0395d0Smrg		    ignore_locks = True;
1337a0395d0Smrg		    continue;
1347a0395d0Smrg		  case 'n':		/* -n */
1357a0395d0Smrg		    no_name_lookups = True;
1367a0395d0Smrg		    continue;
1378abc0ccfSmrg		  case 'V':		/* -V */
1388abc0ccfSmrg		    puts(PACKAGE_VERSION);
1398abc0ccfSmrg	   	    exit(0);
1407a0395d0Smrg		  default:
1417a0395d0Smrg		    usage ();
1428abc0ccfSmrg	        }
1437a0395d0Smrg	    }
1447a0395d0Smrg	} else {
1457a0395d0Smrg	    sourcename = "(argv)";
1467a0395d0Smrg	    nargs = argc - i;
1477a0395d0Smrg	    arglist = argv + i;
1487a0395d0Smrg	    if (verbose == -1) verbose = 0;
1497a0395d0Smrg	    break;
1507a0395d0Smrg	}
1517a0395d0Smrg    }
1527a0395d0Smrg
1537a0395d0Smrg    if (verbose == -1) {		/* set default, don't junk stdout */
1547a0395d0Smrg	verbose = (isatty(fileno(stdout)) != 0);
1557a0395d0Smrg    }
1567a0395d0Smrg
1577a0395d0Smrg    if (!authfilename) {
1587a0395d0Smrg	authfilename = XauFileName ();	/* static name, do not free */
1597a0395d0Smrg	if (!authfilename) {
1607a0395d0Smrg	    fprintf (stderr,
1617a0395d0Smrg		     "%s:  unable to generate an authority file name\n",
1627a0395d0Smrg		     ProgramName);
1637a0395d0Smrg	    exit (1);
1647a0395d0Smrg	}
1657a0395d0Smrg    }
1667a0395d0Smrg    if (auth_initialize (authfilename) != 0) {
1677a0395d0Smrg	/* error message printed in auth_initialize */
1687a0395d0Smrg	exit (1);
1697a0395d0Smrg    }
1707a0395d0Smrg
1717a0395d0Smrg    status = process_command (sourcename, 1, nargs, arglist);
1727a0395d0Smrg
1737a0395d0Smrg    (void) auth_finalize ();
1747a0395d0Smrg    exit ((status != 0) ? 1 : 0);
1757a0395d0Smrg}
1767a0395d0Smrg
1777a0395d0Smrg
178