libnmb: Make nb_packet_read_recv return a talloc'ed pkt
[samba.git] / source3 / libsmb / clirap2.c
index 20efca3dc325906c7b32005f3bf9e75d7b92d0c2..3dd299c1cb33f25b56942fde62cc5e5d9499c694 100644 (file)
 /*****************************************************/
 
 #include "includes.h"
+#include "libsmb/libsmb.h"
 #include "../librpc/gen_ndr/rap.h"
 #include "../librpc/gen_ndr/svcctl.h"
+#include "libsmb/clirap.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 #define WORDSIZE 2
 #define DWORDSIZE 4
@@ -215,7 +218,7 @@ static size_t rap_getstringp(TALLOC_CTX *ctx, char *p, char **dest, char *r, uin
        return 4;
 }
 
-static char *make_header(char *param, uint16 apinum, const char *reqfmt, const char *datafmt)
+static char *make_header(char *param, uint16_t apinum, const char *reqfmt, const char *datafmt)
 {
        PUTWORD(param,apinum);
        if (reqfmt)
@@ -328,7 +331,7 @@ int cli_NetGroupAdd(struct cli_state *cli, struct rap_group_info_1 *grinfo)
 
        if (cli_api(cli,
              param, sizeof(param), 1024, /* Param, length, maxlen */
-             data, soffset, sizeof(data), /* data, length, maxlen */
+             data, soffset, data_size, /* data, length, maxlen */
              &rparam, &rprcnt,   /* return params, length */
              &rdata, &rdrcnt))   /* return data, length */
        {
@@ -864,10 +867,8 @@ int cli_NetUserAdd(struct cli_state *cli, struct rap_user_info_1 * userinfo )
 
        PUTWORD(p, 1); /* info level */
        PUTWORD(p, 0); /* pwencrypt */
-       if(userinfo->passwrd)
-               PUTWORD(p,MIN(strlen((const char *)userinfo->passwrd), RAP_UPASSWD_LEN));
-       else
-               PUTWORD(p, 0); /* password length */
+       PUTWORD(p, MIN(strlen((const char *)userinfo->passwrd),
+                      RAP_UPASSWD_LEN));
 
        p = data;
        memset(data, '\0', soffset);
@@ -1096,7 +1097,7 @@ int cli_RNetUserEnum0(struct cli_state *cli,
  Call a NetFileClose2 - close open file on another session to server.
 ****************************************************************************/
 
-int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
+int cli_NetFileClose(struct cli_state *cli, uint32_t file_id )
 {
        char *rparam = NULL;
        char *rdata = NULL;
@@ -1124,7 +1125,7 @@ int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
                if (res == 0) {
                        /* nothing to do */
                } else if (res == 2314){
-                       DEBUG(1, ("NetFileClose2 - attempt to close non-existant file open instance\n"));
+                       DEBUG(1, ("NetFileClose2 - attempt to close non-existent file open instance\n"));
                } else {
                        DEBUG(4,("NetFileClose2 res=%d\n", res));
                }
@@ -1144,7 +1145,7 @@ int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
  workstation.
 ****************************************************************************/
 
-int cli_NetFileGetInfo(struct cli_state *cli, uint32 file_id, void (*fn)(const char *, const char *, uint16, uint16, uint32))
+int cli_NetFileGetInfo(struct cli_state *cli, uint32_t file_id, void (*fn)(const char *, const char *, uint16_t, uint16_t, uint32_t))
 {
        char *rparam = NULL;
        char *rdata = NULL;
@@ -1244,8 +1245,8 @@ int cli_NetFileGetInfo(struct cli_state *cli, uint32 file_id, void (*fn)(const c
 
 int cli_NetFileEnum(struct cli_state *cli, const char * user,
                    const char * base_path,
-                   void (*fn)(const char *, const char *, uint16, uint16,
-                              uint32))
+                   void (*fn)(const char *, const char *, uint16_t, uint16_t,
+                              uint32_t))
 {
        char *rparam = NULL;
        char *rdata = NULL;
@@ -1531,8 +1532,10 @@ bool cli_get_pdc_name(struct cli_state *cli, const char *workgroup, char **pdc_n
                                TALLOC_FREE(frame);
                        }
                } else {
-                       DEBUG(4,("cli_get_pdc_name: machine %s failed the NetServerEnum call. "
-                               "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
+                       DEBUG(4, ("cli_get_pdc_name: machine %s failed the "
+                                 "NetServerEnum call. Error was : %s.\n",
+                                 smbXcli_conn_remote_name(cli->conn),
+                                 win_errstr(W_ERROR(cli->rap_error))));
                }
        }
 
@@ -1542,150 +1545,6 @@ bool cli_get_pdc_name(struct cli_state *cli, const char *workgroup, char **pdc_n
        return(count > 0);
 }
 
-/*************************************************************************
-*
-* Function Name:  cli_get_server_domain
-*
-* PURPOSE:  Remotes a NetWkstaGetInfo API call to the current server
-*           requesting wksta_info_10 level information to determine
-*           the domain the server belongs to. On success, this
-*           routine sets the server_domain field in the cli_state structure
-*           to the server's domain name.
-*
-* Dependencies: none
-*
-* Parameters:
-*             cli       - pointer to cli_state structure
-*
-* Returns:
-*             True      - success
-*             False     - failure
-*
-* Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
-*
-************************************************************************/
-
-bool cli_get_server_domain(struct cli_state *cli)
-{
-       char *rparam = NULL;
-       char *rdata = NULL;
-       unsigned int rdrcnt,rprcnt;
-       char *p;
-       char param[WORDSIZE                      /* api number    */
-                       +sizeof(RAP_WWkstaGetInfo_REQ) /* req string    */
-                       +sizeof(RAP_WKSTA_INFO_L10)    /* return string */
-                       +WORDSIZE                      /* info level    */
-                       +WORDSIZE];                    /* buffer size   */
-       int res = -1;
-
-       /* send a SMBtrans command with api NetWkstaGetInfo */
-       p = make_header(param, RAP_WWkstaGetInfo,
-               RAP_WWkstaGetInfo_REQ, RAP_WKSTA_INFO_L10);
-       PUTWORD(p, 10); /* info level */
-       PUTWORD(p, CLI_BUFFER_SIZE);
-
-       if (cli_api(cli, param, PTR_DIFF(p,param), 8, /* params, length, max */
-                       NULL, 0, CLI_BUFFER_SIZE,         /* data, length, max */
-                       &rparam, &rprcnt,         /* return params, return size */
-                       &rdata, &rdrcnt)) {       /* return data, return size */
-               char *endp = rparam + rprcnt;
-               res = GETRES(rparam, endp);
-
-               if (res == 0) {
-                       TALLOC_CTX *frame = talloc_stackframe();
-                       char *server_domain;
-                       int converter = 0;
-
-                       p = rparam + WORDSIZE;
-                       GETWORD(p, converter, endp);
-
-                       p = rdata + DWORDSIZE + DWORDSIZE; /* skip computer & user names */
-                       endp = rdata + rdrcnt;
-                       p += rap_getstringp(frame,
-                               p,
-                               &server_domain,
-                               rdata,
-                               converter,
-                               endp);
-
-                       if (server_domain) {
-                               fstrcpy(cli->server_domain, server_domain);
-                       }
-                       TALLOC_FREE(frame);
-               }
-       }
-
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return(res == 0);
-}
-
-/*************************************************************************
-*
-* Function Name:  cli_get_server_type
-*
-* PURPOSE:  Remotes a NetServerGetInfo API call to the current server
-*           requesting server_info_1 level information to retrieve
-*           the server type.
-*
-* Dependencies: none
-*
-* Parameters:
-*             cli       - pointer to cli_state structure
-*             pstype    - pointer to uint32 to contain returned server type
-*
-* Returns:
-*             True      - success
-*             False     - failure
-*
-* Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
-*
-************************************************************************/
-
-bool cli_get_server_type(struct cli_state *cli, uint32 *pstype)
-{
-       char *rparam = NULL;
-       char *rdata = NULL;
-       unsigned int rdrcnt,rprcnt;
-       char *p;
-       char param[WORDSIZE                       /* api number    */
-               +sizeof(RAP_WserverGetInfo_REQ) /* req string    */
-               +sizeof(RAP_SERVER_INFO_L1)     /* return string */
-               +WORDSIZE                       /* info level    */
-               +WORDSIZE];                     /* buffer size   */
-       int res = -1;
-
-       /* send a SMBtrans command with api NetServerGetInfo */
-       p = make_header(param, RAP_WserverGetInfo,
-               RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
-       PUTWORD(p, 1); /* info level */
-       PUTWORD(p, CLI_BUFFER_SIZE);
-
-       if (cli_api(cli,
-                       param, PTR_DIFF(p,param), 8, /* params, length, max */
-                       NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
-                       &rparam, &rprcnt,         /* return params, return size */
-                       &rdata, &rdrcnt           /* return data, return size */
-                       )) {
-               char *endp = rparam + rprcnt;
-               res = GETRES(rparam,endp);
-
-               if (res == 0 || res == ERRmoredata) {
-                       p = rdata;
-                       endp = rparam + rprcnt;
-                       p += 18;
-                       GETDWORD(p,*pstype,endp);
-                       *pstype &= ~SV_TYPE_LOCAL_LIST_ONLY;
-               }
-       }
-
-       SAFE_FREE(rparam);
-       SAFE_FREE(rdata);
-
-       return(res == 0 || res == ERRmoredata);
-}
-
 bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
                         char **servername)
 {
@@ -1752,7 +1611,7 @@ bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
 * PURPOSE:  Remotes a NetServerEnum2 API call to the current server
 *           requesting server_info_0 level information of machines
 *           matching the given server type. If the returned server
-*           list contains the machine name contained in cli->desthost
+*           list contains the machine name contained in smbXcli_conn_remote_name(->conn)
 *           then we conclude the server type checks out. This routine
 *           is useful to retrieve list of server's of a certain
 *           type when all you have is a null session connection and
@@ -1772,7 +1631,7 @@ bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
 *
 ************************************************************************/
 
-bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 stype)
+bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32_t stype)
 {
        char *rparam = NULL;
        char *rdata = NULL;
@@ -1787,6 +1646,7 @@ bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 sty
                +RAP_MACHNAME_LEN];             /* workgroup     */
        bool found_server = false;
        int res = -1;
+       const char *remote_name = smbXcli_conn_remote_name(cli->conn);
 
        /* send a SMBtrans command with api NetServerEnum */
        p = make_header(param, RAP_NetServerEnum2,
@@ -1822,14 +1682,16 @@ bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 sty
                                                RAP_MACHNAME_LEN,
                                                RAP_MACHNAME_LEN,
                                                endp);
-                               if (strequal(ret_server, cli->desthost)) {
+                               if (strequal(ret_server, remote_name)) {
                                        found_server = true;
                                        break;
                                }
                        }
                } else {
-                       DEBUG(4,("cli_ns_check_server_type: machine %s failed the NetServerEnum call. "
-                               "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
+                       DEBUG(4, ("cli_ns_check_server_type: machine %s "
+                                 "failed the NetServerEnum call. Error was : "
+                                 "%s.\n", remote_name,
+                                 win_errstr(W_ERROR(cli->rap_error))));
                }
        }
 
@@ -1868,12 +1730,16 @@ bool cli_NetWkstaUserLogoff(struct cli_state *cli, const char *user, const char
        PUTDWORD(p, 0); /* Null pointer */
        PUTDWORD(p, 0); /* Null pointer */
        strlcpy(upperbuf, user, sizeof(upperbuf));
-       strupper_m(upperbuf);
+       if (!strupper_m(upperbuf)) {
+               return false;
+       }
        tmp = upperbuf;
        PUTSTRINGF(p, tmp, RAP_USERNAME_LEN);
        p++; /* strange format, but ok */
        strlcpy(upperbuf, workstation, sizeof(upperbuf));
-       strupper_m(upperbuf);
+       if (!strupper_m(upperbuf)) {
+               return false;
+       }
        tmp = upperbuf;
        PUTSTRINGF(p, tmp, RAP_MACHNAME_LEN);
        PUTWORD(p, CLI_BUFFER_SIZE);
@@ -1900,8 +1766,8 @@ bool cli_NetWkstaUserLogoff(struct cli_state *cli, const char *user, const char
 }
 
 int cli_NetPrintQEnum(struct cli_state *cli,
-               void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
-               void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,unsigned int,unsigned int,const char*))
+               void (*qfn)(const char*,uint16_t,uint16_t,uint16_t,const char*,const char*,const char*,const char*,const char*,uint16_t,uint16_t),
+               void (*jfn)(uint16_t,const char*,const char*,const char*,const char*,uint16_t,uint16_t,const char*,unsigned int,unsigned int,const char*))
 {
        char param[WORDSIZE                         /* api number    */
                +sizeof(RAP_NetPrintQEnum_REQ)    /* req string    */
@@ -2008,7 +1874,7 @@ int cli_NetPrintQEnum(struct cli_state *cli,
                        if (jobcount) {
                                int j;
                                for (j=0;j<jobcount;j++) {
-                                       uint16 jid = 0, pos = 0, fsstatus = 0;
+                                       uint16_t jid = 0, pos = 0, fsstatus = 0;
                                        char ownername[RAP_USERNAME_LEN];
                                        char notifyname[RAP_MACHNAME_LEN];
                                        char datatype[RAP_DATATYPE_LEN];
@@ -2076,8 +1942,8 @@ int cli_NetPrintQEnum(struct cli_state *cli,
 }
 
 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
-       void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
-       void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,unsigned int,unsigned int,const char*))
+       void (*qfn)(const char*,uint16_t,uint16_t,uint16_t,const char*,const char*,const char*,const char*,const char*,uint16_t,uint16_t),
+       void (*jfn)(uint16_t,const char*,const char*,const char*,const char*,uint16_t,uint16_t,const char*,unsigned int,unsigned int,const char*))
 {
        char param[WORDSIZE                         /* api number    */
                +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string    */
@@ -2337,7 +2203,7 @@ int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const ch
  Call a NetSessionEnum - list workstations with sessions to an SMB server.
 ****************************************************************************/
 
-int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16, uint16, uint16, unsigned int, unsigned int, unsigned int, char *))
+int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16_t, uint16_t, uint16_t, unsigned int, unsigned int, unsigned int, char *))
 {
        char param[WORDSIZE                       /* api number    */
                +sizeof(RAP_NetSessionEnum_REQ) /* parm string   */
@@ -2437,7 +2303,7 @@ int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16,
 ****************************************************************************/
 
 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation,
-               void (*fn)(const char *, const char *, uint16, uint16, uint16, unsigned int, unsigned int, unsigned int, const char *))
+               void (*fn)(const char *, const char *, uint16_t, uint16_t, uint16_t, unsigned int, unsigned int, unsigned int, const char *))
 {
        char param[WORDSIZE                          /* api number    */
                +sizeof(RAP_NetSessionGetInfo_REQ) /* req string    */