cliquota: factor out parsing of a quota record buffer
[metze/samba/wip.git] / source3 / libsmb / cliquota.c
index 5627d28bb5d455a7024915f0e44356217c00b946..19c2e7ea2cad9b6f089d186ae04e83fe4e4f1949 100644 (file)
@@ -2,43 +2,43 @@
    Unix SMB/CIFS implementation.
    client quota functions
    Copyright (C) Stefan (metze) Metzmacher     2003
-   
+
    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,
    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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-
-BOOL cli_get_quota_handle(struct cli_state *cli, int *quota_fnum)
+#include "libsmb/libsmb.h"
+#include "../librpc/gen_ndr/ndr_security.h"
+#include "fake_file.h"
+#include "../libcli/security/security.h"
+#include "trans2.h"
+#include "../libcli/smb/smbXcli_base.h"
+
+NTSTATUS cli_get_quota_handle(struct cli_state *cli, uint16_t *quota_fnum)
 {
-       *quota_fnum = cli_nt_create_full(cli, FAKE_FILE_NAME_QUOTA_WIN32,
+       return cli_ntcreate(cli, FAKE_FILE_NAME_QUOTA_WIN32,
                 0x00000016, DESIRED_ACCESS_PIPE,
                 0x00000000, FILE_SHARE_READ|FILE_SHARE_WRITE,
-                FILE_OPEN, 0x00000000, 0x03);
-                
-       if (*quota_fnum == (-1)) {
-               return False;
-       }
-
-       return True;
+                FILE_OPEN, 0x00000000, 0x03, quota_fnum, NULL);
 }
 
 void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
 {
-       if (!qt_list)
+       if (!qt_list || !*qt_list) {
                return;
-               
+       }
+
        if ((*qt_list)->mem_ctx)
                talloc_destroy((*qt_list)->mem_ctx);
 
@@ -47,20 +47,24 @@ void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
        return; 
 }
 
