1/* 2 Unix SMB/CIFS implementation. 3 NBT netbios routines and daemon - version 2 4 Copyright (C) Andrew Tridgell 1994-1998 5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998 6 Copyright (C) Jeremy Allison 1994-1998 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 22*/ 23 24#include "includes.h" 25 26extern int ClientNMB; 27 28int updatecount = 0; 29 30/******************************************************************* 31 Remove all the servers in a work group. 32 ******************************************************************/ 33 34void remove_all_servers(struct work_record *work) 35{ 36 struct server_record *servrec; 37 struct server_record *nexts; 38 39 for (servrec = work->serverlist; servrec; servrec = nexts) { 40 DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name)); 41 nexts = servrec->next; 42 43 if (servrec->prev) 44 servrec->prev->next = servrec->next; 45 if (servrec->next) 46 servrec->next->prev = servrec->prev; 47 48 if (work->serverlist == servrec) 49 work->serverlist = servrec->next; 50 51 ZERO_STRUCTP(servrec); 52 SAFE_FREE(servrec); 53 } 54 55 work->subnet->work_changed = True; 56} 57 58/*************************************************************************** 59 Add a server into the a workgroup serverlist. 60 **************************************************************************/ 61 62static void add_server_to_workgroup(struct work_record *work, 63 struct server_record *servrec) 64{ 65 struct server_record *servrec2; 66 67 if (!work->serverlist) { 68 work->serverlist = servrec; 69 servrec->prev = NULL; 70 servrec->next = NULL; 71 return; 72 } 73 74 for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next) 75 ; 76 77 servrec2->next = servrec; 78 servrec->next = NULL; 79 servrec->prev = servrec2; 80 work->subnet->work_changed = True; 81} 82 83/**************************************************************************** 84 Find a server in a server list. 85 **************************************************************************/ 86 87struct server_record *find_server_in_workgroup(struct work_record *work, const char *name) 88{ 89 struct server_record *ret; 90 91 for (ret = work->serverlist; ret; ret = ret->next) { 92 if (strequal(ret->serv.name,name)) 93 return ret; 94 } 95 return NULL; 96} 97 98 99/**************************************************************************** 100 Remove a server entry from this workgroup. 101 ****************************************************************************/ 102 103void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec) 104{ 105 if (servrec->prev) 106 servrec->prev->next = servrec->next; 107 if (servrec->next) 108 servrec->next->prev = servrec->prev; 109 110 if (work->serverlist == servrec) 111 work->serverlist = servrec->next; 112 113 ZERO_STRUCTP(servrec); 114 SAFE_FREE(servrec); 115 work->subnet->work_changed = True; 116} 117 118/**************************************************************************** 119 Create a server entry on this workgroup. 120 ****************************************************************************/ 121 122struct server_record *create_server_on_workgroup(struct work_record *work, 123 const char *name,int servertype, 124 int ttl, const char *comment) 125{ 126 struct server_record *servrec; 127 128 if (name[0] == '*') { 129 DEBUG(7,("create_server_on_workgroup: not adding name starting with '*' (%s)\n", 130 name)); 131 return (NULL); 132 } 133 134 if((servrec = find_server_in_workgroup(work, name)) != NULL) { 135 DEBUG(0,("create_server_on_workgroup: Server %s already exists on \ 136workgroup %s. This is a bug.\n", name, work->work_group)); 137 return NULL; 138 } 139 140 if((servrec = SMB_MALLOC_P(struct server_record)) == NULL) { 141 DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n")); 142 return NULL; 143 } 144 145 memset((char *)servrec,'\0',sizeof(*servrec)); 146 147 servrec->subnet = work->subnet; 148 149 fstrcpy(servrec->serv.name,name); 150 fstrcpy(servrec->serv.comment,comment); 151 strupper_m(servrec->serv.name); 152 servrec->serv.type = servertype; 153 154 update_server_ttl(servrec, ttl); 155 156 add_server_to_workgroup(work, servrec); 157 158 DEBUG(3,("create_server_on_workgroup: Created server entry %s of type %x (%s) on \ 159workgroup %s.\n", name,servertype,comment, work->work_group)); 160 161 work->subnet->work_changed = True; 162 163 return(servrec); 164} 165 166/******************************************************************* 167 Update the ttl field of a server record. 168*******************************************************************/ 169 170void update_server_ttl(struct server_record *servrec, int ttl) 171{ 172 if(ttl > lp_max_ttl()) 173 ttl = lp_max_ttl(); 174 175 if(is_myname(servrec->serv.name)) 176 servrec->death_time = PERMANENT_TTL; 177 else 178 servrec->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL; 179 180 servrec->subnet->work_changed = True; 181} 182 183/******************************************************************* 184 Expire old servers in the serverlist. A time of -1 indicates 185 everybody dies except those with a death_time of PERMANENT_TTL (which is 0). 186 This should only be called from expire_workgroups_and_servers(). 187 ******************************************************************/ 188 189void expire_servers(struct work_record *work, time_t t) 190{ 191 struct server_record *servrec; 192 struct server_record *nexts; 193 194 for (servrec = work->serverlist; servrec; servrec = nexts) { 195 nexts = servrec->next; 196 197 if ((servrec->death_time != PERMANENT_TTL) && ((t == -1) || (servrec->death_time < t))) { 198 DEBUG(3,("expire_old_servers: Removing timed out server %s\n",servrec->serv.name)); 199 remove_server_from_workgroup(work, servrec); 200 work->subnet->work_changed = True; 201 } 202 } 203} 204 205/******************************************************************* 206 Decide if we should write out a server record for this server. 207 We return zero if we should not. Check if we've already written 208 out this server record from an earlier subnet. 209******************************************************************/ 210 211static uint32 write_this_server_name( struct subnet_record *subrec, 212 struct work_record *work, 213 struct server_record *servrec) 214{ 215 struct subnet_record *ssub; 216 struct work_record *iwork; 217 218 /* Go through all the subnets we have already seen. */ 219 for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) { 220 for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next) { 221 if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL) { 222 /* 223 * We have already written out this server record, don't 224 * do it again. This gives precedence to servers we have seen 225 * on the broadcast subnets over servers that may have been 226 * added via a sync on the unicast_subet. 227 * 228 * The correct way to do this is to have a serverlist file 229 * per subnet - this means changes to smbd as well. I may 230 * add this at a later date (JRA). 231 */ 232 233 return 0; 234 } 235 } 236 } 237 238 return servrec->serv.type; 239} 240 241/******************************************************************* 242 Decide if we should write out a workgroup record for this workgroup. 243 We return zero if we should not. Don't write out lp_workgroup() (we've 244 already done it) and also don't write out a second workgroup record 245 on the unicast subnet that we've already written out on one of the 246 broadcast subnets. 247******************************************************************/ 248 249static uint32 write_this_workgroup_name( struct subnet_record *subrec, 250 struct work_record *work) 251{ 252 struct subnet_record *ssub; 253 254 if(strequal(lp_workgroup(), work->work_group)) 255 return 0; 256 257 /* This is a workgroup we have seen on a broadcast subnet. All 258 these have the same type. */ 259 260 if(subrec != unicast_subnet) 261 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY); 262 263 for(ssub = FIRST_SUBNET; ssub; ssub = NEXT_SUBNET_EXCLUDING_UNICAST(ssub)) { 264 /* This is the unicast subnet so check if we've already written out 265 this subnet when we passed over the broadcast subnets. */ 266 267 if(find_workgroup_on_subnet( ssub, work->work_group) != NULL) 268 return 0; 269 } 270 271 /* All workgroups on the unicast subnet (except our own, which we 272 have already written out) cannot be local. */ 273 274 return (SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT); 275} 276 277/******************************************************************* 278 Write out the browse.dat file. 279 ******************************************************************/ 280 281void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type, 282 const char *local_master_browser_name, const char *description) 283{ 284 fstring tmp; 285 286 slprintf(tmp,sizeof(tmp)-1, "\"%s\"", name); 287 x_fprintf(fp, "%-25s ", tmp); 288 x_fprintf(fp, "%08x ", rec_type); 289 slprintf(tmp, sizeof(tmp)-1, "\"%s\" ", local_master_browser_name); 290 x_fprintf(fp, "%-30s", tmp); 291 x_fprintf(fp, "\"%s\"\n", description); 292} 293 294void write_browse_list(time_t t, BOOL force_write) 295{ 296 struct subnet_record *subrec; 297 struct work_record *work; 298 struct server_record *servrec; 299 pstring fname,fnamenew; 300 uint32 stype; 301 int i; 302 XFILE *fp; 303 BOOL list_changed = force_write; 304 static time_t lasttime = 0; 305 306 /* Always dump if we're being told to by a signal. */ 307 if(force_write == False) { 308 if (!lasttime) 309 lasttime = t; 310 if (t - lasttime < 5) 311 return; 312 } 313 314 lasttime = t; 315 316 dump_workgroups(force_write); 317 318 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) { 319 if(subrec->work_changed) { 320 list_changed = True; 321 break; 322 } 323 } 324 325 if(!list_changed) 326 return; 327 328 updatecount++; 329 330 pstrcpy(fname,lp_lockdir()); 331 trim_char(fname,'\0' ,'/'); 332 pstrcat(fname,"/"); 333 pstrcat(fname,SERVER_LIST); 334 pstrcpy(fnamenew,fname); 335 pstrcat(fnamenew,"."); 336 337 fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644); 338 339 if (!fp) { 340 DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n", 341 fnamenew,strerror(errno))); 342 return; 343 } 344 345 /* 346 * Write out a record for our workgroup. Use the record from the first 347 * subnet. 348 */ 349 350 if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) { 351 DEBUG(0,("write_browse_list: Fatal error - cannot find my workgroup %s\n", 352 lp_workgroup())); 353 x_fclose(fp); 354 return; 355 } 356 357 write_browse_list_entry(fp, work->work_group, 358 SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY, 359 work->local_master_browser_name, work->work_group); 360 361 /* 362 * We need to do something special for our own names. 363 * This is due to the fact that we may be a local master browser on 364 * one of our broadcast subnets, and a domain master on the unicast 365 * subnet. We iterate over the subnets and only write out the name 366 * once. 367 */ 368 369 for (i=0; my_netbios_names(i); i++) { 370 stype = 0; 371 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) { 372 if((work = find_workgroup_on_subnet( subrec, lp_workgroup() )) == NULL) 373 continue; 374 if((servrec = find_server_in_workgroup( work, my_netbios_names(i))) == NULL) 375 continue; 376 377 stype |= servrec->serv.type; 378 } 379 380 /* Output server details, plus what workgroup they're in. */ 381 write_browse_list_entry(fp, my_netbios_names(i), stype, 382 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup()); 383 } 384 385 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) { 386 subrec->work_changed = False; 387 388 for (work = subrec->workgrouplist; work ; work = work->next) { 389 /* Write out a workgroup record for a workgroup. */ 390 uint32 wg_type = write_this_workgroup_name( subrec, work); 391 392 if(wg_type) { 393 write_browse_list_entry(fp, work->work_group, wg_type, 394 work->local_master_browser_name, 395 work->work_group); 396 } 397 398 /* Now write out any server records a workgroup may have. */ 399 400 for (servrec = work->serverlist; servrec ; servrec = servrec->next) { 401 uint32 serv_type; 402 403 /* We have already written our names here. */ 404 if(is_myname(servrec->serv.name)) 405 continue; 406 407 serv_type = write_this_server_name(subrec, work, servrec); 408 if(serv_type) { 409 /* Output server details, plus what workgroup they're in. */ 410 write_browse_list_entry(fp, servrec->serv.name, serv_type, 411 servrec->serv.comment, work->work_group); 412 } 413 } 414 } 415 } 416 417 x_fclose(fp); 418 unlink(fname); 419 chmod(fnamenew,0644); 420 rename(fnamenew,fname); 421 DEBUG(3,("write_browse_list: Wrote browse list into file %s\n",fname)); 422} 423