s3/libsmb: adjust smb2 code for new idl structs & generated ndr push/pull funcs.
[samba.git] / source3 / libsmb / cliquota.c
index e40dac368d9ae6b5e686dcecdf202b4970ede921..7e8565b44b16fd41f7faec2088f598c715500bb1 100644 (file)
 */
 
 #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"
+#include "librpc/gen_ndr/ndr_quota.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);
@@ -46,593 +48,643 @@ 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 add_record_to_ntquota_list(TALLOC_CTX *mem_ctx,
+                               SMB_NTQUOTA_STRUCT *pqt,
+                               SMB_NTQUOTA_LIST **pqt_list)
 {
-       int sid_len;
-       SMB_NTQUOTA_STRUCT qt;
-
-       ZERO_STRUCT(qt);
+       SMB_NTQUOTA_LIST *tmp_list_ent;
 
-       if (!rdata||!offset||!pqt) {
-               smb_panic("parse_quota_record: called with NULL POINTER!");
+       if ((tmp_list_ent = talloc_zero(mem_ctx, SMB_NTQUOTA_LIST)) == NULL) {
+               return false;
        }
 
-       if (rdata_count < 40) {
-               return False;
+       if ((tmp_list_ent->quotas = talloc_zero(mem_ctx, SMB_NTQUOTA_STRUCT)) ==
+           NULL) {
+               return false;
        }
 
-       /* offset to next quota record.
-        * 4 bytes IVAL(rdata,0)
-        * unused here...
-        */
-       *offset = IVAL(rdata,0);
+       *tmp_list_ent->quotas = *pqt;
+       tmp_list_ent->mem_ctx = mem_ctx;
 
-       /* sid len */
-       sid_len = IVAL(rdata,4);
+       DLIST_ADD((*pqt_list), tmp_list_ent);
 
-       if (rdata_count < 40+sid_len) {
-               return False;           
-       }
-
-       /* unknown 8 bytes in pdata 
-        * maybe its the change time in NTTIME
-        */
+       return true;
+}
 
-       /* the used space 8 bytes (uint64_t)*/
-       qt.usedspace = (uint64_t)IVAL(rdata,16);
-#ifdef LARGE_SMB_OFF_T
-       qt.usedspace |= (((uint64_t)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 */
+bool parse_user_quota_record(const uint8_t *rdata,
+                            unsigned int rdata_count,
+                            unsigned int *offset,
+                            SMB_NTQUOTA_STRUCT *pqt)
+{
+       struct file_quota_information info = {0};
+       TALLOC_CTX *frame = talloc_stackframe();
+       DATA_BLOB blob;
+       enum ndr_err_code err;
+       bool result = false;
 
-       /* the soft quotas 8 bytes (uint64_t)*/
-       qt.softlim = (uint64_t)IVAL(rdata,24);
-#ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((uint64_t)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 */
+       blob.data = discard_const_p(uint8_t, rdata);
+       blob.length = rdata_count;
+       err = ndr_pull_struct_blob(
+                       &blob,
+                       frame,
+                       &info,
+                       (ndr_pull_flags_fn_t)ndr_pull_file_quota_information);
 
-       /* the hard quotas 8 bytes (uint64_t)*/
-       qt.hardlim = (uint64_t)IVAL(rdata,32);
-#ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((uint64_t)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;
+       if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
+               goto out;
        }
-#endif /* LARGE_SMB_OFF_T */
 
-       sid_parse(rdata+40,sid_len,&qt.sid);
+       *offset = info.next_entry_offset;
 
-       qt.qtype = SMB_USER_QUOTA_TYPE;
+       ZERO_STRUCTP(pqt);
+       pqt->usedspace = info.quota_used;
 
-       *pqt = qt;
+       pqt->softlim = info.quota_threshold;
+
+       pqt->hardlim = info.quota_limit;
 
-       return True;
+       pqt->qtype = SMB_USER_QUOTA_TYPE;
+       pqt->sid = info.sid;
+       result = true;
+out:
+       TALLOC_FREE(frame);
+       return result;
 }
 
-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)
 {
-       bool ret = False;
-       uint16 setup;
-       char 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;
-       unsigned int sid_len;
-       unsigned int offset;
-
-       if (!cli||!pqt) {
-               smb_panic("cli_get_user_quota() called with NULL Pointer!");
-       }
-
-       setup = 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);
+       NTSTATUS status = NT_STATUS_OK;
+       unsigned offset;
+       SMB_NTQUOTA_STRUCT qt;
 
-       sid_len = ndr_size_dom_sid(&pqt->sid, NULL, 0);
-       data_len = sid_len+8;
-       SIVAL(data, 0, 0x00000000);
-       SIVAL(data, 4, sid_len);
-       sid_linearize(data+8, sid_len, &pqt->sid);
+       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 (!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 (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;
+               }
 
-       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 (!add_record_to_ntquota_list(mem_ctx, &qt, pqt_list)) {
+                       status = NT_STATUS_NO_MEMORY;
+                       break;
+               }
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
+               curdata += offset;
+               curdata_count -= offset;
 
-       if ((rparam&&rdata)&&(rparam_count>=4&&rdata_count>=8)) {
-               ret = parse_user_quota_record(rdata, rdata_count, &offset, pqt);
-       } else {
-               DEBUG(0,("Got INVALID NT_TRANSACT_GET_USER_QUOTA reply.\n"));
-               ret = False; 
+               if (offset == 0) {
+                       break;
+               }
        }
 
- cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata); 
-       return ret;
+       return status;
 }
 
-bool cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+NTSTATUS parse_fs_quota_buffer(const uint8_t *rdata,
+                              unsigned int rdata_count,
+                              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;
-       unsigned int sid_len;   
-       memset(data,'\0',112);
+       SMB_NTQUOTA_STRUCT qt;
 
-       if (!cli||!pqt) {
-               smb_panic("cli_set_user_quota() called with NULL Pointer!");
+       ZERO_STRUCT(qt);
+
+       if (rdata_count < 48) {
+               /* minimum length is not enforced by SMB2 client.
+                */
+               DEBUG(1, ("small returned fs quota buffer\n"));
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
 
-       setup = NT_TRANSACT_SET_USER_QUOTA;
+       /* unknown_1 24 NULL bytes in pdata*/
 
-       SSVAL(params,0,quota_fnum);
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = BVAL(rdata, 24);
 
-       sid_len = ndr_size_dom_sid(&pqt->sid, NULL, 0);
-       SIVAL(data,0,0);
-       SIVAL(data,4,sid_len);
-       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;
-       }
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = BVAL(rdata, 32);
 
+       /* quota_flags 2 bytes **/
+       qt.qflags = SVAL(rdata, 40);
 
-       if (!cli_receive_nt_trans(cli, 
-                                 &rparam, &rparam_count,
-                                 &rdata, &rdata_count)) {
-               DEBUG(1,("NT_TRANSACT_SET_USER_QUOTA failed\n"));
-               goto cleanup;
-       }
+       qt.qtype = SMB_USER_FS_QUOTA_TYPE;
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
+       *pqt = qt;
 
-  cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-       return ret;
+       return NT_STATUS_OK;
 }
 
-bool cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST **pqt_list)
+NTSTATUS build_user_quota_buffer(SMB_NTQUOTA_LIST *qt_list,
+                                uint32_t maxlen,
+                                TALLOC_CTX *mem_ctx,
+                                DATA_BLOB *outbuf,
+                                SMB_NTQUOTA_LIST **end_ptr)
 {
-       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;
+       return fill_quota_buffer(mem_ctx,
+                                qt_list,
+                                false,
+                                maxlen,
+                                outbuf,
+                                end_ptr);
+}
 
-       if (!cli||!pqt_list) {
-               smb_panic("cli_list_user_quota() called with NULL Pointer!");
-       }
+NTSTATUS build_fs_quota_buffer(TALLOC_CTX *mem_ctx,
+                              const SMB_NTQUOTA_STRUCT *pqt,
+                              DATA_BLOB *blob,
+                              uint32_t maxlen)
+{
+       uint8_t *buf;
 
-       setup = NT_TRANSACT_GET_USER_QUOTA;
+       if (maxlen > 0 && maxlen < 48) {
+               return NT_STATUS_BUFFER_TOO_SMALL;
+       }
 
-       SSVAL(params, 0,quota_fnum);
-       SSVAL(params, 2,TRANSACT_GET_USER_QUOTA_LIST_START);
-       SIVAL(params, 4,0x00000000);
-       SIVAL(params, 8,0x00000000);
-       SIVAL(params,12,0x00000000);
+       *blob = data_blob_talloc_zero(mem_ctx, 48);
 
-       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 (!blob->data) {
+               return NT_STATUS_NO_MEMORY;
        }
 
+       buf = blob->data;
 
-       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;
-       }
+       /* Unknown1 24 NULL bytes*/
+       SBIG_UINT(buf, 0, (uint64_t)0);
+       SBIG_UINT(buf, 8, (uint64_t)0);
+       SBIG_UINT(buf, 16, (uint64_t)0);
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
-       }
+       /* Default Soft Quota 8 bytes */
+       SBIG_UINT(buf, 24, pqt->softlim);
 
