Check error returns from strupper_m() (in all reasonable places).
[samba.git] / source3 / nmbd / nmbd_sendannounce.c
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    SMB Version handling
9    Copyright (C) John H Terpstra 1995-1998
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23    
24 */
25
26 #include "includes.h"
27 #include "../librpc/gen_ndr/svcctl.h"
28 #include "nmbd/nmbd.h"
29
30 extern int  updatecount;
31 extern bool found_lm_clients;
32
33 /****************************************************************************
34  Send a browser reset packet.
35 **************************************************************************/
36
37 void send_browser_reset(int reset_type, const char *to_name, int to_type, struct in_addr to_ip)
38 {
39         char outbuf[1024];
40         char *p;
41
42         DEBUG(3,("send_browser_reset: sending reset request type %d to %s<%02x> IP %s.\n",
43                 reset_type, to_name, to_type, inet_ntoa(to_ip) ));
44
45         memset(outbuf,'\0',sizeof(outbuf));
46         p = outbuf;
47         SCVAL(p,0,ANN_ResetBrowserState);
48         p++;
49         SCVAL(p,0,reset_type);
50         p++;
51
52         send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
53                 lp_netbios_name(), 0x0, to_name, to_type, to_ip,
54                 FIRST_SUBNET->myip, DGRAM_PORT);
55 }
56
57 /****************************************************************************
58   Broadcast a packet to the local net requesting that all servers in this
59   workgroup announce themselves to us.
60   **************************************************************************/
61
62 void broadcast_announce_request(struct subnet_record *subrec, struct work_record *work)
63 {
64         char outbuf[1024];
65         char *p;
66
67         work->needannounce = True;
68
69         DEBUG(3,("broadcast_announce_request: sending announce request for workgroup %s \
70 to subnet %s\n", work->work_group, subrec->subnet_name));
71
72         memset(outbuf,'\0',sizeof(outbuf));
73         p = outbuf;
74         SCVAL(p,0,ANN_AnnouncementRequest);
75         p++;
76
77         SCVAL(p,0,work->token); /* (local) Unique workgroup token id. */
78         p++;
79         p +=  push_string_check(p+1, lp_netbios_name(), 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
80   
81         send_mailslot(False, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
82                 lp_netbios_name(), 0x0, work->work_group,0x1e, subrec->bcast_ip,
83                 subrec->myip, DGRAM_PORT);
84 }
85
86 /****************************************************************************
87   Broadcast an announcement.
88   **************************************************************************/
89
90 static void send_announcement(struct subnet_record *subrec, int announce_type,
91                               const char *from_name, const char *to_name, int to_type, struct in_addr to_ip,
92                               time_t announce_interval,
93                               const char *server_name, int server_type, const char *server_comment)
94 {
95         char outbuf[1024];
96         unstring upper_server_name;
97         char *p;
98
99         memset(outbuf,'\0',sizeof(outbuf));
100         p = outbuf+1;
101
102         SCVAL(outbuf,0,announce_type);
103
104         /* Announcement parameters. */
105         SCVAL(p,0,updatecount);
106         SIVAL(p,1,announce_interval*1000); /* Milliseconds - despite the spec. */
107
108         strlcpy(upper_server_name, server_name ? server_name : "", sizeof(upper_server_name));
109         if (!strupper_m(upper_server_name)) {
110                 DEBUG(2,("strupper_m %s failed\n", upper_server_name));
111                 return;
112         }
113         push_string_check(p+5, upper_server_name, 16, STR_ASCII|STR_TERMINATE);
114
115         SCVAL(p,21,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
116         SCVAL(p,22,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
117
118         SIVAL(p,23,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
119         /* Browse version: got from NT/AS 4.00  - Value defined in smb.h (JHT). */
120         SSVAL(p,27,BROWSER_ELECTION_VERSION);
121         SSVAL(p,29,BROWSER_CONSTANT); /* Browse signature. */
122
123         p += 31 + push_string_check(p+31, server_comment, sizeof(outbuf) - (p + 31 - outbuf), STR_ASCII|STR_TERMINATE);
124
125         send_mailslot(False,BROWSE_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
126                         from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
127                         DGRAM_PORT);
128 }
129
130 /****************************************************************************
131   Broadcast a LanMan announcement.
132 **************************************************************************/
133
134 static void send_lm_announcement(struct subnet_record *subrec, int announce_type,
135                               char *from_name, char *to_name, int to_type, struct in_addr to_ip,
136                               time_t announce_interval,
137                               char *server_name, int server_type, char *server_comment)
138 {
139         char outbuf[1024];
140         char *p=outbuf;
141
142         memset(outbuf,'\0',sizeof(outbuf));
143
144         SSVAL(p,0,announce_type);
145         SIVAL(p,2,server_type & ~SV_TYPE_LOCAL_LIST_ONLY);
146         SCVAL(p,6,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* Major version. */
147         SCVAL(p,7,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* Minor version. */
148         SSVAL(p,8,announce_interval);            /* In seconds - according to spec. */
149
150         p += 10;
151         p += push_string_check(p, server_name, 15, STR_ASCII|STR_UPPER|STR_TERMINATE);
152         p += push_string_check(p, server_comment, sizeof(outbuf)- (p - outbuf), STR_ASCII|STR_UPPER|STR_TERMINATE);
153
154         send_mailslot(False,LANMAN_MAILSLOT, outbuf, PTR_DIFF(p,outbuf),
155                 from_name, 0x0, to_name, to_type, to_ip, subrec->myip,
156                 DGRAM_PORT);
157 }
158
159 /****************************************************************************
160  We are a local master browser. Announce this to WORKGROUP<1e>.
161 ****************************************************************************/
162
163 static void send_local_master_announcement(struct subnet_record *subrec, struct work_record *work,
164                                            struct server_record *servrec)
165 {
166         /* Ensure we don't have the prohibited bit set. */
167         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
168
169         DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
170                 type, lp_netbios_name(), subrec->subnet_name, work->work_group));
171
172         send_announcement(subrec, ANN_LocalMasterAnnouncement,
173                         lp_netbios_name(),                 /* From nbt name. */
174                         work->work_group, 0x1e,          /* To nbt name. */
175                         subrec->bcast_ip,                /* To ip. */
176                         work->announce_interval,         /* Time until next announce. */
177                         lp_netbios_name(),                 /* Name to announce. */
178                         type,                            /* Type field. */
179                         servrec->serv.comment);
180 }
181
182 /****************************************************************************
183  Announce the workgroup WORKGROUP to MSBROWSE<01>.
184 ****************************************************************************/
185
186 static void send_workgroup_announcement(struct subnet_record *subrec, struct work_record *work)
187 {
188         DEBUG(3,("send_workgroup_announcement: on subnet %s for workgroup %s\n",
189                 subrec->subnet_name, work->work_group));
190
191         send_announcement(subrec, ANN_DomainAnnouncement,
192                         lp_netbios_name(),                 /* From nbt name. */
193                         MSBROWSE, 0x1,                   /* To nbt name. */
194                         subrec->bcast_ip,                /* To ip. */
195                         work->announce_interval,         /* Time until next announce. */
196                         work->work_group,                /* Name to announce. */
197                         SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT,  /* workgroup announce flags. */
198                         lp_netbios_name());                /* From name as comment. */
199 }
200
201 /****************************************************************************
202  Announce the given host to WORKGROUP<1d>.
203 ****************************************************************************/
204
205 static void send_host_announcement(struct subnet_record *subrec, struct work_record *work,
206                                    struct server_record *servrec)
207 {
208         /* Ensure we don't have the prohibited bits set. */
209         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
210
211         DEBUG(3,("send_host_announcement: type %x for host %s on subnet %s for workgroup %s\n",
212                 type, servrec->serv.name, subrec->subnet_name, work->work_group));
213
214         send_announcement(subrec, ANN_HostAnnouncement,
215                         servrec->serv.name,              /* From nbt name. */
216                         work->work_group, 0x1d,          /* To nbt name. */
217                         subrec->bcast_ip,                /* To ip. */
218                         work->announce_interval,         /* Time until next announce. */
219                         servrec->serv.name,              /* Name to announce. */
220                         type,                            /* Type field. */
221                         servrec->serv.comment);
222 }
223
224 /****************************************************************************
225  Announce the given LanMan host
226 ****************************************************************************/
227
228 static void send_lm_host_announcement(struct subnet_record *subrec, struct work_record *work,
229                                    struct server_record *servrec, int lm_interval)
230 {
231         /* Ensure we don't have the prohibited bits set. */
232         uint32 type = servrec->serv.type & ~SV_TYPE_LOCAL_LIST_ONLY;
233
234         DEBUG(3,("send_lm_host_announcement: type %x for host %s on subnet %s for workgroup %s, ttl: %d\n",
235                 type, servrec->serv.name, subrec->subnet_name, work->work_group, lm_interval));
236
237         send_lm_announcement(subrec, ANN_HostAnnouncement,
238                         servrec->serv.name,              /* From nbt name. */
239                         work->work_group, 0x00,          /* To nbt name. */
240                         subrec->bcast_ip,                /* To ip. */
241                         lm_interval,                     /* Time until next announce. */
242                         servrec->serv.name,              /* Name to announce (fstring not netbios name struct). */
243                         type,                            /* Type field. */
244                         servrec->serv.comment);
245 }
246
247 /****************************************************************************
248   Announce a server record.
249   ****************************************************************************/
250
251 static void announce_server(struct subnet_record *subrec, struct work_record *work,
252                      struct server_record *servrec)
253 {
254         /* Only do domain announcements if we are a master and it's
255                 our primary name we're being asked to announce. */
256
257         if (AM_LOCAL_MASTER_BROWSER(work) && strequal(lp_netbios_name(),servrec->serv.name)) {
258                 send_local_master_announcement(subrec, work, servrec);
259                 send_workgroup_announcement(subrec, work);
260         } else {
261                 send_host_announcement(subrec, work, servrec);
262         }
263 }
264
265 /****************************************************************************
266   Go through all my registered names on all broadcast subnets and announce
267   them if the timeout requires it.
268   **************************************************************************/
269
270 void announce_my_server_names(time_t t)
271 {
272         struct subnet_record *subrec;
273
274         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
275                 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
276
277                 if(work) {
278                         struct server_record *servrec;
279
280                         if (work->needannounce) {
281                                 /* Drop back to a max 3 minute announce. This is to prevent a
282                                         single lost packet from breaking things for too long. */
283
284                                 work->announce_interval = MIN(work->announce_interval,
285                                                         CHECK_TIME_MIN_HOST_ANNCE*60);
286                                 work->lastannounce_time = t - (work->announce_interval+1);
287                                 work->needannounce = False;
288                         }
289
290                         /* Announce every minute at first then progress to every 12 mins */
291                         if ((t - work->lastannounce_time) < work->announce_interval)
292                                 continue;
293
294                         if (work->announce_interval < (CHECK_TIME_MAX_HOST_ANNCE * 60))
295                                 work->announce_interval += 60;
296
297                         work->lastannounce_time = t;
298
299                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
300                                 if (is_myname(servrec->serv.name))
301                                         announce_server(subrec, work, servrec);
302                         }
303                 } /* if work */
304         } /* for subrec */
305 }
306
307 /****************************************************************************
308   Go through all my registered names on all broadcast subnets and announce
309   them as a LanMan server if the timeout requires it.
310 **************************************************************************/
311
312 void announce_my_lm_server_names(time_t t)
313 {
314         struct subnet_record *subrec;
315         static time_t last_lm_announce_time=0;
316         int announce_interval = lp_lm_interval();
317         int lm_announce = lp_lm_announce();
318
319         if ((announce_interval <= 0) || (lm_announce <= 0)) {
320                 /* user absolutely does not want LM announcements to be sent. */
321                 return;
322         }
323
324         if ((lm_announce >= 2) && (!found_lm_clients)) {
325                 /* has been set to 2 (Auto) but no LM clients detected (yet). */
326                 return;
327         }
328
329         /* Otherwise: must have been set to 1 (Yes), or LM clients *have*
330                 been detected. */
331
332         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
333                 struct work_record *work = find_workgroup_on_subnet(subrec, lp_workgroup());
334
335                 if(work) {
336                         struct server_record *servrec;
337
338                         if (last_lm_announce_time && ((t - last_lm_announce_time) < announce_interval ))
339                                 continue;
340
341                         last_lm_announce_time = t;
342
343                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
344                                 if (is_myname(servrec->serv.name))
345                                         /* skipping equivalent of announce_server() */
346                                         send_lm_host_announcement(subrec, work, servrec, announce_interval);
347                         }
348                 } /* if work */
349         } /* for subrec */
350 }
351
352 /* Announce timer. Moved into global static so it can be reset
353    when a machine becomes a local master browser. */
354 static time_t announce_timer_last=0;
355
356 /****************************************************************************
357  Reset the announce_timer so that a local master browser announce will be done
358  immediately.
359  ****************************************************************************/
360
361 void reset_announce_timer(void)
362 {
363         announce_timer_last = time(NULL) - (CHECK_TIME_MST_ANNOUNCE * 60);
364 }
365
366 /****************************************************************************
367   Announce myself as a local master browser to a domain master browser.
368   **************************************************************************/
369
370 void announce_myself_to_domain_master_browser(time_t t)
371 {
372         struct subnet_record *subrec;
373         struct work_record *work;
374
375         if(!we_are_a_wins_client()) {
376                 DEBUG(10,("announce_myself_to_domain_master_browser: no unicast subnet, ignoring.\n"));
377                 return;
378         }
379
380         if (!announce_timer_last)
381                 announce_timer_last = t;
382
383         if ((t-announce_timer_last) < (CHECK_TIME_MST_ANNOUNCE * 60)) {
384                 DEBUG(10,("announce_myself_to_domain_master_browser: t (%d) - last(%d) < %d\n",
385                         (int)t, (int)announce_timer_last, 
386                         CHECK_TIME_MST_ANNOUNCE * 60 ));
387                 return;
388         }
389
390         announce_timer_last = t;
391
392         /* Look over all our broadcast subnets to see if any of them
393                 has the state set as local master browser. */
394
395         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
396                 for (work = subrec->workgrouplist; work; work = work->next) {
397                         if (AM_LOCAL_MASTER_BROWSER(work)) {
398                                 DEBUG(4,( "announce_myself_to_domain_master_browser: I am a local master browser for \
399 workgroup %s on subnet %s\n", work->work_group, subrec->subnet_name));
400
401                                 /* Look in nmbd_browsersync.c for the rest of this code. */
402                                 announce_and_sync_with_domain_master_browser(subrec, work);
403                         }
404                 }
405         }
406 }
407
408 /****************************************************************************
409 Announce all samba's server entries as 'gone'.
410 This must *only* be called on shutdown.
411 ****************************************************************************/
412
413 void announce_my_servers_removed(void)
414 {
415         int announce_interval = lp_lm_interval();
416         int lm_announce = lp_lm_announce();
417         struct subnet_record *subrec; 
418
419         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
420                 struct work_record *work;
421                 for (work = subrec->workgrouplist; work; work = work->next) {
422                         struct server_record *servrec;
423
424                         work->announce_interval = 0;
425                         for (servrec = work->serverlist; servrec; servrec = servrec->next) {
426                                 if (!is_myname(servrec->serv.name))
427                                         continue;
428                                 servrec->serv.type = 0;
429                                 if(AM_LOCAL_MASTER_BROWSER(work))
430                                         send_local_master_announcement(subrec, work, servrec);
431                                 send_host_announcement(subrec, work, servrec);
432
433                                 if ((announce_interval <= 0) || (lm_announce <= 0)) {
434                                         /* user absolutely does not want LM announcements to be sent. */
435                                         continue;
436                                 }
437
438                                 if ((lm_announce >= 2) && (!found_lm_clients)) {
439                                         /* has been set to 2 (Auto) but no LM clients detected (yet). */
440                                         continue;
441                                 }
442
443                                 /* 
444                                  * lm announce was set or we have seen lm announcements, so do
445                                  * a lm announcement of host removed.
446                                  */
447
448                                 send_lm_host_announcement(subrec, work, servrec, 0);
449                         }
450                 }
451         }
452 }
453
454 /****************************************************************************
455   Do all the "remote" announcements. These are used to put ourselves
456   on a remote browse list. They are done blind, no checking is done to
457   see if there is actually a local master browser at the other end.
458   **************************************************************************/
459
460 void announce_remote(time_t t)
461 {
462         char *s;
463         const char *ptr;
464         static time_t last_time = 0;
465         char *s2;
466         struct in_addr addr;
467         char *comment;
468         int stype = lp_default_server_announce();
469         TALLOC_CTX *frame = NULL;
470
471         if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
472                 return;
473
474         last_time = t;
475
476         s = lp_remote_announce(talloc_tos());
477         if (!*s)
478                 return;
479
480         comment = string_truncate(lp_serverstring(talloc_tos()),
481                                   MAX_SERVER_STRING_LENGTH);
482
483         frame = talloc_stackframe();
484         for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
485                 /* The entries are of the form a.b.c.d/WORKGROUP with
486                                 WORKGROUP being optional */
487                 const char *wgroup;
488                 char *pwgroup;
489                 int i;
490
491                 pwgroup = strchr_m(s2,'/');
492                 if (pwgroup)
493                         *pwgroup++ = 0;
494                 if (!pwgroup || !*pwgroup)
495                         wgroup = lp_workgroup();
496                 else
497                         wgroup = pwgroup;
498
499                 addr = interpret_addr2(s2);
500
501                 /* Announce all our names including aliases */
502                 /* Give the ip address as the address of our first
503                                 broadcast subnet. */
504
505                 for(i=0; my_netbios_names(i); i++) {
506                         const char *name = my_netbios_names(i);
507
508                         DEBUG(5,("announce_remote: Doing remote announce for server %s to IP %s.\n",
509                                 name, inet_ntoa(addr) ));
510
511                         send_announcement(FIRST_SUBNET, ANN_HostAnnouncement,
512                                                 name,                      /* From nbt name. */
513                                                 wgroup, 0x1d,              /* To nbt name. */
514                                                 addr,                      /* To ip. */
515                                                 REMOTE_ANNOUNCE_INTERVAL,  /* Time until next announce. */
516                                                 name,                      /* Name to announce. */
517                                                 stype,                     /* Type field. */
518                                                 comment);
519                 }
520         }
521         TALLOC_FREE(frame);
522 }
523
524 /****************************************************************************
525   Implement the 'remote browse sync' feature Andrew added.
526   These are used to put our browse lists into remote browse lists.
527 **************************************************************************/
528
529 void browse_sync_remote(time_t t)
530 {
531         char *s;
532         const char *ptr;
533         static time_t last_time = 0;
534         char *s2;
535         struct in_addr addr;
536         struct work_record *work;
537         char outbuf[1024];
538         char *p;
539         unstring myname;
540         TALLOC_CTX *frame = NULL;
541
542         if (last_time && (t < (last_time + REMOTE_ANNOUNCE_INTERVAL)))
543                 return;
544
545         last_time = t;
546
547         s = lp_remote_browse_sync(talloc_tos());
548         if (!*s)
549                 return;
550
551         /*
552          * We only do this if we are the local master browser
553          * for our workgroup on the firsst subnet.
554          */
555
556         if((work = find_workgroup_on_subnet(FIRST_SUBNET, lp_workgroup())) == NULL) {   
557                 DEBUG(0,("browse_sync_remote: Cannot find workgroup %s on subnet %s\n",
558                         lp_workgroup(), FIRST_SUBNET->subnet_name ));
559                 return;
560         }
561
562         if(!AM_LOCAL_MASTER_BROWSER(work)) {
563                 DEBUG(5,("browse_sync_remote: We can only do this if we are a local master browser \
564 for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name ));
565                 return;
566         }
567
568         memset(outbuf,'\0',sizeof(outbuf));
569         p = outbuf;
570         SCVAL(p,0,ANN_MasterAnnouncement);
571         p++;
572
573         unstrcpy(myname, lp_netbios_name());
574         if (!strupper_m(myname)) {
575                 DEBUG(2,("strupper_m %s failed\n", myname));
576                 return;
577         }
578         myname[15]='\0';
579         push_ascii(p, myname, sizeof(outbuf)-PTR_DIFF(p,outbuf)-1, STR_TERMINATE);
580
581         p = skip_string(outbuf,sizeof(outbuf),p);
582
583         frame = talloc_stackframe();
584         for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
585                 /* The entries are of the form a.b.c.d */
586                 addr = interpret_addr2(s2);
587
588                 DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
589                         lp_netbios_name(), inet_ntoa(addr) ));
590
591                 send_mailslot(True, BROWSE_MAILSLOT, outbuf,PTR_DIFF(p,outbuf),
592                         lp_netbios_name(), 0x0, "*", 0x0, addr, FIRST_SUBNET->myip, DGRAM_PORT);
593         }
594         TALLOC_FREE(frame);
595 }