summaryrefslogtreecommitdiffstats
path: root/contrib/opie/permsfile.c
blob: 46650b1aeba43b18f1adcbdbbfd8dabc290a52bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* permsfile.c: implement SunOS /etc/fbtab and Solaris /etc/logindevperm
   functionality to set device permissions on login

%%% portions-copyright-cmetz-96
Portions of this software are Copyright 1996-1998 by Craig Metz, All Rights
Reserved. The Inner Net License Version 2 applies to these portions of
the software.
You should have received a copy of the license with this software. If
you didn't get a copy, you may request one from <license@inner.net>.

Portions of this software are Copyright 1995 by Randall Atkinson and Dan
McDonald, All Rights Reserved. All Rights under this copyright are assigned
to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
License Agreement applies to this software.

	History:

	Modified by cmetz for OPIE 2.31. Include unistd.h.
	Modified by cmetz for OPIE 2.3. Check for NULL return from
	    ftpglob(), combine some expressions, fix a typo. Made file
	    selection a bit more generic.
	Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
            Add opie.h. Ifdef around a header.
	Written at NRL for OPIE 2.0.
*/

#include "opie_cfg.h"
#ifdef HAVE_LOGIN_PERMFILE
#include <stdio.h>
#include <sys/types.h>
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <syslog.h>
#include "opie.h"

/* Line buffer size (one more than max line length) */
#define BUFSIZE 128
/* Maximum number of list items in a field */
#define LISTSIZE 10

static char buf[BUFSIZE], buf2[8];

char **ftpglob __P((char *));

VOIDRET opiefatal FUNCTION((x), char *x)
{
  fprintf(stderr, x);
  exit(1);
}

#include "glob.c"

static int getalist FUNCTION((string, list), char **string AND char **list)
{
  char *s = *string;
  int i = 0;

  while (*s && (*s != '\n') && (*s != ' ') && (*s != '\t'))
    if ((*s == ':') || (*s == ',')) {
      *(s++) = 0;
      list[i++] = *string;
      *string = s;
      if (i == LISTSIZE)
	return i;
    } else
      s++;

  if ((int) (s) - (int) (*string)) {
    *s = 0;
    list[i++] = *string;
  }
  *string = ++s;

  return i;
}

static VOIDRET doaline FUNCTION((line, name, ttyn, uid, gid), char *line AND char *name AND char *ttyn AND uid_t uid AND gid_t gid)
{
  char *ptr;
  int i;
  int applies, llen;
  char *listbuf[LISTSIZE], **globlist;

  if (ptr = strchr(buf, '#'))
    *ptr = 0;

  /* Skip whitespace */
  for (ptr = buf; *ptr && ((*ptr == ' ') || (*ptr == '\t'));
       ptr++);

  if (!*ptr)
    return;

  /* (Optional) Field 1: user name(s) */
  if ((*ptr != '/') && (*ptr != '~')) {
    llen = getalist(&ptr, listbuf);
    for (applies = i = 0; (i < llen) && !applies; i++)
      if (!strcmp(listbuf[i], name))
	applies++;
    while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
      ptr++;
    if (!applies || !*ptr)
      return;
  }
  /* Field 2: terminal(s) */
  llen = getalist(&ptr, listbuf);
  for (applies = i = 0; (i < llen) && !applies; i++)
    if (!strcmp(listbuf[i], ttyn))
      applies++;

  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
    ptr++;

  if (!applies || !*ptr)
    return;

  /* Field 3: mode */
  for (applies = 0; *ptr && (*ptr >= '0') && (*ptr <= '7');
       applies = (applies << 3) | (*(ptr++) - '0'));

  while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
    ptr++;

  if (!*ptr)
    return;

  /* Field 4: devices (the fun part...) */
  llen = getalist(&ptr, listbuf);
  for (i = 0; i < llen; i++) {
    if (globlist = ftpglob(listbuf[i]))
      while (*globlist) {
#ifdef DEBUG
        syslog(LOG_DEBUG, "setting %s to %d/%d %o", *globlist, uid, gid, applies);
#endif /* DEBUG */
        if ((chown(*globlist, uid, gid) < 0) && (errno != ENOENT))
	  perror("chown");
        if ((chmod(*(globlist++), applies) < 0) && (errno != ENOENT))
	  perror("chmod");
    }
  }
}

VOIDRET permsfile FUNCTION((name, ttyn, uid, gid), char *name AND char *ttyn AND uid_t uid AND gid_t gid)
{
  FILE *fh;

  if (!(fh = fopen(HAVE_LOGIN_PERMFILE, "r"))) {
    syslog(LOG_ERR, "Can't open %s!", HAVE_LOGIN_PERMFILE);
    fprintf(stderr, "Warning: Can't set device permissions.\n");
    return;
  }
  do {
    if (feof(fh))
      return;
    if (fgets(buf, BUFSIZE, fh) == NULL)
      return;
    buf[BUFSIZE] = 0;

    doaline(buf, name, ttyn, uid, gid);
  }
  while (1);
}
#endif /* HAVE_LOGIN_PERMFILE */
OpenPOWER on IntegriCloud