summaryrefslogtreecommitdiff
path: root/source3/nmbd/nmbd_serverlistdb.c
blob: 711ddb2490a10627208c9d1a53fe2d02fd04286d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   NBT netbios routines and daemon - version 2
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
*/

#include "includes.h"
#include "smb.h"

extern int ClientNMB;

extern int DEBUGLEVEL;

extern fstring global_myworkgroup;
extern char **my_netbios_names;

int updatecount = 0;

/*******************************************************************
  Remove all the servers in a work group.
  ******************************************************************/

void remove_all_servers(struct work_record *work)
{
  struct server_record *servrec;
  struct server_record *nexts;

  for (servrec = work->serverlist; servrec; servrec = nexts)
  {
    DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
    nexts = servrec->next;

    if (servrec->prev)
      servrec->prev->next = servrec->next;
    if (servrec->next)
      servrec->next->prev = servrec->prev;

    if (work->serverlist == servrec)
      work->serverlist = servrec->next;

    ZERO_STRUCTP(servrec);
    free((char *)servrec);

  }

  work->subnet->work_changed = True;
}

/***************************************************************************
  Add a server into the a workgroup serverlist.
  **************************************************************************/

static void add_server_to_workgroup(struct work_record *work,
                             struct server_record *servrec)
{
  struct server_record *servrec2;

  if (!work->serverlist)
  {
    work->serverlist = servrec;
    servrec->prev = NULL;
    servrec->next = NULL;
    return;
  }

  for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
    ;

  servrec2->next = servrec;
  servrec->next = NULL;
  servrec->prev = servrec2;
  work->subnet->work_changed = True;
}

/****************************************************************************
  Find a server in a server list.
  **************************************************************************/

struct server_record *find_server_in_workgroup(struct work_record *work, char *name)
{
  struct server_record *ret;
  
  for (ret = work->serverlist; ret; ret = ret->next)
  {
    if (strequal(ret->serv.name,name))
      return ret;
  }
  return NULL;
}


/****************************************************************************
  Remove a server entry from this workgroup.
  ****************************************************************************/

void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
{
  if (servrec->prev)
    servrec->prev->next = servrec->next;
  if (servrec->next)
    servrec->next->prev = servrec->prev;

  if (work->serverlist == servrec) 
    work->serverlist = servrec->next; 

  ZERO_STRUCTP(servrec);
  free((char *)servrec);
  work->subnet->work_changed = True;
}

/****************************************************************************
  Create a server entry on this workgroup.
  ****************************************************************************/

struct server_record *create_server_on_workgroup(struct work_record *work,
                                                 char *name,int servertype, 
                                                 int ttl,char *comment)
{
  struct server_record *servrec;
  
  if (name[0] == '*')
  {
      DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n",
             name));
      return (NULL);
  }
  
  if((servrec = find_server_in_workgroup(work, name)) != NULL)
  {
    DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
workgroup %s. This is a bug.\n", name, work->work_group));
    return NULL;
  }
  
  if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL)
  {
    DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
    return NULL;
  }

  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);
  strupper(servrec->serv.name);
  servrec->serv.type  = servertype;

  update_server_ttl(servrec, ttl);
  
  add_server_to_workgroup(work, servrec);
      
  DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \
workgroup %s.\n", name,servertype,comment, work->work_group));
 
  work->subnet->work_changed = True;
 
  return(servrec);
}

/*******************************************************************
 Update the ttl field of a server record.
*******************************************************************/

void update_server_ttl(struct server_record *servrec, int ttl)
{
  if(ttl > lp_max_ttl())
    ttl = lp_max_ttl();

  if(is_myname(servrec->serv.name))
    servrec->death_time = PERMANENT_TTL;
  else
    servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;

  servrec->subnet->work_changed = True;
}

/*******************************************************************
  Expire old servers in the serverlist. A time of -1 indicates 
  everybody dies except those with a death_time of PERMANENT_TTL (which is 0).
  This should only be called from expire_workgroups_and_servers().
  ******************************************************************/

void expire_servers(struct work_record *work, time_t t)
{
  struct server_record *servrec;
  struct server_record *nexts;
  
  for (servrec = work->serverlist; servrec; servrec = nexts)
  {
    nexts = servrec->next;

    if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t)))
    {
      DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name));
      remove_server_from_workgroup(work, servrec);
      work->subnet->work_changed = True;
    }
  }
}

/*******************************************************************
 Decide if we should write out a server record for this server.
 We return zero if we should not. Check if we've already written
 out this server record from an earlier subnet.
******************************************************************/

static uint32 write_this_server_name( struct subnet_record *subrec,
                                      struct work_record *work,
                                      struct server_record *servrec)
{
  struct subnet_record *ssub;
  struct work_record *iwork;

  /* 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(find_server_in_workgroup( iwork, servrec->serv.name) != NULL)
      {
        /*
         * We have already written out this server record, don't
         * do it again. This gives precedence to servers we have seen
         * on the broadcast subnets over servers that may have been
         * added via a sync on the unicast_subet.
         *
         * The correct way to do this is to have a serverlist file
         * per subnet - this means changes to smbd as well. I may
         * add this at a later date (JRA).
         */

