00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "includes.h"
00031
00032 extern fstring local_machine;
00033
00034 struct sync_record {
00035 struct sync_record *next, *prev;
00036 unstring workgroup;
00037 unstring server;
00038 pstring fname;
00039 struct in_addr ip;
00040 pid_t pid;
00041 };
00042
00043
00044 static struct sync_record *syncs;
00045
00046 static XFILE *fp;
00047
00048
00049
00050
00051
00052
00053 static void callback(const char *sname, uint32 stype,
00054 const char *comment, void *state)
00055 {
00056 x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 static void sync_child(char *name, int nm_type,
00066 char *workgroup,
00067 struct in_addr ip, BOOL local, BOOL servers,
00068 char *fname)
00069 {
00070 fstring unix_workgroup;
00071 struct cli_state *cli;
00072 uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
00073 struct nmb_name called, calling;
00074 NTSTATUS status;
00075
00076
00077
00078
00079
00080 cli = cli_initialise();
00081 if (!cli) {
00082 return;
00083 }
00084
00085 if (!cli_set_port(cli, 139)) {
00086 return;
00087 }
00088
00089 status = cli_connect(cli, name, &ip);
00090 if (!NT_STATUS_IS_OK(status)) {
00091 return;
00092 }
00093
00094 make_nmb_name(&calling, local_machine, 0x0);
00095 make_nmb_name(&called , name, nm_type);
00096
00097 if (!cli_session_request(cli, &calling, &called)) {
00098 cli_shutdown(cli);
00099 return;
00100 }
00101
00102 if (!cli_negprot(cli)) {
00103 cli_shutdown(cli);
00104 return;
00105 }
00106
00107 if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
00108 workgroup))) {
00109 cli_shutdown(cli);
00110 return;
00111 }
00112
00113 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
00114 cli_shutdown(cli);
00115 return;
00116 }
00117
00118
00119 fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
00120
00121
00122 cli_NetServerEnum(cli, unix_workgroup,
00123 local_type|SV_TYPE_DOMAIN_ENUM,
00124 callback, NULL);
00125
00126
00127 if (servers) {
00128 fstrcpy(unix_workgroup, workgroup);
00129 cli_NetServerEnum(cli, unix_workgroup,
00130 local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
00131 callback, NULL);
00132 }
00133
00134 cli_shutdown(cli);
00135 }
00136
00137
00138
00139
00140
00141
00142
00143 void sync_browse_lists(struct work_record *work,
00144 char *name, int nm_type,
00145 struct in_addr ip, BOOL local, BOOL servers)
00146 {
00147 struct sync_record *s;
00148 static int counter;
00149
00150 START_PROFILE(sync_browse_lists);
00151
00152
00153 if (ismyip(ip)) {
00154 done:
00155 END_PROFILE(sync_browse_lists);
00156 return;
00157 }
00158
00159 s = SMB_MALLOC_P(struct sync_record);
00160 if (!s) goto done;
00161
00162 ZERO_STRUCTP(s);
00163
00164 unstrcpy(s->workgroup, work->work_group);
00165 unstrcpy(s->server, name);
00166 s->ip = ip;
00167
00168 slprintf(s->fname, sizeof(pstring)-1,
00169 "%s/sync.%d", lp_lockdir(), counter++);
00170 all_string_sub(s->fname,"//", "/", 0);
00171
00172 DLIST_ADD(syncs, s);
00173
00174
00175
00176 CatchChild();
00177 if ((s->pid = sys_fork())) return;
00178
00179 BlockSignals( False, SIGTERM );
00180
00181 DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
00182 work->work_group, name, inet_ntoa(ip)));
00183
00184 fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
00185 if (!fp) {
00186 END_PROFILE(sync_browse_lists);
00187 _exit(1);
00188 }
00189
00190 sync_child(name, nm_type, work->work_group, ip, local, servers,
00191 s->fname);
00192
00193 x_fclose(fp);
00194 END_PROFILE(sync_browse_lists);
00195 _exit(0);
00196 }
00197
00198
00199
00200
00201
00202 static void complete_one(struct sync_record *s,
00203 char *sname, uint32 stype, char *comment)
00204 {
00205 struct work_record *work;
00206 struct server_record *servrec;
00207
00208 stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
00209
00210 if (stype & SV_TYPE_DOMAIN_ENUM) {
00211
00212 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
00213
00214
00215 update_workgroup_ttl(work,lp_max_ttl());
00216 } else {
00217
00218 work = create_workgroup_on_subnet(unicast_subnet,
00219 sname, lp_max_ttl());
00220 if (work) {
00221
00222 unstrcpy(work->local_master_browser_name, comment);
00223 }
00224 }
00225 return;
00226 }
00227
00228 work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
00229 if (!work) {
00230 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
00231 s->workgroup));
00232 return;
00233 }
00234
00235 if ((servrec = find_server_in_workgroup( work, sname))) {
00236
00237
00238 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
00239
00240
00241 update_server_ttl(servrec, lp_max_ttl());
00242
00243 servrec->serv.type = stype;
00244 }
00245 return;
00246 }
00247
00248
00249 create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
00250 }
00251
00252
00253
00254
00255
00256 static void complete_sync(struct sync_record *s)
00257 {
00258 XFILE *f;
00259 unstring server, type_str;
00260 unsigned type;
00261 pstring comment;
00262 pstring line;
00263 const char *ptr;
00264 int count=0;
00265
00266 f = x_fopen(s->fname,O_RDONLY, 0);
00267
00268 if (!f)
00269 return;
00270
00271 while (!x_feof(f)) {
00272
00273 if (!fgets_slash(line,sizeof(pstring),f))
00274 continue;
00275
00276 ptr = line;
00277
00278 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
00279 !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
00280 !next_token(&ptr,comment,NULL, sizeof(comment))) {
00281 continue;
00282 }
00283
00284 sscanf(type_str, "%X", &type);
00285
00286 complete_one(s, server, type, comment);
00287
00288 count++;
00289 }
00290
00291 x_fclose(f);
00292
00293 unlink(s->fname);
00294
00295 DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
00296 s->server, inet_ntoa(s->ip), s->workgroup, count));
00297 }
00298
00299
00300
00301
00302
00303 void sync_check_completion(void)
00304 {
00305 struct sync_record *s, *next;
00306
00307 for (s=syncs;s;s=next) {
00308 next = s->next;
00309 if (!process_exists_by_pid(s->pid)) {
00310
00311 complete_sync(s);
00312 DLIST_REMOVE(syncs, s);
00313 ZERO_STRUCTP(s);
00314 SAFE_FREE(s);
00315 }
00316 }
00317 }