home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / op / part01 < prev    next >
Encoding:
Text File  |  1992-12-26  |  73.3 KB  |  2,982 lines

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