r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.git] / source3 / nmbd / nmbd_synclists.c
index bf03d4d1cf279e335e53054e77d80fa6771f5233..0e67c3f69c0bf94fbcaa6c780ea13f2009463eb8 100644 (file)
    also allows us to have more than 1 sync going at once (tridge) */
 
 #include "includes.h"
-#include "smb.h"
+
+extern fstring local_machine;
 
 struct sync_record {
        struct sync_record *next, *prev;
-       fstring workgroup;
-       fstring server;
+       unstring workgroup;
+       unstring server;
        pstring fname;
        struct in_addr ip;
        pid_t pid;
@@ -48,6 +49,7 @@ static XFILE *fp;
   This is the NetServerEnum callback.
   Note sname and comment are in UNIX codepage format.
   ******************************************************************/
+
 static void callback(const char *sname, uint32 stype, 
                      const char *comment, void *state)
 {
@@ -59,70 +61,78 @@ static void callback(const char *sname, uint32 stype,
   Log in on the remote server's SMB port to their IPC$ service,
   do a NetServerEnum and record the results in fname
 ******************************************************************/
+
 static void sync_child(char *name, int nm_type, 
                       char *workgroup,
                       struct in_addr ip, BOOL local, BOOL servers,
                       char *fname)
 {
-       extern fstring local_machine;
        fstring unix_workgroup;
-       static struct cli_state cli;
+       struct cli_state *cli;
        uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
        struct nmb_name called, calling;
 
-       if (!cli_initialise(&cli) || !cli_connect(&cli, name, &ip)) {
+       /* 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;
+       }
+
+       if (!cli_set_port(cli, 139) || !cli_connect(cli, name, &ip)) {
                return;
        }
 
        make_nmb_name(&calling, local_machine, 0x0);
-       make_nmb_name(&called , name         , nm_type);
+       make_nmb_name(&called , name, nm_type);
 
-       if (!cli_session_request(&cli, &calling, &called))
-       {
-               cli_shutdown(&cli);
+       if (!cli_session_request(cli, &calling, &called)) {
+               cli_shutdown(cli);
                return;
        }
 
-       if (!cli_negprot(&cli)) {
-               cli_shutdown(&cli);
+       if (!cli_negprot(cli)) {
+               cli_shutdown(cli);
                return;
        }
 
-       if (!cli_session_setup(&cli, "", "", 1, "", 0, workgroup)) {
-               cli_shutdown(&cli);
+       if (!cli_session_setup(cli, "", "", 1, "", 0, workgroup)) {
+               cli_shutdown(cli);
                return;
        }
 
-       if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
-               cli_shutdown(&cli);
+       if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
+               cli_shutdown(cli);
                return;
        }
 
        /* All the cli_XX functions take UNIX character set. */
-       fstrcpy(unix_workgroup, cli.server_domain?cli.server_domain:workgroup);
+       fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
 
        /* Fetch a workgroup list. */
-       cli_NetServerEnum(&cli, unix_workgroup,
+       cli_NetServerEnum(cli, unix_workgroup,
                          local_type|SV_TYPE_DOMAIN_ENUM, 
                          callback, NULL);
        
        /* Now fetch a server list. */
        if (servers) {
                fstrcpy(unix_workgroup, workgroup);
-               cli_NetServerEnum(&cli, unix_workgroup, 
+               cli_NetServerEnum(cli, unix_workgroup, 
                                  local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
                                  callback, NULL);
        }
        
-       cli_shutdown(&cli);
+       cli_shutdown(cli);
 }
 
-
 /*******************************************************************
   initialise a browse sync with another browse server.  Log in on the
   remote server's SMB port to their IPC$ service, do a NetServerEnum
   and record the results
 ******************************************************************/
+
 void sync_browse_lists(struct work_record *work,
                       char *name, int nm_type, 
                       struct in_addr ip, BOOL local, BOOL servers)
@@ -139,13 +149,13 @@ done:
                return;
        }
 
-       s = (struct sync_record *)malloc(sizeof(*s));
+       s = SMB_MALLOC_P(struct sync_record);
        if (!s) goto done;
 
        ZERO_STRUCTP(s);
        
-       fstrcpy(s->workgroup, work->work_group);
-       fstrcpy(s->server, name);
+       unstrcpy(s->workgroup, work->work_group);
+       unstrcpy(s->server, name);
        s->ip = ip;
 
        slprintf(s->fname, sizeof(pstring)-1,
@@ -179,8 +189,9 @@ done:
 }
 
 /**********************************************************************
-handle one line from a completed sync file
+ Handle one line from a completed sync file.
  **********************************************************************/
+
 static void complete_one(struct sync_record *s, 
                         char *sname, uint32 stype, char *comment)
 {
@@ -201,8 +212,7 @@ static void complete_one(struct sync_record *s,
                                                          sname, lp_max_ttl());
                        if (work) {
                                /* remember who the master is */
-                               fstrcpy(work->local_master_browser_name, 
-                                       comment);
+                               unstrcpy(work->local_master_browser_name, comment);
                        }
                }
                return;
@@ -232,27 +242,29 @@ static void complete_one(struct sync_record *s,
        create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
 }
                
-
 /**********************************************************************
-read the completed sync info
- **********************************************************************/
+ Read the completed sync info.
+**********************************************************************/
+
 static void complete_sync(struct sync_record *s)
 {
        XFILE *f;
-       fstring server, type_str;
+       unstring server, type_str;
        unsigned type;
        pstring comment;
        pstring line;
-       char *ptr;
+       const char *ptr;
        int count=0;
 
        f = x_fopen(s->fname,O_RDONLY, 0);
 
-       if (!f) return;
+       if (!f)
+               return;
        
        while (!x_feof(f)) {
                
-               if (!fgets_slash(line,sizeof(pstring),f)) continue;
+               if (!fgets_slash(line,sizeof(pstring),f))
+                       continue;
                
                ptr = line;
 
@@ -278,15 +290,16 @@ static void complete_sync(struct sync_record *s)
 }
 
 /**********************************************************************
-check for completion of any of the child processes
- **********************************************************************/
+ Check for completion of any of the child processes.
+**********************************************************************/
+
 void sync_check_completion(void)
 {
        struct sync_record *s, *next;
 
        for (s=syncs;s;s=next) {
                next = s->next;
-               if (!process_exists(s->pid)) {
+               if (!process_exists_by_pid(s->pid)) {
                        /* it has completed - grab the info */
                        complete_sync(s);
                        DLIST_REMOVE(syncs, s);