-       if (rdata_count == 0) {
-               *pqt_list = NULL;
-               return True;
-       }
+       /* Default Hard Quota 8 bytes */
+       SBIG_UINT(buf, 32, pqt->hardlim);
 
-       if ((mem_ctx=talloc_init("SMB_USER_QUOTA_LIST"))==NULL) {
-               DEBUG(0,("talloc_init() failed\n"));
-               return (-1);
-       }
+       /* Quota flag 4 bytes */
+       SIVAL(buf, 40, pqt->qflags);
 
-       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;
-               }
+       /* 4 padding bytes */
+       SIVAL(buf, 44, 0);
 
-               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);
-               }
+       return NT_STATUS_OK;
+}
 
-               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);
-               }
+NTSTATUS cli_get_user_quota(struct cli_state *cli, int quota_fnum,
+                           SMB_NTQUOTA_STRUCT *pqt)
+{
+       uint16_t setup[1];
+       uint8_t *rparam = NULL, *rdata = NULL;
+       uint32_t rparam_count, rdata_count;
+       unsigned int sid_len;
+       unsigned int offset;
+       struct nttrans_query_quota_params get_quota = {0};
+       struct file_get_quota_info info =  {0};
+       enum ndr_err_code err;
+       struct ndr_push *ndr_push = NULL;
+       NTSTATUS status;
+       TALLOC_CTX *frame = talloc_stackframe();
+       DATA_BLOB data_blob = data_blob_null;
 
-               memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
-               tmp_list_ent->mem_ctx = mem_ctx;                
+       if (!cli||!pqt) {
+               smb_panic("cli_get_user_quota() called with NULL Pointer!");
+       }
 
-               DLIST_ADD((*pqt_list),tmp_list_ent);
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               TALLOC_FREE(frame);
+               return cli_smb2_get_user_quota(cli, quota_fnum, pqt);
        }
 
