se_access_check() tests.
authorTim Potter <tpot@samba.org>
Thu, 6 Jul 2000 07:12:13 +0000 (07:12 +0000)
committerTim Potter <tpot@samba.org>
Thu, 6 Jul 2000 07:12:13 +0000 (07:12 +0000)
(This used to be commit bba912cad8dfcef3dbc1db020304ab29fd71d5cb)

testsuite/smbd/Makefile.se_access_check [new file with mode: 0644]
testsuite/smbd/se_access_check.exp [new file with mode: 0644]
testsuite/smbd/se_access_check_allowall.c [new file with mode: 0644]
testsuite/smbd/se_access_check_allowsome.c [new file with mode: 0644]
testsuite/smbd/se_access_check_denyall.c [new file with mode: 0644]
testsuite/smbd/se_access_check_denysome.c [new file with mode: 0644]
testsuite/smbd/se_access_check_empty.c [new file with mode: 0644]
testsuite/smbd/se_access_check_nullsd.c [new file with mode: 0644]
testsuite/smbd/se_access_check_printer.c [new file with mode: 0644]
testsuite/smbd/se_access_check_utils.c [new file with mode: 0644]
testsuite/smbd/se_access_check_utils.h [new file with mode: 0644]

diff --git a/testsuite/smbd/Makefile.se_access_check b/testsuite/smbd/Makefile.se_access_check
new file mode 100644 (file)
index 0000000..5637fa2
--- /dev/null
@@ -0,0 +1,24 @@
+#
+# Makefile for se_access_check tests
+#
+
+include ../../source/Makefile
+
+# Objects common to all tests
+
+SE_ACCESS_CHECK_OBJ1 = $(LIB_OBJ) $(UBIQX_OBJ) $(PARAM_OBJ) $(RPC_PARSE_OBJ) \
+       $(LIBSMB_OBJ) lib/util_seaccess.o nsswitch/common.o
+
+SE_ACCESS_CHECK_OBJS = $(SE_ACCESS_CHECK_OBJ1:%=$(srcdir)/%) \
+       se_access_check_utils.o
+
+# Targets for individual tests
+
+se_access_check_nullsd: $(SE_ACCESS_CHECK_OBJS) se_access_check_nullsd.o
+se_access_check_everyone: $(SE_ACCESS_CHECK_OBJS) se_access_check_everyone.o
+se_access_check_allowall: $(SE_ACCESS_CHECK_OBJS) se_access_check_allowall.o
+se_access_check_denyall: $(SE_ACCESS_CHECK_OBJS) se_access_check_denyall.o
+se_access_check_allowsome: $(SE_ACCESS_CHECK_OBJS) se_access_check_allowsome.o
+se_access_check_denysome: $(SE_ACCESS_CHECK_OBJS) se_access_check_denysome.o
+se_access_check_empty: $(SE_ACCESS_CHECK_OBJS) se_access_check_empty.o
+se_access_check_printer: $(SE_ACCESS_CHECK_OBJS) se_access_check_printer.o
diff --git a/testsuite/smbd/se_access_check.exp b/testsuite/smbd/se_access_check.exp
new file mode 100644 (file)
index 0000000..cd84ab8
--- /dev/null
@@ -0,0 +1,54 @@
+#
+# @(#) Test se_access_check() function
+#
+
+#
+# Unix SMB/Netbios implementation.
+# Copyright (C) Tim Potter 2000
+#   
+# 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 of the License, 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; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+
+load_lib "compile.exp"
+load_lib "util-defs.exp"
+
+# Run tests from C source files
+
+set se_access_check_tests [list \
+       { "null security descriptor" "se_access_check_nullsd" } \
+       { "security descriptor allow everyone" "se_access_check_allowall" } \
+       { "security descriptor allow everyone" "se_access_check_allowall" } \
+       { "security descriptor deny everyone" "se_access_check_denyall" } \
+       { "empty security descriptor" "se_access_check_empty" } \
+       { "allow some users access" "se_access_check_allowsome" } \
+       { "deny some users access" "se_access_check_denysome" } \
+       { "printer access permissions" "se_access_check_printer" } \
+       ]
+
+foreach { test } $se_access_check_tests {
+    set test_desc [lindex $test 0]
+    set test_file [lindex $test 1]
+
+    simple_make "se_access_check" $test_file
+    set output [util_start "$srcdir/$subdir/$test_file" ]
+
+    if { [regexp "PASS" $output] } {
+        pass $test_desc
+        file delete "$srcdir/$subdir/$test_file" "$srcdir/$subdir/$test_file.o"
+    } else {
+        fail $test_desc
+        puts $output
+    }
+}
diff --git a/testsuite/smbd/se_access_check_allowall.c b/testsuite/smbd/se_access_check_allowall.c
new file mode 100644 (file)
index 0000000..b49e8e5
--- /dev/null
@@ -0,0 +1,87 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_allowall[] = {
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "S-1-1-0" },
+       { 0, 0, 0, NULL}
+};
+
+/* Check that access is always allowed for a NULL security descriptor */
+
+BOOL allowall_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       BOOL result;
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       if (!result || status != NT_STATUS_NO_PROBLEMO ||
+           acc_granted != GENERIC_ALL_ACCESS) {
+               printf("FAIL: allowall se_access_check %d/%d\n",
+                      pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+       
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_allowall, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       visit_pwdb(allowall_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_allowsome.c b/testsuite/smbd/se_access_check_allowsome.c
new file mode 100644 (file)
index 0000000..529b200
--- /dev/null
@@ -0,0 +1,104 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_allowsome[] = {
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "user0" },
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "user2" },
+       { 0, 0, 0, NULL}
+};
+
+BOOL allowsome_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       fstring name;
+       BOOL result;
+       int len1, len2;
+
+       /* Check only user0 and user2 allowed access */
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       len1 = (int)strlen(pw->pw_name) - strlen("user0");
+       len2 = (int)strlen(pw->pw_name) - strlen("user2");
+
+       if ((strncmp("user0", &pw->pw_name[MAX(len1, 0)], 
+                    strlen("user0")) == 0) ||
+           (strncmp("user2", &pw->pw_name[MAX(len2, 0)], 
+                    strlen("user2")) == 0)) {
+               if (!result || acc_granted != GENERIC_ALL_ACCESS) {
+                       printf("FAIL: access not granted for %s\n",
+                              pw->pw_name);
+               }
+       } else {
+               if (result || acc_granted != 0) {
+                       printf("FAIL: access granted for %s\n", pw->pw_name);
+               }
+       }
+
+       printf("result %s for user %s\n", result ? "allowed" : "denied",
+              pw->pw_name);
+
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_allowsome, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       visit_pwdb(allowsome_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_denyall.c b/testsuite/smbd/se_access_check_denyall.c
new file mode 100644 (file)
index 0000000..016e6f6
--- /dev/null
@@ -0,0 +1,86 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_denyall[] = {
+       { SEC_ACE_TYPE_ACCESS_DENIED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "S-1-1-0" },
+       { 0, 0, 0, NULL}
+};
+
+/* Check that access is always allowed for a NULL security descriptor */
+
+BOOL denyall_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       BOOL result;
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       if (result || acc_granted != 0) {
+               printf("FAIL: denyall se_access_check %d/%d\n",
+                      pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_denyall, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       visit_pwdb(denyall_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_denysome.c b/testsuite/smbd/se_access_check_denysome.c
new file mode 100644 (file)
index 0000000..1f0aaaf
--- /dev/null
@@ -0,0 +1,106 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_denysome[] = {
+       { SEC_ACE_TYPE_ACCESS_DENIED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "user1" },
+       { SEC_ACE_TYPE_ACCESS_DENIED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "user3" },
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         GENERIC_ALL_ACCESS, "S-1-1-0" },
+       { 0, 0, 0, NULL}
+};
+
+BOOL denysome_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       fstring name;
+       BOOL result;
+       int len1, len2;
+
+       /* Check only user1 and user3 denied access */
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       len1 = (int)strlen(pw->pw_name) - strlen("user1");
+       len2 = (int)strlen(pw->pw_name) - strlen("user3");
+
+       if ((strncmp("user1", &pw->pw_name[MAX(len1, 0)], 
+                    strlen("user1")) == 0) ||
+           (strncmp("user3", &pw->pw_name[MAX(len2, 0)], 
+                    strlen("user3")) == 0)) {
+               if (result || acc_granted != 0) {
+                       printf("FAIL: access not denied for %s\n",
+                              pw->pw_name);
+               }
+       } else {
+               if (!result || acc_granted != GENERIC_ALL_ACCESS) {
+                       printf("FAIL: access denied for %s\n", pw->pw_name);
+               }
+       }
+
+       printf("result %s for user %s\n", result ? "allowed" : "denied",
+              pw->pw_name);
+
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_denysome, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       visit_pwdb(denysome_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_empty.c b/testsuite/smbd/se_access_check_empty.c
new file mode 100644 (file)
index 0000000..6602b11
--- /dev/null
@@ -0,0 +1,109 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_empty[] = {
+       { 0, 0, 0, NULL}
+};
+
+/* Check that access is always allowed for a NULL security descriptor */
+
+BOOL emptysd_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       BOOL result;
+
+       /* For no DACL, access is allowed and the desired access mask is
+          returned */
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       if (!result || !(acc_granted == SEC_RIGHTS_MAXIMUM_ALLOWED)) {
+               printf("FAIL: no dacl for %s (%d/%d)\n", pw->pw_name,
+                      pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 0x1234,
+                                &acc_granted, &status);
+       
+       if (!result || !(acc_granted == 0x1234)) {
+               printf("FAIL: no dacl2 for %s (%d/%d)\n", pw->pw_name,
+                      pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+
+       /* If desired access mask is empty then no access is allowed */
+
+       result = se_access_check(sd, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 0,
+                                &acc_granted, &status);
+       
+       if (result) {
+               printf("FAIL: zero desired access for %s (%d/%d)\n",
+                      pw->pw_name, pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+       
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_empty, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       visit_pwdb(emptysd_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_nullsd.c b/testsuite/smbd/se_access_check_nullsd.c
new file mode 100644 (file)
index 0000000..c042c2b
--- /dev/null
@@ -0,0 +1,74 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+
+/* Check that access is always allowed for a NULL security descriptor */
+
+BOOL nullsd_check(struct passwd *pw, int ngroups, gid_t *groups)
+{
+       uint32 acc_granted, status;
+       BOOL result;
+
+       result = se_access_check(NULL, pw->pw_uid, pw->pw_gid,
+                                ngroups, groups, 
+                                SEC_RIGHTS_MAXIMUM_ALLOWED,
+                                &acc_granted, &status);
+       
+       if (!result || status != NT_STATUS_NO_PROBLEMO ||
+           acc_granted != SEC_RIGHTS_MAXIMUM_ALLOWED) {
+               printf("FAIL: null se_access_check %d/%d\n",
+                      pw->pw_uid, pw->pw_gid);
+               failed = True;
+       }
+       
+       printf("access check passed for user %s (%d/%d)\n",
+              pw->pw_name, pw->pw_uid, pw->pw_gid);
+       
+       return True;
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Run test */
+
+       visit_pwdb(nullsd_check);
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_printer.c b/testsuite/smbd/se_access_check_printer.c
new file mode 100644 (file)
index 0000000..e73a23c
--- /dev/null
@@ -0,0 +1,212 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+/* Globals */
+
+BOOL failed;
+SEC_DESC *sd;
+
+struct ace_entry acl_printer[] = {
+
+       /* Everyone is allowed to print */
+
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_PRINT, "S-1-1-0" },
+
+       /* Except for user0 who uses too much paper */
+
+       { SEC_ACE_TYPE_ACCESS_DENIED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_FULL_CONTROL, "user0" },
+
+       /* Users 1 and 2 can manage documents */
+
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_MANAGE_DOCUMENTS, "user1" },
+
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_MANAGE_DOCUMENTS, "user2" },
+
+       /* Domain Admins can also manage documents */
+
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_MANAGE_DOCUMENTS, "Domain Admins" },
+
+       /* User 3 is da man */
+
+       { SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_ACE_FLAG_CONTAINER_INHERIT,
+         PRINTER_ACE_FULL_CONTROL, "user3" },
+
+       { 0, 0, 0, NULL}
+};
+
+BOOL test_user(char *username, uint32 acc_desired, uint32 *acc_granted)
+{
+       struct passwd *pw;
+       uint32 status;
+
+       if (!(pw = getpwnam(username))) {
+               printf("FAIL: could not lookup user info for %s\n",
+                      username);
+               exit(1);
+       }
+
+       return se_access_check(sd, pw->pw_uid, pw->pw_gid, 0, NULL,
+                              acc_desired, acc_granted, &status);
+}
+
+static char *pace_str(uint32 ace_flags)
+{
+       if ((ace_flags & PRINTER_ACE_FULL_CONTROL) == 
+           PRINTER_ACE_FULL_CONTROL) return "full control";
+
+       if ((ace_flags & PRINTER_ACE_MANAGE_DOCUMENTS) ==
+           PRINTER_ACE_MANAGE_DOCUMENTS) return "manage documents";
+
+       if ((ace_flags & PRINTER_ACE_PRINT) == PRINTER_ACE_PRINT)
+               return "print";
+
+       return "UNKNOWN";
+}
+
+uint32 perms[] = {
+       PRINTER_ACE_PRINT,
+       PRINTER_ACE_FULL_CONTROL,
+       PRINTER_ACE_MANAGE_DOCUMENTS,
+       0
+};
+
+void runtest(void)
+{
+       uint32 acc_granted;
+       BOOL result;
+       int i, j;
+
+       for (i = 0; perms[i]; i++) {
+
+               /* Test 10 users */
+               
+               for (j = 0; j < 10; j++) {
+                       fstring name;
+
+                       /* Test user against ACL */
+
+                       snprintf(name, sizeof(fstring), "%s/user%d", 
+                                getenv("TEST_WORKGROUP"), j);
+                       
+                       result = test_user(name, perms[i], &acc_granted);
+
+                       printf("%s: %s %s 0x%08x\n", name, 
+                              pace_str(perms[i]), 
+                              result ? "TRUE " : "FALSE", acc_granted);
+
+                       /* Check results */
+
+                       switch (perms[i]) {
+
+                       case PRINTER_ACE_PRINT: {
+                               if (!result || acc_granted !=
+                                   PRINTER_ACE_PRINT) {
+                                       printf("FAIL: user %s can't print\n",
+                                              name);
+                                       failed = True;
+                               }
+                               break;
+                       }
+
+                       case PRINTER_ACE_FULL_CONTROL: {
+                               if (j == 3) {
+                                       if (!result || acc_granted !=
+                                           PRINTER_ACE_FULL_CONTROL) {
+                                               printf("FAIL: user %s doesn't "
+                                                      "have full control\n",
+                                                      name);
+                                               failed = True;
+                                       }
+                               } else {
+                                       if (result || acc_granted != 0) {
+                                               printf("FAIL: user %s has full "
+                                                      "control\n", name);
+                                               failed = True;
+                                       }
+                               }
+                               break;
+                       }
+                       case PRINTER_ACE_MANAGE_DOCUMENTS: {
+                               if (j == 1 || j == 2) {
+                                       if (!result || acc_granted !=
+                                           PRINTER_ACE_MANAGE_DOCUMENTS) {
+                                               printf("FAIL: user %s can't "
+                                                      "manage documents\n",
+                                                      name);
+                                               failed = True;
+                                       }
+                               } else {
+                                       if (result || acc_granted != 0) {
+                                               printf("FAIL: user %s can "
+                                                      "manage documents\n",
+                                                      name);
+                                               failed = True;
+                                       }
+                               }
+                               break;
+                       }
+
+                       default:
+                               printf("FAIL: internal error\n");
+                               exit(1);
+                       }
+               }
+       }
+}
+
+/* Main function */
+
+int main(int argc, char **argv)
+{
+       /* Initialisation */
+
+       generate_wellknown_sids();
+
+       /* Create security descriptor */
+
+       sd = build_sec_desc(acl_printer, NULL, NULL_SID, NULL_SID);
+
+       if (!sd) {
+               printf("FAIL: could not build security descriptor\n");
+               return 1;
+       }
+
+       /* Run test */
+
+       runtest();
+
+       /* Return */
+
+        if (!failed) {
+               printf("PASS\n");
+               return 0;
+       } 
+
+       return 1;
+}
diff --git a/testsuite/smbd/se_access_check_utils.c b/testsuite/smbd/se_access_check_utils.c
new file mode 100644 (file)
index 0000000..316bb5d
--- /dev/null
@@ -0,0 +1,158 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "se_access_check_utils.h"
+
+void char_to_sid(DOM_SID *sid, char *sid_str)
+{
+       /* If it looks like a SID, call string_to_sid() else look it up
+          using wbinfo. */
+
+       if (strncmp(sid_str, "S-", 2) == 0) {
+               string_to_sid(sid, sid_str);
+       } else {
+               struct winbindd_request request;
+               struct winbindd_response response;
+
+               /* Send off request */
+
+               ZERO_STRUCT(request);
+               ZERO_STRUCT(response);
+               
+               fstrcpy(request.data.name, sid_str);
+               if (winbindd_request(WINBINDD_LOOKUPNAME, &request, 
+                                    &response) != NSS_STATUS_SUCCESS) {
+                       printf("FAIL: unable to look up sid for name %s\n",
+                              sid_str);
+                       exit(1);
+               }
+
+               string_to_sid(sid, response.data.sid.sid);
+               printf("converted char %s to sid %s\n", sid_str, 
+                      response.data.sid.sid);
+       }       
+}
+
+/* Construct an ACL from a list of ace_entry structures */
+
+SEC_ACL *build_acl(struct ace_entry *ace_list)
+{
+       SEC_ACE *aces = NULL;
+       SEC_ACL *result;
+       int num_aces = 0;
+
+       if (ace_list == NULL) return NULL;
+
+       /* Create aces */
+
+       while(ace_list->sid) {
+               SEC_ACCESS sa;
+               DOM_SID sid;
+
+               /* Create memory for new ACE */
+
+               if (!(aces = Realloc(aces, 
+                                    sizeof(SEC_ACE) * (num_aces + 1)))) {
+                       return NULL;
+               }
+
+               /* Create ace */
+
+               init_sec_access(&sa, ace_list->mask);
+
+               char_to_sid(&sid, ace_list->sid);
+               init_sec_ace(&aces[num_aces], &sid, ace_list->type,
+                            sa, ace_list->flags);
+
+               num_aces++;
+               ace_list++;
+       }
+
+       /* Create ACL from list of ACEs */
+
+       result = make_sec_acl(ACL_REVISION, num_aces, aces);
+       free(aces);
+
+       return result;
+}
+
+/* Make a security descriptor */
+
+SEC_DESC *build_sec_desc(struct ace_entry *dacl, struct ace_entry *sacl, 
+                        char *owner_sid, char *group_sid)
+{
+       DOM_SID the_owner_sid, the_group_sid;
+       SEC_ACL *the_dacl, *the_sacl;
+       SEC_DESC *result;
+       size_t size;
+
+       /* Build up bits of security descriptor */
+
+       char_to_sid(&the_owner_sid, owner_sid);
+       char_to_sid(&the_group_sid, group_sid);
+
+       the_dacl = build_acl(dacl);
+       the_sacl = build_acl(sacl);
+
+       result =  make_sec_desc(SEC_DESC_REVISION, 
+                               SEC_DESC_SELF_RELATIVE | SEC_DESC_DACL_PRESENT,
+                               &the_owner_sid, &the_group_sid, 
+                               the_sacl, the_dacl, &size);
+
+       free_sec_acl(&the_dacl);
+       free_sec_acl(&the_sacl);
+
+       return result;
+}
+
+/* Iterate over password database and call a user-specified function */
+
+void visit_pwdb(BOOL (*fn)(struct passwd *pw, int ngroups, gid_t *groups))
+{
+       struct passwd *pw;
+       int ngroups;
+       gid_t *groups;
+
+       setpwent();
+
+       while ((pw = getpwent())) {
+               BOOL result;
+
+               /* Get grouplist */
+
+               ngroups = getgroups(0, NULL);
+
+               groups = malloc(sizeof(gid_t) * ngroups);
+               getgroups(ngroups, groups);
+
+               /* Call function */
+
+               result = fn(pw, ngroups, groups);
+               if (!result) break;
+
+               /* Clean up */
+
+               free(groups);
+       }
+
+       endpwent();
+}
diff --git a/testsuite/smbd/se_access_check_utils.h b/testsuite/smbd/se_access_check_utils.h
new file mode 100644 (file)
index 0000000..bae0d17
--- /dev/null
@@ -0,0 +1,46 @@
+/* 
+   Unix SMB/Netbios implementation.
+   Version 1.9.
+   Security context tests
+   Copyright (C) Tim Potter 2000
+   
+   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 of the License, 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; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#ifndef _SE_ACCESS_CHECK_UTILS_H
+#define _SE_ACCESS_CHECK_UTILS_H
+
+#include "includes.h"
+
+/* Structure to build ACE lists from */
+
+struct ace_entry {
+       uint8 type, flags;
+       uint32 mask;
+       char *sid;
+};
+
+#define NULL_SID  "S-1-0-0"
+#define WORLD_SID "S-1-1-0"
+
+/* Function prototypes */
+
+SEC_ACL *build_acl(struct ace_entry *ace_list);
+SEC_DESC *build_sec_desc(struct ace_entry *dacl, struct ace_entry *sacl, 
+                        char *owner_sid, char *group_sid);
+
+void visit_pwdb(BOOL (*fn)(struct passwd *pw, int ngroups, gid_t *groups));
+
+#endif