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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
|
/* Copyright (c) 2008, 2009
* Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
* Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
* Micah Cowan (micah@cowan.name)
* Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
* Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
* 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 3, 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, see
* http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*
****************************************************************
*/
#include "config.h"
#include <sys/types.h>
/* XXX: WHY IS THIS HERE?? :XXX */
#ifdef CHECKLOGIN
# ifdef _SEQUENT_
# include <stdio.h> /* needed by <pwd.h> */
# endif /* _SEQUENT_ */
# include <pwd.h>
# ifdef SHADOWPW
# include <shadow.h>
# endif /* SHADOWPW */
#endif /* CHECKLOGIN */
#ifndef NOSYSLOG
# include <syslog.h>
#endif
#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 char NullStr[];
extern char SockPath[];
extern struct display *display, *displays;
struct acluser *users;
#ifdef MULTIUSER
int maxusercount = 0; /* used in process.c: RC_MONITOR, RC_SILENCE */
/* record given user ids here */
static AclBits userbits;
/*
* rights a new unknown user will have on windows and cmds.
* These are changed by a "umask ?-..." command:
*/
static char default_w_bit[ACL_BITS_PER_WIN] =
{
1, /* EXEC */
1, /* WRITE */
1 /* READ */
};
static char default_c_bit[ACL_BITS_PER_CMD] =
{
0 /* EXEC */
};
/* rights of all users per newly created window */
/*
* are now stored per user (umask)
* static AclBits default_w_userbits[ACL_BITS_PER_WIN];
* static AclBits default_c_userbits[ACL_BITS_PER_CMD];
*/
static int GrowBitfield __P((AclBits *, int, int, int));
static struct aclusergroup **FindGroupPtr __P((struct aclusergroup **, struct acluser *, int));
static int AclSetPermCmd __P((struct acluser *, char *, struct comm *));
static int AclSetPermWin __P((struct acluser *, struct acluser *, char *, struct win *));
static int UserAcl __P((struct acluser *, struct acluser **, int, char **));
static int UserAclCopy __P((struct acluser **, struct acluser **));
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 acluser **
FindUserPtr(name)
char *name;
{
struct acluser **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)->u_id:-1);
#else /* MULTIUSER */
debug2("FindUserPtr %s %sfound\n", name, (*u)?"":"not ");
#endif /* MULTIUSER */
return u;
}
int DefaultEsc = -1; /* initialised by screen.c:main() */
int DefaultMetaEsc = -1;
/*
* Add a new user. His password may be NULL or "" if none. His name must not
* be "none", as this represents the NULL-pointer when dealing with groups.
* He has default rights, determined by umask.
*/
int
UserAdd(name, pass, up)
char *name, *pass;
struct acluser **up;
{
#ifdef MULTIUSER
int j;
#endif
if (!up)
up = FindUserPtr(name);
if (*up)
{
if (pass)
(*up)->u_password = SaveStr(pass);
return 1; /* he is already there */
}
if (strcmp("none", name)) /* "none" is a reserved word */
*up = (struct acluser *)calloc(1, sizeof(struct acluser));
if (!*up)
return -1; /* he still does not exist */
#ifdef COPY_PASTE
(*up)->u_plop.buf = NULL;
(*up)->u_plop.len = 0;
# ifdef ENCODINGS
(*up)->u_plop.enc = 0;
# endif
#endif
(*up)->u_Esc = DefaultEsc;
(*up)->u_MetaEsc = DefaultMetaEsc;
strncpy((*up)->u_name, name, MAXLOGINLEN);
(*up)->u_password = NULL;
if (pass)
(*up)->u_password = SaveStr(pass);
if (!(*up)->u_password)
(*up)->u_password = NullStr;
(*up)->u_detachwin = -1;
(*up)->u_detachotherwin = -1;
#ifdef MULTIUSER
(*up)->u_group = NULL;
/* now find an unused index */
for ((*up)->u_id = 0; (*up)->u_id < maxusercount; (*up)->u_id++)
if (!(ACLBIT((*up)->u_id) & ACLBYTE(userbits, (*up)->u_id)))
break;
debug2("UserAdd %s id %d\n", name, (*up)->u_id);
if ((*up)->u_id == maxusercount)
{
int j;
struct win *w;
struct acluser *u;
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 (j = 0; j <= RC_LAST; j++)
{
int i;
for (i = 0; i < ACL_BITS_PER_CMD; i++)
if (GrowBitfield(&comms[j].userbits[i], maxusercount, USER_CHUNK,
default_c_bit[i]))
{
free((char *)*up); *up = NULL; return -1;
}
}
/* fourth, default window creation bits per user */
for (u = users; u != *up; u = u->u_next)
{
for (j = 0; j < ACL_BITS_PER_WIN; j++)
{
if (GrowBitfield(&u->u_umask_w_bits[j], maxusercount, USER_CHUNK,
default_w_bit[j]))
{
free((char *)*up); *up = NULL; return -1;
}
}
}
/* fifth, the bits for each window */
/* keep these in sync with NewWindowAcl() */
for (w = windows; w; w = w->w_next)
{
/* five a: the access control list */
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;
}
/* five b: the activity notify list */
/* five c: the silence notify list */
if (GrowBitfield(&w->w_mon_notify, maxusercount, USER_CHUNK, 0) ||
GrowBitfield(&w->w_lio_notify, maxusercount, USER_CHUNK, 0))
{
free((char *)*up); *up = NULL; return -1;
}
}
maxusercount += USER_CHUNK;
}
/* mark the user-entry as "in-use" */
ACLBYTE(userbits, (*up)->u_id) |= ACLBIT((*up)->u_id);
/* user id 0 is the session creator, he has all rights */
if ((*up)->u_id == 0)
AclSetPerm(NULL, *up, "+a", "#?");
/* user nobody has a fixed set of rights: */
if (!strcmp((*up)->u_name, "nobody"))
{
AclSetPerm(NULL, *up, "-rwx", "#?");
AclSetPerm(NULL, *up, "+x", "su");
AclSetPerm(NULL, *up, "+x", "detach");
AclSetPerm(NULL, *up, "+x", "displays");
AclSetPerm(NULL, *up, "+x", "version");
}
/*
* Create his umask:
* Give default_w_bit's for all users,
* but allow himself everything on "his" windows.
*/
for (j = 0; j < ACL_BITS_PER_WIN; j++)
{
if (GrowBitfield(&(*up)->u_umask_w_bits[j], 0, maxusercount,
default_w_bit[j]))
{
free((char *)*up); *up = NULL; return -1;
}
ACLBYTE((*up)->u_umask_w_bits[j], (*up)->u_id) |= ACLBIT((*up)->u_id);
}
#else /* MULTIUSER */
debug1("UserAdd %s\n", name);
#endif /* MULTIUSER */
return 0;
}
#if 0
/* change user's password */
int
UserSetPass(name, pass, up)
char *name, *pass;
struct acluser **up;
{
if (!up)
up = FindUserPtr(name);
if (!*up)
return UserAdd(name, pass, up);
if (!strcmp(name, "nobody")) /* he remains without password */
return -1;
strncpy((*up)->u_password, pass ? pass : "", 20);
(*up)->u_password[20] = '\0';
return 0;
}
#endif
/*
* Remove a user from the list.
* Destroy all his permissions and completely detach him from the session.
*/
int
UserDel(name, up)
char *name;
struct acluser **up;
{
struct acluser *u;
#ifdef MULTIUSER
int i;
#endif
struct display *old, *next;
if (!up)
up = FindUserPtr(name);
if (!(u = *up))
return -1; /* he who does not exist cannot be removed */
old = display;
for (display = displays; display; display = next)
{
next = display->d_next; /* read the next ptr now, Detach may zap it. */
if (D_user != u)
continue;
if (display == old)
old = NULL;
Detach(D_REMOTE);
}
display = old;
*up = u->u_next;
#ifdef MULTIUSER
for (up = &users; *up; up = &(*up)->u_next)
{
/* unlink all group references to this user */
struct aclusergroup **g = &(*up)->u_group;
while (*g)
{
if ((*g)->u == u)
{
struct aclusergroup *next = (*g)->next;
free((char *)(*g));
*g = next;
}
else
g = &(*g)->next;
}
}
ACLBYTE(userbits, u->u_id) &= ~ACLBIT(u->u_id);
/* restore the bits in his slot to default: */
AclSetPerm(NULL, u, default_w_bit[ACL_READ] ? "+r" : "-r", "#");
AclSetPerm(NULL, u, default_w_bit[ACL_WRITE]? "+w" : "-w", "#");
AclSetPerm(NULL, u, default_w_bit[ACL_EXEC] ? "+x" : "-x", "#");
AclSetPerm(NULL, u, default_c_bit[ACL_EXEC] ? "+x" : "-x", "?");
for (i = 0; i < ACL_BITS_PER_WIN; i++)
free((char *)u->u_umask_w_bits[i]);
#endif /* MULTIUSER */
debug1("FREEING user structure for %s\n", u->u_name);
#ifdef COPY_PASTE
UserFreeCopyBuffer(u);
#endif
free((char *)u);
if (!users)
{
debug("Last user deleted. Feierabend.\n");
Finit(0); /* Destroying whole session. Noone could ever attach again. */
}
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 acluser *u;
{
struct win *w;
struct paster *pa;
if (!u->u_plop.buf)
return 1;
for (w = windows; w; w = w->w_next)
{
pa = &w->w_paster;
if (pa->pa_pasteptr >= u->u_plop.buf &&
pa->pa_pasteptr - u->u_plop.buf < u->u_plop.len)
FreePaster(pa);
}
free((char *)u->u_plop.buf);
u->u_plop.len = 0;
u->u_plop.buf = 0;
return 0;
}
#endif /* COPY_PASTE */
#ifdef MULTIUSER
/*
* Traverses group nodes. It searches for a node that references user u.
* If recursive is true, nodes found in the users are also searched using
* depth first method. If none of the nodes references u, the address of
* the last next pointer is returned. This address will contain NULL.
*/
static struct aclusergroup **
FindGroupPtr(gp, u, recursive)
struct aclusergroup **gp;
struct acluser *u;
int recursive;
{
struct aclusergroup **g;
ASSERT(recursive < 1000); /* Ouch, cycle detection failed */
while (*gp)
{
if ((*gp)->u == u)
return gp; /* found him here. */
if (recursive &&
*(g = FindGroupPtr(&(*gp)->u->u_group, u, recursive + 1)))
return g; /* found him there. */
gp = &(*gp)->next;
}
return gp; /* *gp is NULL */
}
static int
PasswordMatches(pw, password)
const char *pw, *password;
{
if (!*password)
return 0;
char *buf = crypt((char *)pw, (char *)password);
return (buf && !strcmp(buf, password));
}
/*
* Returns nonzero if failed or already linked.
* Both users are created on demand.
* Cyclic links are prevented.
*/
int
AclLinkUser(from, to)
char *from, *to;
{
struct acluser **u1, **u2;
struct aclusergroup **g;
if (!*(u1 = FindUserPtr(from)) && UserAdd(from, NULL, u1))
return -1;
if (!*(u2 = FindUserPtr(to)) && UserAdd(to, NULL, u2))
return -1; /* hmm, could not find both users. */
if (*FindGroupPtr(&(*u2)->u_group, *u1, 1))
return 1; /* cyclic link detected! */
if (*(g = FindGroupPtr(&(*u1)->u_group, *u2, 0)))
return 2; /* aha, we are already linked! */
if (!(*g = (struct aclusergroup *)malloc(sizeof(struct aclusergroup))))
return -1; /* Could not alloc link. Poor screen */
(*g)->u = (*u2);
(*g)->next = NULL;
return 0;
}
/*
* The user pointer stored at *up will be substituted by a pointer
* to the named user's structure, if passwords match.
* returns NULL if successfull, an static error string otherwise
*/
char *
DoSu(up, name, pw1, pw2)
struct acluser **up;
char *name, *pw1, *pw2;
{
struct acluser *u;
int sorry = 0;
if (!(u = *FindUserPtr(name)))
sorry++;
else
{
#ifdef CHECKLOGIN
struct passwd *pp;
#ifdef SHADOWPW
struct spwd *ss;
int t, c;
#endif
char *pass = "";
if (!(pp = getpwnam(name)))
{
debug1("getpwnam(\"%s\") failed\n", name);
if (!(pw1 && *pw1 && *pw1 != '\377'))
{
debug("no unix account, no screen passwd\n");
sorry++;
}
}
else
pass = pp->pw_passwd;
#ifdef SHADOWPW
for (t = 0; t < 13; t++)
{
c = pass[t];
if (!(c == '.' || c == '/' ||
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z')))
break;
}
if (t < 13)
{
if (!(ss = getspnam(name)))
{
debug1("getspnam(\"%s\") failed\n", name);
sorry++;
}
else
pass = ss->sp_pwdp;
}
#endif /* SHADOWPW */
if (pw2 && *pw2 && *pw2 != '\377') /* provided a system password */
{
if (!PasswordMatches(pw2, pass))
{
debug("System password mismatch\n");
sorry++;
}
}
else /* no pasword provided */
if (*pass) /* but need one */
sorry++;
#endif /* CHECKLOGIN */
if (pw1 && *pw1 && *pw1 != '\377') /* provided a screen password */
{
if (!PasswordMatches(pw1, u->u_password))
{
debug("screen password mismatch\n");
sorry++;
}
}
else /* no pasword provided */
if (*u->u_password) /* but need one */
sorry++;
}
debug2("syslog(LOG_NOTICE, \"screen %s: \"su %s\" ", SockPath, name);
debug2("%s for \"%s\"\n", sorry ? "failed" : "succeeded", (*up)->u_name);
#ifndef NOSYSLOG
# ifdef BSD_42
openlog("screen", LOG_PID);
# else
openlog("screen", LOG_PID, LOG_AUTH);
# endif /* BSD_42 */
syslog(LOG_NOTICE, "%s: \"su %s\" %s for \"%s\"", SockPath, name,
sorry ? "failed" : "succeeded", (*up)->u_name);
closelog();
#else
debug("NOT LOGGED.\n");
#endif /* NOSYSLOG */
if (sorry)
return "Sorry.";
else
*up = u; /* substitute user now */
return NULL;
}
#endif /* MULTIUSER */
/************************************************************************
* end of user managing code *
************************************************************************/
#ifdef MULTIUSER
/* This gives the users default rights to the new window w created by u */
int
NewWindowAcl(w, u)
struct win *w;
struct acluser *u;
{
int i, j;
debug2("NewWindowAcl %s's umask_w_bits for window %d\n",
u ? u->u_name : "everybody", w->w_number);
/* keep these in sync with UserAdd part five. */
if (GrowBitfield(&w->w_mon_notify, 0, maxusercount, 0) ||
GrowBitfield(&w->w_lio_notify, 0, maxusercount, 0))
return -1;
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]);
free((char *)w->w_mon_notify);
free((char *)w->w_lio_notify);
return -1;
}
for (i = 0; i < maxusercount; i++)
if (u ? (ACLBIT(i) & ACLBYTE(u->u_umask_w_bits[j], i)) :
default_w_bit[j])
ACLBYTE(w->w_userbits[j], i) |= ACLBIT(i);
}
return 0;
}
void
FreeWindowAcl(w)
struct win *w;
{
int i;
for (i = 0; i < ACL_BITS_PER_WIN; i++)
free((char *)w->w_userbits[i]);
free((char *)w->w_mon_notify);
free((char *)w->w_lio_notify);
}
/* if mode starts with '-' we remove the users exec bit for cmd */
/*
* NOTE: before you make this function look the same as
* AclSetPermWin, try to merge both functions.
*/
static int
AclSetPermCmd(u, mode, cmd)
struct acluser *u;
char *mode;
struct comm *cmd;
{
int neg = 0;
char *m = mode;
while (*m)
{
switch (*m++)
{
case '-':
neg = 1;
continue;
case '+':
neg = 0;
continue;
case 'a':
case 'e':
case 'x':
/* debug3("AclSetPermCmd %s %s %s\n", u->u_name, mode, cmd->name); */
if (neg)
ACLBYTE(cmd->userbits[ACL_EXEC], u->u_id) &= ~ACLBIT(u->u_id);
else
ACLBYTE(cmd->userbits[ACL_EXEC], u->u_id) |= ACLBIT(u->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.
* Letter n allows network access on a window.
* uu should be NULL, except if you want to change his umask.
*/
static int
AclSetPermWin(uu, u, mode, win)
struct acluser *u, *uu;
char *mode;
struct win *win;
{
int neg = 0;
int bit, bits;
AclBits *bitarray;
char *m = mode;
if (uu)
{
debug3("AclSetPermWin %s UMASK %s %s\n", uu->u_name, u->u_name, mode);
bitarray = uu->u_umask_w_bits;
}
else
{
ASSERT(win);
bitarray = win->w_userbits;
debug3("AclSetPermWin %s %s %d\n", u->u_name, mode, win->w_number);
}
while (*m)
{
switch (*m++)
{
case '-':
neg = 1;
continue;
case '+':
neg = 0;
continue;
case 'r':
bits = (1 << ACL_READ);
break;
case 'w':
bits = (1 << ACL_WRITE);
break;
case 'x':
bits = (1 << ACL_EXEC);
break;
case 'a':
bits = (1 << ACL_BITS_PER_WIN) - 1;
break;
default:
return -1;
}
for (bit = 0; bit < ACL_BITS_PER_WIN; bit++)
{
if (!(bits & (1 << bit)))
continue;
if (neg)
ACLBYTE(bitarray[bit], u->u_id) &= ~ACLBIT(u->u_id);
else
ACLBYTE(bitarray[bit], u->u_id) |= ACLBIT(u->u_id);
if (!uu && (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;
}
}
}
if (uu && u->u_name[0] == '?' && u->u_name[1] == '\0')
{
/*
* It is Mr. '?', the unknown user. He deserves special treatment as
* he defines the defaults. Sorry, this is global, not per user.
*/
if (win)
{
debug1("AclSetPermWin: default_w_bits '%s'.\n", mode);
for (bit = 0; bit < ACL_BITS_PER_WIN; bit++)
default_w_bit[bit] =
(ACLBYTE(bitarray[bit], u->u_id) & ACLBIT(u->u_id)) ? 1 : 0;
}
else
{
/*
* Hack. I do not want to duplicate all the above code for
* AclSetPermCmd. This asumes that there are not more bits
* per cmd than per win.
*/
debug1("AclSetPermWin: default_c_bits '%s'.\n", mode);
for (bit = 0; bit < ACL_BITS_PER_CMD; bit++)
default_c_bit[bit] =
(ACLBYTE(bitarray[bit], u->u_id) & ACLBIT(u->u_id)) ? 1 : 0;
}
UserDel(u->u_name, NULL);
}
return 0;
}
/*
* String is broken down into comand and window names, mode applies
* A command name matches first, so do not use these as window names.
* uu should be NULL, except if you want to change his umask.
*/
int
AclSetPerm(uu, u, mode, s)
struct acluser *uu, *u;
char *mode, *s;
{
struct win *w;
int i;
char *p, ch;
debug3("AclSetPerm(uu, user '%s', mode '%s', object '%s')\n",
u->u_name, mode, s);
while (*s)
{
switch (*s)
{
case '*': /* all windows and all commands */
return AclSetPerm(uu, u, mode, "#?");
case '#':
if (uu) /* window umask or .. */
AclSetPermWin(uu, u, mode, (struct win *)1);
else /* .. or all windows */
for (w = windows; w; w = w->w_next)
AclSetPermWin((struct acluser *)0, u, mode, w);
s++;
break;
case '?':
if (uu) /* command umask or .. */
AclSetPermWin(uu, u, mode, (struct win *)0);
else /* .. or 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))
*p++ = '\0';
if ((i = FindCommnr(s)) != RC_ILLEGAL)
AclSetPermCmd(u, mode, &comms[i]);
else if (((i = WindowByNoN(s)) >= 0) && wtab[i])
AclSetPermWin((struct acluser *)0, u, mode, wtab[i]);
else
/* checking group name */
return -1;
if (ch)
p[-1] = ch;
s = p;
}
}
return 0;
}
/*
* Generic ACL Manager:
*
* This handles acladd and aclchg identical.
* With 2 or 4 parameters, the second parameter is a password.
* With 3 or 4 parameters the last two parameters specify the permissions
* else user is added with full permissions.
* With 1 parameter the users permissions are copied from user *argv.
* Unlike the other cases, u->u_name should not match *argv here.
* uu should be NULL, except if you want to change his umask.
*/
static int
UserAcl(uu, u, argc, argv)
struct acluser *uu, **u;
int argc;
char **argv;
{
if ((*u && !strcmp((*u)->u_name, "nobody")) ||
(argc > 1 && !strcmp(argv[0], "nobody")))
return -1; /* do not change nobody! */
switch (argc)
{
case 1+1+2:
debug2("UserAcl: user '%s', password '%s':", argv[0], argv[1]);
return (UserAdd(argv[0], argv[1], u) < 0) ||
AclSetPerm(uu, *u, argv[2], argv[3]);
case 1+2:
debug1("UserAcl: user '%s', no password:", argv[0]);
return (UserAdd(argv[0], NULL, u) < 0) ||
AclSetPerm(uu, *u, argv[1], argv[2]);
case 1+1:
debug2("UserAcl: user '%s', password '%s'\n", argv[0], argv[1]);
return UserAdd(argv[0], argv[1], u) < 0;
case 1:
debug1("UserAcl: user '%s', no password:", argv[0]);
return (UserAdd(argv[0], NULL, u) < 0) ||
AclSetPerm(uu, *u, "+a", "#?");
default:
return -1;
}
}
static int
UserAclCopy(to_up, from_up)
struct acluser **to_up, **from_up;
{
struct win *w;
int i, j, to_id, from_id;
if (!*to_up || !*from_up)
return -1;
debug2("UserAclCopy: from user '%s' to user '%s'\n",
(*from_up)->u_name, (*to_up)->u_name);
if ((to_id = (*to_up)->u_id) == (from_id = (*from_up)->u_id))
return -1;
for (w = windows; w; w = w->w_next)
{
for (i = 0; i < ACL_BITS_PER_WIN; i++)
{
if (ACLBYTE(w->w_userbits[i], from_id) & ACLBIT(from_id))
ACLBYTE(w->w_userbits[i], to_id) |= ACLBIT(to_id);
else
{
ACLBYTE(w->w_userbits[i], to_id) &= ~ACLBIT(to_id);
if ((w->w_wlockuser == *to_up) && (i == ACL_WRITE))
{
debug2("%s lost wlock on win %d\n",
(*to_up)->u_name, w->w_number);
w->w_wlockuser = NULL;
if (w->w_wlock == WLOCK_ON)
w->w_wlock = WLOCK_AUTO;
}
}
}
}
for (j = 0; j <= RC_LAST; j++)
{
for (i = 0; i < ACL_BITS_PER_CMD; i++)
{
if (ACLBYTE(comms[j].userbits[i], from_id) & ACLBIT(from_id))
ACLBYTE(comms[j].userbits[i], to_id) |= ACLBIT(to_id);
else
ACLBYTE(comms[j].userbits[i], to_id) &= ~ACLBIT(to_id);
}
}
return 0;
}
/*
* Syntax:
* user [password] [+rwx #?]
* * [password] [+rwx #?]
* user1,user2,user3 [password] [+rwx #?]
* user1,user2,user3=user
* uu should be NULL, except if you want to change his umask.
*/
int
UsersAcl(uu, argc, argv)
struct acluser *uu;
int argc;
char **argv;
{
char *s;
int r;
struct acluser **cf_u = NULL;
if (argc == 1)
{
char *p = NULL;
s = argv[0];
while (*s)
if (*s++ == '=') p = s;
if (p)
{
p[-1] = '\0';
cf_u = FindUserPtr(p);
}
}
if (argv[0][0] == '*' && argv[0][1] == '\0')
{
struct acluser **u;
debug("all users acls.\n");
for (u = &users; *u; u = &(*u)->u_next)
if (strcmp("nobody", (*u)->u_name) &&
((cf_u) ?
((r = UserAclCopy(u, cf_u)) < 0) :
((r = UserAcl(uu, u, argc, argv)) < 0)))
return -1;
return 0;
}
do
{
for (s = argv[0]; *s && *s!=' ' && *s!='\t' && *s!=',' && *s!='='; s++)
;
*s ? (*s++ = '\0') : (*s = '\0');
debug2("UsersAcl(uu, \"%s\", argc=%d)\n", argv[0], argc);
if ((cf_u) ?
((r = UserAclCopy(FindUserPtr(argv[0]), cf_u)) < 0) :
((r = UserAcl(uu, FindUserPtr(argv[0]), argc, argv)) < 0))
return -1;
} while (*(argv[0] = s));
return 0;
}
/*
* Preprocess argments, so that umask can be set with UsersAcl
*
* all current users umask rwxn
* one specific user umask user1rwxn
* several users umask user1,user2,...rwxn
* default_w_bits umask ?rwxn
* default_c_bits umask ??rwxn
*/
int
AclUmask(u, str, errp)
struct acluser *u;
char *str;
char **errp;
{
char mode[16];
char *av[3];
char *p, c = '\0';
/* split str into user and bits section. */
for (p = str; *p; p++)
if ((c = *p) == '+' || c == '-')
break;
if (!*p)
{
*errp = "Bad argument. Should be ``[user[,user...]{+|-}rwxn''.";
return -1;
}
strncpy(mode, p, 15);
mode[15] = '\0';
*p = '\0';
/* construct argument vector */
if (!strcmp("??", str))
{
str++;
av[2] = "?";
}
else
av[2] = "#";
av[1] = mode;
av[0] = *str ? str : "*";
/* call UsersAcl */
if (UsersAcl(u, 3, av))
{
*errp = "UsersAcl failed. Hmmm.";
*p = c;
return -1;
}
*p = c;
return 0;
}
void
AclWinSwap(a, b)
int a, b;
{
debug2("AclWinSwap(%d, %d) NOP.\n", a, b);
}
struct acluser *EffectiveAclUser = NULL; /* hook for AT command permission */
int
AclCheckPermWin(u, mode, w)
struct acluser *u;
int mode;
struct win *w;
{
int ok;
if (mode < 0 || mode >= ACL_BITS_PER_WIN)
return -1;
if (EffectiveAclUser)
{
debug1("AclCheckPermWin: WARNING user %s overridden!\n", u->u_name);
u = EffectiveAclUser;
}
ok = ACLBYTE(w->w_userbits[mode], u->u_id) & ACLBIT(u->u_id);
debug3("AclCheckPermWin(%s, %d, %d) = ", u->u_name, mode, w->w_number);
if (!ok)
{
struct aclusergroup **g = &u->u_group;
struct acluser *saved_eff = EffectiveAclUser;
EffectiveAclUser = NULL;
while (*g)
{
if (!AclCheckPermWin((*g)->u, mode, w))
break;
g = &(*g)->next;
}
EffectiveAclUser = saved_eff;
if (*g)
ok = 1;
}
debug1("%d\n", !ok);
return !ok;
}
int
AclCheckPermCmd(u, mode, c)
struct acluser *u;
int mode;
struct comm *c;
{
int ok;
if (mode < 0 || mode >= ACL_BITS_PER_CMD)
return -1;
if (EffectiveAclUser)
{
debug1("AclCheckPermCmd: WARNING user %s overridden!\n", u->u_name);
u = EffectiveAclUser;
}
ok = ACLBYTE(c->userbits[mode], u->u_id) & ACLBIT(u->u_id);
debug3("AclCheckPermCmd(%s %d %s) = ", u->u_name, mode, c->name);
if (!ok)
{
struct aclusergroup **g = &u->u_group;
struct acluser *saved_eff = EffectiveAclUser;
EffectiveAclUser = NULL;
while (*g)
{
if (!AclCheckPermCmd((*g)->u, mode, c))
break;
g = &(*g)->next;
}
EffectiveAclUser = saved_eff;
if (*g)
ok = 1;
}
debug1("%d\n", !ok);
return !ok;
}
#endif /* MULTIUSER */
|