s3:nmbd: make use of cli_session_setup_anon()
[samba.git] / source3 / nmbd / nmbd_synclists.c
index df71e6db437baa7257990e9c9d6d05fd293e2997..f3e524d932a292a7d697d0170266cd75869154ec 100644 (file)
@@ -4,20 +4,19 @@
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
    Copyright (C) Jeremy Allison 1994-1998
-   
+
    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 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, see <http://www.gnu.org/licenses/>.
-   
 */
 
 /* this file handles asynchronous browse synchronisation requests. The
    also allows us to have more than 1 sync going at once (tridge) */
 
 #include "includes.h"
-
-extern fstring local_machine;
+#include "system/filesys.h"
+#include "../librpc/gen_ndr/svcctl.h"
+#include "nmbd/nmbd.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 struct sync_record {
        struct sync_record *next, *prev;
        unstring workgroup;
        unstring server;
-       pstring fname;
+       char *fname;
        struct in_addr ip;
        pid_t pid;
 };
@@ -49,7 +52,7 @@ static XFILE *fp;
   Note sname and comment are in UNIX codepage format.
   ******************************************************************/
 
-static void callback(const char *sname, uint32 stype, 
+static void callback(const char *sname, uint32_t stype,
                      const char *comment, void *state)
 {
        x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
@@ -68,48 +71,37 @@ static void sync_child(char *name, int nm_type,
 {
        fstring unix_workgroup;
        struct cli_state *cli;
-       uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
-       struct nmb_name called, calling;
+       uint32_t local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
+       struct sockaddr_storage ss;
        NTSTATUS status;
 
        /* W2K DMB's return empty browse lists on port 445. Use 139.
         * Patch from Andy Levine andyl@epicrealm.com.
         */
 
-       cli = cli_initialise();
-       if (!cli) {
-               return;
-       }
+       in_addr_to_sockaddr_storage(&ss, ip);
 
-       if (!cli_set_port(cli, 139)) {
-               return;
-       }
-
-       status = cli_connect(cli, name, &ip);
+       status = cli_connect_nb(name, &ss, NBT_SMB_PORT, nm_type,
+                               get_local_machine_name(), SMB_SIGNING_DEFAULT,
+                               0, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                return;
        }
 
-       make_nmb_name(&calling, local_machine, 0x0);
-       make_nmb_name(&called , name, nm_type);
-
-       if (!cli_session_request(cli, &calling, &called)) {
-               cli_shutdown(cli);
-               return;
-       }
-
-       if (!cli_negprot(cli)) {
+       status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
+                                PROTOCOL_NT1);
+       if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(cli);
                return;
        }
 
-       if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
-                                              workgroup))) {
+       status = cli_session_setup_anon(cli);
+       if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(cli);
                return;
        }
 
-       if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
+       if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", "", 1))) {
                cli_shutdown(cli);
                return;
        }
@@ -121,7 +113,7 @@ static void sync_child(char *name, int nm_type,
        cli_NetServerEnum(cli, unix_workgroup,
                          local_type|SV_TYPE_DOMAIN_ENUM, 
                          callback, NULL);
-       
+
        /* Now fetch a server list. */
        if (servers) {
                fstrcpy(unix_workgroup, workgroup);
@@ -129,7 +121,7 @@ static void sync_child(char *name, int nm_type,
                                  local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
                                  callback, NULL);
        }
-       
+
        cli_shutdown(cli);
 }
 