        return 0;
      }
    }
  }

  return servrec->serv.type;
}

/*******************************************************************
 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
 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.
******************************************************************/

static uint32 write_this_workgroup_name( struct subnet_record *subrec, 
                                         struct work_record *work)
{
  struct subnet_record *ssub;

  if(strequal(global_myworkgroup, work->work_group))
    return 0;

  /* This is a workgroup we have seen on a broadcast subnet. All
     these have the same type. */

  if(subrec != unicast_subnet)
    return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY);

  for(ssub = FIRST_SUBNET; ssub;  ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub))
  {
    /* This is the unicast subnet so check if we've already written out
       this subnet when we passed over the broadcast subnets. */

    if(find_workgroup_on_subnet( ssub, work->work_group) != NULL)
      return 0;
  }

  /* All workgroups on the unicast subnet (except our own, which we
     have already written out) cannot be local. */

  return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT);
}

/*******************************************************************
  Write out the browse.dat file.
  ******************************************************************/

void write_browse_list(time_t t, BOOL force_write)
{   
  struct subnet_record *subrec;
  struct work_record *work;
  struct server_record *servrec;
  pstring fname,fnamenew;
  uint32 stype;
  fstring tmp;
  int i;
  XFILE *fp;
  BOOL list_changed = force_write;
  static time_t lasttime = 0;
    
  /* Always dump if we're being told to by a signal. */
  if(force_write == False)
  {
    if (!lasttime)
      lasttime = t;
    if (t - lasttime < 5)
      return;
  }

  lasttime = t;

  dump_workgroups(force_write);
 
  for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
  {
    if(subrec->work_changed)
    {
      list_changed = True;
      break;
    }
  }

  if(!list_changed)
    return;

  updatecount++;
    
  pstrcpy(fname,lp_lockdir());
  trim_string(fname,NULL,"/");
  pstrcat(fname,"/");
  pstrcat(fname,SERVER_LIST);
  pstrcpy(fnamenew,fname);
  pstrcat(fnamenew,".");
 
  fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
 
  if (!fp)
  {
    DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
              fnamenew,strerror(errno)));
    return;
  } 
  
  /*
   * Write out a record for our workgroup. Use the record from the first
   * subnet.
   */

  if((work = find_workgroup_on_subnet(FIRST_SUBNET, global_myworkgroup)) == NULL)
  { 
    DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n",
             global_myworkgroup));
    x_fclose(fp);
    return;
  }

  slprintf(tmp,sizeof(tmp)-1, "\"%s\"", work->work_group);
  x_fprintf(fp, "%-25s ", tmp);
  x_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);
  x_fprintf(fp, "%-30s", tmp);
  x_fprintf(fp, "\"%s\"\n", work->work_group);

  /* 
   * We need to do something special for our own names.
   * This is due to the fact that we may be a local master browser on
   * one of our broadcast subnets, and a domain master on the unicast
   * subnet. We iterate over the subnets and only write out the name
   * once.
   */

  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)
        continue;
      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]);
    x_fprintf(fp, "%-25s ", tmp);
    x_fprintf(fp, "%08x ", stype);
    slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", 
	     string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
    x_fprintf(fp, "%-30s", tmp);
    x_fprintf(fp, "\"%s\"\n", global_myworkgroup);
  }
      
  for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) 
  { 
    subrec->work_changed = False;

    for (work = subrec->workgrouplist; work ; work = work->next)
    {
      /* Write out a workgroup record for a workgroup. */
      uint32 wg_type = write_this_workgroup_name( subrec, work);

      if(wg_type)
      {
        slprintf(tmp, sizeof(tmp)-1, "\"%s\"", work->work_group);
        x_fprintf(fp, "%-25s ", tmp);

        x_fprintf(fp, "%08x ", wg_type);
        slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", work->local_master_browser_name);
        x_fprintf(fp, "%-30s", tmp);
        x_fprintf(fp, "\"%s\"\n", work->work_group);
      }

      /* Now write out any server records a workgroup may have. */

      for (servrec = work->serverlist; servrec ; servrec = servrec->next)
      {
        uint32 serv_type;

        /* We have already written our names here. */
        if(is_myname(servrec->serv.name))
          continue; 

        serv_type = write_this_server_name(subrec, work, servrec);

        if(serv_type)
        {
          /* Output server details, plus what workgroup they're in. */
          slprintf(tmp, sizeof(tmp)-1, "\"%s\"", servrec->serv.name);
          x_fprintf(fp, "%-25s ", tmp);
          x_fprintf(fp, "%08x ", serv_type);
          slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", servrec->serv.comment);
          x_fprintf(fp, "%-30s", tmp);
          x_fprintf(fp, "\"%s\"\n", work->work_group);
        }
      }
    }
  } 
  
  x_fclose(fp);
  unlink(fname);
  chmod(fnamenew,0644);
  rename(fnamenew,fname);
  DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname));
}