s4-dns: dlz_bind9: Fix ipv6 updates
[samba.git] / source3 / torture / locktest2.c
index db5e17921c6555e6564f746ef5a4e71ad08b6675..6ab1ed4fa19e1f444e99c885139949000015f9f7 100644 (file)
@@ -1,12 +1,11 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 2.0
+   Unix SMB/CIFS implementation.
    byte range lock tester - with local filesystem support
    Copyright (C) Andrew Tridgell 1999
    
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
+#include "libsmb/libsmb.h"
+#include "system/filesys.h"
+#include "locking/proto.h"
 
 static fstring password;
 static fstring username;
 static int got_pass;
 static int numops = 1000;
-static BOOL showall;
-static BOOL analyze;
-static BOOL hide_unlock_fails;
-static BOOL use_oplocks;
+static bool showall;
+static bool analyze;
+static bool hide_unlock_fails;
+static bool use_oplocks;
+
+extern char *optarg;
+extern int optind;
 
 #define FILENAME "\\locktest.dat"
 #define LOCKRANGE 100
@@ -63,28 +65,39 @@ struct record {
 
 static struct record *recorded;
 
-static int try_open(struct cli_state *c, char *nfs, int fstype, char *fname, int flags)
+static int try_open(struct cli_state *c, char *nfs, int fstype, const char *fname, int flags)
 {
-       pstring path;
+       char *path;
 
        switch (fstype) {
        case FSTYPE_SMB:
-               return cli_open(c, fname, flags, DENY_NONE);
+               {
+                       uint16_t fd;
+                       if (!NT_STATUS_IS_OK(cli_openx(c, fname, flags, DENY_NONE, &fd))) {
+                               return -1;
+                       }
+                       return fd;
+               }
 
        case FSTYPE_NFS:
-               slprintf(path, sizeof(path), "%s%s", nfs, fname);
-               pstring_sub(path,"\\", "/");
-               return open(path, flags, 0666);
+               if (asprintf(&path, "%s%s", nfs, fname) > 0) {
+                       int ret;
+                       string_replace(path,'\\', '/');
+                       ret = open(path, flags, 0666);
+                       SAFE_FREE(path);
+                       return ret;
+               }
+               break;
        }
 
        return -1;
 }
 
-static BOOL try_close(struct cli_state *c, int fstype, int fd)
+static bool try_close(struct cli_state *c, int fstype, int fd)
 {
        switch (fstype) {
        case FSTYPE_SMB:
-               return cli_close(c, fd);
+               return NT_STATUS_IS_OK(cli_close(c, fd));
 
        case FSTYPE_NFS:
                return close(fd) == 0;
@@ -93,7 +106,7 @@ static BOOL try_close(struct cli_state *c, int fstype, int fd)
        return False;
 }
 
-static BOOL try_lock(struct cli_state *c, int fstype, 
+static bool try_lock(struct cli_state *c, int fstype, 
                     int fd, unsigned start, unsigned len,
                     enum brl_type op)
 {
@@ -101,7 +114,8 @@ static BOOL try_lock(struct cli_state *c, int fstype,
 
        switch (fstype) {
        case FSTYPE_SMB:
-               return cli_lock(c, fd, start, len, LOCK_TIMEOUT, op);
+               return NT_STATUS_IS_OK(cli_lock32(c, fd, start, len,
+                                      LOCK_TIMEOUT, op));
 
        case FSTYPE_NFS:
                lock.l_type = (op==READ_LOCK) ? F_RDLCK:F_WRLCK;
@@ -115,14 +129,14 @@ static BOOL try_lock(struct cli_state *c, int fstype,
        return False;
 }
 
-static BOOL try_unlock(struct cli_state *c, int fstype, 
+static bool try_unlock(struct cli_state *c, int fstype, 
                       int fd, unsigned start, unsigned len)
 {
        struct flock lock;
 
        switch (fstype) {
        case FSTYPE_SMB:
-               return cli_unlock(c, fd, start, len);
+               return NT_STATUS_IS_OK(cli_unlock(c, fd, start, len));
 
        case FSTYPE_NFS:
                lock.l_type = F_UNLCK;
@@ -136,12 +150,14 @@ static BOOL try_unlock(struct cli_state *c, int fstype,
        return False;
 }      
 
-static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
+static void print_brl(struct file_id id, struct server_id pid, 
                      enum brl_type lock_type,
-                     br_off start, br_off size)
+                     enum brl_flavour lock_flav,
+                     br_off start, br_off size,
+                     void *private_data)
 {
-       printf("%6d   %05x:%05x    %s  %.0f:%.0f(%.0f)\n", 
-              (int)pid, (int)dev, (int)ino, 
+       printf("%6d   %s    %s  %.0f:%.0f(%.0f)\n", 
+              (int)procid_to_pid(&pid), file_id_string_tos(&id),
               lock_type==READ_LOCK?"R":"W",
               (double)start, (double)start+size-1,(double)size);
 
@@ -150,16 +166,14 @@ static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-struct cli_state *connect_one(char *share)
+static struct cli_state *connect_one(char *share)
 {
        struct cli_state *c;
-       struct nmb_name called, calling;
        char *server_n;
        fstring server;
-       struct in_addr ip;
-       extern struct in_addr ipzero;
        fstring myname;
        static int count;
+       NTSTATUS nt_status;
 
        fstrcpy(server,share+2);
        share = strchr_m(server,'\\');
@@ -169,78 +183,27 @@ struct cli_state *connect_one(char *share)
 
        server_n = server;
        
-       ip = ipzero;
-
-       slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), count++);
-
-       make_nmb_name(&calling, myname, 0x0);
-       make_nmb_name(&called , server, 0x20);
-
- again:
-       ip = ipzero;
-
-       /* have to open a new connection */
-       if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
-               DEBUG(0,("Connection to %s failed\n", server_n));
-               return NULL;
-       }
-
-       if (!cli_session_request(c, &calling, &called)) {
-               DEBUG(0,("session request to %s failed\n", called.name));
-               cli_shutdown(c);
-               if (strcmp(called.name, "*SMBSERVER")) {
-                       make_nmb_name(&called , "*SMBSERVER", 0x20);
-                       goto again;
-               }
-               return NULL;
-       }
-
-       DEBUG(4,(" session request ok\n"));
-
-       if (!cli_negprot(c)) {
-               DEBUG(0,("protocol negotiation failed\n"));
-               cli_shutdown(c);
-               return NULL;
-       }
-
        if (!got_pass) {
-               char *pass = getpass("Password: ");
-               if (pass) {
-                       pstrcpy(password, pass);
+               char pwd[256] = {0};
+               int rc;
+
+               rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
+               if (rc == 0) {
+                       fstrcpy(password, pwd);
                }
        }
 
-       if (!cli_session_setup(c, username, 
-                              password, strlen(password),
-                              password, strlen(password),
-                              lp_workgroup())) {
-               DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
-               return NULL;
-       }
+       slprintf(myname,sizeof(myname), "lock-%lu-%u", (unsigned long)getpid(), count++);
 
-       /*
-        * These next two lines are needed to emulate
-        * old client behaviour for people who have
-        * scripts based on client output.
-        * QUESTION ? Do we want to have a 'client compatibility
-        * mode to turn these on/off ? JRA.
-        */
-
-       if (*c->server_domain || *c->server_os || *c->server_type)
-               DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
-                       c->server_domain,c->server_os,c->server_type));
-       
-       DEBUG(4,(" session setup ok\n"));
+       nt_status = cli_full_connection(&c, myname, server_n, NULL, 0, share, "?????", 
+                                       username, lp_workgroup(), password, 0,
+                                       SMB_SIGNING_DEFAULT);
 
-       if (!cli_send_tconX(c, share, "?????",
-                           password, strlen(password)+1)) {
-               DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
-               cli_shutdown(c);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               DEBUG(0, ("cli_full_connection failed with error %s\n", nt_errstr(nt_status)));
                return NULL;
        }
 
-       DEBUG(4,(" tconx ok\n"));
-
        c->use_oplocks = use_oplocks;
 
        return c;
@@ -267,7 +230,6 @@ static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
                        }
                        cli_ulogoff(cli[server][conn]);
                        cli_shutdown(cli[server][conn]);
-                       SAFE_FREE(cli[server][conn]);
                }
                cli[server][conn] = connect_one(share[server]);
                if (!cli[server][conn]) {
@@ -279,7 +241,7 @@ static void reconnect(struct cli_state *cli[NSERVERS][NCONNECTIONS],
 
 
 
-static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
+static bool test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS], 
                     char *nfs[NSERVERS],
                     int fnum[NSERVERS][NUMFSTYPES][NCONNECTIONS][NFILES],
                     struct record *rec)
@@ -293,7 +255,7 @@ static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
        unsigned r2 = rec->r2;
        enum brl_type op;
        int server;
-       BOOL ret[NSERVERS];
+       bool ret[NSERVERS];
 
        if (r1 < READ_PCT) {
                op = READ_LOCK; 
@@ -315,7 +277,7 @@ static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
                               op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
                               ret[0], ret[1]);
                }
-               if (showall) brl_forall(print_brl);
+               if (showall) brl_forall(print_brl, NULL);
                if (ret[0] != ret[1]) return False;
        } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
                /* unset a lock */
@@ -330,7 +292,7 @@ static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
                               start, start+len-1, len,
                               ret[0], ret[1]);
                }
-               if (showall) brl_forall(print_brl);
+               if (showall) brl_forall(print_brl, NULL);
                if (!hide_unlock_fails && ret[0] != ret[1]) return False;
        } else {
                /* reopen the file */
@@ -346,7 +308,7 @@ static BOOL test_one(struct cli_state *cli[NSERVERS][NCONNECTIONS],
                if (showall) {
                        printf("reopen conn=%u fstype=%u f=%u\n",
                               conn, fstype, f);
-                       brl_forall(print_brl);
+                       brl_forall(print_brl, NULL);
                }
        }
        return True;
@@ -368,7 +330,7 @@ static void close_files(struct cli_state *cli[NSERVERS][NCONNECTIONS],
                }
        }
        for (server=0;server<NSERVERS;server++) {
-               cli_unlink(cli[server][0], FILENAME);
+               cli_unlink(cli[server][0], FILENAME, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
        }
 }
 