@@ -146,12 +138,10 @@ void sync_browse_lists(struct work_record *work,
        struct sync_record *s;
        static int counter;
 
-       START_PROFILE(sync_browse_lists);
        /* Check we're not trying to sync with ourselves. This can
           happen if we are a domain *and* a local master browser. */
        if (ismyip_v4(ip)) {
 done:
-               END_PROFILE(sync_browse_lists);
                return;
        }
 
@@ -159,21 +149,24 @@ done:
        if (!s) goto done;
 
        ZERO_STRUCTP(s);
-       
+
        unstrcpy(s->workgroup, work->work_group);
        unstrcpy(s->server, name);
        s->ip = ip;
 
-       slprintf(s->fname, sizeof(pstring)-1,
-                "%s/sync.%d", lp_lockdir(), counter++);
+       if (asprintf(&s->fname, "%s/sync.%d", lp_lock_directory(), counter++) < 0) {
+               SAFE_FREE(s);
+               goto done;
+       }
+       /* Safe to use as 0 means no size change. */
        all_string_sub(s->fname,"//", "/", 0);
-       
+
        DLIST_ADD(syncs, s);
 
        /* the parent forks and returns, leaving the child to do the
-          actual sync and call END_PROFILE*/
+          actual sync */
        CatchChild();
-       if ((s->pid = sys_fork())) return;
+       if ((s->pid = fork())) return;
 
        BlockSignals( False, SIGTERM );
 
@@ -182,15 +175,13 @@ done:
 
        fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
        if (!fp) {
-               END_PROFILE(sync_browse_lists);
-               _exit(1);       
+               _exit(1);
        }
 
        sync_child(name, nm_type, work->work_group, ip, local, servers,
                   s->fname);
 
        x_fclose(fp);
-       END_PROFILE(sync_browse_lists);
        _exit(0);
 }
 
@@ -198,8 +189,8 @@ done:
  Handle one line from a completed sync file.
  **********************************************************************/
 
-static void complete_one(struct sync_record *s, 
-                        char *sname, uint32 stype, char *comment)
+static void complete_one(struct sync_record *s,
+                        char *sname, uint32_t stype, char *comment)
 {
        struct work_record *work;
        struct server_record *servrec;
@@ -247,7 +238,7 @@ static void complete_one(struct sync_record *s,
        /* Create the server in the workgroup. */ 
        create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
 }
-               
+
 /**********************************************************************
  Read the completed sync info.
 **********************************************************************/
@@ -255,10 +246,11 @@ static void complete_one(struct sync_record *s,
 static void complete_sync(struct sync_record *s)
 {
        XFILE *f;
-       unstring server, type_str;
+       char *server;
+       char *type_str;
        unsigned type;
-       pstring comment;
-       pstring line;
+       char *comment;
+       char line[1024];
        const char *ptr;
        int count=0;
 
@@ -266,17 +258,20 @@ static void complete_sync(struct sync_record *s)
 
        if (!f)
                return;
-       
+
        while (!x_feof(f)) {
-               
-               if (!fgets_slash(line,sizeof(pstring),f))
+               TALLOC_CTX *frame = NULL;
+
+               if (!fgets_slash(line,sizeof(line),f))
                        continue;
-               
+
                ptr = line;
 
-               if (!next_token(&ptr,server,NULL,sizeof(server)) ||
-                   !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
-                   !next_token(&ptr,comment,NULL, sizeof(comment))) {
+               frame = talloc_stackframe();
+               if (!next_token_talloc(frame,&ptr,&server,NULL) ||
+                   !next_token_talloc(frame,&ptr,&type_str,NULL) ||
+                   !next_token_talloc(frame,&ptr,&comment,NULL)) {
+                       TALLOC_FREE(frame);
                        continue;
                }
 
@@ -285,8 +280,8 @@ static void complete_sync(struct sync_record *s)
                complete_one(s, server, type, comment);
 
                count++;
+               TALLOC_FREE(frame);
        }
-
        x_fclose(f);
 
        unlink(s->fname);
@@ -309,7 +304,7 @@ void sync_check_completion(void)
                        /* it has completed - grab the info */
                        complete_sync(s);
                        DLIST_REMOVE(syncs, s);
-                       ZERO_STRUCTP(s);
+                       SAFE_FREE(s->fname);
                        SAFE_FREE(s);
                }
        }