-       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;
-               }
+       get_quota.fid = quota_fnum;
+       get_quota.return_single_entry = 1;
+       get_quota.restart_scan = 0;
 
-               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;
-               }
+       sid_len = ndr_size_dom_sid(&pqt->sid, 0);
 
-               if (cli_is_error(cli)) {
-                       ret = False;
-                       goto cleanup;
-               } else {
-                       ret = True;
-               }
+       info.next_entry_offset = 0;
+       info.sid_length = sid_len;
+       info.sid = pqt->sid;
 
-               if (rdata_count == 0) {
-                       break;  
-               }
+       err = ndr_push_struct_blob(
+                       &data_blob,
+                       frame,
+                       &info,
+                       (ndr_push_flags_fn_t)ndr_push_file_get_quota_info);
 
-               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 (!NDR_ERR_CODE_IS_SUCCESS(err)) {
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto out;
+       }
 
-                       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;
-                       }
+       get_quota.sid_list_length = data_blob.length;
+       get_quota.start_sid_offset = data_blob.length;
 
-                       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;
-                       }
+       ndr_push = ndr_push_init_ctx(frame);
+
+       if (!ndr_push) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
 
-                       memcpy(tmp_list_ent->quotas,&qt,sizeof(qt));
-                       tmp_list_ent->mem_ctx = mem_ctx;                
+       err = ndr_push_nttrans_query_quota_params(ndr_push,
+                                            NDR_SCALARS | NDR_BUFFERS,
+                                            &get_quota);
 
-                       DLIST_ADD((*pqt_list),tmp_list_ent);
-               }
+       if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto out;
        }
 
+       status = cli_trans(talloc_tos(), cli, SMBnttrans,
+                          NULL, -1, /* name, fid */
+                          NT_TRANSACT_GET_USER_QUOTA, 0,
+                          setup, 1, 0, /* setup */
+                          ndr_push->data, ndr_push->offset, 4, /* params */
+                          data_blob.data, data_blob.length, 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)));
+               goto out;
+       }
 
