Fix bug #7669.
[obnox/samba-ctdb.git] / source3 / libsmb / cliquota.c
index 8bba453d526278a46510b4e27c774e54d9279c74..2af5b22da5695c1e46f6bacba3aa5c473cce9256 100644 (file)
@@ -2,31 +2,30 @@
    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)
+bool cli_get_quota_handle(struct cli_state *cli, int *quota_fnum)
 {
        *quota_fnum = cli_nt_create_full(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;
        }
@@ -38,7 +37,7 @@ void free_ntquota_list(SMB_NTQUOTA_LIST **qt_list)
 {
        if (!qt_list)
                return;
-               
+
        if ((*qt_list)->mem_ctx)
                talloc_destroy((*qt_list)->mem_ctx);
 
@@ -47,7 +46,7 @@ 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)
+static bool parse_user_quota_record(const char *rdata, unsigned int rdata_count, unsigned int *offset, SMB_NTQUOTA_STRUCT *pqt)
 {
        int sid_len;
        SMB_NTQUOTA_STRUCT qt;
@@ -61,7 +60,7 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
        if (rdata_count < 40) {
                return False;
        }
-               
+
        /* offset to next quota record.
         * 4 bytes IVAL(rdata,0)
         * unused here...
@@ -79,10 +78,10 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
         * maybe its the change time in NTTIME
         */
 
-       /* the used space 8 bytes (SMB_BIG_UINT)*/
-       qt.usedspace = (SMB_BIG_UINT)IVAL(rdata,16);
+       /* the used space 8 bytes (uint64_t)*/
+       qt.usedspace = (uint64_t)IVAL(rdata,16);
 #ifdef LARGE_SMB_OFF_T
-       qt.usedspace |= (((SMB_BIG_UINT)IVAL(rdata,20)) << 32);
+       qt.usedspace |= (((uint64_t)IVAL(rdata,20)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(rdata,20) != 0)&&
                ((qt.usedspace != 0xFFFFFFFF)||
@@ -92,10 +91,10 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.softlim = (SMB_BIG_UINT)IVAL(rdata,24);
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = (uint64_t)IVAL(rdata,24);
 #ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
+       qt.softlim |= (((uint64_t)IVAL(rdata,28)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(rdata,28) != 0)&&
                ((qt.softlim != 0xFFFFFFFF)||
@@ -105,10 +104,10 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = (uint64_t)IVAL(rdata,32);
 #ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
+       qt.hardlim |= (((uint64_t)IVAL(rdata,36)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(rdata,36) != 0)&&
                ((qt.hardlim != 0xFFFFFFFF)||
@@ -117,8 +116,10 @@ static BOOL parse_user_quota_record(const char *rdata, unsigned int rdata_count,
                return False;
        }
 #endif /* LARGE_SMB_OFF_T */
-       
-       sid_parse(rdata+40,sid_len,&qt.sid);
+
+       if (!sid_parse(rdata+40,sid_len,&qt.sid)) {
+               return false;
+       }
 
        qt.qtype = SMB_USER_QUOTA_TYPE;
 
@@ -127,9 +128,9 @@ 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)
+bool cli_get_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
+       bool ret = False;
        uint16 setup;
        char params[16];
        unsigned int data_len;
@@ -150,13 +151,13 @@ BOOL cli_get_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUC
        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, NULL, 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, 
@@ -195,9 +196,9 @@ BOOL cli_get_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUC
        return ret;
 }
 
-BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+bool cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
+       bool ret = False;
        uint16 setup;
        char params[2];
        char data[112];
@@ -205,7 +206,7 @@ BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUC
        unsigned int rparam_count=0, rdata_count=0;
        unsigned int sid_len;   
        memset(data,'\0',112);
-       
+
        if (!cli||!pqt) {
                smb_panic("cli_set_user_quota() called with NULL Pointer!");
        }
@@ -214,15 +215,15 @@ BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUC
 
        SSVAL(params,0,quota_fnum);
 
-       sid_len = sid_size(&pqt->sid);
+       sid_len = ndr_size_dom_sid(&pqt->sid, NULL, 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, 
@@ -254,9 +255,9 @@ BOOL cli_set_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUC
        return ret;
 }
 
-BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST **pqt_list)
+bool cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST **pqt_list)
 {
-       BOOL ret = False;
+       bool ret = False;
        uint16 setup;
        char params[16];
        char *rparam=NULL, *rdata=NULL;
@@ -279,7 +280,7 @@ BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST
        SIVAL(params, 4,0x00000000);
        SIVAL(params, 8,0x00000000);
        SIVAL(params,12,0x00000000);
-       
+
        if (!cli_send_nt_trans(cli, 
                               NT_TRANSACT_GET_USER_QUOTA, 
                               0, 
@@ -354,7 +355,7 @@ BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST
                        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,
@@ -370,7 +371,7 @@ BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST
                } else {
                        ret = True;
                }
-       
+
                if (rdata_count == 0) {
                        break;  
                }
@@ -390,32 +391,32 @@ BOOL cli_list_user_quota(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_LIST
                                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);
                }
        }
 
+
        ret = True;
  cleanup:
        SAFE_FREE(rparam);
        SAFE_FREE(rdata);
+
        return ret;
 }
 
-BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+bool cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
+       bool ret = False;
        uint16 setup;
        char param[2];
        char *rparam=NULL, *rdata=NULL;
@@ -428,9 +429,9 @@ BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
        }
 
        setup = TRANSACT2_QFSINFO;
-       
+
        SSVAL(param,0,SMB_FS_QUOTA_INFORMATION);
-       
+
        if (!cli_send_trans(cli, SMBtrans2, 
                    NULL, 
                    0, 0,
@@ -439,7 +440,7 @@ BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
                    NULL, 0, 560)) {
                goto cleanup;
        }
-       
+
        if (!cli_receive_trans(cli, SMBtrans2,
                               &rparam, &rparam_count,
                               &rdata, &rdata_count)) {
@@ -456,13 +457,13 @@ BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
        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);
+       /* the soft quotas 8 bytes (uint64_t)*/
+       qt.softlim = (uint64_t)IVAL(rdata,24);
 #ifdef LARGE_SMB_OFF_T
-       qt.softlim |= (((SMB_BIG_UINT)IVAL(rdata,28)) << 32);
+       qt.softlim |= (((uint64_t)IVAL(rdata,28)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(rdata,28) != 0)&&
                ((qt.softlim != 0xFFFFFFFF)||
@@ -472,10 +473,10 @@ BOOL cli_get_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
        }
 #endif /* LARGE_SMB_OFF_T */
 
-       /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
-       qt.hardlim = (SMB_BIG_UINT)IVAL(rdata,32);
+       /* the hard quotas 8 bytes (uint64_t)*/
+       qt.hardlim = (uint64_t)IVAL(rdata,32);
 #ifdef LARGE_SMB_OFF_T
-       qt.hardlim |= (((SMB_BIG_UINT)IVAL(rdata,36)) << 32);
+       qt.hardlim |= (((uint64_t)IVAL(rdata,36)) << 32);
 #else /* LARGE_SMB_OFF_T */
        if ((IVAL(rdata,36) != 0)&&
                ((qt.hardlim != 0xFFFFFFFF)||
@@ -500,9 +501,9 @@ cleanup:
        return ret;     
 }
 
-BOOL cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
+bool cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_STRUCT *pqt)
 {
-       BOOL ret = False;
+       bool ret = False;
        uint16 setup;
        char param[4];
        char data[48];
@@ -542,7 +543,7 @@ BOOL cli_set_fs_quota_info(struct cli_state *cli, int quota_fnum, SMB_NTQUOTA_ST
                    data, 48, 0)) {
                goto cleanup;
        }
-       
+
        if (!cli_receive_trans(cli, SMBtrans2,
                               &rparam, &rparam_count,
                               &rdata, &rdata_count)) {
@@ -563,26 +564,22 @@ cleanup:
        return ret;     
 }
 
-static char *quota_str_static(SMB_BIG_UINT val, BOOL special, BOOL _numeric)
+static const char *quota_str_static(uint64_t val, bool special, bool _numeric)
 {
-       static fstring buffer;
-       
-       memset(buffer,'\0',sizeof(buffer));
+       const char *result;
 
        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;
+               return "NO LIMIT";
+       }
+       result = talloc_asprintf(talloc_tos(), "%"PRIu64, val);
+       SMB_ASSERT(result != NULL);
+       return result;
 }
 
-void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, BOOL _verbose, BOOL _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, BOOL _numeric))
+void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, bool _verbose, bool _numeric, void (*_sidtostring)(fstring str, DOM_SID *sid, bool _numeric))
 {
+       TALLOC_CTX *frame = talloc_stackframe();
+
        if (!qt) {
                smb_panic("dump_ntquota() called with NULL pointer");
        }
@@ -605,11 +602,11 @@ void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, BOOL _verbose, BOOL _numeric, void (*_
                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));
+                                       sid_to_fstring(username_str, &qt->sid);
                                }
 
                                if (_verbose) { 
@@ -627,11 +624,12 @@ void dump_ntquota(SMB_NTQUOTA_STRUCT *qt, BOOL _verbose, BOOL _numeric, void (*_
                        break;
                default:
                        d_printf("dump_ntquota() invalid qtype(%d)\n",qt->qtype);
-                       return;
        }
+       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))
+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;