home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sources / unix / 279 < prev    next >
Encoding:
Text File  |  1992-12-27  |  73.6 KB  |  2,989 lines

  1. Path: sparky!uunet!paladin.american.edu!gatech!concert!decwrl!pa.dec.com!vixie
  2. From: koblas@mips.com (David Koblas)
  3. Newsgroups: comp.sources.unix
  4. Subject: v26i080: op - a tool to allow customizable super user access, Part01/01
  5. Date: 27 Dec 1992 23:11:08 GMT
  6. Organization: Digital Equipment Corporation Palo Alto, CA
  7. Lines: 2975
  8. Sender: unix-sources-moderator@pa.dec.com
  9. Approved: vixie@pa.dec.com
  10. Message-ID: <1hld6cINN81i@usenet.pa.dec.com>
  11. NNTP-Posting-Host: cognition.pa.dec.com
  12. Originator: vixie@cognition.pa.dec.com
  13.  
  14. Submitted-By: koblas@mips.com (David Koblas)
  15. Posting-Number: Volume 26, Issue 80
  16. Archive-Name: op/part01
  17.  
  18. 'op' is a tool designed to allow customizable super user access,
  19. you can do everthing from emulating giving a super user shell
  20. for nothing to only allowing one or two users access via login
  21. names, or special passwords that are neither root, nor their own.
  22.  
  23. Plus, as an added bonus, for those commands that you would like
  24. users to be able to use, but need to place restrictions on the
  25. arguments, you can configure that as well.  (ie. if you want
  26. your users to be able to mount NFS file systems).
  27.  
  28. Op was orginally developed at Convex by Tom Christiansen, and
  29. subsequently publised in a USENEX LISA procedings.  This version
  30. was developed entierly from the description that was published.
  31.  
  32. This release is the first official release of 'op', for those of you
  33. who have the alpha release, only two tiny fixes have been made.  
  34. It has been sudgested that I get the syslog logging to conform to
  35. the Convex version, but this will follow in a latter patch.
  36.  
  37. If you have any problems or find any bugs, please report them to:
  38.  
  39.             op-bugs@mips.com
  40.  
  41. Enjoy,
  42.  
  43. David Koblas            (koblas@mips.com)
  44.  
  45. #! /bin/sh
  46. # This is a shell archive.  Remove anything before this line, then unpack
  47. # it by saving it into a file and typing "sh file".  To overwrite existing
  48. # files, type "sh file -c".  You can also feed this as standard input via
  49. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  50. # will see the following message at the end:
  51. #        "End of archive 1 (of 1)."
  52. # Contents:  MANIFEST Makefile README atov.c defs.h lex.l main.c op.1
  53. #   op.access regerror.c regexp.c regexp.h regmagic.h regsub.c
  54. #   vfprintf.c
  55. # Wrapped by vixie@cognition.pa.dec.com on Sun Dec 27 15:09:10 1992
  56. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  57. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  58.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  59. else
  60. echo shar: Extracting \"'MANIFEST'\" \(579 characters\)
  61. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  62. X   File Name        Archive #    Description
  63. X-----------------------------------------------------------
  64. X MANIFEST                   1    This shipping list
  65. X Makefile                   1    
  66. X README                     1    
  67. X atov.c                     1    
  68. X defs.h                     1    
  69. X lex.l                      1    
  70. X main.c                     1    
  71. X op.1                       1    
  72. X op.access                  1    
  73. X regerror.c                 1    
  74. X regexp.c                   1    
  75. X regexp.h                   1    
  76. X regmagic.h                 1    
  77. X regsub.c                   1    
  78. X vfprintf.c                 1    
  79. END_OF_FILE
  80. if test 579 -ne `wc -c <'MANIFEST'`; then
  81.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  82. fi
  83. # end of 'MANIFEST'
  84. fi
  85. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  86.   echo shar: Will not clobber existing file \"'Makefile'\"
  87. else
  88. echo shar: Extracting \"'Makefile'\" \(346 characters\)
  89. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  90. CFLAGS=-g -I.
  91. X
  92. VPF = vfprintf.o
  93. REG = regexp.o regsub.o regerror.o
  94. OBJ = lex.o main.o atov.o $(REG) $(VPF)
  95. X
  96. all: op
  97. X
  98. op: $(OBJ) $(REG)
  99. X    $(CC) $(CFLAGS) -o $@ $(OBJ) -ll
  100. X
  101. clean:
  102. X    rm -f $(OBJ) op core 
  103. X
  104. kit:
  105. X    @makekit README \
  106. X        atov.c defs.h lex.l main.c op.1 \
  107. X        op.access regerror.c regexp.c regexp.h regmagic.h \
  108. X        regsub.c vfprintf.c Makefile
  109. END_OF_FILE
  110. if test 346 -ne `wc -c <'Makefile'`; then
  111.     echo shar: \"'Makefile'\" unpacked with wrong size!
  112. fi
  113. # end of 'Makefile'
  114. fi
  115. if test -f 'README' -a "${1}" != "-c" ; then 
  116.   echo shar: Will not clobber existing file \"'README'\"
  117. else
  118. echo shar: Extracting \"'README'\" \(1079 characters\)
  119. sed "s/^X//" >'README' <<'END_OF_FILE'
  120. X'op' is a tool designed to allow customizable super user access,
  121. you can do everthing from emulating giving a super user shell
  122. for nothing to only allowing one or two users access via login
  123. names, or special passwords that are neither root, nor their own.
  124. X
  125. Plus, as an added bonus, for those commands that you would like
  126. users to be able to use, but need to place restrictions on the
  127. arguments, you can configure that as well.  (ie. if you want
  128. your users to be able to mount NFS file systems).
  129. X
  130. Op was orginally developed at Convex by Tom Christiansen, and
  131. subsequently publised in a USENEX LISA procedings.  This version
  132. was developed entierly from the description that was published.
  133. X
  134. This release is the first official release of 'op', for those of you
  135. who have the alpha release, only two tiny fixes have been made.  
  136. It has been sudgested that I get the syslog logging to conform to
  137. the Convex version, but this will follow in a latter patch.
  138. X
  139. If you have any problems or find any bugs, please report them to:
  140. X
  141. X            op-bugs@mips.com
  142. X
  143. XEnjoy,
  144. X
  145. David Koblas            (koblas@mips.com)
  146. END_OF_FILE
  147. if test 1079 -ne `wc -c <'README'`; then
  148.     echo shar: \"'README'\" unpacked with wrong size!
  149. fi
  150. # end of 'README'
  151. fi
  152. if test -f 'atov.c' -a "${1}" != "-c" ; then 
  153.   echo shar: Will not clobber existing file \"'atov.c'\"
  154. else
  155. echo shar: Extracting \"'atov.c'\" \(1561 characters\)
  156. sed "s/^X//" >'atov.c' <<'END_OF_FILE'
  157. X/* +-------------------------------------------------------------------+ */
  158. X/* | Copyright 1988,1991, David Koblas.                                | */
  159. X/* |   Permission to use, copy, modify, and distribute this software   | */
  160. X/* |   and its documentation for any purpose and without fee is hereby | */
  161. X/* |   granted, provided that the above copyright notice appear in all | */
  162. X/* |   copies and that both that copyright notice and this permission  | */
  163. X/* |   notice appear in supporting documentation.  This software is    | */
  164. X/* |   provided "as is" without express or implied warranty.           | */
  165. X/* +-------------------------------------------------------------------+ */
  166. X
  167. X#include    <ctype.h>
  168. X
  169. X#ifdef TEST
  170. main(argc,argv)
  171. int    argc;
  172. char    **argv;
  173. X{
  174. X    int    i;
  175. X    for (i=1;i<argc;i++)
  176. X        printf("%10s  == %d\n",argv[i],atov(argv[i],0));
  177. X}
  178. X#endif
  179. X
  180. atov(str,type)
  181. char    *str;
  182. int    type;
  183. X{
  184. X    int        sign = 1;
  185. X    int        i;
  186. X    char        c;
  187. X    int        val=0,n;
  188. X
  189. X    i=0;
  190. X    while ((str[i]==' ') || (str[i]=='\t')) i++;
  191. X    if (str[i]=='-')  {
  192. X        sign = -1;
  193. X        i++;
  194. X    } else if (str[i]=='+') {
  195. X        sign = 1;
  196. X        i++;
  197. X    }
  198. X    if (type==0)  {
  199. X        if (str[i]=='0') {
  200. X            i++;
  201. X            if (str[i]=='%') {
  202. X                i++;
  203. X                type=2;
  204. X            } else if (str[i]=='x') {
  205. X                i++;
  206. X                type=16;
  207. X            } else {
  208. X                type=8;
  209. X            }
  210. X        } else {
  211. X            type=10;
  212. X        }
  213. X    }
  214. X    for (;i<strlen(str);i++) {
  215. X        c=str[i];
  216. X        if (isdigit(c)) {
  217. X            n = c - '0';
  218. X        } else if (isupper(c)) {
  219. X            n = c - 'A' + 10;
  220. X        } else if (islower(c)) {
  221. X            n = c - 'a' + 10;
  222. X        } else {
  223. X            goto    out;
  224. X        }
  225. X        if (n>=type)
  226. X            goto out;
  227. X        val = (val*type)+n;
  228. X    }
  229. out:    
  230. X    return(val * sign);
  231. X}
  232. END_OF_FILE
  233. if test 1561 -ne `wc -c <'atov.c'`; then
  234.     echo shar: \"'atov.c'\" unpacked with wrong size!
  235. fi
  236. # end of 'atov.c'
  237. fi
  238. if test -f 'defs.h' -a "${1}" != "-c" ; then 
  239.   echo shar: Will not clobber existing file \"'defs.h'\"
  240. else
  241. echo shar: Extracting \"'defs.h'\" \(870 characters\)
  242. sed "s/^X//" >'defs.h' <<'END_OF_FILE'
  243. X/* +-------------------------------------------------------------------+ */
  244. X/* | Copyright 1991, David Koblas.                                     | */
  245. X/* |   Permission to use, copy, modify, and distribute this software   | */
  246. X/* |   and its documentation for any purpose and without fee is hereby | */
  247. X/* |   granted, provided that the above copyright notice appear in all | */
  248. X/* |   copies and that both that copyright notice and this permission  | */
  249. X/* |   notice appear in supporting documentation.  This software is    | */
  250. X/* |   provided "as is" without express or implied warranty.           | */
  251. X/* +-------------------------------------------------------------------+ */
  252. X
  253. typedef struct cmd_s {
  254. X    char        *name;
  255. X    int        nargs, nopts;
  256. X    int        margs, mopts;
  257. X    char        **args, **opts;
  258. X    struct cmd_s    *next;
  259. X} cmd_t;
  260. X
  261. extern cmd_t    *First, *Build();
  262. X
  263. X#define MAXSTRLEN    256
  264. END_OF_FILE
  265. if test 870 -ne `wc -c <'defs.h'`; then
  266.     echo shar: \"'defs.h'\" unpacked with wrong size!
  267. fi
  268. # end of 'defs.h'
  269. fi
  270. if test -f 'lex.l' -a "${1}" != "-c" ; then 
  271.   echo shar: Will not clobber existing file \"'lex.l'\"
  272. else
  273. echo shar: Extracting \"'lex.l'\" \(5725 characters\)
  274. sed "s/^X//" >'lex.l' <<'END_OF_FILE'
  275. X%{
  276. X/* +-------------------------------------------------------------------+ */
  277. X/* | Copyright 1991, David Koblas.                                     | */
  278. X/* |   Permission to use, copy, modify, and distribute this software   | */
  279. X/* |   and its documentation for any purpose and without fee is hereby | */
  280. X/* |   granted, provided that the above copyright notice appear in all | */
  281. X/* |   copies and that both that copyright notice and this permission  | */
  282. X/* |   notice appear in supporting documentation.  This software is    | */
  283. X/* |   provided "as is" without express or implied warranty.           | */
  284. X/* +-------------------------------------------------------------------+ */
  285. X
  286. X#include <stdio.h>
  287. X#include <varargs.h>
  288. X#include <ctype.h>
  289. X#include "defs.h"
  290. X
  291. static cmd_t    *newcmd();
  292. char    *savestr();
  293. X
  294. extern char    *index();
  295. X
  296. int    yyline = 1;
  297. X
  298. X%}
  299. X
  300. WS        [ \t]*
  301. NWS        [^ \n\t;]+
  302. X
  303. X%s ARGS
  304. X
  305. X%%
  306. X    int    state = 0;
  307. X    cmd_t    *cmd;
  308. X
  309. X#[^\n]*            ;
  310. X\n            { yyline++; BEGIN 0; }
  311. X^[^ \n\t]+        { cmd = newcmd(yytext); 
  312. X                state = (strcmp(yytext,"DEFAULT")==0) ? 1 : 0;
  313. X                BEGIN ARGS; }
  314. X^{WS}            BEGIN ARGS;
  315. X<ARGS>";"        state++;
  316. X<ARGS>{NWS}        addarg(state, cmd, yytext);
  317. X<ARGS>{WS}        ;
  318. X%%
  319. X#include <sys/types.h>
  320. X#include <sys/stat.h>
  321. X#include <syslog.h>
  322. X
  323. msg(va_alist)
  324. X va_dcl
  325. X{
  326. X#if 0
  327. X    va_list    ap;
  328. X    char    *s;
  329. X
  330. X    va_start(ap);
  331. X    s = va_arg(ap, char *);
  332. X    fprintf(stderr,"line %d: ",yyline);
  333. X    vfprintf(stderr, s, ap);
  334. X    fputc('\n', stderr);
  335. X    va_end(ap);
  336. X#endif
  337. X}
  338. X
  339. static addarg(state, cmd, str)
  340. int    state;
  341. cmd_t    *cmd;
  342. char    *str;
  343. X{
  344. X    if (state == 0) {
  345. X        msg("cmd='%s' add arg '%s'",cmd->name,str);
  346. X        if (cmd->margs == cmd->nargs)  {
  347. X            cmd->margs += cmd->margs;
  348. X            cmd->args = (char **)realloc(cmd->args, 
  349. X                    sizeof(char *) * cmd->margs);
  350. X            if (cmd->args == NULL)
  351. X                fatal("Unable to groupw args");
  352. X        }
  353. X        cmd->args[cmd->nargs++] = savestr(str);
  354. X    } else if (state == 1) {
  355. X        msg("cmd='%s' add opt '%s'",cmd->name,str);
  356. X        if (cmd->mopts == cmd->nopts) {
  357. X            cmd->mopts += cmd->mopts;
  358. X            cmd->opts = (char **)realloc(cmd->opts, 
  359. X                    sizeof(char *) * cmd->mopts);
  360. X            if (cmd->opts == NULL)
  361. X                fatal("Unable to groupw opts");
  362. X        }
  363. X        cmd->opts[cmd->nopts++] = savestr(str);
  364. X    } else {
  365. X        fatal("bad state (%d) received\n",state);
  366. X    }
  367. X}
  368. X
  369. char    *savestr(str)
  370. char    *str;
  371. X{
  372. X    char    *s = (char *)malloc(strlen(str)+1);
  373. X
  374. X    if (s == NULL) 
  375. X        fatal("No string space");
  376. X
  377. X    strcpy(s, str);
  378. X    return s;
  379. X}
  380. X
  381. static cmd_t    *newcmd(name)
  382. char    *name;
  383. X{
  384. X    cmd_t    *cmd = (cmd_t *)malloc(sizeof(cmd_t));
  385. X
  386. X    cmd->next = First;
  387. X    First = cmd;
  388. X
  389. X    if (cmd == NULL)
  390. X        fatal("Unable to alloc space for new command");
  391. X
  392. X    cmd->name = savestr(name);
  393. X    cmd->nargs = 0;        cmd->margs = 16;
  394. X    cmd->nopts = 0;        cmd->mopts = 16;
  395. X    cmd->args = (char **)malloc(sizeof(char *)*cmd->margs);
  396. X    cmd->opts = (char **)malloc(sizeof(char *)*cmd->mopts);
  397. X
  398. X    return cmd;
  399. X}
  400. X
  401. ReadFile(file)
  402. char    *file;
  403. X{
  404. X    struct stat    statbuf;
  405. X    FILE        *fd;
  406. X    
  407. X    if ((stat(file, &statbuf) < 0) || 
  408. X        (statbuf.st_uid != 0) || /* Owned by root */
  409. X        ((statbuf.st_mode & 0077) != 0)) {/* no perm */
  410. X        syslog(LOG_ERR, "Permission problems on %s", file);
  411. X        fatal("Permission problems on %s", file);
  412. X    }
  413. X    if ((fd = fopen(file,"r")) == NULL) {
  414. X        syslog(LOG_ERR, "Couldn't open on %s", file);
  415. X        fatal("Couldn't open %s", file);
  416. X    }
  417. X
  418. X    yyin = fd;
  419. X    yylex();
  420. X
  421. X    return 0;
  422. X}
  423. X
  424. CountArgs(cmd)
  425. cmd_t    *cmd;
  426. X{
  427. X    int    i, val;
  428. X    int    wild = 0, max = 0;
  429. X    char    *cp, *np, str[MAXSTRLEN];
  430. X
  431. X    for (i = 0; i < cmd->nargs; i++) {
  432. X        np = cmd->args[i];
  433. X
  434. X        while ((cp = index(np, '$')) != NULL) {
  435. X            if ((cp != cmd->args[i]) && (*(cp-1) == '\\'))
  436. X                np = cp + 1;
  437. X            else
  438. X                break;
  439. X        }
  440. X
  441. X        if (cp == NULL)
  442. X            continue;
  443. X        if (*(cp+1) == '*') {
  444. X            wild = 1;
  445. X            continue;
  446. X        }
  447. X        
  448. X        cp++;
  449. X        np = cp;
  450. X        
  451. X        while (isdigit(*cp))
  452. X            cp++;
  453. X        if ((cp - np) == 0)
  454. X            continue;
  455. X        strncpy(str, np, cp - np);
  456. X        str[cp - np] = '\0';
  457. X        val = atoi(str);
  458. X        if (val > max)
  459. X            max = val;
  460. X    }
  461. X
  462. X    if (wild)
  463. X        return -max;
  464. X    return max;
  465. X}
  466. X
  467. static int    cmpopts(a, b)
  468. char    *a, *b;
  469. X{
  470. X    char    *cp_a, *cp_b;
  471. X    int    val_a, val_b;
  472. X    char    str_a[MAXSTRLEN], str_b[MAXSTRLEN];
  473. X
  474. X    if (*a != '$' && *b != '$')
  475. X        return 0;
  476. X    if (*a == '$' && *b != '$')
  477. X        return -1;
  478. X    if (*a != '$' && *b == '$')
  479. X        return  1;
  480. X
  481. X    cp_a = ++a;
  482. X    cp_b = ++b;
  483. X    while ((*cp_a != '\0') && (*cp_a != '='))
  484. X        if (! isdigit(*cp_a))
  485. X            break;
  486. X    while ((*cp_b != '\0') && (*cp_b != '='))
  487. X        if (! isdigit(*cp_b))
  488. X            break;
  489. X    
  490. X    if (*cp_a != '=' && *cp_b != '=')
  491. X        return 0;
  492. X    if (*cp_a == '=' && *cp_b != '=')
  493. X        return -1;
  494. X    if (*cp_a != '=' && *cp_b == '=')
  495. X        return  1;
  496. X
  497. X    strncpy(str_a, a, cp_a - a);
  498. X    str_a[cp_a - a] = '\0';
  499. X    val_a = atoi(str_a);
  500. X    strncpy(str_b, b, cp_b - a);
  501. X    str_a[cp_b - b] = '\0';
  502. X    val_b = atoi(str_b);
  503. X
  504. X    if (val_a < val_b)
  505. X        return -1;
  506. X    if (val_a > val_b)
  507. X        return  1;
  508. X    return 0;
  509. X}
  510. X
  511. sortopts(cmd)
  512. cmd_t    *cmd;
  513. X{
  514. X    qsort(cmd->opts, cmd->nopts, sizeof(char *), cmpopts);
  515. X}
  516. X
  517. cmd_t    *Build(def, cmd)
  518. cmd_t    *def, *cmd;
  519. X{
  520. X    cmd_t        *new = newcmd("");
  521. X    char        defname[MAXSTRLEN], optname[MAXSTRLEN], *cp;
  522. X    int        i, j;
  523. X    extern char    *index();
  524. X
  525. X    if (cmd == NULL)
  526. X        return def;
  527. X    if (def == NULL)
  528. X        return cmd;
  529. X
  530. X    for (i = 0; i < cmd->nargs; i++)
  531. X        addarg(0, new, cmd->args[i]);
  532. X
  533. X    for (i = 0; i < def->nopts; i++) {
  534. X        if ((cp = index(def->opts[i], '=')) == NULL)
  535. X            strcpy(defname, def->opts[i]);
  536. X        else {
  537. X            int    l = cp - def->opts[i];
  538. X            strncpy(defname, def->opts[i], l);
  539. X            defname[l] = '\0';
  540. X        }
  541. X        for (j = 0; j < cmd->nopts; j++) {
  542. X            if ((cp = index(cmd->opts[j], '=')) == NULL)
  543. X                strcpy(optname, cmd->opts[j]);
  544. X            else {
  545. X                int    l = cp - cmd->opts[j];
  546. X                strncpy(optname, cmd->opts[j], l);
  547. X                optname[l] = '\0';
  548. X            }
  549. X            if (strcmp(defname, optname) == 0)
  550. X                def->opts[i][0] = '\0';
  551. X        }
  552. X        if (def->opts[i][0] != '\0')
  553. X            addarg(1, new, def->opts[i]);
  554. X    }
  555. X    for (j = 0; j < cmd->nopts; j++)
  556. X        addarg(1, new, cmd->opts[j]);
  557. X
  558. X    /* sortopts(new); */
  559. X
  560. X    return new;
  561. X}
  562. END_OF_FILE
  563. if test 5725 -ne `wc -c <'lex.l'`; then
  564.     echo shar: \"'lex.l'\" unpacked with wrong size!
  565. fi
  566. # end of 'lex.l'
  567. fi
  568. if test -f 'main.c' -a "${1}" != "-c" ; then 
  569.   echo shar: Will not clobber existing file \"'main.c'\"
  570. else
  571. echo shar: Extracting \"'main.c'\" \(11440 characters\)
  572. sed "s/^X//" >'main.c' <<'END_OF_FILE'
  573. X/* +-------------------------------------------------------------------+ */
  574. X/* | Copyright 1991, David Koblas.                                     | */
  575. X/* |   Permission to use, copy, modify, and distribute this software   | */
  576. X/* |   and its documentation for any purpose and without fee is hereby | */
  577. X/* |   granted, provided that the above copyright notice appear in all | */
  578. X/* |   copies and that both that copyright notice and this permission  | */
  579. X/* |   notice appear in supporting documentation.  This software is    | */
  580. X/* |   provided "as is" without express or implied warranty.           | */
  581. X/* +-------------------------------------------------------------------+ */
  582. X
  583. X#include <stdio.h>
  584. X#include <varargs.h>
  585. X#include <syslog.h>
  586. X#include <pwd.h>
  587. X#include <grp.h>
  588. X#include <ctype.h>
  589. X#include "defs.h"
  590. X#include "regexp.h"
  591. X
  592. X#ifndef LOG_AUTH
  593. X/*
  594. X**  Pmax's don't have LOG_AUTH
  595. X*/
  596. X#define LOG_AUTH LOG_WARNING
  597. X#endif
  598. X
  599. X/*
  600. X**  File containing 'op' definitions.
  601. X*/
  602. X#define ACCESS_FILE    "/etc/op.access"
  603. X
  604. X#define    MAXARG    1024
  605. X#define    MAXENV    MAXARG
  606. X
  607. extern char    *index();
  608. extern char    *savestr();
  609. extern char    *getpass(), *crypt();
  610. X
  611. char    *Progname;
  612. cmd_t    *Find();
  613. cmd_t    *First = NULL;
  614. X
  615. Usage()
  616. X{
  617. X    fatal("Usage: %s mnemonic [args]\n       %s -h [-u username] mnemonic",
  618. X            Progname, Progname);
  619. X}
  620. X
  621. main(argc, argv)
  622. int    argc;
  623. char    **argv;
  624. X{
  625. X    int        num, argStart = 1;
  626. X    char        user[MAXSTRLEN];
  627. X    cmd_t        *cmd, *def, *new;
  628. X    struct passwd    *pw;
  629. X    int        hflag = 0;
  630. X    char        *uptr = NULL;
  631. X
  632. X    Progname = argv[0];
  633. X
  634. X    while (1) {
  635. X        if (argStart >= argc)
  636. X            break;
  637. X
  638. X        if (strcmp("-h", argv[argStart]) == 0) {
  639. X            hflag++;
  640. X            argStart++;
  641. X        } else if (strcmp("-u", argv[argStart]) == 0) {
  642. X            if (strlen(argv[argStart]) == 2) {
  643. X                if (argStart+1 >= argc)
  644. X                    Usage();
  645. X                argStart++;
  646. X                uptr = argv[argStart];
  647. X            }
  648. X            argStart++;
  649. X        } else if (strcmp("-uh", argv[argStart]) == 0) {
  650. X            hflag++;
  651. X            if (strlen(argv[argStart]) == 3) {
  652. X                if (argStart+1 >= argc)
  653. X                    Usage();
  654. X                argStart++;
  655. X                uptr = argv[argStart];
  656. X            }
  657. X            argStart++;
  658. X        } else if (strcmp("-hu", argv[argStart]) == 0) {
  659. X            hflag++;
  660. X            if (strlen(argv[argStart]) == 3) {
  661. X                if (argStart+1 >= argc)
  662. X                    Usage();
  663. X                argStart++;
  664. X                uptr = argv[argStart];
  665. X            }
  666. X            argStart++;
  667. X        } else {
  668. X            break;
  669. X        }
  670. X    }
  671. X
  672. X    if (openlog("op", 0, LOG_AUTH) < 0) 
  673. X        fatal("openlog failed");
  674. X
  675. X    ReadFile(ACCESS_FILE);
  676. X
  677. X    if (hflag) {
  678. X        if (uptr != NULL) {
  679. X            if (getuid() != 0) 
  680. X                fatal("Permission denied for -u option");
  681. X        }
  682. X    }
  683. X    if (uptr != NULL) 
  684. X        Usage();
  685. X
  686. X    if (argStart >= argc)
  687. X        Usage();
  688. X
  689. X    def = Find("DEFAULT");
  690. X    cmd = Find(argv[argStart]);
  691. X
  692. X    if (cmd == NULL) 
  693. X        fatal("No such command %s", argv[1]);
  694. X
  695. X    argc -= argStart;
  696. X    argv += argStart;
  697. X
  698. X    new = Build(def, cmd);
  699. X    num = CountArgs(new);
  700. X
  701. X    if ((num < 0) && ((argc-1) < -num))
  702. X        fatal("Improper number of arguments");
  703. X    if ((num > 0) && ((argc-1) != num)) 
  704. X        fatal("Improper number of arguments");
  705. X    if (num <0)
  706. X        num = -num;
  707. X
  708. X    if ((pw = getpwuid(getuid())) == NULL) 
  709. X        exit(1);
  710. X    strcpy(user, pw->pw_name);
  711. X    if (Verify(new, num, argc, argv) < 0) {
  712. X        syslog(LOG_NOTICE, "user %s FAILED to execute '%s'", 
  713. X                user, argv[0]);
  714. X        fatal("Permission denied");
  715. X    } else {
  716. X        syslog(LOG_NOTICE, "user %s SUCCEDED to execute '%s'",
  717. X                user, argv[0]);
  718. X    }
  719. X
  720. X    return Go(new, num, argc, argv);
  721. X}
  722. X
  723. fatal(va_alist)
  724. X va_dcl
  725. X{
  726. X    va_list    ap;
  727. X    char    *s;
  728. X
  729. X    va_start(ap);
  730. X    s = va_arg(ap, char *);
  731. X    vfprintf(stderr, s, ap);
  732. X    fputc('\n', stderr);
  733. X    va_end(ap);
  734. X
  735. X    exit(1);
  736. X}
  737. X
  738. cmd_t    *Find(name)
  739. char    *name;
  740. X{
  741. X    cmd_t    *cmd;
  742. X
  743. X    for (cmd = First; cmd != NULL; cmd = cmd ->next) {
  744. X        if (strcmp(cmd->name, name) == 0)
  745. X            break;
  746. X    }
  747. X
  748. X    return cmd;
  749. X}
  750. X
  751. char    *FindOpt(cmd, str)
  752. cmd_t    *cmd;
  753. char    *str;
  754. X{
  755. X    static char    nul[2] = "";
  756. X    int        i;
  757. X    char        *cp;
  758. X
  759. X    for (i = 0; i < cmd->nopts; i++) {
  760. X        if ((cp = index(cmd->opts[i], '=')) == NULL) {
  761. X            if (strcmp(cmd->opts[i], str) == 0)
  762. X                return nul;
  763. X        } else {
  764. X            int    l = cp - cmd->opts[i];
  765. X            if (strncmp(cmd->opts[i], str, l) == 0)
  766. X                return cp+1;
  767. X        }
  768. X    }
  769. X
  770. X    return NULL;
  771. X}
  772. X
  773. char    *GetField(cp, str)
  774. char    *cp, *str;
  775. X{
  776. X    if (*cp == '\0')
  777. X        return NULL;
  778. X
  779. X    while ((*cp != '\0') && (*cp != ',')) {
  780. X        if (*cp == '\\')
  781. X            if (*(cp+1) == ',') {
  782. X                *str++ = ',';
  783. X                cp++;
  784. X            } else
  785. X                *str++ = '\\';
  786. X        else
  787. X            *str++ = *cp;
  788. X        cp++;
  789. X    }
  790. X
  791. X    *str = '\0';
  792. X
  793. X    return (*cp == '\0') ? cp : (cp+1);
  794. X}
  795. X
  796. Verify(cmd, num, argc, argv)
  797. cmd_t    *cmd;
  798. int    argc;
  799. int    num;
  800. char    **argv;
  801. X{
  802. X    int        gr_fail = 1, uid_fail = 1;
  803. X    int        i, j, val;
  804. X    char        *np, *cp, str[MAXSTRLEN], buf[MAXSTRLEN];
  805. X    regexp        *reg = NULL;
  806. X    struct passwd    *pw, spw;
  807. X    struct group    *gr;
  808. X
  809. X    if ((pw = getpwuid(getuid())) == NULL) 
  810. X        return -1;
  811. X
  812. X    if ((cp=FindOpt(cmd, "password")) != NULL) {
  813. X        if ((np = getpass("Password:")) == NULL)
  814. X            return -1;
  815. X        if (((cp = GetField(cp, str)) != NULL) && 
  816. X            ((pw = getpwnam(str)) == NULL))
  817. X            return -1;
  818. X        if (strcmp(crypt(np, pw->pw_passwd), pw->pw_passwd) != 0)
  819. X            return -1;
  820. X    }
  821. X
  822. X    if ((pw = getpwuid(getuid())) == NULL) 
  823. X        return -1;
  824. X
  825. X    if ((cp = FindOpt(cmd, "groups")) != NULL) {
  826. X        for (cp=GetField(cp, str); cp!=NULL; cp=GetField(cp, str)) {
  827. X            if (re_comp(str) != 0)
  828. X                return -1;
  829. X            if ((gr = getgrgid(pw->pw_gid)) != NULL) {
  830. X                if (re_exec(gr->gr_name) == 1) {
  831. X                    gr_fail = 0;
  832. X                    break;
  833. X                }
  834. X            }
  835. X            setgrent();
  836. X            while ((gr = getgrent()) != NULL) {
  837. X                i = 0;
  838. X                while (gr->gr_mem[i] != NULL) {
  839. X                    if (strcmp(gr->gr_mem[i],
  840. X                            pw->pw_name)==0)
  841. X                        break;
  842. X                    i++;
  843. X                }
  844. X                if ((gr->gr_mem[i] != NULL) && 
  845. X                    (re_exec(gr->gr_name) == 1)) {
  846. X                        gr_fail = 0;
  847. X                        break;
  848. X                }
  849. X            }
  850. X        }
  851. X    }
  852. X    if (gr_fail && ((cp = FindOpt(cmd, "users")) != NULL)) {
  853. X        for (cp=GetField(cp, str); cp!=NULL; cp=GetField(cp, str)) {
  854. X            if (re_comp(str) != 0)
  855. X                return -1;
  856. X            if (re_exec(pw->pw_name) == 1) {
  857. X                uid_fail = 0;
  858. X                break;
  859. X            }
  860. X        }
  861. X    }
  862. X
  863. X    if (gr_fail && uid_fail)
  864. X        return -1;
  865. X    
  866. X    for (i = 0; i < cmd->nopts; i++) {
  867. X        if ((cmd->opts[i][0] != '$') || 
  868. X            ((cp = index(cmd->opts[i], '=')) == NULL))
  869. X            continue;
  870. X        if (cmd->opts[i][1] != '*') {
  871. X            for (np = cmd->opts[i] + 1; np != cp; np++) 
  872. X                if (!isdigit(*np))
  873. X                    break;
  874. X            if (np != cp)
  875. X                continue;
  876. X        } else {
  877. X            if (cmd->opts[i][2] != '=')
  878. X                continue;
  879. X            np = cmd->opts[i] + 3;
  880. X            for (j = num+1; j < argc; j++) {
  881. X                cp = np;
  882. X                for (cp=GetField(cp, str); cp!=NULL; 
  883. X                        cp=GetField(cp, str)) {
  884. X                    if (re_comp(str) != 0)
  885. X                        return -1;
  886. X                    if (re_exec(argv[j]) == 1)
  887. X                        break;
  888. X                }
  889. X                if (cp == NULL)
  890. X                    return -1;
  891. X            }
  892. X        }
  893. X        
  894. X        strncpy(str, cmd->opts[i] + 1, cp - cmd->opts[i] - 1);
  895. X        str[cp - cmd->opts[i] - 1] = '\0';
  896. X        val = atoi(str);
  897. X
  898. X        if (val >= argc)
  899. X            continue;
  900. X        cp++;
  901. X        np = cp;
  902. X        if (reg != NULL) {
  903. X            for (cp=GetField(cp, str); cp!=NULL; 
  904. X                    cp=GetField(cp, str)) {
  905. X                regsub(reg, str, buf);
  906. X                if (strcmp(buf, argv[val]) == 0)
  907. X                    break;
  908. X            }
  909. X
  910. X            if (cp != NULL)
  911. X                continue;
  912. X
  913. X            free(reg);
  914. X            reg = NULL;
  915. X        }
  916. X
  917. X        if ((reg == NULL) || (cp == NULL)) {
  918. X            cp = np;
  919. X            for (cp=GetField(cp, str); cp!=NULL; 
  920. X                    cp=GetField(cp, str)) {
  921. X                if ((reg = regcomp(str)) == NULL) 
  922. X                    return -1;
  923. X                if (regexec(reg, argv[val]) == 1)
  924. X                    break;
  925. X
  926. X                free(reg);
  927. X                reg = NULL;
  928. X            }
  929. X        }
  930. X
  931. X        if (cp == NULL)
  932. X            return -1;
  933. X    }
  934. X}
  935. X
  936. Go(cmd, num, argc, argv)
  937. cmd_t    *cmd;
  938. int    argc;
  939. int    num;
  940. char    **argv;
  941. X{
  942. X    extern char    **environ;
  943. X    int        i, j, flag, val, len = 0;
  944. X    char        *cp, *np;
  945. X    struct passwd    *pw;
  946. X    struct group    *gr;
  947. X        int             ngroups=0, gidset[256];
  948. X    int        curenv = 0, curarg = 0;
  949. X    char        *new_envp[MAXENV];
  950. X    char        *new_argv[MAXARG];
  951. X    char        str[MAXSTRLEN], buf[4*MAXSTRLEN];
  952. X
  953. X        if ((cp = FindOpt(cmd, "gid")) == NULL) {
  954. X                ;               /* don't have a default */
  955. X        } else {
  956. X                for (; cp!=NULL; cp=GetField(cp, str)) {
  957. X                        if ((gr = getgrnam(cp)) != NULL)
  958. X                                gidset[ngroups++] = gr->gr_gid;
  959. X                }
  960. X                if (ngroups == 0) 
  961. X                        fatal("invalid groups");
  962. X                if (setgroups(ngroups, gidset) < 0)
  963. X                        fatal("setgroups failed");
  964. X                if (setgid(gidset[0]))
  965. X                        fatal("setgid failed");
  966. X        }
  967. X
  968. X    if ((cp = FindOpt(cmd, "uid")) == NULL) {
  969. X                if (setuid(geteuid()) < 0)
  970. X            fatal("Unable to set uid to default", cp);
  971. X    } else {
  972. X        if ((pw = getpwnam(cp)) == NULL) {
  973. X            if (setuid(atoi(cp)) < 0)
  974. X                fatal("Unable to set uid to %s", cp);
  975. X        }
  976. X        if (setuid(pw->pw_uid) < 0)
  977. X            fatal("Unable to set uid to %s", cp);
  978. X    }
  979. X
  980. X    if ((cp = FindOpt(cmd, "umask")) == NULL) {
  981. X        if (umask(0022) < 0)
  982. X            fatal("Unable to set umask to default");
  983. X    } else {
  984. X        if (umask(atov(cp, 8)) < 0)
  985. X            fatal("Unable to set umask to %s", cp);
  986. X    }
  987. X
  988. X    if ((cp = FindOpt(cmd, "chroot")) == NULL) {
  989. X        ;        /* don't have a default */
  990. X    } else {
  991. X        if (chroot(cp) < 0)
  992. X            fatal("Unable to chroot to %s", cp);
  993. X    }
  994. X
  995. X    if ((cp = FindOpt(cmd, "dir")) == NULL) {
  996. X        ;        /* don't have a default */
  997. X    } else {
  998. X        if (chdir(cp) < 0) 
  999. X            fatal("Unable to chdir to %s", cp);
  1000. X    }
  1001. X
  1002. X    if (FindOpt(cmd, "environment") == NULL) {
  1003. X        for (i = 0; i < cmd->nopts; i++) {
  1004. X            if (cmd->opts[i][0] != '$')
  1005. X                continue;
  1006. X            cp = cmd->opts[i] + 1;
  1007. X            flag = 0;
  1008. X            while ((*cp != '\0') && (*cp != '=')) {
  1009. X                if (! isdigit(*cp))
  1010. X                    flag = 1;
  1011. X                cp++;
  1012. X            }
  1013. X            if (! flag)
  1014. X                continue;
  1015. X            if (index(cmd->opts[i], '=') != NULL) {
  1016. X                new_envp[curenv++] = cmd->opts[i] + 1;
  1017. X                continue;
  1018. X            }
  1019. X            for (j = 0; environ[j] != NULL ; j++) {
  1020. X                if ((cp = index(environ[j], '=')) == NULL)
  1021. X                    continue;
  1022. X                if (strncmp(cmd->opts[i] + 1, environ[j],
  1023. X                        cp - environ[j]) == 0) {
  1024. X                    new_envp[curenv++] = environ[j];
  1025. X                    break;
  1026. X                }
  1027. X            }
  1028. X        }
  1029. X    } else {
  1030. X        for (i = 0; environ[i] != NULL; i++)
  1031. X            new_envp[curenv++] = environ[i];
  1032. X    }
  1033. X    new_envp[curenv] = NULL;
  1034. X
  1035. X    if (strcmp("MAGIC_SHELL", cmd->args[0]) == 0) {
  1036. X        for (i = 0; environ[i] != NULL; i++) 
  1037. X            if (strncmp("SHELL=", environ[i], 6) == 0)
  1038. X                break;
  1039. X
  1040. X        if (environ[i] != NULL)
  1041. X            new_argv[curarg++] = environ[i] + 6;
  1042. X        else {
  1043. X            fprintf(stderr,"No shell\n");
  1044. X            exit(1);
  1045. X        }
  1046. X
  1047. X        if (argc != 1) {
  1048. X            new_argv[curarg++] = "-c";
  1049. X
  1050. X            for (i = 1; i < argc; i++)
  1051. X                len += strlen(argv[i]) + 1;
  1052. X
  1053. X            if ((cp = (char *)malloc(len + 10)) == NULL) {
  1054. X                fprintf(stderr, "Unable to create buffer");
  1055. X                exit(1);
  1056. X            }
  1057. X
  1058. X            len = 0;
  1059. X            *cp = '\0';
  1060. X
  1061. X            for (i = 1; i < argc; i++) {
  1062. X                strcat(cp, argv[i]);
  1063. X                strcat(cp, " ");
  1064. X            }
  1065. X            new_argv[curarg++] = cp;
  1066. X        }
  1067. X    } else {
  1068. X        for (i = 0; i < cmd->nargs; i++) {
  1069. X            np = cmd->args[i];
  1070. X
  1071. X            while ((cp = index(np, '$')) != NULL) {
  1072. X                if ((cp != cmd->args[i]) && (*(cp-1) == '\\'))
  1073. X                    np = cp + 1;
  1074. X                else
  1075. X                    break;
  1076. X            }
  1077. X
  1078. X            if (cp == NULL) {
  1079. X                new_argv[curarg++] = cmd->args[i];
  1080. X                continue;
  1081. X            }
  1082. X            if (*(cp+1) == '*') {
  1083. X                for (j = num + 1; j < argc; j++) {
  1084. X                    new_argv[curarg++] = argv[j];
  1085. X                }
  1086. X                continue;
  1087. X            }
  1088. X
  1089. X            cp++;
  1090. X            np = cp;
  1091. X            while (isdigit(*cp))
  1092. X                cp++;
  1093. X            if ((cp - np) == 0) {
  1094. X                new_argv[curarg++] = cmd->args[i];
  1095. X                continue;
  1096. X            }
  1097. X            strncpy(str, np, cp - np);
  1098. X            str[cp - np] = '\0';
  1099. X            val = atoi(str);
  1100. X            buf[0] = '\0';
  1101. X            strncpy(buf, cmd->args[i], np - cmd->args[i] - 1);
  1102. X            strcat(buf, argv[val]);
  1103. X            strcat(buf, cp);
  1104. X            new_argv[curarg++] = savestr(buf);
  1105. X        }
  1106. X    }
  1107. X    new_argv[curarg] = NULL;
  1108. X
  1109. X    if (execve(new_argv[0], new_argv, new_envp) < 0)
  1110. X        perror("execve");
  1111. X}
  1112. X
  1113. output(cmd)
  1114. cmd_t    *cmd;
  1115. X{
  1116. X    int    i;
  1117. X
  1118. X    printf("cmd '%s'\n",cmd->name);
  1119. X    printf("\n  args\t");
  1120. X    for (i = 0; i < cmd->nargs; i++)
  1121. X        printf("'%s' ",cmd->args[i]);
  1122. X    printf("\n  opts\t");
  1123. X    for (i = 0; i < cmd->nopts; i++)
  1124. X        printf("'%s' ",cmd->opts[i]);
  1125. X    printf("\n");
  1126. X}
  1127. END_OF_FILE
  1128. if test 11440 -ne `wc -c <'main.c'`; then
  1129.     echo shar: \"'main.c'\" unpacked with wrong size!
  1130. fi
  1131. # end of 'main.c'
  1132. fi
  1133. if test -f 'op.1' -a "${1}" != "-c" ; then 
  1134.   echo shar: Will not clobber existing file \"'op.1'\"
  1135. else
  1136. echo shar: Extracting \"'op.1'\" \(6076 characters\)
  1137. sed "s/^X//" >'op.1' <<'END_OF_FILE'
  1138. X.TH OP 1 "December 14, 1989"
  1139. X.UC 4
  1140. X.SH NAME
  1141. op \- operator access
  1142. X.SH SYNOPSIS
  1143. X.B op 
  1144. mnemonic [arg]
  1145. X.SH DESCRIPTION
  1146. The 
  1147. X.I op
  1148. tool provides a flexible means for system administrators to grant
  1149. trusted users access to certain 
  1150. X.B root
  1151. operations without having to give them full superuser privileges.
  1152. Different sets of users may access different operations, and the
  1153. security-related aspects of environment of each
  1154. operation can be carefully controlled.
  1155. X.PP 
  1156. The fields of the entries in 
  1157. X.I op.access 
  1158. are separated by white space.  Each entry may span several lines and
  1159. continues until the next alphanumeric string is found at the beginning of
  1160. a lines (which is taken to be the next
  1161. X.I mnemonic,
  1162. and thus the beginning of a new entry).  Comments may be embedded
  1163. beginning with a # character.  Each entry in op
  1164. X.I op.access
  1165. has the following form:
  1166. X.RS
  1167. X.DT
  1168. X\fImnemonic    command \fR[\fI arg ... \fR]\fI ; \fR[\fI option ... \fR]
  1169. X.RE
  1170. where the fields are interpreted in the following manner:
  1171. X.TP
  1172. X.I mnemonic
  1173. a unique, alphanumeric identifier for each operator function.
  1174. X.TP
  1175. X.I command
  1176. the full pathname of the executable to be run by
  1177. X.I op
  1178. when the associated 
  1179. X.I mnemonic
  1180. is chosen.
  1181. X.TP
  1182. X.I arg(s)
  1183. any arguments, either literal or variable, needed by 
  1184. X.I command.
  1185. Literal arguments are simply specified directly, like specific command
  1186. options (\fB0Gun\fR) or files (\fB/dev/rmt20\fR).  Variable arguments
  1187. are specified here as \fB$1, $2 ... $\fR\fIn\fR; these are described
  1188. more fully in the options section below. \fB$*\fR indicates any number
  1189. trailing arguments.
  1190. X.TP
  1191. X.I option(s)
  1192. a set of optional parameters to specify settings or restoring for the
  1193. particular 
  1194. X.I mnemonic,
  1195. define variable arguments specified for the 
  1196. X.I command,
  1197. space and are of the form
  1198. X.I keyword=value.
  1199. The absence of a specific list of values separated by commas, where
  1200. appropriate.
  1201. There should be no white space in each element of the 
  1202. X.I value
  1203. string unless quoted.  The
  1204. X.I keyword
  1205. is any of the following types:
  1206. X.TP
  1207. X.B uid
  1208. Set the user id to the value specified.  The value can be numeric user
  1209. ID or a login name.  The default is 
  1210. X.B root.
  1211. X.TP
  1212. X.B gid
  1213. Set the group id's to the values specified.  Each value can be a numeric
  1214. group ID or a group name.
  1215. X.TP
  1216. X.B dir
  1217. Change the current working directory to the path specified.
  1218. X.TP
  1219. X.B chroot
  1220. Change the root directory to the path specified using 
  1221. X.I chroot.
  1222. X.TP
  1223. X.B umask
  1224. Set the file creation umask to the octal value specified.  The default
  1225. is to set it to 
  1226. X.B 022.
  1227. X.TP
  1228. X.B groups
  1229. Allow any user who belongs to a group listed here to execute this 
  1230. X.I op
  1231. function.  The default is not to allow any specific group.
  1232. X.TP
  1233. X.B users
  1234. Allow any user listed here to execute this 
  1235. X.I op 
  1236. function.  The default is not allow any specific users.  You may user
  1237. the regular expression .* to indicate that all users may use this
  1238. mnemonic.
  1239. X.TP
  1240. X.B password
  1241. Queries the user for a password, if there is no = part the
  1242. users own password is asked.
  1243. X.TP
  1244. X.BI $VAR
  1245. where 
  1246. X.I VAR
  1247. is the name of an environment variable.  The specified environment
  1248. case, simply using
  1249. X.I $VAR with no = part (as in 
  1250. X.B $USER)
  1251. means that this environment variable is inherited unchanged from
  1252. the caller's shell.
  1253. X.TP
  1254. X.B environment
  1255. Disables the destruction of the users environment.
  1256. X.TP
  1257. X.B $n
  1258. defines the \fIn\fRth variable argument specified in the command 
  1259. X.I arg
  1260. list.  The value for this type may be a comma-separated list of regular
  1261. expressions using \fIegrep\fR(1).  option defines the range of values
  1262. allowed for the variable arguments  A variable argument specified as a
  1263. command 
  1264. X.I arg
  1265. but not described in the 
  1266. X.I options
  1267. section may take on any value.  If an argument does not match any
  1268. of its permitted values, then a diagnostic is printed and the
  1269. command is not executed.  When using '(' syntax to pass values
  1270. to other options, only the next options can use values from
  1271. the previous search.
  1272. X.TP
  1273. X.B $*
  1274. is used in the 
  1275. X.I options
  1276. section to place restriction on the trailing arguments
  1277. specified as $* in the
  1278. X.I args 
  1279. section.  If any of these (possibly many) arguments do not match, then
  1280. a diagnostic is printed, and the command is not executed.
  1281. X.PP
  1282. There can also be a special entry in the file beginning at the first
  1283. non-comment line
  1284. that can define default values to override the builtin defaults listed
  1285. here, yet still be overridden by any entry that wants to redefine any of
  1286. the keyword fields described above.  It should have the following format:
  1287. X.RS
  1288. X.DT
  1289. X\fBDEFAULT\fR    \fIkeyword_option\fR
  1290. X.RE
  1291. where \fIkeyword_option\fR is a \fIkeyword=value\fR string mentioned above
  1292. under \fIoptions\fR.
  1293. X.PP
  1294. It should be noted that if any regular 
  1295. X.I mnemonic
  1296. entry defines its own
  1297. X.I option,
  1298. the value given for that entry must explicitly include the item from the
  1299. DEFAULT line if the default values is to be included.  That is, the 
  1300. X.I options
  1301. definitions completely override any
  1302. defaults; they do not add to them  In this way, if a value specified on
  1303. the DEFAULT line for 
  1304. X.B users
  1305. or
  1306. X.B groups
  1307. X(for example) needs to be "erased" without redefining new values
  1308. X(that is, we want no users or groups to be allowed to run the mnemonic),
  1309. then the default value must be overridden with nothing (as in
  1310. X\fBusers=\fR).  For the
  1311. X.B users
  1312. or
  1313. X.B groups
  1314. fields, such a null setting has the effect of setting the list of
  1315. allowable users or groups to be empty.  For the other keywords (\fR
  1316. uid, gid, dir, chroot, \fRand\fB umask\fR), a null setting leaves that
  1317. attribute as it is upon invocation of the 
  1318. X.I op
  1319. program, overriding any defaults.
  1320. X.PP
  1321. Another note is that if the 
  1322. X.I command 
  1323. for a 
  1324. X.I mnemonic
  1325. is 
  1326. X.B MAGIC_SHELL
  1327. then a shell (using the users $SHELL environment variable) is created, 
  1328. if there are arguments in addition to the 
  1329. X.I mnemonic
  1330. on the command line then the shell is invoked "-c args".
  1331. X.SH FILES
  1332. X.DT
  1333. X/etc/op.access    access control description
  1334. X.SH "SEE ALSO"
  1335. su(1), chroot(2), egrep(1)
  1336. X.SH CREDIT
  1337. X.B "Op: A flexible Tool for Restricted Superuser Access",
  1338. by
  1339. X.I "Tom Christiansen",
  1340. CONVEX Computer Corporation,
  1341. X.B "Proceedings of the Large Installation Systems Administration III Workshop"
  1342. END_OF_FILE
  1343. if test 6076 -ne `wc -c <'op.1'`; then
  1344.     echo shar: \"'op.1'\" unpacked with wrong size!
  1345. fi
  1346. # end of 'op.1'
  1347. fi
  1348. if test -f 'op.access' -a "${1}" != "-c" ; then 
  1349.   echo shar: Will not clobber existing file \"'op.access'\"
  1350. else
  1351. echo shar: Extracting \"'op.access'\" \(1952 characters\)
  1352. sed "s/^X//" >'op.access' <<'END_OF_FILE'
  1353. X# first, define the site defaults we want to use here
  1354. X# we would like the people in 'operator' group to be able to execute
  1355. X# almost everything, so it iseasier to put it here than on every line...
  1356. X# set up default envariables
  1357. X#
  1358. DEFAULT groups=operator $USER $TERM $PATH=/usr/ucb:/usr/bin:/bin
  1359. X#
  1360. X# find out who's filled up the disk; anyone may do this
  1361. X#
  1362. full    /usr/etc/quot $1; users=.*
  1363. X#
  1364. X# filesystem backups
  1365. X#
  1366. daily    /etc/dump 5Gun $1; $1=/,/usr[0-9]*,/project
  1367. weekly    /etc/dump 0Gun $1; $1=/,/usr[0-9]*,/project
  1368. X#
  1369. X# tape handling commands
  1370. X# must include 'operator' if we want them to be allowed as well
  1371. X#
  1372. tape    /etc/tpc $1 $2; groups=tapeopers,operator users=boss
  1373. X    $1=enable,disable,stop,restart $2=all,unit[01]
  1374. X#
  1375. mounted    /etc/tpc mounted unit$1 $2; $1=[0-3]
  1376. X#
  1377. X# taking the system down
  1378. X# $1 shows a good user of regular expression;
  1379. X# $2 can be anything, but is required; no instant shutdowns
  1380. X#
  1381. shutdown    /etc/shutdown -h $1 $2; $1=+[1-9][0-9]*,[0-9]*:[0-9]*
  1382. reboot        /etc/shutdown -r $1 $2; $1=+[1-9][0-9]*,[0-9]*:[0-9]*
  1383. X#
  1384. X# start up disco daemon
  1385. disco    /etc/opbin/start_disco ; uid=disco gid=proj dir=/scratch
  1386. X        umask=027 groups=geo,disco users=snoopy,linus
  1387. X        $USER=disco $SHELL=/bin/shell
  1388. X#
  1389. X# let certain people mount and unmount the removable drive
  1390. X#
  1391. rdsmount    /etc/mount $1 $2; groups=operator,swdev,disco
  1392. X            users=bob,steve $1=/dev/dd0[a-z] $2=/.*
  1393. rdsumount    /etc/umount $1 ; groups=operator,swdev,disco
  1394. X            users=bob,steve $1=/dev/dd0[a-z]
  1395. X#
  1396. X# allow operators to give files away; notice that they
  1397. X# must give at least two s, but may give more
  1398. X#
  1399. chown    /bsd43/bin//chown $1 $2 $*; $1=[a-z0-9][a-z0-9]*
  1400. X#
  1401. X# permit development personnel to run install
  1402. X#
  1403. inst    /usr/bin/install -o root -g system $1 $2; groups=devel
  1404. X        $2=/bin,/usr/bin,/usr/ucb,/usr/new,/usr/local
  1405. X#
  1406. nfsmount    /etc/mount -o timeo=100,hard,inter $1 $2; 
  1407. X            groups=devel,operator 
  1408. X            $1=([a-zA-Z0-9_]*):(.*) $2=/remote/\1\2
  1409. X
  1410. foo    /bin/cat $1; users=koblas
  1411. X
  1412. shell    MAGIC_SHELL ; password environment 
  1413. END_OF_FILE
  1414. if test 1952 -ne `wc -c <'op.access'`; then
  1415.     echo shar: \"'op.access'\" unpacked with wrong size!
  1416. fi
  1417. # end of 'op.access'
  1418. fi
  1419. if test -f 'regerror.c' -a "${1}" != "-c" ; then 
  1420.   echo shar: Will not clobber existing file \"'regerror.c'\"
  1421. else
  1422. echo shar: Extracting \"'regerror.c'\" \(224 characters\)
  1423. sed "s/^X//" >'regerror.c' <<'END_OF_FILE'
  1424. X#include <stdio.h>
  1425. X
  1426. void
  1427. regerror(s)
  1428. char *s;
  1429. X{
  1430. X#ifdef ERRAVAIL
  1431. X    error("regexp: %s", s);
  1432. X#else
  1433. X/*
  1434. X    fprintf(stderr, "regexp(3): %s\n", s);
  1435. X    exit(1);
  1436. X*/
  1437. X    return;      /* let std. egrep handle errors */
  1438. X#endif
  1439. X    /* NOTREACHED */
  1440. X}
  1441. END_OF_FILE
  1442. if test 224 -ne `wc -c <'regerror.c'`; then
  1443.     echo shar: \"'regerror.c'\" unpacked with wrong size!
  1444. fi
  1445. # end of 'regerror.c'
  1446. fi
  1447. if test -f 'regexp.c' -a "${1}" != "-c" ; then 
  1448.   echo shar: Will not clobber existing file \"'regexp.c'\"
  1449. else
  1450. echo shar: Extracting \"'regexp.c'\" \(31149 characters\)
  1451. sed "s/^X//" >'regexp.c' <<'END_OF_FILE'
  1452. X/*
  1453. X * regcomp and regexec -- regsub and regerror are elsewhere
  1454. X *
  1455. X *    Copyright (c) 1986 by University of Toronto.
  1456. X *    Written by Henry Spencer.  Not derived from licensed software.
  1457. X *
  1458. X *    Permission is granted to anyone to use this software for any
  1459. X *    purpose on any computer system, and to redistribute it freely,
  1460. X *    subject to the following restrictions:
  1461. X *
  1462. X *    1. The author is not responsible for the consequences of use of
  1463. X *        this software, no matter how awful, even if they arise
  1464. X *        from defects in it.
  1465. X *
  1466. X *    2. The origin of this software must not be misrepresented, either
  1467. X *        by explicit claim or by omission.
  1468. X *
  1469. X *    3. Altered versions must be plainly marked as such, and must not
  1470. X *        be misrepresented as being the original software.
  1471. X *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
  1472. X *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
  1473. X *** to assist in implementing egrep.
  1474. X *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
  1475. X *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
  1476. X *** as in BSD grep and ex.
  1477. X *** THIS IS AN ALTERED VERSION.  It was altered by John Gilmore,
  1478. X *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
  1479. X *** THIS IS AN ALTERED VERSION.  It was altered by James A. Woods,
  1480. X *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
  1481. X *
  1482. X * Beware that some of this code is subtly aware of the way operator
  1483. X * precedence is structured in regular expressions.  Serious changes in
  1484. X * regular-expression syntax might require a total rethink.
  1485. X */
  1486. X#include <stdio.h>
  1487. X#include <ctype.h>
  1488. X#include <regexp.h>
  1489. X#include "regmagic.h"
  1490. X
  1491. X/*
  1492. X * The "internal use only" fields in regexp.h are present to pass info from
  1493. X * compile to execute that permits the execute phase to run lots faster on
  1494. X * simple cases.  They are:
  1495. X *
  1496. X * regstart    char that must begin a match; '\0' if none obvious
  1497. X * reganch    is the match anchored (at beginning-of-line only)?
  1498. X * regmust    string (pointer into program) that match must include, or NULL
  1499. X * regmlen    length of regmust string
  1500. X *
  1501. X * Regstart and reganch permit very fast decisions on suitable starting points
  1502. X * for a match, cutting down the work a lot.  Regmust permits fast rejection
  1503. X * of lines that cannot possibly match.  The regmust tests are costly enough
  1504. X * that regcomp() supplies a regmust only if the r.e. contains something
  1505. X * potentially expensive (at present, the only such thing detected is * or +
  1506. X * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  1507. X * supplied because the test in regexec() needs it and regcomp() is computing
  1508. X * it anyway.
  1509. X */
  1510. X
  1511. X/*
  1512. X * Structure for regexp "program".  This is essentially a linear encoding
  1513. X * of a nondeterministic finite-state machine (aka syntax charts or
  1514. X * "railroad normal form" in parsing technology).  Each node is an opcode
  1515. X * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  1516. X * all nodes except BRANCH implement concatenation; a "next" pointer with
  1517. X * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  1518. X * have one of the subtle syntax dependencies:  an individual BRANCH (as
  1519. X * opposed to a collection of them) is never concatenated with anything
  1520. X * because of operator precedence.)  The operand of some types of node is
  1521. X * a literal string; for others, it is a node leading into a sub-FSM.  In
  1522. X * particular, the operand of a BRANCH node is the first node of the branch.
  1523. X * (NB this is *not* a tree structure:  the tail of the branch connects
  1524. X * to the thing following the set of BRANCHes.)  The opcodes are:
  1525. X */
  1526. X
  1527. X/* definition    number    opnd?    meaning */
  1528. X#define    END    0    /* no    End of program. */
  1529. X#define    BOL    1    /* no    Match "" at beginning of line. */
  1530. X#define    EOL    2    /* no    Match "" at end of line. */
  1531. X#define    ANY    3    /* no    Match any one character. */
  1532. X#define    ANYOF    4    /* str    Match any character in this string. */
  1533. X#define    ANYBUT    5    /* str    Match any character not in this string. */
  1534. X#define    BRANCH    6    /* node    Match this alternative, or the next... */
  1535. X#define    BACK    7    /* no    Match "", "next" ptr points backward. */
  1536. X#define    EXACTLY    8    /* str    Match this string. */
  1537. X#define    NOTHING    9    /* no    Match empty string. */
  1538. X#define    STAR    10    /* node    Match this (simple) thing 0 or more times. */
  1539. X#define    PLUS    11    /* node    Match this (simple) thing 1 or more times. */
  1540. X#define    WORDA    12    /* no    Match "" at wordchar, where prev is nonword */
  1541. X#define    WORDZ    13    /* no    Match "" at nonwordchar, where prev is word */
  1542. X#define    OPEN    20    /* no    Mark this point in input as start of #n. */
  1543. X            /*    OPEN+1 is number 1, etc. */
  1544. X#define    CLOSE    30    /* no    Analogous to OPEN. */
  1545. X
  1546. X/*
  1547. X * Opcode notes:
  1548. X *
  1549. X * BRANCH    The set of branches constituting a single choice are hooked
  1550. X *        together with their "next" pointers, since precedence prevents
  1551. X *        anything being concatenated to any individual branch.  The
  1552. X *        "next" pointer of the last BRANCH in a choice points to the
  1553. X *        thing following the whole choice.  This is also where the
  1554. X *        final "next" pointer of each individual branch points; each
  1555. X *        branch starts with the operand node of a BRANCH node.
  1556. X *
  1557. X * BACK        Normal "next" pointers all implicitly point forward; BACK
  1558. X *        exists to make loop structures possible.
  1559. X *
  1560. X * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  1561. X *        BRANCH structures using BACK.  Simple cases (one character
  1562. X *        per match) are implemented with STAR and PLUS for speed
  1563. X *        and to minimize recursive plunges.
  1564. X *
  1565. X * OPEN,CLOSE    ...are numbered at compile time.
  1566. X */
  1567. X
  1568. X/*
  1569. X * A node is one char of opcode followed by two chars of "next" pointer.
  1570. X * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  1571. X * value is a positive offset from the opcode of the node containing it.
  1572. X * An operand, if any, simply follows the node.  (Note that much of the
  1573. X * code generation knows about this implicit relationship.)
  1574. X *
  1575. X * Using two bytes for the "next" pointer is vast overkill for most things,
  1576. X * but allows patterns to get big without disasters.
  1577. X */
  1578. X#define    OP(p)    (*(p))
  1579. X#define    NEXT(p)    (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
  1580. X#define    OPERAND(p)    ((p) + 3)
  1581. X
  1582. X/*
  1583. X * See regmagic.h for one further detail of program structure.
  1584. X */
  1585. X
  1586. X
  1587. X/*
  1588. X * Utility definitions.
  1589. X */
  1590. X#ifndef CHARBITS
  1591. X#define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  1592. X#else
  1593. X#define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  1594. X#endif
  1595. X
  1596. X#define    FAIL(m)    { regerror(m); return(NULL); }
  1597. X#define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  1598. X
  1599. X/*
  1600. X * Flags to be passed up and down.
  1601. X */
  1602. X#define    HASWIDTH    01    /* Known never to match null string. */
  1603. X#define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  1604. X#define    SPSTART        04    /* Starts with * or +. */
  1605. X#define    WORST        0    /* Worst case. */
  1606. X
  1607. X/*
  1608. X * Global work variables for regcomp().
  1609. X */
  1610. static char *regparse;        /* Input-scan pointer. */
  1611. static int regnpar;        /* () count. */
  1612. static char regdummy;
  1613. static char *regcode;        /* Code-emit pointer; ®dummy = don't. */
  1614. static long regsize;        /* Code size. */
  1615. X
  1616. X/*
  1617. X * Forward declarations for regcomp()'s friends.
  1618. X */
  1619. X#ifndef STATIC
  1620. X#define    STATIC    static
  1621. X#endif
  1622. STATIC char *reg();
  1623. STATIC char *regbranch();
  1624. STATIC char *regpiece();
  1625. STATIC char *regatom();
  1626. STATIC char *regnode();
  1627. STATIC char *regnext();
  1628. STATIC void regc();
  1629. STATIC void reginsert();
  1630. STATIC void regtail();
  1631. STATIC void regoptail();
  1632. X#ifdef STRCSPN
  1633. STATIC int strcspn();
  1634. X#endif
  1635. X
  1636. X/*
  1637. X - regcomp - compile a regular expression into internal code
  1638. X *
  1639. X * We can't allocate space until we know how big the compiled form will be,
  1640. X * but we can't compile it (and thus know how big it is) until we've got a
  1641. X * place to put the code.  So we cheat:  we compile it twice, once with code
  1642. X * generation turned off and size counting turned on, and once "for real".
  1643. X * This also means that we don't allocate space until we are sure that the
  1644. X * thing really will compile successfully, and we never have to move the
  1645. X * code and thus invalidate pointers into it.  (Note that it has to be in
  1646. X * one piece because free() must be able to free it all.)
  1647. X *
  1648. X * Beware that the optimization-preparation code in here knows about some
  1649. X * of the structure of the compiled regexp.
  1650. X */
  1651. regexp *
  1652. regcomp(exp)
  1653. char *exp;
  1654. X{
  1655. X    register regexp *r;
  1656. X    register char *scan;
  1657. X    register char *longest;
  1658. X    register int len;
  1659. X    int flags;
  1660. X    extern char *malloc();
  1661. X
  1662. X    if (exp == NULL)
  1663. X        FAIL("NULL argument");
  1664. X
  1665. X    /* First pass: determine size, legality. */
  1666. X    if (exp[0] == '.' && exp[1] == '*') exp += 2;  /* aid grep */
  1667. X    regparse = exp;
  1668. X    regnpar = 1;
  1669. X    regsize = 0L;
  1670. X    regcode = ®dummy;
  1671. X    regc(MAGIC);
  1672. X    if (reg(0, &flags) == NULL)
  1673. X        return(NULL);
  1674. X
  1675. X    /* Small enough for pointer-storage convention? */
  1676. X    if (regsize >= 32767L)        /* Probably could be 65535L. */
  1677. X        FAIL("regexp too big");
  1678. X
  1679. X    /* Allocate space. */
  1680. X    r = (regexp *)malloc(sizeof(regexp) + (unsigned)regsize);
  1681. X    if (r == NULL)
  1682. X        FAIL("out of space");
  1683. X
  1684. X    /* Second pass: emit code. */
  1685. X    regparse = exp;
  1686. X    regnpar = 1;
  1687. X    regcode = r->program;
  1688. X    regc(MAGIC);
  1689. X    if (reg(0, &flags) == NULL)
  1690. X        return(NULL);
  1691. X
  1692. X    /* Dig out information for optimizations. */
  1693. X    r->regstart = '\0';    /* Worst-case defaults. */
  1694. X    r->reganch = 0;
  1695. X    r->regmust = NULL;
  1696. X    r->regmlen = 0;
  1697. X    scan = r->program+1;            /* First BRANCH. */
  1698. X    if (OP(regnext(scan)) == END) {        /* Only one top-level choice. */
  1699. X        scan = OPERAND(scan);
  1700. X
  1701. X        /* Starting-point info. */
  1702. X        if (OP(scan) == EXACTLY)
  1703. X            r->regstart = *OPERAND(scan);
  1704. X        else if (OP(scan) == BOL)
  1705. X            r->reganch++;
  1706. X
  1707. X        /*
  1708. X         * If there's something expensive in the r.e., find the
  1709. X         * longest literal string that must appear and make it the
  1710. X         * regmust.  Resolve ties in favor of later strings, since
  1711. X         * the regstart check works with the beginning of the r.e.
  1712. X         * and avoiding duplication strengthens checking.  Not a
  1713. X         * strong reason, but sufficient in the absence of others.
  1714. X         */
  1715. X        if (flags&SPSTART) {
  1716. X            longest = NULL;
  1717. X            len = 0;
  1718. X            for (; scan != NULL; scan = regnext(scan))
  1719. X                if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
  1720. X                    longest = OPERAND(scan);
  1721. X                    len = strlen(OPERAND(scan));
  1722. X                }
  1723. X            r->regmust = longest;
  1724. X            r->regmlen = len;
  1725. X        }
  1726. X    }
  1727. X
  1728. X    return(r);
  1729. X}
  1730. X
  1731. X/*
  1732. X - reg - regular expression, i.e. main body or parenthesized thing
  1733. X *
  1734. X * Caller must absorb opening parenthesis.
  1735. X *
  1736. X * Combining parenthesis handling with the base level of regular expression
  1737. X * is a trifle forced, but the need to tie the tails of the branches to what
  1738. X * follows makes it hard to avoid.
  1739. X */
  1740. static char *
  1741. reg(paren, flagp)
  1742. int paren;            /* Parenthesized? */
  1743. int *flagp;
  1744. X{
  1745. X    register char *ret;
  1746. X    register char *br;
  1747. X    register char *ender;
  1748. X    register int parno;
  1749. X    int flags;
  1750. X
  1751. X    *flagp = HASWIDTH;    /* Tentatively. */
  1752. X
  1753. X    /* Make an OPEN node, if parenthesized. */
  1754. X    if (paren) {
  1755. X        if (regnpar >= NSUBEXP)
  1756. X            FAIL("too many ()");
  1757. X        parno = regnpar;
  1758. X        regnpar++;
  1759. X        ret = regnode(OPEN+parno);
  1760. X    } else
  1761. X        ret = NULL;
  1762. X
  1763. X    /* Pick up the branches, linking them together. */
  1764. X    br = regbranch(&flags);
  1765. X    if (br == NULL)
  1766. X        return(NULL);
  1767. X    if (ret != NULL)
  1768. X        regtail(ret, br);    /* OPEN -> first. */
  1769. X    else
  1770. X        ret = br;
  1771. X    if (!(flags&HASWIDTH))
  1772. X        *flagp &= ~HASWIDTH;
  1773. X    *flagp |= flags&SPSTART;
  1774. X    while (*regparse == '|' || *regparse == '\n') {
  1775. X        regparse++;
  1776. X        br = regbranch(&flags);
  1777. X        if (br == NULL)
  1778. X            return(NULL);
  1779. X        regtail(ret, br);    /* BRANCH -> BRANCH. */
  1780. X        if (!(flags&HASWIDTH))
  1781. X            *flagp &= ~HASWIDTH;
  1782. X        *flagp |= flags&SPSTART;
  1783. X    }
  1784. X
  1785. X    /* Make a closing node, and hook it on the end. */
  1786. X    ender = regnode((paren) ? CLOSE+parno : END);    
  1787. X    regtail(ret, ender);
  1788. X
  1789. X    /* Hook the tails of the branches to the closing node. */
  1790. X    for (br = ret; br != NULL; br = regnext(br))
  1791. X        regoptail(br, ender);
  1792. X
  1793. X    /* Check for proper termination. */
  1794. X    if (paren && *regparse++ != ')') {
  1795. X        FAIL("unmatched ()");
  1796. X    } else if (!paren && *regparse != '\0') {
  1797. X        if (*regparse == ')') {
  1798. X            FAIL("unmatched ()");
  1799. X        } else
  1800. X            FAIL("junk on end");    /* "Can't happen". */
  1801. X        /* NOTREACHED */
  1802. X    }
  1803. X
  1804. X    return(ret);
  1805. X}
  1806. X
  1807. X/*
  1808. X - regbranch - one alternative of an | operator
  1809. X *
  1810. X * Implements the concatenation operator.
  1811. X */
  1812. static char *
  1813. regbranch(flagp)
  1814. int *flagp;
  1815. X{
  1816. X    register char *ret;
  1817. X    register char *chain;
  1818. X    register char *latest;
  1819. X    int flags;
  1820. X
  1821. X    *flagp = WORST;        /* Tentatively. */
  1822. X
  1823. X    ret = regnode(BRANCH);
  1824. X    chain = NULL;
  1825. X    while (*regparse != '\0' && *regparse != ')' &&
  1826. X           *regparse != '\n' && *regparse != '|') {
  1827. X        latest = regpiece(&flags);
  1828. X        if (latest == NULL)
  1829. X            return(NULL);
  1830. X        *flagp |= flags&HASWIDTH;
  1831. X        if (chain == NULL)    /* First piece. */
  1832. X            *flagp |= flags&SPSTART;
  1833. X        else
  1834. X            regtail(chain, latest);
  1835. X        chain = latest;
  1836. X    }
  1837. X    if (chain == NULL)    /* Loop ran zero times. */
  1838. X        (void) regnode(NOTHING);
  1839. X
  1840. X    return(ret);
  1841. X}
  1842. X
  1843. X/*
  1844. X - regpiece - something followed by possible [*+?]
  1845. X *
  1846. X * Note that the branching code sequences used for ? and the general cases
  1847. X * of * and + are somewhat optimized:  they use the same NOTHING node as
  1848. X * both the endmarker for their branch list and the body of the last branch.
  1849. X * It might seem that this node could be dispensed with entirely, but the
  1850. X * endmarker role is not redundant.
  1851. X */
  1852. static char *
  1853. regpiece(flagp)
  1854. int *flagp;
  1855. X{
  1856. X    register char *ret;
  1857. X    register char op;
  1858. X    register char *next;
  1859. X    int flags;
  1860. X
  1861. X    ret = regatom(&flags);
  1862. X    if (ret == NULL)
  1863. X        return(NULL);
  1864. X
  1865. X    op = *regparse;
  1866. X    if (!ISMULT(op)) {
  1867. X        *flagp = flags;
  1868. X        return(ret);
  1869. X    }
  1870. X
  1871. X    if (!(flags&HASWIDTH) && op != '?')
  1872. X        FAIL("*+ operand could be empty");
  1873. X    *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
  1874. X
  1875. X    if (op == '*' && (flags&SIMPLE))
  1876. X        reginsert(STAR, ret);
  1877. X    else if (op == '*') {
  1878. X        /* Emit x* as (x&|), where & means "self". */
  1879. X        reginsert(BRANCH, ret);            /* Either x */
  1880. X        regoptail(ret, regnode(BACK));        /* and loop */
  1881. X        regoptail(ret, ret);            /* back */
  1882. X        regtail(ret, regnode(BRANCH));        /* or */
  1883. X        regtail(ret, regnode(NOTHING));        /* null. */
  1884. X    } else if (op == '+' && (flags&SIMPLE))
  1885. X        reginsert(PLUS, ret);
  1886. X    else if (op == '+') {
  1887. X        /* Emit x+ as x(&|), where & means "self". */
  1888. X        next = regnode(BRANCH);            /* Either */
  1889. X        regtail(ret, next);
  1890. X        regtail(regnode(BACK), ret);        /* loop back */
  1891. X        regtail(next, regnode(BRANCH));        /* or */
  1892. X        regtail(ret, regnode(NOTHING));        /* null. */
  1893. X    } else if (op == '?') {
  1894. X        /* Emit x? as (x|) */
  1895. X        reginsert(BRANCH, ret);            /* Either x */
  1896. X        regtail(ret, regnode(BRANCH));        /* or */
  1897. X        next = regnode(NOTHING);        /* null. */
  1898. X        regtail(ret, next);
  1899. X        regoptail(ret, next);
  1900. X    }
  1901. X    regparse++;
  1902. X    if (ISMULT(*regparse))
  1903. X        FAIL("nested *?+");
  1904. X
  1905. X    return(ret);
  1906. X}
  1907. X
  1908. X/*
  1909. X - regatom - the lowest level
  1910. X *
  1911. X * Optimization:  gobbles an entire sequence of ordinary characters so that
  1912. X * it can turn them into a single node, which is smaller to store and
  1913. X * faster to run.  Backslashed characters are exceptions, each becoming a
  1914. X * separate node; the code is simpler that way and it's not worth fixing.
  1915. X */
  1916. static char *
  1917. regatom(flagp)
  1918. int *flagp;
  1919. X{
  1920. X    register char *ret;
  1921. X    int flags;
  1922. X
  1923. X    *flagp = WORST;        /* Tentatively. */
  1924. X
  1925. X    switch (*regparse++) {
  1926. X    /* FIXME: these chars only have meaning at beg/end of pat? */
  1927. X    case '^':
  1928. X        ret = regnode(BOL);
  1929. X        break;
  1930. X    case '$':
  1931. X        ret = regnode(EOL);
  1932. X        break;
  1933. X    case '.':
  1934. X        ret = regnode(ANY);
  1935. X        *flagp |= HASWIDTH|SIMPLE;
  1936. X        break;
  1937. X    case '[': {
  1938. X            register int class;
  1939. X            register int classend;
  1940. X
  1941. X            if (*regparse == '^') {    /* Complement of range. */
  1942. X                ret = regnode(ANYBUT);
  1943. X                regparse++;
  1944. X            } else
  1945. X                ret = regnode(ANYOF);
  1946. X            if (*regparse == ']' || *regparse == '-')
  1947. X                regc(*regparse++);
  1948. X            while (*regparse != '\0' && *regparse != ']') {
  1949. X                if (*regparse == '-') {
  1950. X                    regparse++;
  1951. X                    if (*regparse == ']' || *regparse == '\0')
  1952. X                        regc('-');
  1953. X                    else {
  1954. X                        class = UCHARAT(regparse-2)+1;
  1955. X                        classend = UCHARAT(regparse);
  1956. X                        if (class > classend+1)
  1957. X                            FAIL("invalid [] range");
  1958. X                        for (; class <= classend; class++)
  1959. X                            regc(class);
  1960. X                        regparse++;
  1961. X                    }
  1962. X                } else
  1963. X                    regc(*regparse++);
  1964. X            }
  1965. X            regc('\0');
  1966. X            if (*regparse != ']')
  1967. X                FAIL("unmatched []");
  1968. X            regparse++;
  1969. X            *flagp |= HASWIDTH|SIMPLE;
  1970. X        }
  1971. X        break;
  1972. X    case '(':
  1973. X        ret = reg(1, &flags);
  1974. X        if (ret == NULL)
  1975. X            return(NULL);
  1976. X        *flagp |= flags&(HASWIDTH|SPSTART);
  1977. X        break;
  1978. X    case '\0':
  1979. X    case '|':
  1980. X    case '\n':
  1981. X    case ')':
  1982. X        FAIL("internal urp");    /* Supposed to be caught earlier. */
  1983. X        break;
  1984. X    case '?':
  1985. X    case '+':
  1986. X    case '*':
  1987. X        FAIL("?+* follows nothing");
  1988. X        break;
  1989. X    case '\\':
  1990. X        switch (*regparse++) {
  1991. X        case '\0':
  1992. X            FAIL("trailing \\");
  1993. X            break;
  1994. X        case '<':
  1995. X            ret = regnode(WORDA);
  1996. X            break;
  1997. X        case '>':
  1998. X            ret = regnode(WORDZ);
  1999. X            break;
  2000. X        /* FIXME: Someday handle \1, \2, ... */
  2001. X        default:
  2002. X            /* Handle general quoted chars in exact-match routine */
  2003. X            goto de_fault;
  2004. X        }
  2005. X        break;
  2006. X    de_fault:
  2007. X    default:
  2008. X        /*
  2009. X         * Encode a string of characters to be matched exactly.
  2010. X         *
  2011. X         * This is a bit tricky due to quoted chars and due to
  2012. X         * '*', '+', and '?' taking the SINGLE char previous
  2013. X         * as their operand.
  2014. X         *
  2015. X         * On entry, the char at regparse[-1] is going to go
  2016. X         * into the string, no matter what it is.  (It could be
  2017. X         * following a \ if we are entered from the '\' case.)
  2018. X         * 
  2019. X         * Basic idea is to pick up a good char in  ch  and
  2020. X         * examine the next char.  If it's *+? then we twiddle.
  2021. X         * If it's \ then we frozzle.  If it's other magic char
  2022. X         * we push  ch  and terminate the string.  If none of the
  2023. X         * above, we push  ch  on the string and go around again.
  2024. X         *
  2025. X         *  regprev  is used to remember where "the current char"
  2026. X         * starts in the string, if due to a *+? we need to back
  2027. X         * up and put the current char in a separate, 1-char, string.
  2028. X         * When  regprev  is NULL,  ch  is the only char in the
  2029. X         * string; this is used in *+? handling, and in setting
  2030. X         * flags |= SIMPLE at the end.
  2031. X         */
  2032. X        {
  2033. X            char *regprev;
  2034. X            register char ch;
  2035. X
  2036. X            regparse--;            /* Look at cur char */
  2037. X            ret = regnode(EXACTLY);
  2038. X            for ( regprev = 0 ; ; ) {
  2039. X                ch = *regparse++;    /* Get current char */
  2040. X                switch (*regparse) {    /* look at next one */
  2041. X
  2042. X                default:
  2043. X                    regc(ch);    /* Add cur to string */
  2044. X                    break;
  2045. X
  2046. X                case '.': case '[': case '(':
  2047. X                case ')': case '|': case '\n':
  2048. X                case '$': case '^':
  2049. X                case '\0':
  2050. X                /* FIXME, $ and ^ should not always be magic */
  2051. X                magic:
  2052. X                    regc(ch);    /* dump cur char */
  2053. X                    goto done;    /* and we are done */
  2054. X
  2055. X                case '?': case '+': case '*':
  2056. X                    if (!regprev)     /* If just ch in str, */
  2057. X                        goto magic;    /* use it */
  2058. X                    /* End mult-char string one early */
  2059. X                    regparse = regprev; /* Back up parse */
  2060. X                    goto done;
  2061. X
  2062. X                case '\\':
  2063. X                    regc(ch);    /* Cur char OK */
  2064. X                    switch (regparse[1]){ /* Look after \ */
  2065. X                    case '\0':
  2066. X                    case '<':
  2067. X                    case '>':
  2068. X                    /* FIXME: Someday handle \1, \2, ... */
  2069. X                        goto done; /* Not quoted */
  2070. X                    default:
  2071. X                        /* Backup point is \, scan                             * point is after it. */
  2072. X                        regprev = regparse;
  2073. X                        regparse++; 
  2074. X                        continue;    /* NOT break; */
  2075. X                    }
  2076. X                }
  2077. X                regprev = regparse;    /* Set backup point */
  2078. X            }
  2079. X        done:
  2080. X            regc('\0');
  2081. X            *flagp |= HASWIDTH;
  2082. X            if (!regprev)        /* One char? */
  2083. X                *flagp |= SIMPLE;
  2084. X        }
  2085. X        break;
  2086. X    }
  2087. X
  2088. X    return(ret);
  2089. X}
  2090. X
  2091. X/*
  2092. X - regnode - emit a node
  2093. X */
  2094. static char *            /* Location. */
  2095. regnode(op)
  2096. char op;
  2097. X{
  2098. X    register char *ret;
  2099. X    register char *ptr;
  2100. X
  2101. X    ret = regcode;
  2102. X    if (ret == ®dummy) {
  2103. X        regsize += 3;
  2104. X        return(ret);
  2105. X    }
  2106. X
  2107. X    ptr = ret;
  2108. X    *ptr++ = op;
  2109. X    *ptr++ = '\0';        /* Null "next" pointer. */
  2110. X    *ptr++ = '\0';
  2111. X    regcode = ptr;
  2112. X
  2113. X    return(ret);
  2114. X}
  2115. X
  2116. X/*
  2117. X - regc - emit (if appropriate) a byte of code
  2118. X */
  2119. static void
  2120. regc(b)
  2121. char b;
  2122. X{
  2123. X    if (regcode != ®dummy)
  2124. X        *regcode++ = b;
  2125. X    else
  2126. X        regsize++;
  2127. X}
  2128. X
  2129. X/*
  2130. X - reginsert - insert an operator in front of already-emitted operand
  2131. X *
  2132. X * Means relocating the operand.
  2133. X */
  2134. static void
  2135. reginsert(op, opnd)
  2136. char op;
  2137. char *opnd;
  2138. X{
  2139. X    register char *src;
  2140. X    register char *dst;
  2141. X    register char *place;
  2142. X
  2143. X    if (regcode == ®dummy) {
  2144. X        regsize += 3;
  2145. X        return;
  2146. X    }
  2147. X
  2148. X    src = regcode;
  2149. X    regcode += 3;
  2150. X    dst = regcode;
  2151. X    while (src > opnd)
  2152. X        *--dst = *--src;
  2153. X
  2154. X    place = opnd;        /* Op node, where operand used to be. */
  2155. X    *place++ = op;
  2156. X    *place++ = '\0';
  2157. X    *place++ = '\0';
  2158. X}
  2159. X
  2160. X/*
  2161. X - regtail - set the next-pointer at the end of a node chain
  2162. X */
  2163. static void
  2164. regtail(p, val)
  2165. char *p;
  2166. char *val;
  2167. X{
  2168. X    register char *scan;
  2169. X    register char *temp;
  2170. X    register int offset;
  2171. X
  2172. X    if (p == ®dummy)
  2173. X        return;
  2174. X
  2175. X    /* Find last node. */
  2176. X    scan = p;
  2177. X    for (;;) {
  2178. X        temp = regnext(scan);
  2179. X        if (temp == NULL)
  2180. X            break;
  2181. X        scan = temp;
  2182. X    }
  2183. X
  2184. X    if (OP(scan) == BACK)
  2185. X        offset = scan - val;
  2186. X    else
  2187. X        offset = val - scan;
  2188. X    *(scan+1) = (offset>>8)&0377;
  2189. X    *(scan+2) = offset&0377;
  2190. X}
  2191. X
  2192. X/*
  2193. X - regoptail - regtail on operand of first argument; nop if operandless
  2194. X */
  2195. static void
  2196. regoptail(p, val)
  2197. char *p;
  2198. char *val;
  2199. X{
  2200. X    /* "Operandless" and "op != BRANCH" are synonymous in practice. */
  2201. X    if (p == NULL || p == ®dummy || OP(p) != BRANCH)
  2202. X        return;
  2203. X    regtail(OPERAND(p), val);
  2204. X}
  2205. X
  2206. X/*
  2207. X * regexec and friends
  2208. X */
  2209. X
  2210. X/*
  2211. X * Global work variables for regexec().
  2212. X */
  2213. static char *reginput;        /* String-input pointer. */
  2214. static char *regbol;        /* Beginning of input, for ^ check. */
  2215. static char **regstartp;    /* Pointer to startp array. */
  2216. static char **regendp;        /* Ditto for endp. */
  2217. X
  2218. X/*
  2219. X * Forwards.
  2220. X */
  2221. STATIC int regtry();
  2222. STATIC int regmatch();
  2223. STATIC int regrepeat();
  2224. X
  2225. X#ifdef DEBUG
  2226. int regnarrate = 0;
  2227. void regdump();
  2228. STATIC char *regprop();
  2229. X#endif
  2230. X
  2231. X/*
  2232. X - regexec - match a regexp against a string
  2233. X */
  2234. int
  2235. regexec(prog, string)
  2236. register regexp *prog;
  2237. register char *string;
  2238. X{
  2239. X    register char *s;
  2240. X    extern char *strchr();
  2241. X
  2242. X    /* Be paranoid... */
  2243. X    if (prog == NULL || string == NULL) {
  2244. X        regerror("NULL parameter");
  2245. X        return(0);
  2246. X    }
  2247. X
  2248. X    /* Check validity of program. */
  2249. X    if (UCHARAT(prog->program) != MAGIC) {
  2250. X        regerror("corrupted program");
  2251. X        return(0);
  2252. X    }
  2253. X
  2254. X    /* If there is a "must appear" string, look for it. */
  2255. X    if (prog->regmust != NULL) {
  2256. X        s = string;
  2257. X        while ((s = strchr(s, prog->regmust[0])) != NULL) {
  2258. X            if (strncmp(s, prog->regmust, prog->regmlen) == 0)
  2259. X                break;    /* Found it. */
  2260. X            s++;
  2261. X        }
  2262. X        if (s == NULL)    /* Not present. */
  2263. X            return(0);
  2264. X    }
  2265. X
  2266. X    /* Mark beginning of line for ^ . */
  2267. X    regbol = string;
  2268. X
  2269. X    /* Simplest case:  anchored match need be tried only once. */
  2270. X    if (prog->reganch)
  2271. X        return(regtry(prog, string));
  2272. X
  2273. X    /* Messy cases:  unanchored match. */
  2274. X    s = string;
  2275. X    if (prog->regstart != '\0')
  2276. X        /* We know what char it must start with. */
  2277. X        while ((s = strchr(s, prog->regstart)) != NULL) {
  2278. X            if (regtry(prog, s))
  2279. X                return(1);
  2280. X            s++;
  2281. X        }
  2282. X    else
  2283. X        /* We don't -- general case. */
  2284. X        do {
  2285. X            if (regtry(prog, s))
  2286. X                return(1);
  2287. X        } while (*s++ != '\0');
  2288. X
  2289. X    /* Failure. */
  2290. X    return(0);
  2291. X}
  2292. X
  2293. X/*
  2294. X - regtry - try match at specific point
  2295. X */
  2296. static int            /* 0 failure, 1 success */
  2297. regtry(prog, string)
  2298. regexp *prog;
  2299. char *string;
  2300. X{
  2301. X    register int i;
  2302. X    register char **sp;
  2303. X    register char **ep;
  2304. X
  2305. X    reginput = string;
  2306. X    regstartp = prog->startp;
  2307. X    regendp = prog->endp;
  2308. X
  2309. X    sp = prog->startp;
  2310. X    ep = prog->endp;
  2311. X    for (i = NSUBEXP; i > 0; i--) {
  2312. X        *sp++ = NULL;
  2313. X        *ep++ = NULL;
  2314. X    }
  2315. X    if (regmatch(prog->program + 1)) {
  2316. X        prog->startp[0] = string;
  2317. X        prog->endp[0] = reginput;
  2318. X        return(1);
  2319. X    } else
  2320. X        return(0);
  2321. X}
  2322. X
  2323. X/*
  2324. X - regmatch - main matching routine
  2325. X *
  2326. X * Conceptually the strategy is simple:  check to see whether the current
  2327. X * node matches, call self recursively to see whether the rest matches,
  2328. X * and then act accordingly.  In practice we make some effort to avoid
  2329. X * recursion, in particular by going through "ordinary" nodes (that don't
  2330. X * need to know whether the rest of the match failed) by a loop instead of
  2331. X * by recursion.
  2332. X */
  2333. static int            /* 0 failure, 1 success */
  2334. regmatch(prog)
  2335. char *prog;
  2336. X{
  2337. X    register char *scan;    /* Current node. */
  2338. X    char *next;        /* Next node. */
  2339. X    extern char *strchr();
  2340. X
  2341. X    scan = prog;
  2342. X#ifdef DEBUG
  2343. X    if (scan != NULL && regnarrate)
  2344. X        fprintf(stderr, "%s(\n", regprop(scan));
  2345. X#endif
  2346. X    while (scan != NULL) {
  2347. X#ifdef DEBUG
  2348. X        if (regnarrate)
  2349. X            fprintf(stderr, "%s...\n", regprop(scan));
  2350. X#endif
  2351. X        next = regnext(scan);
  2352. X
  2353. X        switch (OP(scan)) {
  2354. X        case BOL:
  2355. X            if (reginput != regbol)
  2356. X                return(0);
  2357. X            break;
  2358. X        case EOL:
  2359. X            if (*reginput != '\0')
  2360. X                return(0);
  2361. X            break;
  2362. X        case WORDA:
  2363. X            /* Must be looking at a letter, digit, or _ */
  2364. X            if ((!isalnum(*reginput)) && *reginput != '_')
  2365. X                return(0);
  2366. X            /* Prev must be BOL or nonword */
  2367. X            if (reginput > regbol &&
  2368. X                (isalnum(reginput[-1]) || reginput[-1] == '_'))
  2369. X                return(0);
  2370. X            break;
  2371. X        case WORDZ:
  2372. X            /* Must be looking at non letter, digit, or _ */
  2373. X            if (isalnum(*reginput) || *reginput == '_')
  2374. X                return(0);
  2375. X            /* We don't care what the previous char was */
  2376. X            break;
  2377. X        case ANY:
  2378. X            if (*reginput == '\0')
  2379. X                return(0);
  2380. X            reginput++;
  2381. X            break;
  2382. X        case EXACTLY: {
  2383. X                register int len;
  2384. X                register char *opnd;
  2385. X
  2386. X                opnd = OPERAND(scan);
  2387. X                /* Inline the first character, for speed. */
  2388. X                if (*opnd != *reginput)
  2389. X                    return(0);
  2390. X                len = strlen(opnd);
  2391. X                if (len > 1 && strncmp(opnd, reginput, len) != 0)
  2392. X                    return(0);
  2393. X                reginput += len;
  2394. X            }
  2395. X            break;
  2396. X        case ANYOF:
  2397. X             if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
  2398. X                return(0);
  2399. X            reginput++;
  2400. X            break;
  2401. X        case ANYBUT:
  2402. X             if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
  2403. X                return(0);
  2404. X            reginput++;
  2405. X            break;
  2406. X        case NOTHING:
  2407. X            break;
  2408. X        case BACK:
  2409. X            break;
  2410. X        case OPEN+1:
  2411. X        case OPEN+2:
  2412. X        case OPEN+3:
  2413. X        case OPEN+4:
  2414. X        case OPEN+5:
  2415. X        case OPEN+6:
  2416. X        case OPEN+7:
  2417. X        case OPEN+8:
  2418. X        case OPEN+9: {
  2419. X                register int no;
  2420. X                register char *save;
  2421. X
  2422. X                no = OP(scan) - OPEN;
  2423. X                save = reginput;
  2424. X
  2425. X                if (regmatch(next)) {
  2426. X                    /*
  2427. X                     * Don't set startp if some later
  2428. X                     * invocation of the same parentheses
  2429. X                     * already has.
  2430. X                     */
  2431. X                    if (regstartp[no] == NULL)
  2432. X                        regstartp[no] = save;
  2433. X                    return(1);
  2434. X                } else
  2435. X                    return(0);
  2436. X            }
  2437. X            break;
  2438. X        case CLOSE+1:
  2439. X        case CLOSE+2:
  2440. X        case CLOSE+3:
  2441. X        case CLOSE+4:
  2442. X        case CLOSE+5:
  2443. X        case CLOSE+6:
  2444. X        case CLOSE+7:
  2445. X        case CLOSE+8:
  2446. X        case CLOSE+9: {
  2447. X                register int no;
  2448. X                register char *save;
  2449. X
  2450. X                no = OP(scan) - CLOSE;
  2451. X                save = reginput;
  2452. X
  2453. X                if (regmatch(next)) {
  2454. X                    /*
  2455. X                     * Don't set endp if some later
  2456. X                     * invocation of the same parentheses
  2457. X                     * already has.
  2458. X                     */
  2459. X                    if (regendp[no] == NULL)
  2460. X                        regendp[no] = save;
  2461. X                    return(1);
  2462. X                } else
  2463. X                    return(0);
  2464. X            }
  2465. X            break;
  2466. X        case BRANCH: {
  2467. X                register char *save;
  2468. X
  2469. X                if (OP(next) != BRANCH)        /* No choice. */
  2470. X                    next = OPERAND(scan);    /* Avoid recursion. */
  2471. X                else {
  2472. X                    do {
  2473. X                        save = reginput;
  2474. X                        if (regmatch(OPERAND(scan)))
  2475. X                            return(1);
  2476. X                        reginput = save;
  2477. X                        scan = regnext(scan);
  2478. X                    } while (scan != NULL && OP(scan) == BRANCH);
  2479. X                    return(0);
  2480. X                    /* NOTREACHED */
  2481. X                }
  2482. X            }
  2483. X            break;
  2484. X        case STAR:
  2485. X        case PLUS: {
  2486. X                register char nextch;
  2487. X                register int no;
  2488. X                register char *save;
  2489. X                register int min;
  2490. X
  2491. X                /*
  2492. X                 * Lookahead to avoid useless match attempts
  2493. X                 * when we know what character comes next.
  2494. X                 */
  2495. X                nextch = '\0';
  2496. X                if (OP(next) == EXACTLY)
  2497. X                    nextch = *OPERAND(next);
  2498. X                min = (OP(scan) == STAR) ? 0 : 1;
  2499. X                save = reginput;
  2500. X                no = regrepeat(OPERAND(scan));
  2501. X                while (no >= min) {
  2502. X                    /* If it could work, try it. */
  2503. X                    if (nextch == '\0' || *reginput == nextch)
  2504. X                        if (regmatch(next))
  2505. X                            return(1);
  2506. X                    /* Couldn't or didn't -- back up. */
  2507. X                    no--;
  2508. X                    reginput = save + no;
  2509. X                }
  2510. X                return(0);
  2511. X            }
  2512. X            break;
  2513. X        case END:
  2514. X            return(1);    /* Success! */
  2515. X            break;
  2516. X        default:
  2517. X            regerror("memory corruption");
  2518. X            return(0);
  2519. X            break;
  2520. X        }
  2521. X
  2522. X        scan = next;
  2523. X    }
  2524. X
  2525. X    /*
  2526. X     * We get here only if there's trouble -- normally "case END" is
  2527. X     * the terminating point.
  2528. X     */
  2529. X    regerror("corrupted pointers");
  2530. X    return(0);
  2531. X}
  2532. X
  2533. X/*
  2534. X - regrepeat - repeatedly match something simple, report how many
  2535. X */
  2536. static int
  2537. regrepeat(p)
  2538. char *p;
  2539. X{
  2540. X    register int count = 0;
  2541. X    register char *scan;
  2542. X    register char *opnd;
  2543. X
  2544. X    scan = reginput;
  2545. X    opnd = OPERAND(p);
  2546. X    switch (OP(p)) {
  2547. X    case ANY:
  2548. X        count = strlen(scan);
  2549. X        scan += count;
  2550. X        break;
  2551. X    case EXACTLY:
  2552. X        while (*opnd == *scan) {
  2553. X            count++;
  2554. X            scan++;
  2555. X        }
  2556. X        break;
  2557. X    case ANYOF:
  2558. X        while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
  2559. X            count++;
  2560. X            scan++;
  2561. X        }
  2562. X        break;
  2563. X    case ANYBUT:
  2564. X        while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
  2565. X            count++;
  2566. X            scan++;
  2567. X        }
  2568. X        break;
  2569. X    default:        /* Oh dear.  Called inappropriately. */
  2570. X        regerror("internal foulup");
  2571. X        count = 0;    /* Best compromise. */
  2572. X        break;
  2573. X    }
  2574. X    reginput = scan;
  2575. X
  2576. X    return(count);
  2577. X}
  2578. X
  2579. X/*
  2580. X - regnext - dig the "next" pointer out of a node
  2581. X */
  2582. static char *
  2583. regnext(p)
  2584. register char *p;
  2585. X{
  2586. X    register int offset;
  2587. X
  2588. X    if (p == ®dummy)
  2589. X        return(NULL);
  2590. X
  2591. X    offset = NEXT(p);
  2592. X    if (offset == 0)
  2593. X        return(NULL);
  2594. X
  2595. X    if (OP(p) == BACK)
  2596. X        return(p-offset);
  2597. X    else
  2598. X        return(p+offset);
  2599. X}
  2600. X
  2601. X#ifdef DEBUG
  2602. X
  2603. STATIC char *regprop();
  2604. X
  2605. X/*
  2606. X - regdump - dump a regexp onto stdout in vaguely comprehensible form
  2607. X */
  2608. void
  2609. regdump(r)
  2610. regexp *r;
  2611. X{
  2612. X    register char *s;
  2613. X    register char op = EXACTLY;    /* Arbitrary non-END op. */
  2614. X    register char *next;
  2615. X    extern char *strchr();
  2616. X
  2617. X
  2618. X    s = r->program + 1;
  2619. X    while (op != END) {    /* While that wasn't END last time... */
  2620. X        op = OP(s);
  2621. X        printf("%2d%s", s-r->program, regprop(s));    /* Where, what. */
  2622. X        next = regnext(s);
  2623. X        if (next == NULL)        /* Next ptr. */
  2624. X            printf("(0)");
  2625. X        else 
  2626. X            printf("(%d)", (s-r->program)+(next-s));
  2627. X        s += 3;
  2628. X        if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
  2629. X            /* Literal string, where present. */
  2630. X            while (*s != '\0') {
  2631. X                putchar(*s);
  2632. X                s++;
  2633. X            }
  2634. X            s++;
  2635. X        }
  2636. X        putchar('\n');
  2637. X    }
  2638. X
  2639. X    /* Header fields of interest. */
  2640. X    if (r->regstart != '\0')
  2641. X        printf("start `%c' ", r->regstart);
  2642. X    if (r->reganch)
  2643. X        printf("anchored ");
  2644. X    if (r->regmust != NULL)
  2645. X        printf("must have \"%s\"", r->regmust);
  2646. X    printf("\n");
  2647. X}
  2648. X
  2649. X/*
  2650. X - regprop - printable representation of opcode
  2651. X */
  2652. static char *
  2653. regprop(op)
  2654. char *op;
  2655. X{
  2656. X    register char *p;
  2657. X    static char buf[50];
  2658. X
  2659. X    (void) strcpy(buf, ":");
  2660. X
  2661. X    switch (OP(op)) {
  2662. X    case BOL:
  2663. X        p = "BOL";
  2664. X        break;
  2665. X    case EOL:
  2666. X        p = "EOL";
  2667. X        break;
  2668. X    case ANY:
  2669. X        p = "ANY";
  2670. X        break;
  2671. X    case ANYOF:
  2672. X        p = "ANYOF";
  2673. X        break;
  2674. X    case ANYBUT:
  2675. X        p = "ANYBUT";
  2676. X        break;
  2677. X    case BRANCH:
  2678. X        p = "BRANCH";
  2679. X        break;
  2680. X    case EXACTLY:
  2681. X        p = "EXACTLY";
  2682. X        break;
  2683. X    case NOTHING:
  2684. X        p = "NOTHING";
  2685. X        break;
  2686. X    case BACK:
  2687. X        p = "BACK";
  2688. X        break;
  2689. X    case END:
  2690. X        p = "END";
  2691. X        break;
  2692. X    case OPEN+1:
  2693. X    case OPEN+2:
  2694. X    case OPEN+3:
  2695. X    case OPEN+4:
  2696. X    case OPEN+5:
  2697. X    case OPEN+6:
  2698. X    case OPEN+7:
  2699. X    case OPEN+8:
  2700. X    case OPEN+9:
  2701. X        sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
  2702. X        p = NULL;
  2703. X        break;
  2704. X    case CLOSE+1:
  2705. X    case CLOSE+2:
  2706. X    case CLOSE+3:
  2707. X    case CLOSE+4:
  2708. X    case CLOSE+5:
  2709. X    case CLOSE+6:
  2710. X    case CLOSE+7:
  2711. X    case CLOSE+8:
  2712. X    case CLOSE+9:
  2713. X        sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
  2714. X        p = NULL;
  2715. X        break;
  2716. X    case STAR:
  2717. X        p = "STAR";
  2718. X        break;
  2719. X    case PLUS:
  2720. X        p = "PLUS";
  2721. X        break;
  2722. X    case WORDA:
  2723. X        p = "WORDA";
  2724. X        break;
  2725. X    case WORDZ:
  2726. X        p = "WORDZ";
  2727. X        break;
  2728. X    default:
  2729. X        regerror("corrupted opcode");
  2730. X        break;
  2731. X    }
  2732. X    if (p != NULL)
  2733. X        (void) strcat(buf, p);
  2734. X    return(buf);
  2735. X}
  2736. X#endif
  2737. X
  2738. X/*
  2739. X * The following is provided for those people who do not have strcspn() in
  2740. X * their C libraries.  They should get off their butts and do something
  2741. X * about it; at least one public-domain implementation of those (highly
  2742. X * useful) string routines has been published on Usenet.
  2743. X */
  2744. X#ifdef STRCSPN
  2745. X/*
  2746. X * strcspn - find length of initial segment of s1 consisting entirely
  2747. X * of characters not from s2
  2748. X */
  2749. X
  2750. static int
  2751. strcspn(s1, s2)
  2752. char *s1;
  2753. char *s2;
  2754. X{
  2755. X    register char *scan1;
  2756. X    register char *scan2;
  2757. X    register int count;
  2758. X
  2759. X    count = 0;
  2760. X    for (scan1 = s1; *scan1 != '\0'; scan1++) {
  2761. X        for (scan2 = s2; *scan2 != '\0';)    /* ++ moved down. */
  2762. X            if (*scan1 == *scan2++)
  2763. X                return(count);
  2764. X        count++;
  2765. X    }
  2766. X    return(count);
  2767. X}
  2768. X#endif
  2769. END_OF_FILE
  2770. if test 31149 -ne `wc -c <'regexp.c'`; then
  2771.     echo shar: \"'regexp.c'\" unpacked with wrong size!
  2772. fi
  2773. # end of 'regexp.c'
  2774. fi
  2775. if test -f 'regexp.h' -a "${1}" != "-c" ; then 
  2776.   echo shar: Will not clobber existing file \"'regexp.h'\"
  2777. else
  2778. echo shar: Extracting \"'regexp.h'\" \(574 characters\)
  2779. sed "s/^X//" >'regexp.h' <<'END_OF_FILE'
  2780. X/*
  2781. X * Definitions etc. for regexp(3) routines.
  2782. X *
  2783. X * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
  2784. X * not the System V one.
  2785. X */
  2786. X#define NSUBEXP  10
  2787. typedef struct regexp {
  2788. X    char *startp[NSUBEXP];
  2789. X    char *endp[NSUBEXP];
  2790. X    char regstart;        /* Internal use only. */
  2791. X    char reganch;        /* Internal use only. */
  2792. X    char *regmust;        /* Internal use only. */
  2793. X    int regmlen;        /* Internal use only. */
  2794. X    char program[1];    /* Unwarranted chumminess with compiler. */
  2795. X} regexp;
  2796. X
  2797. extern regexp *regcomp();
  2798. extern int regexec();
  2799. extern void regsub();
  2800. extern void regerror();
  2801. END_OF_FILE
  2802. if test 574 -ne `wc -c <'regexp.h'`; then
  2803.     echo shar: \"'regexp.h'\" unpacked with wrong size!
  2804. fi
  2805. # end of 'regexp.h'
  2806. fi
  2807. if test -f 'regmagic.h' -a "${1}" != "-c" ; then 
  2808.   echo shar: Will not clobber existing file \"'regmagic.h'\"
  2809. else
  2810. echo shar: Extracting \"'regmagic.h'\" \(153 characters\)
  2811. sed "s/^X//" >'regmagic.h' <<'END_OF_FILE'
  2812. X/*
  2813. X * The first byte of the regexp internal "program" is actually this magic
  2814. X * number; the start node begins in the second byte.
  2815. X */
  2816. X#define    MAGIC    0234
  2817. END_OF_FILE
  2818. if test 153 -ne `wc -c <'regmagic.h'`; then
  2819.     echo shar: \"'regmagic.h'\" unpacked with wrong size!
  2820. fi
  2821. # end of 'regmagic.h'
  2822. fi
  2823. if test -f 'regsub.c' -a "${1}" != "-c" ; then 
  2824.   echo shar: Will not clobber existing file \"'regsub.c'\"
  2825. else
  2826. echo shar: Extracting \"'regsub.c'\" \(1962 characters\)
  2827. sed "s/^X//" >'regsub.c' <<'END_OF_FILE'
  2828. X/*
  2829. X * regsub
  2830. X *
  2831. X *    Copyright (c) 1986 by University of Toronto.
  2832. X *    Written by Henry Spencer.  Not derived from licensed software.
  2833. X *
  2834. X *    Permission is granted to anyone to use this software for any
  2835. X *    purpose on any computer system, and to redistribute it freely,
  2836. X *    subject to the following restrictions:
  2837. X *
  2838. X *    1. The author is not responsible for the consequences of use of
  2839. X *        this software, no matter how awful, even if they arise
  2840. X *        from defects in it.
  2841. X *
  2842. X *    2. The origin of this software must not be misrepresented, either
  2843. X *        by explicit claim or by omission.
  2844. X *
  2845. X *    3. Altered versions must be plainly marked as such, and must not
  2846. X *        be misrepresented as being the original software.
  2847. X */
  2848. X#include <stdio.h>
  2849. X#include <regexp.h>
  2850. X#include "regmagic.h"
  2851. X
  2852. X#ifndef CHARBITS
  2853. X#define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  2854. X#else
  2855. X#define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  2856. X#endif
  2857. X
  2858. X/*
  2859. X - regsub - perform substitutions after a regexp match
  2860. X */
  2861. void
  2862. regsub(prog, source, dest)
  2863. regexp *prog;
  2864. char *source;
  2865. char *dest;
  2866. X{
  2867. X    register char *src;
  2868. X    register char *dst;
  2869. X    register char c;
  2870. X    register int no;
  2871. X    register int len;
  2872. X    extern char *strncpy();
  2873. X
  2874. X    if (prog == NULL || source == NULL || dest == NULL) {
  2875. X        regerror("NULL parm to regsub");
  2876. X        return;
  2877. X    }
  2878. X    if (UCHARAT(prog->program) != MAGIC) {
  2879. X        regerror("damaged regexp fed to regsub");
  2880. X        return;
  2881. X    }
  2882. X
  2883. X    src = source;
  2884. X    dst = dest;
  2885. X    while ((c = *src++) != '\0') {
  2886. X        if (c == '&')
  2887. X            no = 0;
  2888. X        else if (c == '\\' && '0' <= *src && *src <= '9')
  2889. X            no = *src++ - '0';
  2890. X        else
  2891. X            no = -1;
  2892. X         if (no < 0) {    /* Ordinary character. */
  2893. X             if (c == '\\' && (*src == '\\' || *src == '&'))
  2894. X                 c = *src++;
  2895. X             *dst++ = c;
  2896. X         } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
  2897. X            len = prog->endp[no] - prog->startp[no];
  2898. X            (void) strncpy(dst, prog->startp[no], len);
  2899. X            dst += len;
  2900. X            if (len != 0 && *(dst-1) == '\0') {    /* strncpy hit NUL. */
  2901. X                regerror("damaged match string");
  2902. X                return;
  2903. X            }
  2904. X        }
  2905. X    }
  2906. X    *dst++ = '\0';
  2907. X}
  2908. END_OF_FILE
  2909. if test 1962 -ne `wc -c <'regsub.c'`; then
  2910.     echo shar: \"'regsub.c'\" unpacked with wrong size!
  2911. fi
  2912. # end of 'regsub.c'
  2913. fi
  2914. if test -f 'vfprintf.c' -a "${1}" != "-c" ; then 
  2915.   echo shar: Will not clobber existing file \"'vfprintf.c'\"
  2916. else
  2917. echo shar: Extracting \"'vfprintf.c'\" \(1408 characters\)
  2918. sed "s/^X//" >'vfprintf.c' <<'END_OF_FILE'
  2919. X/*
  2920. X * Copyright (c) 1988 Regents of the University of California.
  2921. X * All rights reserved.
  2922. X *
  2923. X * Redistribution and use in source and binary forms are permitted
  2924. X * provided that the above copyright notice and this paragraph are
  2925. X * duplicated in all such forms and that any documentation,
  2926. X * advertising materials, and other materials related to such
  2927. X * distribution and use acknowledge that the software was developed
  2928. X * by the University of California, Berkeley.  The name of the
  2929. X * University may not be used to endorse or promote products derived
  2930. X * from this software without specific prior written permission.
  2931. X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  2932. X * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  2933. X * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  2934. X */
  2935. X
  2936. X#if defined(LIBC_SCCS) && !defined(lint)
  2937. static char sccsid[] = "@(#)vfprintf.c    5.2 (Berkeley) 6/27/88";
  2938. X#endif /* LIBC_SCCS and not lint */
  2939. X
  2940. X#include <stdio.h>
  2941. X#include <varargs.h>
  2942. X
  2943. int
  2944. vfprintf(iop, fmt, ap)
  2945. X    FILE *iop;
  2946. X    char *fmt;
  2947. X    va_list ap;
  2948. X{
  2949. X    int len;
  2950. X    char localbuf[BUFSIZ];
  2951. X
  2952. X    if (iop->_flag & _IONBF) {
  2953. X        iop->_flag &= ~_IONBF;
  2954. X        iop->_ptr = iop->_base = localbuf;
  2955. X        len = _doprnt(fmt, ap, iop);
  2956. X        (void) fflush(iop);
  2957. X        iop->_flag |= _IONBF;
  2958. X        iop->_base = NULL;
  2959. X        iop->_bufsiz = 0;
  2960. X        iop->_cnt = 0;
  2961. X    } else
  2962. X        len = _doprnt(fmt, ap, iop);
  2963. X
  2964. X    return (ferror(iop) ? EOF : len);
  2965. X}
  2966. END_OF_FILE
  2967. if test 1408 -ne `wc -c <'vfprintf.c'`; then
  2968.     echo shar: \"'vfprintf.c'\" unpacked with wrong size!
  2969. fi
  2970. # end of 'vfprintf.c'
  2971. fi
  2972. echo shar: End of archive 1 \(of 1\).
  2973. cp /dev/null ark1isdone
  2974. MISSING=""
  2975. for I in 1 ; do
  2976.     if test ! -f ark${I}isdone ; then
  2977.     MISSING="${MISSING} ${I}"
  2978.     fi
  2979. done
  2980. if test "${MISSING}" = "" ; then
  2981.     echo You have the archive.
  2982.     rm -f ark[1-9]isdone
  2983. else
  2984.     echo You still need to unpack the following archives:
  2985.     echo "        " ${MISSING}
  2986. fi
  2987. ##  End of shell archive.
  2988. exit 0
  2989.