-       ret = True;
- cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
+       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"));
+       }
 
-       return ret;
+out:
+       TALLOC_FREE(rparam);
+       TALLOC_FREE(rdata);
+       TALLOC_FREE(frame);
+       return status;
 }
 
-bool cli_get_fs_quota_info(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_LIST *qtl)
 {
-       bool ret = False;
-       uint16 setup;
-       char param[2];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-       SMB_NTQUOTA_STRUCT qt;
-       ZERO_STRUCT(qt);
+       uint16_t setup[1];
+       uint8_t params[2];
+       DATA_BLOB data = data_blob_null;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
-       if (!cli||!pqt) {
-               smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
+       if (!cli || !qtl) {
+               smb_panic("cli_set_user_quota() called with NULL Pointer!");
        }
 
-       setup = TRANSACT2_QFSINFO;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_set_user_quota(cli, quota_fnum, qtl);
+       }
 
-       SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
+       status = build_user_quota_buffer(qtl, 0, talloc_tos(), &data, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * smb1 doesn't send NT_STATUS_NO_MORE_ENTRIES so swallow
+                * this status.
+                */
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+                       status = NT_STATUS_OK;
+               } else {
+                       goto cleanup;
+               }
+       }
 
-       if (!cli_send_trans(cli, SMBtrans2, 
-                   NULL, 
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 2, 0,
-                   NULL, 0, 560)) {
-               goto cleanup;
+       SSVAL(setup + 0, 0, NT_TRANSACT_SET_USER_QUOTA);
+
+       SSVAL(params,0,quota_fnum);
+
+       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.data, data.length, 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_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
+cleanup:
+       data_blob_free(&data);
+       return status;
+}
+
+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)
+{
+       uint16_t setup[1];
+       DATA_BLOB params_blob = data_blob_null;
+       uint8_t *rparam=NULL, *rdata=NULL;
+       uint32_t rparam_count=0, rdata_count=0;
+       NTSTATUS status;
+       struct nttrans_query_quota_params quota_params = {0};
+       enum ndr_err_code err;
+
+       TALLOC_CTX *frame = NULL;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_list_user_quota_step(cli, mem_ctx, quota_fnum,
+                                                    pqt_list, first);
+       }
+       frame = talloc_stackframe();
+
+       SSVAL(setup + 0, 0, NT_TRANSACT_GET_USER_QUOTA);
+
+       quota_params.fid = quota_fnum;
+       if (first) {
+               quota_params.restart_scan = 1;
+       }
+       err = ndr_push_struct_blob(
+               &params_blob,
+               frame,
+               &quota_params,
+               (ndr_push_flags_fn_t)ndr_push_nttrans_query_quota_params);
+
+       if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
+               status = NT_STATUS_INVALID_PARAMETER;
                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_blob.data, params_blob.length, 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 (rdata_count < 48) {
+       if (!NT_STATUS_IS_OK(status)) {
                goto cleanup;
        }
 
-       /* unknown_1 24 NULL bytes in pdata*/
+       status = parse_user_quota_list(rdata, rdata_count, mem_ctx, pqt_list);
 
-       /* the soft quotas 8 bytes (uint64_t)*/
-       qt.softlim = (uint64_t)IVAL(rdata,24);
-#ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((uint64_t)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 */
+cleanup:
+       TALLOC_FREE(rparam);
+       TALLOC_FREE(rdata);
+       TALLOC_FREE(frame);
 
-       /* the hard quotas 8 bytes (uint64_t)*/
-       qt.hardlim = (uint64_t)IVAL(rdata,32);
-#ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((uint64_t)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;
+       return status;
+}
+
+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 (!cli || !pqt_list) {
+               smb_panic("cli_list_user_quota() called with NULL Pointer!");
        }
-#endif /* LARGE_SMB_OFF_T */
 
-       /* quota_flags 2 bytes **/
-       qt.qflags = SVAL(rdata,40);
+       *pqt_list = NULL;
 
-       qt.qtype = SMB_USER_FS_QUOTA_TYPE;
+       if ((mem_ctx = talloc_init("SMB_USER_QUOTA_LIST")) == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       *pqt = qt;
+       do {
+               status = cli_list_user_quota_step(cli, mem_ctx, quota_fnum,
+                                                 pqt_list, first);
+               first = false;
+       } while (NT_STATUS_IS_OK(status));
 
-       ret = True;
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+               status = NT_STATUS_OK;
+       }
 
