summaryrefslogtreecommitdiff
path: root/acls.c
blob: f13e13c0cc580c2204acfbbc8d586420e23731ea (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/* Copyright (c) 1993
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 ****************************************************************
 */
#include "rcs.h"
RCS_ID("$Id: acls.c,v 1.12 1994/05/31 12:31:21 mlschroe Exp $ FAU")

#include <sys/types.h>

#include "config.h"
#include "screen.h"	/* includes acls.h */
#include "extern.h"


/************************************************************************
 * user managing code, this does not really belong into the acl stuff   *
 ************************************************************************/

extern struct comm comms[];
extern struct win *windows, *wtab[];
extern struct display *display, *displays;
struct user *users;

#ifdef MULTIUSER
/* record given user ids here */
static AclBits userbits;

/* rights a new unknown user will have on windows and cmds */
static char default_w_bit[ACL_BITS_PER_WIN] = 
{ 
  1,	/* EXEC */
  1, 	/* WRITE */
  1 	/* READ */
};

static char default_c_bit[ACL_BITS_PER_CMD] = 
{ 
  1	/* EXEC */
};

/* rights of all users per newly created window */
static AclBits default_w_userbits[ACL_BITS_PER_WIN];

/*
static AclBits default_c_userbits[ACL_BITS_PER_CMD];
*/

static int maxusercount = 0;

static int GrowBitfield __P((AclBits *, int, int, int));



static int
GrowBitfield(bfp, len, delta, defaultbit)
AclBits *bfp;
int len, delta, defaultbit;
{
  AclBits n, o = *bfp;
  int i;

  if (!(n = (AclBits)calloc(1, (unsigned long)(&ACLBYTE((char *)0, len + delta + 1)))))
    return -1;
  for (i = 0; i < (len + delta); i++)
    {
      if (((i <  len) && (ACLBIT(i) & ACLBYTE(o, i))) ||
          ((i >= len) && (defaultbit)))
	ACLBYTE(n, i) |= ACLBIT(i);
    }
  if (len)
    free((char *)o);
  *bfp = n;
  return 0;
}

#endif /* MULTIUSER */

/* 
 * Returns an nonzero Address. Its contents is either a User-ptr, 
 * or NULL which may be replaced by a User-ptr to create the entry.
 */
struct user **
FindUserPtr(name)
char *name;
{
  struct user **u;

  for (u = &users; *u; u = &(*u)->u_next)
    if (!strcmp((*u)->u_name, name))
      break;
#ifdef MULTIUSER
  debug3("FindUserPtr %s %sfound, id %d\n", name, (*u)?"":"not ", 
         (*u)?(*u)->id:-1);
#else
  debug2("FindUserPtr %s %sfound\n", name, (*u)?"":"not ");
#endif
  return u;
}

int DefaultEsc = Ctrl('a');
int DefaultMetaEsc = 'a';

/*
 * Add a new user. His password may be NULL or "" if none. 
 * He has no rights.
 */
int
UserAdd(name, pass, up)
char *name, *pass;
struct user **up;
{
  if (!up)
    up = FindUserPtr(name);
  if (*up)
    return 1;		/* he is already there */
  *up = (struct user *)calloc(1, sizeof(struct user));
  if (!*up)
    return -1;		/* he still does not exist */
#ifdef COPY_PASTE
  (*up)->u_copybuffer = NULL;
  (*up)->u_copylen = 0;
#endif
  (*up)->u_Esc = DefaultEsc;
  (*up)->u_MetaEsc = DefaultMetaEsc;
  strncpy((*up)->u_name, name, 20);
  if (pass)
    strncpy((*up)->u_password, pass, 20);

#ifdef MULTIUSER
  /* now find an unused index */
  for ((*up)->id = 0; (*up)->id < maxusercount; (*up)->id++)
    if (!(ACLBIT((*up)->id) & ACLBYTE(userbits, (*up)->id)))
      break;
  debug2("UserAdd %s id %d\n", name, (*up)->id);
  if ((*up)->id == maxusercount)
    {
      int i, j;
      struct win *w;

      debug2("growing all bitfields %d += %d\n", maxusercount, USER_CHUNK);
      /* the bitfields are full, grow a chunk */
      /* first, the used_uid_indicator: */
      if (GrowBitfield(&userbits, maxusercount, USER_CHUNK, 0))
        {
	  free((char *)*up); *up = NULL; return -1;
	}
      /* second, default command bits  */
      /* (only if we generate commands dynamically) */
/*
      for (j = 0; j < ACL_BITS_PER_CMD; j++)
	if (GrowBitfield(&default_c_userbits[j], maxusercount, USER_CHUNK, 
	    default_c_bit[j]))
	  {
	    free((char *)*up); *up = NULL; return -1;
	  }
*/
      /* third, the bits for each commands */
      for (i = 0; i <= RC_LAST; i++)
        for (j = 0; j < ACL_BITS_PER_CMD; j++)
	  if (GrowBitfield(&comms[i].userbits[j], maxusercount, USER_CHUNK,
	      default_c_bit[j]))
	    {
	      free((char *)*up); *up = NULL; return -1;
	    }
      /* fourth, default window and bits */
      for (j = 0; j < ACL_BITS_PER_WIN; j++)
	if (GrowBitfield(&default_w_userbits[j], maxusercount, USER_CHUNK,
	    default_w_bit[j]))
	  {
	    free((char *)*up); *up = NULL; return -1;
	  }
      /* fifth, the bits for each window */
      for (w = windows; w; w = w->w_next)
        for (j = 0; j < ACL_BITS_PER_WIN; j++)
	  if (GrowBitfield(&w->w_userbits[j], maxusercount, USER_CHUNK,
	      default_w_bit[j]))
	    {
	      free((char *)*up); *up = NULL; return -1;
	    }
      maxusercount += USER_CHUNK;
    }
  ACLBYTE(userbits, (*up)->id) |= ACLBIT((*up)->id);    
  /* user id 0 is the session creator, he has all rights */
  if ((*up)->id == 0)
    AclSetPerm(*up, "+rwx", "#?");
#else /* MULTIUSER */
  debug1("UserAdd %s\n", name);
#endif /* MULTIUSER */
  return 0;
}

/* change user's password */
int 
UserSetPass(name, pass, up)
char *name, *pass;
struct user **up;
{
  if (!up)
    up = FindUserPtr(name);
  if (!*up)
    return UserAdd(name, pass, up);
  strncpy((*up)->u_password, pass ? pass : "", 20);
  (*up)->u_password[20] = '\0';
  return 0;
}

/* 
 * Remove a user from the list. 
 * Destroy all his permissions and completely detach him from the session.
 */
int 
UserDel(name, up)
char *name;
struct user **up;
{
  struct user *u;
  struct display *old, *next;

  if (!up)
    up = FindUserPtr(name);
  if ((u = *up) == 0)
    return -1;			/* he who does not exist cannot be removed */
  old = display;
  for (display = displays; display; display = next)
    {
      next = display->d_next;
      if (D_user != u)
	continue;
      if (display == old)
	old = 0;
      Detach(D_REMOTE);
    }
  display = old;
  *up = u->u_next;
#ifdef MULTIUSER
  ACLBYTE(userbits, u->id) &= ~ACLBIT(u->id);
  /* restore the bits in his slot to default: */
  AclSetPerm(u, default_w_bit[ACL_READ] ? "+r" : "-r", "#");
  AclSetPerm(u, default_w_bit[ACL_WRITE]? "+w" : "-w", "#");
  AclSetPerm(u, default_w_bit[ACL_EXEC] ? "+x" : "-x", "#");
  AclSetPerm(u, default_c_bit[ACL_EXEC] ? "+x" : "-x", "?");
#endif
  debug1("FREEING user structure for %s\n", u->u_name);
#ifdef COPY_PASTE
  UserFreeCopyBuffer(u);
#endif
  free((char *)u);
  return 0;
}


#ifdef COPY_PASTE

/*
 * returns 0 if the copy buffer was really deleted.
 * Also removes any references into the users copybuffer
 */
int
UserFreeCopyBuffer(u)
struct user *u;
{
  struct win *w;

  if (!u->u_copybuffer)
    return 1;
  for (w = windows; w; w = w->w_next)
    {
      if (w->w_pasteptr >= u->u_copybuffer &&
          w->w_pasteptr - u->u_copybuffer < u->u_copylen)
	{
	  if (w->w_pastebuf)
            free(w->w_pastebuf);
          w->w_pastebuf = 0;
	  w->w_pasteptr = 0;
	  w->w_pastelen = 0;
	}
    }
  free(u->u_copybuffer);
  D_user->u_copylen = 0;
  u->u_copybuffer = NULL;
  return 0;
}

#endif	/* COPY_PASTE */

/************************************************************************
 *                     end of user managing code                        *
 ************************************************************************/


#ifdef MULTIUSER

extern char *multi;	/* username */

/* This gives the users default rights to the new window */
int
NewWindowAcl(w)
struct win *w;
{
  int i, j;

  debug1("NewWindowAcl default_w_userbits for window %d\n", w->w_number);
  for (j = 0; j < ACL_BITS_PER_WIN; j++)
    {
      /* we start with len 0 for the new bitfield size and add maxusercount */
      if (GrowBitfield(&w->w_userbits[j], 0, maxusercount, 0))
	{
	  while (--j >= 0)
	    free((char *)w->w_userbits[j]);
	  return -1;
	}
      for (i = 0; i < maxusercount; i++)
        if (ACLBIT(i) & ACLBYTE(default_w_userbits[j], i))
	  ACLBYTE(w->w_userbits[j], i) |= ACLBIT(i);
    }
  return 0;
}

/* if mode starts with '-' we remove the users exec bit for cmd */
int
AclSetPermCmd(u, mode, cmd)
struct user *u;
char *mode;
struct comm *cmd;
{
  int neg = 0;

  if (!multi)
    return 0;
  debug3("AclSetPermCmd %s %s %s\n", u->u_name, mode, cmd->name);
  while (*mode)
    {
      switch (*mode++)
        {
	case '-': 
	  neg = 1;
	  continue;
        case '+':
	  neg = 0;
	  continue;
        case 'e': 
        case 'x': 
	  if (neg)
	    ACLBYTE(cmd->userbits[ACL_EXEC], u->id) &= ~ACLBIT(u->id);
	  else
	    ACLBYTE(cmd->userbits[ACL_EXEC], u->id) |= ACLBIT(u->id);
	  break;
        case 'r':
	case 'w':
	  break;
        default:
	  return -1;
	}
    }
  return 0;
}

/* mode strings of the form +rwx -w+rx r -wx are parsed and evaluated */
/*
 * aclchg nerd -w+w 2
 * releases a writelock on window 2 held by user nerd.
 */
int
AclSetPermWin(u, mode, win)
struct user *u;
char *mode;
struct win *win;
{
  int neg = 0;
  int bit;

  if (!multi)
    return 0;
  debug3("AclSetPermWin %s %s %d\n", u->u_name, mode, win->w_number);
  while (*mode)
    {
      switch (*mode++)
        {
	case '-': 
	  neg = 1;
	  continue;
        case '+':
	  neg = 0;
	  continue;
        case 'r': 
	  bit = ACL_READ;
	  break;
	case 'w':
	  bit = ACL_WRITE;
	  break;
        case 'x':
	  bit = ACL_EXEC;
	  break;
	default:
	  return -1;
        }
      if (neg)
	ACLBYTE(win->w_userbits[bit], u->id) &= ~ACLBIT(u->id);
      else
	ACLBYTE(win->w_userbits[bit], u->id) |= ACLBIT(u->id);
      if ((win->w_wlockuser == u) && neg && (bit == ACL_WRITE))
        {
	  debug2("%s lost writelock on win %d\n", u->u_name, win->w_number);
          win->w_wlockuser = NULL;
	  if (win->w_wlock == WLOCK_ON)
	    win->w_wlock = WLOCK_AUTO;
        }
    }
  return 0;
}

/* string is broken down into comand and window names, mode applies */
int
AclSetPerm(u, mode, s)
struct user *u;
char *mode, *s;
{
  struct win *w;
  int i;
  char *p, ch;

  while (*s)
    {
      switch (*s)
	{
	case '*':	/* all windows and all commands */
	  return AclSetPerm(u, mode, "#?");
	case '#':	/* all windows */
	  for (w = windows; w; w = w->w_next)
	    AclSetPermWin(u, mode, w);
	  s++;
	  break;
	case '?':	/* all commands */
	  for (i = 0; i <= RC_LAST; i++)
	    AclSetPermCmd(u, mode, &comms[i]);
	  s++;
	  break;
	default:
	  for (p = s; *p && *p != ' ' && *p != '\t' && *p != ','; p++)
	    ;
	  if ((ch = *p) != 0)
	    *p++ = '\0';
	  if ((i = FindCommnr(s)) != RC_ILLEGAL)
	    AclSetPermCmd(u, mode, &comms[i]);
	  else if (((i = WindowByNoN(s)) >= 0) && wtab[i])
	    AclSetPermWin(u, mode, wtab[i]);
	  else
	    /* checking group name */
	    return -1;
	  if (ch)
	    p[-1] = ch;
	  s = p; 
	}    
    }
  return 0;
}

#if 0
void
AclWinSwap(a, b)
int a, b;
{
  int a_bit = 0, b_bit = 0;
  AclGroupList **g;
  AclBits p;

  debug2("acl lists swapping windows %d and %d\n", a, b);
  
  for (g = &aclgrouproot; *g; g = &(*g)->next) 
    {
      p = (*g)->group->winbits;
      /* see if there was a bit for window a. zap it */
      if (a >= 0)
	if ((a_bit = ACLBIT(a) & ACLBYTE(p, a)))
	  ACLBYTE(p, a) &= ~ACLBIT(a);
      /* see if there was a bit for window b. zap it */
      if (b >= 0)
	if ((b_bit = ACLBIT(b) & ACLBYTE(p, b)))
	  ACLBYTE(p, b) &= ~ACLBIT(b);
      /* b may cause a set */
      if (b_bit && a >= 0)
        ACLBYTE(p, a) |= ACLBIT(a);
      /* a may cause b set */
      if (a_bit && b >= 0)
        ACLBYTE(p, b) |= ACLBIT(b);
    }
}
#else
void
AclWinSwap(a, b)
int a, b;
{
  debug2("AclWinSwap(%d, %d) DUMMY\n", a, b);
}
#endif

int 
AclCheckPermWin(u, mode, w)
struct user *u;
int mode;
struct win *w;
{
  if (!multi)
    return 0;
  if (mode < 0 || mode >= ACL_BITS_PER_WIN)
    return -1;
  debug3("AclCheckPermWin(%s, %d, %d) = ", u->u_name, mode, w->w_number);
  debug1("%d\n", !(ACLBYTE(w->w_userbits[mode], u->id) & ACLBIT(u->id)));
  return         !(ACLBYTE(w->w_userbits[mode], u->id) & ACLBIT(u->id));
}

int 
AclCheckPermCmd(u, mode, c)
struct user *u;
int mode;
struct comm *c;
{
  if (!multi)
    return 0;
  if (mode < 0 || mode >= ACL_BITS_PER_CMD)
    return -1;
  debug3("AclCheckPermCmd(%s %d %s) = ", u->u_name, mode, c->name); 
  debug1("%d\n", !(ACLBYTE(c->userbits[mode], u->id) & ACLBIT(u->id)));
  return         !(ACLBYTE(c->userbits[mode], u->id) & ACLBIT(u->id));
}

#endif /* MULTIUSER */