-static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count, unsigned int *offset, SMB_NTQUOTA_STRUCT *pqt)
+bool parse_user_quota_record(const uint8_t *rdata,
+                            unsigned int rdata_count,
+                            unsigned int *offset,
+                            SMB_NTQUOTA_STRUCT *pqt)
 {
        int sid_len;
        SMB_NTQUOTA_STRUCT qt;
 
        ZERO_STRUCT(qt);
 
-       if (!rdata||!offset||!pqt)
-               smb_panic("parse_quota_record: called with NULL POINTER!\n");
+       if (!rdata||!offset||!pqt) {
+               smb_panic("parse_quota_record: called with NULL POINTER!");
+       }
 
        if (rdata_count < 40) {
                return False;
        }
-               
+
        /* offset to next quota record.
         * 4 bytes IVAL(rdata,0)
         * unused here...
@@ -69,55 +73,34 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
 
        /* sid len */
        sid_len = IVAL(rdata,4);
+       if (40 + sid_len < 40) {
+               return false;
+       }
 
        if (rdata_count < 40+sid_len) {
                return False;           
        }
 
+       if (*offset != 0 && *offset < 40 + sid_len) {
+               return false;
+       }
+
        /* unknown 8 bytes in pdata 
         * maybe its the change time in NTTIME
         */
 
-       /* the used space 8 bytes (SMB_BIG_UINT)*/
-       qt.usedspace = (SMB_BIG_UINT)IVAL(rdata,16);
-#ifdef LARGE_SMB_OFF_T
-       qt.usedspace |= (((SMB_BIG_UINT)IVAL(rdata,20)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(rdata,20) != 0)&&
-               ((qt.usedspace != 0xFFFFFFFF)||
-                (IVAL(rdata,20)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               return False;
-       }
-#endif /* LARGE_SMB_OFF_T */
-
-       /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.softlim = (SMB_BIG_UINT)IVAL(rdata,24);
-#ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(rdata,28) != 0)&&
-               ((qt.softlim != 0xFFFFFFFF)||
-                (IVAL(rdata,28)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               return False;
-       }
-#endif /* LARGE_SMB_OFF_T */
-
-       /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
-#ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(rdata,36) != 0)&&
-               ((qt.hardlim != 0xFFFFFFFF)||
-                (IVAL(rdata,36)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               return False;
+       /* the used space 8 bytes (uint64_t)*/
+       qt.usedspace = BVAL(rdata,16);
+
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = BVAL(rdata,24);
+
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = BVAL(rdata,32);
+
+       if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
+               return false;
        }
-#endif /* LARGE_SMB_OFF_T */
-       
-       sid_parse(rdata+40,sid_len,&qt.sid);
 
        qt.qtype = SMB_USER_QUOTA_TYPE;
 
@@ -126,359 +109,305 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
        return True;
 }
 
-BOOL cli_get_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+NTSTATUS parse_user_quota_list(const uint8_t *curdata,
+                              uint32_t curdata_count,
+                              TALLOC_CTX *mem_ctx,
+                              SMB_NTQUOTA_LIST **pqt_list)
+{
+       NTSTATUS status = NT_STATUS_OK;
+       unsigned offset;
+       SMB_NTQUOTA_STRUCT qt;
+       SMB_NTQUOTA_LIST *tmp_list_ent;
+
+       while (true) {
+               ZERO_STRUCT(qt);
+               if (!parse_user_quota_record(curdata, curdata_count, &offset,
+                                            &qt)) {
+                       DEBUG(1, ("Failed to parse the quota record\n"));
+                       status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+                       break;
+               }
+
+               if ((tmp_list_ent = talloc_zero(mem_ctx, SMB_NTQUOTA_LIST)) ==
+                   NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       break;
+               }
+
+               if ((tmp_list_ent->quotas =
+                        talloc_zero(mem_ctx, SMB_NTQUOTA_STRUCT)) == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       break;
+               }
+
+               memcpy(tmp_list_ent->quotas, &qt, sizeof(qt));
+               tmp_list_ent->mem_ctx = mem_ctx;
+
+               DLIST_ADD((*pqt_list), tmp_list_ent);
+
+               if (offset > curdata_count) {
+                       DEBUG(1, ("out of bounds offset in quota record\n"));
+                       status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+                       break;
+               }
+
+               if (curdata + offset < curdata) {
+                       DEBUG(1, ("Pointer overflow in quota record\n"));
+                       status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+                       break;
+               }
+
+               curdata += offset;
+               curdata_count -= offset;
+
+               if (offset == 0) {
+                       break;
+               }
+       }
+
+       return status;
+}
+
+NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
+                           SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
-       uint16 setup;
-       char params[16];
+       uint16_t setup[1];
+       uint8_t params[16];
        unsigned int data_len;
-       char data[SID_MAX_SIZE+8];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
+       uint8_t data[SID_MAX_SIZE+8];
+       uint8_t *rparam, *rdata;
+       uint32_t rparam_count, rdata_count;
        unsigned int sid_len;
        unsigned int offset;
+       NTSTATUS status;
 
-       if (!cli||!pqt)
+       if (!cli||!pqt) {
                smb_panic("cli_get_user_quota() called with NULL Pointer!");
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
+       }
 
-       setup = NT_TRANSACT_GET_USER_QUOTA;
+       SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
 
        SSVAL(params, 0,quota_fnum);
        SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_FOR_SID);
        SIVAL(params, 4,0x00000024);
        SIVAL(params, 8,0x00000000);
        SIVAL(params,12,0x00000024);
-       
-       sid_len = sid_size(&pqt->sid);
+
+       sid_len = ndr_size_dom_sid(&pqt->sid, 0);
        data_len = sid_len+8;
        SIVAL(data, 0, 0x00000000);
        SIVAL(data, 4, sid_len);
        sid_linearize(data+8, sid_len, &pqt->sid);
-       
-       if (!cli_send_nt_trans(cli, 
-                              NT_TRANSACT_GET_USER_QUOTA, 
-                              0, 
-                              &setup, 1, 0,
-                              params, 16, 4,
-                              data, data_len, 112)) {
-               DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
-               goto cleanup;
-       }
-
 
-       if (!cli_receive_nt_trans(cli,
-                                 &rparam, &rparam_count,
-                                 &rdata, &rdata_count)) {
-               DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
-               goto cleanup;
-       }
-
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
+       status = cli_trans(talloc_tos(), cli, SMBnttrans,
+                          NULL, -1, /* name, fid */
+                          NT_TRANSACT_GET_USER_QUOTA, 0,
+                          setup, 1, 0, /* setup */
+                          params, 16, 4, /* params */
+                          data, data_len, 112, /* data */
+                          NULL,                /* recv_flags2 */
+                          NULL, 0, NULL,       /* rsetup */
+                          &rparam, 4, &rparam_count,
+                          &rdata, 8, &rdata_count);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("NT_TRANSACT_GET_USER_QUOTA failed: %s\n",
+                         nt_errstr(status)));
+               return status;
        }
 
-       if ((rparam&&rdata)&&(rparam_count>=4&&rdata_count>=8)) {
-               ret = parse_user_quota_record(rdata, rdata_count, &offset, pqt);
-       } else {
+       if (!parse_user_quota_record(rdata, rdata_count, &offset, pqt)) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
                DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
-               ret = False; 
        }
 
- cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata); 
-       return ret;
+       TALLOC_FREE(rparam);
+       TALLOC_FREE(rdata);
+       return status;
 }
 
-BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+NTSTATUS cli_set_user_quota(struct cli_state *cli, int quota_fnum,
+                           SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
-       uint16 setup;
-       char params[2];
-       char data[112];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
+       uint16_t setup[1];
+       uint8_t params[2];
+       uint8_t data[112];
        unsigned int sid_len;   
+       NTSTATUS status;
+
        memset(data,'\0',112);
-       
-       if (!cli||!pqt)
+
+       if (!cli||!pqt) {
                smb_panic("cli_set_user_quota() called with NULL Pointer!");
+       }
 
-       setup = NT_TRANSACT_SET_USER_QUOTA;
+       SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
 
        SSVAL(params,0,quota_fnum);
 
-       sid_len = sid_size(&pqt->sid);
+       sid_len = ndr_size_dom_sid(&pqt->sid, 0);
        SIVAL(data,0,0);
        SIVAL(data,4,sid_len);
-       SBIG_UINT(data, 8,(SMB_BIG_UINT)0);
+       SBIG_UINT(data, 8,(uint64_t)0);
        SBIG_UINT(data,16,pqt->usedspace);
        SBIG_UINT(data,24,pqt->softlim);
        SBIG_UINT(data,32,pqt->hardlim);
        sid_linearize(data+40, sid_len, &pqt->sid);
-       
-       if (!cli_send_nt_trans(cli, 
-                              NT_TRANSACT_SET_USER_QUOTA, 
-                              0, 
-                              &setup, 1, 0,
-                              params, 2, 0,
-                              data, 112, 0)) {
-               DEBUG(1,("Failed to send NT_TRANSACT_SET_USER_QUOTA\n"));
-               goto cleanup;
-       }
 
-
-       if (!cli_receive_nt_trans(cli, 
-                                 &rparam, &rparam_count,
-                                 &rdata, &rdata_count)) {
-               DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
-               goto cleanup;
+       status = cli_trans(talloc_tos(), cli, SMBnttrans,
+                          NULL, -1, /* name, fid */
+                          NT_TRANSACT_SET_USER_QUOTA, 0,
+                          setup, 1, 0, /* setup */
+                          params, 2, 0, /* params */
+                          data, 112, 0, /* data */
+                          NULL,                /* recv_flags2 */
+                          NULL, 0, NULL,       /* rsetup */
+                          NULL, 0, NULL,       /* rparams */
+                          NULL, 0, NULL);      /* rdata */
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("NT_TRANSACT_SET_USER_QUOTA failed: %s\n",
+                         nt_errstr(status)));
        }
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
-
-  cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-       return ret;
+       return status;
 }
 
-BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST **pqt_list)
+static NTSTATUS cli_list_user_quota_step(struct cli_state *cli,
+                                        TALLOC_CTX *mem_ctx,
+                                        int quota_fnum,
+                                        SMB_NTQUOTA_LIST **pqt_list,
+                                        bool first)
 {
-       BOOL ret = False;
-       uint16 setup;
-       char params[16];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-       unsigned int offset;
-       const char *curdata = NULL;
-       unsigned int curdata_count = 0;
-       TALLOC_CTX *mem_ctx = NULL;
-       SMB_NTQUOTA_STRUCT qt;
-       SMB_NTQUOTA_LIST *tmp_list_ent;
+       uint16_t setup[1];
+       uint8_t params[16];
+       uint8_t *rparam=NULL, *rdata=NULL;
+       uint32_t rparam_count=0, rdata_count=0;
+       NTSTATUS status;
+       uint16_t op = first ? TRANSACT_GET_USER_QUOTA_LIST_START
+                           : TRANSACT_GET_USER_QUOTA_LIST_CONTINUE;
 
-       if (!cli||!pqt_list)
-               smb_panic("cli_list_user_quota() called with NULL Pointer!");
-
-       setup = NT_TRANSACT_GET_USER_QUOTA;
+       SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
 
        SSVAL(params, 0,quota_fnum);
-       SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
+       SSVAL(params, 2, op);
        SIVAL(params, 4,0x00000000);
        SIVAL(params, 8,0x00000000);
        SIVAL(params,12,0x00000000);
-       
-       if (!cli_send_nt_trans(cli, 
-                              NT_TRANSACT_GET_USER_QUOTA, 
-                              0, 
-                              &setup, 1, 0,
-                              params, 16, 4,
-                              NULL, 0, 2048)) {
-               DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
-               goto cleanup;
-       }
 
-
-       if (!cli_receive_nt_trans(cli,
-                                 &rparam, &rparam_count,
-                                 &rdata, &rdata_count)) {
-               DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
-               goto cleanup;
+       status = cli_trans(talloc_tos(), cli, SMBnttrans,
+                          NULL, -1, /* name, fid */
+                          NT_TRANSACT_GET_USER_QUOTA, 0,
+                          setup, 1, 0, /* setup */
+                          params, 16, 4, /* params */
+                          NULL, 0, 2048, /* data */
+                          NULL,                /* recv_flags2 */
+                          NULL, 0, NULL,       /* rsetup */
+                          &rparam, 0, &rparam_count,
+                          &rdata, 0, &rdata_count);
+
+       /* compat. with smbd + safeguard against
+        * endless loop
+        */
+       if (NT_STATUS_IS_OK(status) && rdata_count == 0) {
+               status = NT_STATUS_NO_MORE_ENTRIES;
        }
 
-       if (cli_is_error(cli)) {
-               ret = False;
+       if (!NT_STATUS_IS_OK(status)) {
                goto cleanup;
-       } else {
-               ret = True;
        }
 
-       if (rdata_count == 0) {
-               *pqt_list = NULL;
-               return True;
-       }
+       status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
 
-       if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
-               DEBUG(0,("talloc_init() failed\n"));
-               return (-1);
-       }
+cleanup:
+       TALLOC_FREE(rparam);
+       TALLOC_FREE(rdata);
 
-       offset = 1;
-       for (curdata=rdata,curdata_count=rdata_count;
-               ((curdata)&&(curdata_count>=8)&&(offset>0));
-               curdata +=offset,curdata_count -= offset) {
-               ZERO_STRUCT(qt);
-               if (!parse_user_quota_record(curdata, curdata_count, &offset, &qt)) {
-                       DEBUG(1,("Failed to parse the quota record\n"));
-                       goto cleanup;
-               }
+       return status;
+}
 
-               if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
-                       DEBUG(0,("talloc_zero() failed\n"));
-                       talloc_destroy(mem_ctx);
-                       return (-1);
-               }
+NTSTATUS cli_list_user_quota(struct cli_state *cli,
+                            int quota_fnum,
+                            SMB_NTQUOTA_LIST **pqt_list)
+{
+       NTSTATUS status;
+       TALLOC_CTX *mem_ctx = NULL;
+       bool first = true;
 
-               if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
-                       DEBUG(0,("talloc_zero() failed\n"));
-                       talloc_destroy(mem_ctx);
-                       return (-1);
-               }
+       if (!cli || !pqt_list) {
+               smb_panic("cli_list_user_quota() called with NULL Pointer!");
+       }
 
-               memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
-               tmp_list_ent->mem_ctx = mem_ctx;                
+       *pqt_list = NULL;
 
-               DLIST_ADD((*pqt_list),tmp_list_ent);
+       if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
-       SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_CONTINUE); 
-       while(1) {
-               if (!cli_send_nt_trans(cli, 
-                                      NT_TRANSACT_GET_USER_QUOTA, 
-                                      0, 
-                                      &setup, 1, 0,
-                                      params, 16, 4,
-                                      NULL, 0, 2048)) {
-                       DEBUG(1,("Failed to send NT_TRANSACT_GET_USER_QUOTA\n"));
-                       goto cleanup;
-               }
-               
-               SAFE_FREE(rparam);
-               SAFE_FREE(rdata);
-               if (!cli_receive_nt_trans(cli,
-                                         &rparam, &rparam_count,
-                                         &rdata, &rdata_count)) {
-                       DEBUG(1,("Failed to recv NT_TRANSACT_GET_USER_QUOTA\n"));
-                       goto cleanup;
-               }
+       do {
+               status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
+                                                 pqt_list, first);
+               first = false;
+       } while (NT_STATUS_IS_OK(status));
 
-               if (cli_is_error(cli)) {
-                       ret = False;
-                       goto cleanup;
-               } else {
-                       ret = True;
-               }
-       
-               if (rdata_count == 0) {
-                       break;  
-               }
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+               status = NT_STATUS_OK;
+       }
 