-       return ret;     
+       if (!NT_STATUS_IS_OK(status) || *pqt_list == NULL) {
+               TALLOC_FREE(mem_ctx);
+       }
+
+       return status;
 }
 
-bool cli_set_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[4];
-       char data[48];
-       char *rparam=NULL, *rdata=NULL;
-       unsigned int rparam_count=0, rdata_count=0;
-       SMB_NTQUOTA_STRUCT qt;
-       ZERO_STRUCT(qt);
-       memset(data,'\0',48);
+       uint16_t setup[1];
+       uint8_t param[2];
+       uint8_t *rdata=NULL;
+       uint32_t rdata_count=0;
+       NTSTATUS status;
 
        if (!cli||!pqt) {
-               smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
+               smb_panic("cli_get_fs_quota_info() called with NULL Pointer!");
+       }
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_get_fs_quota_info(cli, quota_fnum, pqt);
        }
 
-       setup = TRANSACT2_SETFSINFO;
+       SSVAL(setup + 0, 0, TRANSACT2_QFSINFO);
 
-       SSVAL(param,0,quota_fnum);
-       SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
+       SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
 
-       /* Unknown1 24 NULL bytes*/
+       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);
 
-       /* Default Soft Quota 8 bytes */
-       SBIG_UINT(data,24,pqt->softlim);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
 
-       /* Default Hard Quota 8 bytes */
-       SBIG_UINT(data,32,pqt->hardlim);
+       status = parse_fs_quota_buffer(rdata, rdata_count, pqt);
 
-       /* Quota flag 2 bytes */
-       SSVAL(data,40,pqt->qflags);
+       TALLOC_FREE(rdata);
+       return status;
+}
 
