some cleanups in the clientutil.c code.
[Samba.git] / source / nmbsync.c
blob34d309d00322b508a5750a674c00a4f920881d98
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 DEBUGLEVEL;
29 extern pstring myname;
31 extern int name_type;
32 extern int max_protocol;
33 extern struct in_addr dest_ip;
34 extern int pid;
35 extern int gid;
36 extern int uid;
37 extern int mid;
38 extern BOOL got_pass;
39 extern BOOL have_ip;
40 extern pstring workgroup;
41 extern pstring service;
42 extern pstring desthost;
43 extern BOOL connect_as_ipc;
45 /****************************************************************************
46 fudge for getpass function
47 ****************************************************************************/
48 char *getsmbpass(char *pass)
50 return "dummy"; /* return anything: it should be ignored anyway */
53 /****************************************************************************
54 adds information retrieved from a NetServerEnum call
55 ****************************************************************************/
56 static BOOL add_info(struct subnet_record *d, struct work_record *work, int servertype)
58 char *rparam = NULL;
59 char *rdata = NULL;
60 int rdrcnt,rprcnt;
61 char *p;
62 pstring param;
63 int uLevel = 1;
64 int count = -1;
66 /* now send a SMBtrans command with api ServerEnum? */
67 p = param;
68 SSVAL(p,0,0x68); /* api number */
69 p += 2;
70 strcpy(p,"WrLehDz");
71 p = skip_string(p,1);
73 strcpy(p,"B16BBDz");
75 p = skip_string(p,1);
76 SSVAL(p,0,uLevel);
77 SSVAL(p,2,BUFFER_SIZE - SAFETY_MARGIN); /* buf length */
78 p += 4;
79 SIVAL(p,0,servertype);
80 p += 4;
82 pstrcpy(p, work->work_group);
83 p = skip_string(p,1);
85 if (cli_call_api(PTR_DIFF(p,param),0, 8,BUFFER_SIZE - SAFETY_MARGIN,
86 &rprcnt,&rdrcnt, param,NULL,
87 &rparam,&rdata))
89 int res = SVAL(rparam,0);
90 int converter=SVAL(rparam,2);
91 int i;
93 if (res == 0)
95 count=SVAL(rparam,4);
96 p = rdata;
98 for (i = 0;i < count;i++, p += 26)
100 char *sname = p;
101 uint32 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
102 int comment_offset = IVAL(p,22) & 0xFFFF;
103 char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
105 struct work_record *w = work;
107 DEBUG(4, ("\t%-16.16s %08x %s\n", sname, stype, cmnt));
109 if (stype & SV_TYPE_DOMAIN_ENUM)
111 /* creates workgroup on remote subnet */
112 if ((w = find_workgroupstruct(d,sname,True)))
114 announce_request(w, d->bcast_ip);
118 if (w)
119 add_server_entry(d,w,sname,stype,lp_max_ttl(),cmnt,False);
124 if (rparam) free(rparam);
125 if (rdata) free(rdata);
127 return(True);
131 /*******************************************************************
132 synchronise browse lists with another browse server.
134 log in on the remote server's SMB port to their IPC$ service,
135 do a NetServerEnum and update our server and workgroup databases.
136 ******************************************************************/
137 void sync_browse_lists(struct subnet_record *d, struct work_record *work,
138 char *name, int nm_type, struct in_addr ip, BOOL local)
140 uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
142 if (!d || !work ) return;
144 if(d != wins_subnet) {
145 DEBUG(0,
146 ("sync_browse_lists: ERROR sync requested on non-WINS subnet.\n"));
147 return;
150 pid = getpid();
151 uid = getuid();
152 gid = getgid();
153 mid = pid + 100;
154 name_type = nm_type;
156 got_pass = True;
158 DEBUG(0,("sync_browse_lists: Sync browse lists with %s for %s %s\n",
159 name, work->work_group, inet_ntoa(ip)));
161 strcpy(workgroup,work->work_group);
162 fstrcpy(desthost,name);
163 dest_ip = ip;
165 if (zero_ip(dest_ip)) return;
166 have_ip = True;
168 connect_as_ipc = True;
170 /* connect as server and get domains, then servers */
172 sprintf(service,"\\\\%s\\IPC$", name);
173 strupper(service);
175 if (cli_open_sockets(SMB_PORT))
177 if (cli_send_login(NULL,NULL,True,True))
179 add_info(d, work, local_type|SV_TYPE_DOMAIN_ENUM);
180 if(local)
181 add_info(d, work, SV_TYPE_LOCAL_LIST_ONLY);
182 else
183 add_info(d, work, SV_TYPE_ALL);
186 close_sockets();