This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[kai/samba.git] / source3 / nmbd / nmbd_serverlistdb.c
index a4dab6f419db67e01dd72fdacc00be6df5156f41..e99599e16faa20827d0de5ff4358b21ea62b22e2 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    NBT netbios routines and daemon - version 2
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
 */
 
 #include "includes.h"
-#include "smb.h"
 
 extern int ClientNMB;
 
-extern int DEBUGLEVEL;
-
-extern fstring global_myworkgroup;
-extern char **my_netbios_names;
-
 int updatecount = 0;
 
 /*******************************************************************
@@ -57,7 +50,7 @@ void remove_all_servers(struct work_record *work)
       work->serverlist = servrec->next;
 
     ZERO_STRUCTP(servrec);
-    free((char *)servrec);
+    SAFE_FREE(servrec);
 
   }
 
@@ -94,7 +87,7 @@ static void add_server_to_workgroup(struct work_record *work,
   Find a server in a server list.
   **************************************************************************/
 
-struct server_record *find_server_in_workgroup(struct work_record *work, char *name)
+struct server_record *find_server_in_workgroup(struct work_record *work, const char *name)
 {
   struct server_record *ret;
   
@@ -122,7 +115,7 @@ void remove_server_from_workgroup(struct work_record *work, struct server_record
     work->serverlist = servrec->next; 
 
   ZERO_STRUCTP(servrec);
-  free((char *)servrec);
+  SAFE_FREE(servrec);
   work->subnet->work_changed = True;
 }
 
@@ -131,8 +124,8 @@ void remove_server_from_workgroup(struct work_record *work, struct server_record
   ****************************************************************************/
 
 struct server_record *create_server_on_workgroup(struct work_record *work,
-                                                 char *name,int servertype, 
-                                                 int ttl,char *comment)
+                                                 const char *name,int servertype, 
+                                                 int ttl, const char *comment)
 {
   struct server_record *servrec;
   
@@ -156,12 +149,12 @@ workgroup %s. This is a bug.\n", name, work->work_group));
     return NULL;
   }
 
-  bzero((char *)servrec,sizeof(*servrec));
+  memset((char *)servrec,'\0',sizeof(*servrec));
  
   servrec->subnet = work->subnet;
  
-  StrnCpy(servrec->serv.name,name,sizeof(servrec->serv.name)-1);
-  StrnCpy(servrec->serv.comment,comment,sizeof(servrec->serv.comment)-1);
+  fstrcpy(servrec->serv.name,name);
+  fstrcpy(servrec->serv.comment,comment);
   strupper(servrec->serv.name);
   servrec->serv.type  = servertype;
 
@@ -230,14 +223,13 @@ static uint32 write_this_server_name( struct subnet_record *subrec,
 {
   struct subnet_record *ssub;
   struct work_record *iwork;
-  struct server_record *sserv;
 
   /* Go through all the subnets we have already seen. */
   for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) 
   {
     for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next)
     {
-      if((sserv = find_server_in_workgroup( iwork, servrec->serv.name)) != NULL)
+      if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
       {
         /*
          * We have already written out this server record, don't
@@ -260,7 +252,7 @@ static uint32 write_this_server_name( struct subnet_record *subrec,
 
 /*******************************************************************
  Decide if we should write out a workgroup record for this workgroup.
- We return zero if we should not. Don't write out global_myworkgroup (we've
+ We return zero if we should not. Don't write out lp_workgroup() (we've
  already done it) and also don't write out a second workgroup record
  on the unicast subnet that we've already written out on one of the
  broadcast subnets.
@@ -271,7 +263,7 @@ static uint32 write_this_workgroup_name( struct subnet_record *subrec,
 {
   struct subnet_record *ssub;
 
-  if(strequal(global_myworkgroup, work->work_group))
+  if(strequal(lp_workgroup(), work->work_group))
     return 0;
 
   /* This is a workgroup we have seen on a broadcast subnet. All
@@ -299,6 +291,19 @@ static uint32 write_this_workgroup_name( struct subnet_record *subrec,
   Write out the browse.dat file.
   ******************************************************************/
 
+void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type,
+               const char *local_master_browser_name, const char *description)
+{
+       fstring tmp;
+
+       slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name);
+       x_fprintf(fp, "%-25s ", tmp);
+       x_fprintf(fp, "%08x ", rec_type);
+       slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name);
+       x_fprintf(fp, "%-30s", tmp);
+       x_fprintf(fp, "\"%s\"\n", description);
+}
+
 void write_browse_list(time_t t, BOOL force_write)
 {   
   struct subnet_record *subrec;
@@ -306,9 +311,8 @@ void write_browse_list(time_t t, BOOL force_write)
   struct server_record *servrec;
   pstring fname,fnamenew;
   uint32 stype;
-  fstring tmp;
   int i;
-  FILE *fp;
+  XFILE *fp;
   BOOL list_changed = force_write;
   static time_t lasttime = 0;
     
@@ -321,6 +325,8 @@ void write_browse_list(time_t t, BOOL force_write)
       return;
   }
 
+  lasttime = t;
+
   dump_workgroups(force_write);
  
   for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
@@ -335,7 +341,6 @@ void write_browse_list(time_t t, BOOL force_write)
   if(!list_changed)
     return;
 
-  lasttime = t;
   updatecount++;
     
   pstrcpy(fname,lp_lockdir());
@@ -345,7 +350,7 @@ void write_browse_list(time_t t, BOOL force_write)
   pstrcpy(fnamenew,fname);
   pstrcat(fnamenew,".");
  
-  fp = fopen(fnamenew,"w");
+  fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
  
   if (!fp)
   {
@@ -359,20 +364,17 @@ void write_browse_list(time_t t, BOOL force_write)
    * subnet.
    */
 
-  if((work = find_workgroup_on_subnet(FIRST_SUBNET, global_myworkgroup)) == NULL)
+  if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL)
   { 
     DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
-             global_myworkgroup));
-    fclose(fp);
+             lp_workgroup()));
+    x_fclose(fp);
     return;
   }
 