-               offset = 1;
-               for (curdata=rdata,curdata_count=rdata_count;
-                       ((curdata)&&(curdata_count>=8)&&(offset>0));
-                       curdata +=offset,curdata_count -= offset) {
-                       ZERO_STRUCT(qt);
-                       if (!parse_user_quota_record(curdata, curdata_count, &offset, &qt)) {
-                               DEBUG(1,("Failed to parse the quota record\n"));
-                               goto cleanup;
-                       }
-
-                       if ((tmp_list_ent=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_LIST))==NULL) {
-                               DEBUG(0,("talloc_zero() failed\n"));
-                               talloc_destroy(mem_ctx);
-                               goto cleanup;
-                       }
-       
-                       if ((tmp_list_ent->quotas=TALLOC_ZERO_P(mem_ctx,SMB_NTQUOTA_STRUCT))==NULL) {
-                               DEBUG(0,("talloc_zero() failed\n"));
-                               talloc_destroy(mem_ctx);
-                               goto cleanup;
-                       }
-       
-                       memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
-                       tmp_list_ent->mem_ctx = mem_ctx;                
-       
-                       DLIST_ADD((*pqt_list),tmp_list_ent);
-               }
+       if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
+               TALLOC_FREE(mem_ctx);
        }
 
-       ret = True;
- cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-       return ret;
+       return status;
 }
 
-BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+NTSTATUS cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum,
+                              SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
-       uint16 setup;
-       char param[2];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
+       uint16_t setup[1];
+       uint8_t param[2];
+       uint8_t *rdata=NULL;
+       uint32_t rdata_count=0;
        SMB_NTQUOTA_STRUCT qt;
+       NTSTATUS status;
+
        ZERO_STRUCT(qt);
 
-       if (!cli||!pqt)
+       if (!cli||!pqt) {
                smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
+       }
+
+       SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
 
-       setup = TRANSACT2_QFSINFO;
-       
        SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
-       
-       if (!cli_send_trans(cli, SMBtrans2, 
-                   NULL, 
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 2, 0,
-                   NULL, 0, 560)) {
-               goto cleanup;
-       }
-       
-       if (!cli_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
-               goto cleanup;
-       }
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
+       status = cli_trans(talloc_tos(), cli, SMBtrans2,
+                          NULL, -1, /* name, fid */
+                          0, 0,     /* function, flags */
+                          setup, 1, 0, /* setup */
+                          param, 2, 0, /* param */
+                          NULL, 0, 560, /* data */
+                          NULL,         /* recv_flags2 */
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          &rdata, 48, &rdata_count);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
+                         nt_errstr(status)));
+               return status;
        }
 
-       if (rdata_count < 48) {
-               goto cleanup;
-       }
-       
        /* unknown_1 24 NULL bytes in pdata*/
 
-       /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.softlim = (SMB_BIG_UINT)IVAL(rdata,24);
-#ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(rdata,28) != 0)&&
-               ((qt.softlim != 0xFFFFFFFF)||
-                (IVAL(rdata,28)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               goto cleanup;
-       }
-#endif /* LARGE_SMB_OFF_T */
-
-       /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
-#ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
-#else /* LARGE_SMB_OFF_T */
-       if ((IVAL(rdata,36) != 0)&&
-               ((qt.hardlim != 0xFFFFFFFF)||
-                (IVAL(rdata,36)!=0xFFFFFFFF))) {
-               /* more than 32 bits? */
-               goto cleanup;
-       }
-#endif /* LARGE_SMB_OFF_T */
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = BVAL(rdata,24);
+
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = BVAL(rdata,32);
 
        /* quota_flags 2 bytes **/
        qt.qflags = SVAL(rdata,40);
@@ -487,30 +416,26 @@ BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
 
        *pqt = qt;
 
-       ret = True;
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return ret;     
+       TALLOC_FREE(rdata);
+       return status;
 }
 
-BOOL cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
+                              SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
-       uint16 setup;
-       char param[4];
-       char data[48];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
+       uint16_t setup[1];
+       uint8_t param[4];
+       uint8_t data[48];
        SMB_NTQUOTA_STRUCT qt;
+       NTSTATUS status;
        ZERO_STRUCT(qt);
        memset(data,'\0',48);
 
-       if (!cli||!pqt)
+       if (!cli||!pqt) {
                smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
+       }
 
-       setup = TRANSACT2_SETFSINFO;
+       SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
 
        SSVAL(param,0,quota_fnum);
        SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