-       /* Unknown3 6 NULL bytes */
+NTSTATUS cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum,
+                              SMB_NTQUOTA_STRUCT *pqt)
+{
+       uint16_t setup[1];
+       uint8_t param[4];
+       DATA_BLOB data = data_blob_null;
+       NTSTATUS status;
 
-       if (!cli_send_trans(cli, SMBtrans2, 
-                   NULL, 
-                   0, 0,
-                   &setup, 1, 0,
-                   param, 4, 0,
-                   data, 48, 0)) {
-               goto cleanup;
+       if (!cli||!pqt) {
+               smb_panic("cli_set_fs_quota_info() called with NULL Pointer!");
        }
 
-       if (!cli_receive_trans(cli, SMBtrans2,
-                              &rparam, &rparam_count,
-                              &rdata, &rdata_count)) {
-               goto cleanup;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_set_fs_quota_info(cli, quota_fnum, pqt);
        }
 
-       if (cli_is_error(cli)) {
-               ret = False;
-               goto cleanup;
-       } else {
-               ret = True;
+       status = build_fs_quota_buffer(talloc_tos(), pqt, &data, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
-cleanup:
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
+       SSVAL(setup + 0, 0,TRANSACT2_SETFSINFO);
 
-       return ret;     
-}
+       SSVAL(param,0,quota_fnum);
+       SSVAL(param,2,SMB_FS_QUOTA_INFORMATION);
 
-static const char *quota_str_static(uint64_t val, bool special, bool _numeric)
-{
-       const char *result;
+       status = cli_trans(talloc_tos(), cli, SMBtrans2,
+                          NULL, -1, /* name, fid */
+                          0, 0,     /* function, flags */
+                          setup, 1, 0, /* setup */
+                          param, 4, 0, /* param */
+                          data.data, data.length, 0, /* data */
+                          NULL,         /* recv_flags2 */
+                          NULL, 0, NULL, /* rsetup */
+                          NULL, 0, NULL, /* rparam */
+                          NULL, 0, NULL); /* rdata */
 
-       if (!_numeric&&special&&(val == SMB_NTQUOTAS_NO_LIMIT)) {
-               return "NO LIMIT";
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("SMB_FS_QUOTA_INFORMATION failed: %s\n",
+                         nt_errstr(status)));
        }
-       result = talloc_asprintf(talloc_tos(), "%"PRIu64, val);
-       SMB_ASSERT(result != NULL);
-       return result;
+
+       return status;
 }
 
-void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, bool _verbose, bool _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, bool _numeric))
+NTSTATUS fill_quota_buffer(TALLOC_CTX *mem_ctx,
+                             SMB_NTQUOTA_LIST *qlist,
+                             bool return_single,
+                             uint32_t max_data,
+                             DATA_BLOB *blob,
+                             SMB_NTQUOTA_LIST **end_ptr)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-
-       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");
+       int ndr_flags = NDR_SCALARS | NDR_BUFFERS;
+       struct ndr_push *qndr = ndr_push_init_ctx(mem_ctx);
+       uint32_t start_offset = 0;
+       uint32_t padding = 0;
+       if (qlist == NULL) {
+               /* We must push at least one. */
+               return NT_STATUS_NO_MORE_ENTRIES;
+       }
+       for (;qlist != NULL; qlist = qlist->next) {
+               struct file_quota_information info = {0};
+               enum ndr_err_code err;
+               uint32_t dsize = sizeof(info.next_entry_offset)
+                       + sizeof(info.sid_length)
+                       + sizeof(info.change_time)
+                       + sizeof(info.quota_used)
+                       + sizeof(info.quota_threshold)
+                       + sizeof(info.quota_limit);
+
+
+               info.sid_length = ndr_size_dom_sid(&qlist->quotas->sid, 0);
+
+               if (max_data) {
+                       uint32_t curr_pos_no_padding = qndr->offset - padding;
+                       uint32_t payload = dsize + info.sid_length;
+                       uint32_t new_pos = (curr_pos_no_padding + payload);
+                       if (new_pos < curr_pos_no_padding) {
+                               /* Detect unlikely integer wrap */
+                               DBG_ERR("Integer wrap while adjusting pos "
+                                       "0x%x by offset 0x%x\n",
+                                       curr_pos_no_padding, payload);
+                               return NT_STATUS_INTERNAL_ERROR;
                        }
-                       break;
-               case SMB_USER_QUOTA_TYPE:
-                       {
-                               fstring username_str = {0};
-
-                               if (_sidtostring) {
-                                       _sidtostring(username_str,&qt->sid,_numeric);
-                               } else {
-                                       sid_to_fstring(username_str, &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));
-                               }
+                       if (new_pos > max_data) {
+                               DBG_WARNING("Max data will be exceeded "
+                                           "writing next query info. "
+                                           "cur_pos 0x%x, sid_length 0x%x, "
+                                           "dsize 0x%x, max_data 0x%x\n",
+                                           curr_pos_no_padding,
+                                           info.sid_length,
+                                           dsize,
+                                           max_data);
+                               break;
                        }
+               }
+
+               start_offset = qndr->offset;
+               info.sid = qlist->quotas->sid;
+               info.quota_used = qlist->quotas->usedspace;
+               info.quota_threshold = qlist->quotas->softlim;
+               info.quota_limit = qlist->quotas->hardlim;
+
+               err = ndr_push_file_quota_information(qndr,
+                                                     ndr_flags,
+                                                     &info);
+
+               if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
+                       DBG_DEBUG("Failed to push the quota sid\n");
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+
+               /* pidl will align to 8 bytes due to 8 byte members*/
+               /* Remember how much align padding we've used. */
+               padding = qndr->offset;
+               ndr_push_align(qndr, 8);
+               padding = qndr->offset - padding;
+
+               /*
+                * Overwrite next_entry_offset for this entry now
+                * we know what it should be. We know we're using
+                * LIBNDR_FLAG_LITTLE_ENDIAN here so we can use
+                * SIVAL.
+                */
+               info.next_entry_offset = qndr->offset - start_offset;
+               SIVAL(qndr->data, start_offset, info.next_entry_offset);
+
+               if (return_single) {
                        break;
-               default:
-                       d_printf("dump_ntquota() invalid qtype(%d)\n",qt->qtype);
+               }
        }
-       TALLOC_FREE(frame);
-       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;
+       if (end_ptr != NULL) {
+               *end_ptr = qlist;
+       }
 
-       for (cur = *qtl;cur;cur = cur->next) {
-               if (cur->quotas)
-                       dump_ntquota(cur->quotas,_verbose,_numeric,_sidtostring);
-       }       
+       /* Remove the padding alignment on the last element pushed. */
+       blob->length = qndr->offset - padding;
+       blob->data = qndr->data;
+
+       /*
+        * Terminate the pushed array by setting next_entry_offset
+        * for the last element to zero.
+        */
+       if (blob->length >= sizeof(uint32_t)) {
+               SIVAL(qndr->data, start_offset, 0);
+       }
+       return NT_STATUS_OK;
 }