xauth.c revision 96402570
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 */ 5996402570Smrgstatic void 607a0395d0Smrgusage(void) 617a0395d0Smrg{ 6296402570Smrg static const char *prefixmsg[] = { 637a0395d0Smrg"", 647a0395d0Smrg"where options include:", 657a0395d0Smrg" -f authfilename name of authority file to use", 667a0395d0Smrg" -v turn on extra messages", 677a0395d0Smrg" -q turn off extra messages", 687a0395d0Smrg" -i ignore locks on authority file", 697a0395d0Smrg" -b break locks on authority file", 707a0395d0Smrg"", 717a0395d0Smrg"and commands have the following syntax:", 727a0395d0Smrg"", 737a0395d0SmrgNULL }; 7496402570Smrg static const char *suffixmsg[] = { 757a0395d0Smrg"A dash may be used with the \"merge\" and \"source\" to read from the", 767a0395d0Smrg"standard input. Commands beginning with \"n\" use numeric format.", 777a0395d0Smrg"", 787a0395d0SmrgNULL }; 7996402570Smrg const char **msg; 807a0395d0Smrg 817a0395d0Smrg fprintf (stderr, "usage: %s [-options ...] [command arg ...]\n", 827a0395d0Smrg ProgramName); 837a0395d0Smrg for (msg = prefixmsg; *msg; msg++) { 847a0395d0Smrg fprintf (stderr, "%s\n", *msg); 857a0395d0Smrg } 867a0395d0Smrg print_help (stderr, NULL, " "); /* match prefix indentation */ 877a0395d0Smrg fprintf (stderr, "\n"); 887a0395d0Smrg for (msg = suffixmsg; *msg; msg++) { 897a0395d0Smrg fprintf (stderr, "%s\n", *msg); 907a0395d0Smrg } 917a0395d0Smrg exit (1); 927a0395d0Smrg} 937a0395d0Smrg 947a0395d0Smrg 957a0395d0Smrg/* 967a0395d0Smrg * The main routine - parses command line and calls action procedures 977a0395d0Smrg */ 987a0395d0Smrgint 9996402570Smrgmain(int argc, const char *argv[]) 1007a0395d0Smrg{ 1017a0395d0Smrg int i; 10296402570Smrg const char *sourcename = defsource; 10396402570Smrg const char **arglist = defcmds; 1047a0395d0Smrg int nargs = ndefcmds; 1057a0395d0Smrg int status; 1067a0395d0Smrg 1077a0395d0Smrg ProgramName = argv[0]; 1087a0395d0Smrg 1097a0395d0Smrg for (i = 1; i < argc; i++) { 11096402570Smrg const char *arg = argv[i]; 1117a0395d0Smrg 1127a0395d0Smrg if (arg[0] == '-') { 11396402570Smrg const char *flag; 1147a0395d0Smrg 1157a0395d0Smrg for (flag = (arg + 1); *flag; flag++) { 1167a0395d0Smrg switch (*flag) { 1177a0395d0Smrg case 'f': /* -f authfilename */ 1187a0395d0Smrg if (++i >= argc) usage (); 1197a0395d0Smrg authfilename = argv[i]; 1207a0395d0Smrg continue; 1217a0395d0Smrg case 'v': /* -v */ 1227a0395d0Smrg verbose = 1; 1237a0395d0Smrg continue; 1247a0395d0Smrg case 'q': /* -q */ 1257a0395d0Smrg verbose = 0; 1267a0395d0Smrg continue; 1277a0395d0Smrg case 'b': /* -b */ 1287a0395d0Smrg break_locks = True; 1297a0395d0Smrg continue; 1307a0395d0Smrg case 'i': /* -i */ 1317a0395d0Smrg ignore_locks = True; 1327a0395d0Smrg continue; 1337a0395d0Smrg case 'n': /* -n */ 1347a0395d0Smrg no_name_lookups = True; 1357a0395d0Smrg continue; 1367a0395d0Smrg default: 1377a0395d0Smrg usage (); 1387a0395d0Smrg } 1397a0395d0Smrg } 1407a0395d0Smrg } else { 1417a0395d0Smrg sourcename = "(argv)"; 1427a0395d0Smrg nargs = argc - i; 1437a0395d0Smrg arglist = argv + i; 1447a0395d0Smrg if (verbose == -1) verbose = 0; 1457a0395d0Smrg break; 1467a0395d0Smrg } 1477a0395d0Smrg } 1487a0395d0Smrg 1497a0395d0Smrg if (verbose == -1) { /* set default, don't junk stdout */ 1507a0395d0Smrg verbose = (isatty(fileno(stdout)) != 0); 1517a0395d0Smrg } 1527a0395d0Smrg 1537a0395d0Smrg if (!authfilename) { 1547a0395d0Smrg authfilename = XauFileName (); /* static name, do not free */ 1557a0395d0Smrg if (!authfilename) { 1567a0395d0Smrg fprintf (stderr, 1577a0395d0Smrg "%s: unable to generate an authority file name\n", 1587a0395d0Smrg ProgramName); 1597a0395d0Smrg exit (1); 1607a0395d0Smrg } 1617a0395d0Smrg } 1627a0395d0Smrg if (auth_initialize (authfilename) != 0) { 1637a0395d0Smrg /* error message printed in auth_initialize */ 1647a0395d0Smrg exit (1); 1657a0395d0Smrg } 1667a0395d0Smrg 1677a0395d0Smrg status = process_command (sourcename, 1, nargs, arglist); 1687a0395d0Smrg 1697a0395d0Smrg (void) auth_finalize (); 1707a0395d0Smrg exit ((status != 0) ? 1 : 0); 1717a0395d0Smrg} 1727a0395d0Smrg 1737a0395d0Smrg 174