s3-nmbd: move nmbd proto out of main proto.h
[metze/samba/wip.git] / source3 / nmbd / nmbd_synclists.c
index 300368cd60de37b2c11d09c9e6097fc91013db41..5c139b31afb3723ef938f4a6bb44a6ffdece7639 100644 (file)
    also allows us to have more than 1 sync going at once (tridge) */
 
 #include "includes.h"
+#include "../librpc/gen_ndr/svcctl.h"
+#include "nmbd/nmbd.h"
 
 struct sync_record {
        struct sync_record *next, *prev;
        unstring workgroup;
        unstring server;
-       pstring fname;
+       char *fname;
        struct in_addr ip;
        pid_t pid;
 };
@@ -80,13 +82,12 @@ static void sync_child(char *name, int nm_type,
                return;
        }
 
-       if (!cli_set_port(cli, 139)) {
-               return;
-       }
+       cli_set_port(cli, 139);
 
        in_addr_to_sockaddr_storage(&ss, ip);
        status = cli_connect(cli, name, &ss);
        if (!NT_STATUS_IS_OK(status)) {
+               cli_shutdown(cli);
                return;
        }
 
@@ -98,7 +99,8 @@ static void sync_child(char *name, int nm_type,
                return;
        }
 
-       if (!cli_negprot(cli)) {
+       status = cli_negprot(cli);
+       if (!NT_STATUS_IS_OK(status)) {
                cli_shutdown(cli);
                return;
        }
@@ -109,7 +111,7 @@ static void sync_child(char *name, int nm_type,
                return;
        }
 
-       if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
+       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", "", 1))) {
                cli_shutdown(cli);
                return;
        }
@@ -159,15 +161,18 @@ 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_lockdir(), 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
@@ -183,7 +188,7 @@ 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,
@@ -198,7 +203,7 @@ done:
  Handle one line from a completed sync file.
  **********************************************************************/
 
-static void complete_one(struct sync_record *s, 
+static void complete_one(struct sync_record *s,
                         char *sname, uint32 stype, char *comment)
 {
        struct work_record *work;
@@ -247,7 +252,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 +260,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 +272,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 +294,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 +318,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);
                }
        }