This is the ubiqx binary tree and linked list library.
[Samba.git] / source / nmbsync.c
blobde2f7aa00f1bcaaa6740727464500f97ee27d435
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines to synchronise browse lists
5 Copyright (C) Andrew Tridgell 1994-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* We *must have REPLACE_GETPASS defined here before the includes. */
24 #define REPLACE_GETPASS
25 #include "includes.h"
27 extern int ClientNMB;
28 extern int ClientDGRAM;
30 extern int DEBUGLEVEL;
32 extern pstring myname;
34 extern int name_type;
35 extern int max_protocol;
36 extern struct in_addr dest_ip;
37 extern int pid;
38 extern int gid;
39 extern int uid;
40 extern int mid;
41 extern BOOL got_pass;
42 extern BOOL have_ip;
43 extern pstring workgroup;
44 extern pstring service;
45 extern pstring desthost;
46 extern BOOL connect_as_ipc;
48 /****************************************************************************
49 fudge for getpass function
50 ****************************************************************************/
51 char *getsmbpass(char *pass)
53 return "dummy"; /* return anything: it should be ignored anyway */
56 /****************************************************************************
57 adds information retrieved from a NetServerEnum call
58 ****************************************************************************/
59 static BOOL add_info(struct subnet_record *d, struct work_record *work, int servertype)
61 char *rparam = NULL;
62 char *rdata = NULL;
63 int rdrcnt,rprcnt;
64 char *p;
65 pstring param;
66 int uLevel = 1;
67 int count = -1;
69 /* now send a SMBtrans command with api ServerEnum? */
70 p = param;
71 SSVAL(p,0,0x68); /* api number */
72 p += 2;
73 strcpy(p,"WrLehDz");
74 p = skip_string(p,1);
76 strcpy(p,"B16BBDz");
78 p = skip_string(p,1);
79 SSVAL(p,0,uLevel);
80 SSVAL(p,2,BUFFER_SIZE - SAFETY_MARGIN); /* buf length */
81 p += 4;
82 SIVAL(p,0,servertype);
83 p += 4;
85 pstrcpy(p, work->work_group);
86 p = skip_string(p,1);
88 if (cli_call_api(PTR_DIFF(p,param),0, 8,BUFFER_SIZE - SAFETY_MARGIN,
89 &rprcnt,&rdrcnt, param,NULL,
90 &rparam,&rdata))
92 int res = SVAL(rparam,0);
93 int converter=SVAL(rparam,2);
94 int i;
96 if (res == 0)
98 count=SVAL(rparam,4);
99 p = rdata;
101 for (i = 0;i < count;i++, p += 26)
103 char *sname = p;
104 uint32 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
105 int comment_offset = IVAL(p,22) & 0xFFFF;
106 char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
108 struct work_record *w = work;
110 DEBUG(4, ("\t%-16.16s %08x %s\n", sname, stype, cmnt));
112 if (stype & SV_TYPE_DOMAIN_ENUM)
114 /* creates workgroup on remote subnet */
115 if ((w = find_workgroupstruct(d,sname,True)))
117 announce_request(w, d->bcast_ip);
121 if (w)
122 add_server_entry(d,w,sname,stype,lp_max_ttl(),cmnt,False);
127 if (rparam) free(rparam);
128 if (rdata) free(rdata);
130 return(True);
134 /*******************************************************************
135 synchronise browse lists with another browse server.
137 log in on the remote server's SMB port to their IPC$ service,
138 do a NetServerEnum and update our server and workgroup databases.
139 ******************************************************************/
140 void sync_browse_lists(struct subnet_record *d, struct work_record *work,
141 char *name, int nm_type, struct in_addr ip, BOOL local)
143 uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
145 if (!d || !work ) return;
147 if(d != wins_subnet) {
148 DEBUG(0,
149 ("sync_browse_lists: ERROR sync requested on non-WINS subnet.\n"));
150 return;
153 pid = getpid();
154 uid = getuid();
155 gid = getgid();
156 mid = pid + 100;
157 name_type = nm_type;
159 got_pass = True;
161 DEBUG(0,("sync_browse_lists: Sync browse lists with %s for %s %s\n",
162 name, work->work_group, inet_ntoa(ip)));
164 strcpy(workgroup,work->work_group);
165 fstrcpy(desthost,name);
166 dest_ip = ip;
168 if (zero_ip(dest_ip)) return;
169 have_ip = True;
171 connect_as_ipc = True;
173 /* connect as server and get domains, then servers */
175 sprintf(service,"\\\\%s\\IPC$", name);
176 strupper(service);
178 if (cli_open_sockets(SMB_PORT))
180 if (cli_send_login(NULL,NULL,True,True))
182 add_info(d, work, local_type|SV_TYPE_DOMAIN_ENUM);
183 if(local)
184 add_info(d, work, SV_TYPE_LOCAL_LIST_ONLY);
185 else
186 add_info(d, work, SV_TYPE_ALL);
189 close_sockets();