00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "includes.h"
00025
00026 extern uint16 samba_nb_type;
00027
00028 int workgroup_count = 0;
00029
00030
00031
00032
00033
00034 static void add_workgroup(struct subnet_record *subrec, struct work_record *work)
00035 {
00036 work->subnet = subrec;
00037 DLIST_ADD(subrec->workgrouplist, work);
00038 subrec->work_changed = True;
00039 }
00040
00041
00042
00043
00044
00045 static void name_to_unstring(unstring unname, const char *name)
00046 {
00047 nstring nname;
00048
00049 errno = 0;
00050 push_ascii_nstring(nname, name);
00051 if (errno == E2BIG) {
00052 unstring tname;
00053 pull_ascii_nstring(tname, sizeof(tname), nname);
00054 unstrcpy(unname, tname);
00055 DEBUG(0,("name_to_nstring: workgroup name %s is too long. Truncating to %s\n",
00056 name, tname));
00057 } else {
00058 unstrcpy(unname, name);
00059 }
00060 }
00061
00062
00063
00064
00065
00066 static struct work_record *create_workgroup(const char *name, int ttl)
00067 {
00068 struct work_record *work;
00069 struct subnet_record *subrec;
00070 int t = -1;
00071
00072 if((work = SMB_MALLOC_P(struct work_record)) == NULL) {
00073 DEBUG(0,("create_workgroup: malloc fail !\n"));
00074 return NULL;
00075 }
00076 memset((char *)work, '\0', sizeof(*work));
00077
00078 name_to_unstring(work->work_group, name);
00079
00080 work->serverlist = NULL;
00081
00082 work->RunningElection = False;
00083 work->ElectionCount = 0;
00084 work->announce_interval = 0;
00085 work->needelection = False;
00086 work->needannounce = True;
00087 work->lastannounce_time = time(NULL);
00088 work->mst_state = lp_local_master() ? MST_POTENTIAL : MST_NONE;
00089 work->dom_state = DOMAIN_NONE;
00090 work->log_state = LOGON_NONE;
00091
00092 work->death_time = (ttl != PERMANENT_TTL) ? time(NULL)+(ttl*3) : PERMANENT_TTL;
00093
00094
00095
00096 for (subrec = FIRST_SUBNET; subrec && (t == -1); subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
00097 struct work_record *w;
00098 for (w = subrec->workgrouplist; w && t == -1; w = w->next) {
00099 if (strequal(w->work_group, work->work_group))
00100 t = w->token;
00101 }
00102 }
00103
00104 if (t == -1)
00105 work->token = ++workgroup_count;
00106 else
00107 work->token = t;
00108
00109
00110 *work->local_master_browser_name = '\0';
00111
00112
00113 *work->dmb_name.name = '\0';
00114 zero_ip(&work->dmb_addr);
00115
00116
00117
00118
00119 work->ElectionCriterion = (MAINTAIN_LIST)|(BROWSER_ELECTION_VERSION<<8);
00120 work->ElectionCriterion |= (lp_os_level() << 24);
00121 if (lp_domain_master())
00122 work->ElectionCriterion |= 0x80;
00123
00124 return work;
00125 }
00126
00127
00128
00129
00130
00131 static struct work_record *remove_workgroup_from_subnet(struct subnet_record *subrec,
00132 struct work_record *work)
00133 {
00134 struct work_record *ret_work = NULL;
00135
00136 DEBUG(3,("remove_workgroup: Removing workgroup %s\n", work->work_group));
00137
00138 ret_work = work->next;
00139
00140 remove_all_servers(work);
00141
00142 if (!work->serverlist) {
00143 if (work->prev)
00144 work->prev->next = work->next;
00145 if (work->next)
00146 work->next->prev = work->prev;
00147
00148 if (subrec->workgrouplist == work)
00149 subrec->workgrouplist = work->next;
00150
00151 ZERO_STRUCTP(work);
00152 SAFE_FREE(work);
00153 }
00154
00155 subrec->work_changed = True;
00156
00157 return ret_work;
00158 }
00159
00160
00161
00162
00163
00164 struct work_record *find_workgroup_on_subnet(struct subnet_record *subrec,
00165 const char *name)
00166 {
00167 struct work_record *ret;
00168 unstring un_name;
00169
00170 DEBUG(4, ("find_workgroup_on_subnet: workgroup search for %s on subnet %s: ",
00171 name, subrec->subnet_name));
00172
00173 name_to_unstring(un_name, name);
00174
00175 for (ret = subrec->workgrouplist; ret; ret = ret->next) {
00176 if (strequal(ret->work_group,un_name)) {
00177 DEBUGADD(4, ("found.\n"));
00178 return(ret);
00179 }
00180 }
00181 DEBUGADD(4, ("not found.\n"));
00182 return NULL;
00183 }
00184
00185
00186
00187
00188
00189 struct work_record *create_workgroup_on_subnet(struct subnet_record *subrec,
00190 const char *name, int ttl)
00191 {
00192 struct work_record *work = NULL;
00193
00194 DEBUG(4,("create_workgroup_on_subnet: creating group %s on subnet %s\n",
00195 name, subrec->subnet_name));
00196
00197 if ((work = create_workgroup(name, ttl))) {
00198 add_workgroup(subrec, work);
00199 subrec->work_changed = True;
00200 return(work);
00201 }
00202
00203 return NULL;
00204 }
00205
00206
00207
00208
00209
00210 void update_workgroup_ttl(struct work_record *work, int ttl)
00211 {
00212 if(work->death_time != PERMANENT_TTL)
00213 work->death_time = time(NULL)+(ttl*3);
00214 work->subnet->work_changed = True;
00215 }
00216
00217
00218
00219
00220
00221
00222 static void fail_register(struct subnet_record *subrec, struct response_record *rrec,
00223 struct nmb_name *nmbname)
00224 {
00225 DEBUG(0,("fail_register: Failed to register name %s on subnet %s.\n",
00226 nmb_namestr(nmbname), subrec->subnet_name));
00227 }
00228
00229
00230
00231
00232
00233 void initiate_myworkgroup_startup(struct subnet_record *subrec, struct work_record *work)
00234 {
00235 int i;
00236
00237 if(!strequal(lp_workgroup(), work->work_group))
00238 return;
00239
00240
00241
00242 if ((subrec != unicast_subnet) && (subrec != remote_broadcast_subnet) &&
00243 (subrec != wins_server_subnet) && lp_preferred_master() && lp_local_master()) {
00244 DEBUG(3, ("initiate_myworkgroup_startup: preferred master startup for \
00245 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
00246 work->needelection = True;
00247 work->ElectionCriterion |= (1<<3);
00248 }
00249
00250
00251
00252 register_name(subrec,lp_workgroup(),0x0,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
00253 register_name(subrec,lp_workgroup(),0x1e,samba_nb_type|NB_GROUP, NULL, fail_register,NULL);
00254
00255 for( i = 0; my_netbios_names(i); i++) {
00256 const char *name = my_netbios_names(i);
00257 int stype = lp_default_server_announce() | (lp_local_master() ? SV_TYPE_POTENTIAL_BROWSER : 0 );
00258
00259 if(!strequal(global_myname(), name))
00260 stype &= ~(SV_TYPE_MASTER_BROWSER|SV_TYPE_POTENTIAL_BROWSER|SV_TYPE_DOMAIN_MASTER|SV_TYPE_DOMAIN_MEMBER);
00261
00262 create_server_on_workgroup(work,name,stype|SV_TYPE_LOCAL_LIST_ONLY, PERMANENT_TTL,
00263 string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
00264 DEBUG(3,("initiate_myworkgroup_startup: Added server name entry %s \
00265 on subnet %s\n", name, subrec->subnet_name));
00266 }
00267 }
00268
00269
00270
00271
00272
00273 void dump_workgroups(BOOL force_write)
00274 {
00275 struct subnet_record *subrec;
00276 int debuglevel = force_write ? 0 : 4;
00277
00278 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
00279 if (subrec->workgrouplist) {
00280 struct work_record *work;
00281
00282 if( DEBUGLVL( debuglevel ) ) {
00283 dbgtext( "dump_workgroups()\n " );
00284 dbgtext( "dump workgroup on subnet %15s: ", subrec->subnet_name );
00285 dbgtext( "netmask=%15s:\n", inet_ntoa(subrec->mask_ip) );
00286 }
00287
00288 for (work = subrec->workgrouplist; work; work = work->next) {
00289 DEBUGADD( debuglevel, ( "\t%s(%d) current master browser = %s\n", work->work_group,
00290 work->token, *work->local_master_browser_name ? work->local_master_browser_name : "UNKNOWN" ) );
00291 if (work->serverlist) {
00292 struct server_record *servrec;
00293 for (servrec = work->serverlist; servrec; servrec = servrec->next) {
00294 DEBUGADD( debuglevel, ( "\t\t%s %8x (%s)\n",
00295 servrec->serv.name,
00296 servrec->serv.type,
00297 servrec->serv.comment ) );
00298 }
00299 }
00300 }
00301 }
00302 }
00303 }
00304
00305
00306
00307
00308
00309
00310 void expire_workgroups_and_servers(time_t t)
00311 {
00312 struct subnet_record *subrec;
00313
00314 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
00315 struct work_record *work;
00316 struct work_record *nextwork;
00317
00318 for (work = subrec->workgrouplist; work; work = nextwork) {
00319 nextwork = work->next;
00320 expire_servers(work, t);
00321
00322 if ((work->serverlist == NULL) && (work->death_time != PERMANENT_TTL) &&
00323 ((t == (time_t)-1) || (work->death_time < t))) {
00324 DEBUG(3,("expire_workgroups_and_servers: Removing timed out workgroup %s\n",
00325 work->work_group));
00326 remove_workgroup_from_subnet(subrec, work);
00327 }
00328 }
00329 }
00330 }