@@ -431,7 +393,7 @@ static void test_locks(char *share1, char *share2, char *nfspath1, char *nfspath
        ZERO_STRUCT(fnum);
        ZERO_STRUCT(cli);
 
-       recorded = (struct record *)malloc(sizeof(*recorded) * numops);
+       recorded = SMB_MALLOC_ARRAY(struct record, numops);
 
        for (n=0; n<numops; n++) {
                recorded[n].conn = random() % NCONNECTIONS;
@@ -530,21 +492,19 @@ static void usage(void)
  int main(int argc,char *argv[])
 {
        char *share1, *share2, *nfspath1, *nfspath2;
-       extern char *optarg;
-       extern int optind;
        int opt;
        char *p;
        int seed;
 
        setlinebuf(stdout);
 
-       dbf = x_stderr;
-
        if (argc < 5 || argv[1][0] == '-') {
                usage();
                exit(1);
        }
 
+       setup_logging(argv[0], DEBUG_STDOUT);
+
        share1 = argv[1];
        share2 = argv[2];
        nfspath1 = argv[3];
@@ -553,16 +513,14 @@ static void usage(void)
        all_string_sub(share1,"/","\\",0);
        all_string_sub(share2,"/","\\",0);
 
-       setup_logging(argv[0],True);
-
        argc -= 4;
        argv += 4;
 
-       lp_load(dyn_CONFIGFILE,True,False,False);
+       lp_load_global(get_dyn_CONFIGFILE());
        load_interfaces();
 
        if (getenv("USER")) {
-               pstrcpy(username,getenv("USER"));
+               fstrcpy(username,getenv("USER"));
        }
 
        seed = time(NULL);
@@ -570,11 +528,11 @@ static void usage(void)
        while ((opt = getopt(argc, argv, "U:s:ho:aAW:O")) != EOF) {
                switch (opt) {
                case 'U':
-                       pstrcpy(username,optarg);
+                       fstrcpy(username,optarg);
                        p = strchr_m(username,'%');
                        if (p) {
                                *p = 0;
-                               pstrcpy(password, p+1);
+                               fstrcpy(password, p+1);
                                got_pass = 1;
                        }
                        break;
@@ -611,7 +569,7 @@ static void usage(void)
        DEBUG(0,("seed=%u\n", seed));
        srandom(seed);
 
-       locking_init(1);
+       locking_init_readonly();
        test_locks(share1, share2, nfspath1, nfspath2);
 
        return(0);