@@ -528,108 +453,21 @@ BOOL cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
 
        /* Unknown3 6 NULL bytes */
 
-       if (!cli_send_trans(cli, SMBtrans2, 
-                   NULL, 
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 4, 0,
-                   data, 48, 0)) {
-               goto cleanup;
-       }
-       
-       if (!cli_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
-               goto cleanup;
-       }
-
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
+       status = cli_trans(talloc_tos(), cli, SMBtrans2,
+                          NULL, -1, /* name, fid */
+                          0, 0,     /* function, flags */
+                          setup, 1, 0, /* setup */
+                          param, 4, 0, /* param */
+                          data, 48, 0, /* data */
+                          NULL,         /* recv_flags2 */
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          NULL, 0, NULL); /* rdata */
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
+                         nt_errstr(status)));
        }
 
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return ret;     
-}
-
-static char *quota_str_static(SMB_BIG_UINT val, BOOL special, BOOL _numeric)
-{
-       static fstring buffer;
-       
-       memset(buffer,'\0',sizeof(buffer));
-
-       if (!_numeric&&special&&(val == SMB_NTQUOTAS_NO_LIMIT)) {
-               fstr_sprintf(buffer,"NO LIMIT");
-               return buffer;
-       }
-#if defined(HAVE_LONGLONG)
-       fstr_sprintf(buffer,"%llu",val);
-#else
-       fstr_sprintf(buffer,"%lu",val);
-#endif 
-       return buffer;
-}
-
-void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, BOOL _verbose, BOOL _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, BOOL _numeric))
-{
-       if (!qt)
-               smb_panic("dump_ntquota() called with NULL pointer");
-
-       switch (qt->qtype) {
-               case SMB_USER_FS_QUOTA_TYPE:
-                       {
-                               d_printf("File System QUOTAS:\n");
-                               d_printf("Limits:\n");
-                               d_printf(" Default Soft Limit: %15s\n",quota_str_static(qt->softlim,True,_numeric));
-                               d_printf(" Default Hard Limit: %15s\n",quota_str_static(qt->hardlim,True,_numeric));
-                               d_printf("Quota Flags:\n");
-                               d_printf(" Quotas Enabled: %s\n",
-                                       ((qt->qflags&QUOTAS_ENABLED)||(qt->qflags&QUOTAS_DENY_DISK))?"On":"Off");
-                               d_printf(" Deny Disk:      %s\n",(qt->qflags&QUOTAS_DENY_DISK)?"On":"Off");
-                               d_printf(" Log Soft Limit: %s\n",(qt->qflags&QUOTAS_LOG_THRESHOLD)?"On":"Off");
-                               d_printf(" Log Hard Limit: %s\n",(qt->qflags&QUOTAS_LOG_LIMIT)?"On":"Off");
-                       }
-                       break;
-               case SMB_USER_QUOTA_TYPE:
-                       {
-                               fstring username_str = {0};
-                               
-                               if (_sidtostring) {
-                                       _sidtostring(username_str,&qt->sid,_numeric);
-                               } else {
-                                       fstrcpy(username_str,sid_string_static(&qt->sid));
-                               }
-
-                               if (_verbose) { 
-                                       d_printf("Quotas for User: %s\n",username_str);
-                                       d_printf("Used Space: %15s\n",quota_str_static(qt->usedspace,False,_numeric));
-                                       d_printf("Soft Limit: %15s\n",quota_str_static(qt->softlim,True,_numeric));
-                                       d_printf("Hard Limit: %15s\n",quota_str_static(qt->hardlim,True,_numeric));
-                               } else {
-                                       d_printf("%-30s: ",username_str);
-                                       d_printf("%15s/",quota_str_static(qt->usedspace,False,_numeric));
-                                       d_printf("%15s/",quota_str_static(qt->softlim,True,_numeric));
-                                       d_printf("%15s\n",quota_str_static(qt->hardlim,True,_numeric));
-                               }
-                       }
-                       break;
-               default:
-                       d_printf("dump_ntquota() invalid qtype(%d)\n",qt->qtype);
-                       return;
-       }
-}
-
-void dump_ntquota_list(SMB_NTQUOTA_LIST **qtl, BOOL _verbose, BOOL _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, BOOL _numeric))
-{
-       SMB_NTQUOTA_LIST *cur;
-
-       for (cur = *qtl;cur;cur = cur->next) {
-               if (cur->quotas)
-                       dump_ntquota(cur->quotas,_verbose,_numeric,_sidtostring);
-       }       
+       return status;
 }