-  slprintf(tmp,sizeof(tmp)-1, "\"%s\"", work->work_group);
-  fprintf(fp, "%-25s ", tmp);
-  fprintf(fp, "%08x ", SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);
-  slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
-  fprintf(fp, "%-30s", tmp);
-  fprintf(fp, "\"%s\"\n", work->work_group);
+  write_browse_list_entry(fp, work->work_group,
+     SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
+     work->local_master_browser_name, work->work_group);
 
   /* 
    * We need to do something special for our own names.
@@ -382,26 +384,22 @@ void write_browse_list(time_t t, BOOL force_write)
    * once.
    */
 
-  for (i=0; my_netbios_names[i]; i++)
+  for (i=0; my_netbios_names(i); i++)
   {
     stype = 0;
     for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
     {
-      if((work = find_workgroup_on_subnet( subrec, global_myworkgroup )) == NULL)
+      if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL)
         continue;
-      if((servrec = find_server_in_workgroup( work, my_netbios_names[i])) == NULL)
+      if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL)
         continue;
 
       stype |= servrec->serv.type;
     }
 
     /* Output server details, plus what workgroup they're in. */
-    slprintf(tmp, sizeof(tmp)-1, "\"%s\"", my_netbios_names[i]);
-    fprintf(fp, "%-25s ", tmp);
-    fprintf(fp, "%08x ", stype);
-    slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", lp_serverstring());
-    fprintf(fp, "%-30s", tmp);
-    fprintf(fp, "\"%s\"\n", global_myworkgroup);
+    write_browse_list_entry(fp, my_netbios_names(i), stype,
+        string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup());
   }
       
   for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) 
@@ -415,13 +413,9 @@ void write_browse_list(time_t t, BOOL force_write)
 
       if(wg_type)
       {
-        slprintf(tmp, sizeof(tmp)-1, "\"%s\"", work->work_group);
-        fprintf(fp, "%-25s ", tmp);
-
-        fprintf(fp, "%08x ", wg_type);
-        slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
-        fprintf(fp, "%-30s", tmp);
-        fprintf(fp, "\"%s\"\n", work->work_group);
+        write_browse_list_entry(fp, work->work_group, wg_type,
+                                work->local_master_browser_name,
+                                work->work_group);
       }
 
       /* Now write out any server records a workgroup may have. */
@@ -439,18 +433,14 @@ void write_browse_list(time_t t, BOOL force_write)
         if(serv_type)
         {
           /* Output server details, plus what workgroup they're in. */
-          slprintf(tmp, sizeof(tmp)-1, "\"%s\"", servrec->serv.name);
-          fprintf(fp, "%-25s ", tmp);
-          fprintf(fp, "%08x ", serv_type);
-          slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", servrec->serv.comment);
-          fprintf(fp, "%-30s", tmp);
-          fprintf(fp, "\"%s\"\n", work->work_group);
+          write_browse_list_entry(fp, servrec->serv.name, serv_type,
+                                 servrec->serv.comment, work->work_group);
         }
       }
     }
   } 
   
-  fclose(fp);
+  x_fclose(fp);
   unlink(fname);
   chmod(fnamenew,0644);
   rename(fnamenew,fname);