permsfile.c revision 22347
122347Spst/* permsfile.c: implement SunOS /etc/fbtab and Solaris /etc/logindevperm
222347Spst   functionality to set device permissions on login
322347Spst
422347Spst%%% portions-copyright-cmetz
522347SpstPortions of this software are Copyright 1996 by Craig Metz, All Rights
622347SpstReserved. The Inner Net License Version 2 applies to these portions of
722347Spstthe software.
822347SpstYou should have received a copy of the license with this software. If
922347Spstyou didn't get a copy, you may request one from <license@inner.net>.
1022347Spst
1122347SpstPortions of this software are Copyright 1995 by Randall Atkinson and Dan
1222347SpstMcDonald, All Rights Reserved. All Rights under this copyright are assigned
1322347Spstto the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
1422347SpstLicense Agreement applies to this software.
1522347Spst
1622347Spst	History:
1722347Spst
1822347Spst	Modified by cmetz for OPIE 2.3. Check for NULL return from
1922347Spst	    ftpglob(), combine some expressions, fix a typo. Made file
2022347Spst	    selection a bit more generic.
2122347Spst	Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
2222347Spst            Add opie.h. Ifdef around a header.
2322347Spst	Written at NRL for OPIE 2.0.
2422347Spst*/
2522347Spst
2622347Spst#include "opie_cfg.h"
2722347Spst#ifdef HAVE_LOGIN_PERMFILE
2822347Spst#include <stdio.h>
2922347Spst#include <sys/types.h>
3022347Spst#if HAVE_STRING_H
3122347Spst#include <string.h>
3222347Spst#endif /* HAVE_STRING_H */
3322347Spst#include <syslog.h>
3422347Spst#include "opie.h"
3522347Spst
3622347Spst/* Line buffer size (one more than max line length) */
3722347Spst#define BUFSIZE 128
3822347Spst/* Maximum number of list items in a field */
3922347Spst#define LISTSIZE 10
4022347Spst
4122347Spststatic char buf[BUFSIZE], buf2[8];
4222347Spst
4322347Spstchar **ftpglob __P((char *));
4422347Spst
4522347SpstVOIDRET opiefatal FUNCTION((x), char *x)
4622347Spst{
4722347Spst  fprintf(stderr, x);
4822347Spst  exit(1);
4922347Spst}
5022347Spst
5122347Spst#include "glob.c"
5222347Spst
5322347Spststatic int getalist FUNCTION((string, list), char **string AND char **list)
5422347Spst{
5522347Spst  char *s = *string;
5622347Spst  int i = 0;
5722347Spst
5822347Spst  while (*s && (*s != '\n') && (*s != ' ') && (*s != '\t'))
5922347Spst    if ((*s == ':') || (*s == ',')) {
6022347Spst      *(s++) = 0;
6122347Spst      list[i++] = *string;
6222347Spst      *string = s;
6322347Spst      if (i == LISTSIZE)
6422347Spst	return i;
6522347Spst    } else
6622347Spst      s++;
6722347Spst
6822347Spst  if ((int) (s) - (int) (*string)) {
6922347Spst    *s = 0;
7022347Spst    list[i++] = *string;
7122347Spst  }
7222347Spst  *string = ++s;
7322347Spst
7422347Spst  return i;
7522347Spst}
7622347Spst
7722347Spststatic VOIDRET doaline FUNCTION((line, name, ttyn, uid, gid), char *line AND char *name AND char *ttyn AND uid_t uid AND gid_t gid)
7822347Spst{
7922347Spst  char *ptr;
8022347Spst  int i;
8122347Spst  int applies, llen;
8222347Spst  char *listbuf[LISTSIZE], **globlist;
8322347Spst
8422347Spst  if (ptr = strchr(buf, '#'))
8522347Spst    *ptr = 0;
8622347Spst
8722347Spst  /* Skip whitespace */
8822347Spst  for (ptr = buf; *ptr && ((*ptr == ' ') || (*ptr == '\t'));
8922347Spst       ptr++);
9022347Spst
9122347Spst  if (!*ptr)
9222347Spst    return;
9322347Spst
9422347Spst  /* (Optional) Field 1: user name(s) */
9522347Spst  if ((*ptr != '/') && (*ptr != '~')) {
9622347Spst    llen = getalist(&ptr, listbuf);
9722347Spst    for (applies = i = 0; (i < llen) && !applies; i++)
9822347Spst      if (!strcmp(listbuf[i], name))
9922347Spst	applies++;
10022347Spst    while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
10122347Spst      ptr++;
10222347Spst    if (!applies || !*ptr)
10322347Spst      return;
10422347Spst  }
10522347Spst  /* Field 2: terminal(s) */
10622347Spst  llen = getalist(&ptr, listbuf);
10722347Spst  for (applies = i = 0; (i < llen) && !applies; i++)
10822347Spst    if (!strcmp(listbuf[i], ttyn))
10922347Spst      applies++;
11022347Spst
11122347Spst  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
11222347Spst    ptr++;
11322347Spst
11422347Spst  if (!applies || !*ptr)
11522347Spst    return;
11622347Spst
11722347Spst  /* Field 3: mode */
11822347Spst  for (applies = 0; *ptr && (*ptr >= '0') && (*ptr <= '7');
11922347Spst       applies = (applies << 3) | (*(ptr++) - '0'));
12022347Spst
12122347Spst  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
12222347Spst    ptr++;
12322347Spst
12422347Spst  if (!*ptr)
12522347Spst    return;
12622347Spst
12722347Spst  /* Field 4: devices (the fun part...) */
12822347Spst  llen = getalist(&ptr, listbuf);
12922347Spst  for (i = 0; i < llen; i++) {
13022347Spst    if (globlist = ftpglob(listbuf[i]))
13122347Spst      while (*globlist) {
13222347Spst#ifdef DEBUG
13322347Spst        syslog(LOG_DEBUG, "setting %s to %d/%d %o", *globlist, uid, gid, applies);
13422347Spst#endif /* DEBUG */
13522347Spst        if ((chown(*globlist, uid, gid) < 0) && (errno != ENOENT))
13622347Spst	  perror("chown");
13722347Spst        if ((chmod(*(globlist++), applies) < 0) && (errno != ENOENT))
13822347Spst	  perror("chmod");
13922347Spst    }
14022347Spst  }
14122347Spst}
14222347Spst
14322347SpstVOIDRET permsfile FUNCTION((name, ttyn, uid, gid), char *name AND char *ttyn AND uid_t uid AND gid_t gid)
14422347Spst{
14522347Spst  FILE *fh;
14622347Spst
14722347Spst  if (!(fh = fopen(HAVE_LOGIN_PERMFILE, "r"))) {
14822347Spst    syslog(LOG_ERR, "Can't open %s!", HAVE_LOGIN_PERMFILE);
14922347Spst    fprintf(stderr, "Warning: Can't set device permissions.\n");
15022347Spst    return;
15122347Spst  }
15222347Spst  do {
15322347Spst    if (feof(fh))
15422347Spst      return;
15522347Spst    if (fgets(buf, BUFSIZE, fh) == NULL)
15622347Spst      return;
15722347Spst    buf[BUFSIZE] = 0;
15822347Spst
15922347Spst    doaline(buf, name, ttyn, uid, gid);
16022347Spst  }
16122347Spst  while (1);
16222347Spst}
16322347Spst#endif /* HAVE_LOGIN_PERMFILE */
164