122347Spst/* permsfile.c: implement SunOS /etc/fbtab and Solaris /etc/logindevperm
222347Spst   functionality to set device permissions on login
322347Spst
429964Sache%%% portions-copyright-cmetz-96
592906SmarkmPortions of this software are Copyright 1996-1999 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
1829964Sache	Modified by cmetz for OPIE 2.31. Include unistd.h.
1922347Spst	Modified by cmetz for OPIE 2.3. Check for NULL return from
2022347Spst	    ftpglob(), combine some expressions, fix a typo. Made file
2122347Spst	    selection a bit more generic.
2222347Spst	Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
2322347Spst            Add opie.h. Ifdef around a header.
2422347Spst	Written at NRL for OPIE 2.0.
2522347Spst*/
2622347Spst
2722347Spst#include "opie_cfg.h"
2822347Spst#ifdef HAVE_LOGIN_PERMFILE
2922347Spst#include <stdio.h>
3022347Spst#include <sys/types.h>
3122347Spst#if HAVE_STRING_H
3222347Spst#include <string.h>
3322347Spst#endif /* HAVE_STRING_H */
3429964Sache#if HAVE_UNISTD_H
3529964Sache#include <unistd.h>
3629964Sache#endif /* HAVE_UNISTD_H */
3722347Spst#include <syslog.h>
3822347Spst#include "opie.h"
3922347Spst
4022347Spst/* Line buffer size (one more than max line length) */
4122347Spst#define BUFSIZE 128
4222347Spst/* Maximum number of list items in a field */
4322347Spst#define LISTSIZE 10
4422347Spst
4522347Spststatic char buf[BUFSIZE], buf2[8];
4622347Spst
4722347Spstchar **ftpglob __P((char *));
4822347Spst
4922347SpstVOIDRET opiefatal FUNCTION((x), char *x)
5022347Spst{
5192906Smarkm  fprintf(stderr, x);
5222347Spst  exit(1);
5322347Spst}
5422347Spst
5522347Spst#include "glob.c"
5622347Spst
5722347Spststatic int getalist FUNCTION((string, list), char **string AND char **list)
5822347Spst{
5922347Spst  char *s = *string;
6022347Spst  int i = 0;
6122347Spst
6222347Spst  while (*s && (*s != '\n') && (*s != ' ') && (*s != '\t'))
6322347Spst    if ((*s == ':') || (*s == ',')) {
6422347Spst      *(s++) = 0;
6522347Spst      list[i++] = *string;
6622347Spst      *string = s;
6722347Spst      if (i == LISTSIZE)
6822347Spst	return i;
6922347Spst    } else
7022347Spst      s++;
7122347Spst
7222347Spst  if ((int) (s) - (int) (*string)) {
7322347Spst    *s = 0;
7422347Spst    list[i++] = *string;
7522347Spst  }
7622347Spst  *string = ++s;
7722347Spst
7822347Spst  return i;
7922347Spst}
8022347Spst
8122347Spststatic VOIDRET doaline FUNCTION((line, name, ttyn, uid, gid), char *line AND char *name AND char *ttyn AND uid_t uid AND gid_t gid)
8222347Spst{
8322347Spst  char *ptr;
8422347Spst  int i;
8522347Spst  int applies, llen;
8622347Spst  char *listbuf[LISTSIZE], **globlist;
8722347Spst
8822347Spst  if (ptr = strchr(buf, '#'))
8922347Spst    *ptr = 0;
9022347Spst
9122347Spst  /* Skip whitespace */
9222347Spst  for (ptr = buf; *ptr && ((*ptr == ' ') || (*ptr == '\t'));
9322347Spst       ptr++);
9422347Spst
9522347Spst  if (!*ptr)
9622347Spst    return;
9722347Spst
9822347Spst  /* (Optional) Field 1: user name(s) */
9922347Spst  if ((*ptr != '/') && (*ptr != '~')) {
10022347Spst    llen = getalist(&ptr, listbuf);
10122347Spst    for (applies = i = 0; (i < llen) && !applies; i++)
10222347Spst      if (!strcmp(listbuf[i], name))
10322347Spst	applies++;
10422347Spst    while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
10522347Spst      ptr++;
10622347Spst    if (!applies || !*ptr)
10722347Spst      return;
10822347Spst  }
10922347Spst  /* Field 2: terminal(s) */
11022347Spst  llen = getalist(&ptr, listbuf);
11122347Spst  for (applies = i = 0; (i < llen) && !applies; i++)
11222347Spst    if (!strcmp(listbuf[i], ttyn))
11322347Spst      applies++;
11422347Spst
11522347Spst  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
11622347Spst    ptr++;
11722347Spst
11822347Spst  if (!applies || !*ptr)
11922347Spst    return;
12022347Spst
12122347Spst  /* Field 3: mode */
12222347Spst  for (applies = 0; *ptr && (*ptr >= '0') && (*ptr <= '7');
12322347Spst       applies = (applies << 3) | (*(ptr++) - '0'));
12422347Spst
12522347Spst  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
12622347Spst    ptr++;
12722347Spst
12822347Spst  if (!*ptr)
12922347Spst    return;
13022347Spst
13122347Spst  /* Field 4: devices (the fun part...) */
13222347Spst  llen = getalist(&ptr, listbuf);
13322347Spst  for (i = 0; i < llen; i++) {
13422347Spst    if (globlist = ftpglob(listbuf[i]))
13522347Spst      while (*globlist) {
13622347Spst#ifdef DEBUG
13722347Spst        syslog(LOG_DEBUG, "setting %s to %d/%d %o", *globlist, uid, gid, applies);
13822347Spst#endif /* DEBUG */
13922347Spst        if ((chown(*globlist, uid, gid) < 0) && (errno != ENOENT))
14022347Spst	  perror("chown");
14122347Spst        if ((chmod(*(globlist++), applies) < 0) && (errno != ENOENT))
14222347Spst	  perror("chmod");
14322347Spst    }
14422347Spst  }
14522347Spst}
14622347Spst
14722347SpstVOIDRET permsfile FUNCTION((name, ttyn, uid, gid), char *name AND char *ttyn AND uid_t uid AND gid_t gid)
14822347Spst{
14922347Spst  FILE *fh;
15022347Spst
15122347Spst  if (!(fh = fopen(HAVE_LOGIN_PERMFILE, "r"))) {
15222347Spst    syslog(LOG_ERR, "Can't open %s!", HAVE_LOGIN_PERMFILE);
15322347Spst    fprintf(stderr, "Warning: Can't set device permissions.\n");
15422347Spst    return;
15522347Spst  }
15622347Spst  do {
15722347Spst    if (feof(fh))
15822347Spst      return;
15922347Spst    if (fgets(buf, BUFSIZE, fh) == NULL)
16022347Spst      return;
16122347Spst    buf[BUFSIZE] = 0;
16222347Spst
16322347Spst    doaline(buf, name, ttyn, uid, gid);
16422347Spst  }
16522347Spst  while (1);
16622347Spst}
16722347Spst#endif /* HAVE_LOGIN_PERMFILE */
168