Fix warning messages caused by addition of null check in fstrcpy macro.
[ira/wip.git] / source3 / winbindd / winbindd_wins.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - WINS related functions
5
6    Copyright (C) Andrew Tridgell 1999
7    Copyright (C) Herb Lewis 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25 #include "libsmb/nmblib.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 static struct node_status *lookup_byaddr_backend(TALLOC_CTX *mem_ctx,
31                                                  const char *addr, int *count)
32 {
33         struct sockaddr_storage ss;
34         struct nmb_name nname;
35         struct node_status *result;
36         NTSTATUS status;
37
38         make_nmb_name(&nname, "*", 0);
39         if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
40                 return NULL;
41         }
42         status = node_status_query(mem_ctx, &nname, &ss,
43                                    &result, count, NULL);
44         if (!NT_STATUS_IS_OK(status)) {
45                 return NULL;
46         }
47         return result;
48 }
49
50 static struct sockaddr_storage *lookup_byname_backend(TALLOC_CTX *mem_ctx,
51                                                       const char *name,
52                                                       int *count)
53 {
54         struct ip_service *ret = NULL;
55         struct sockaddr_storage *return_ss = NULL;
56         int j, i;
57         NTSTATUS status;
58
59         *count = 0;
60
61         /* always try with wins first */
62         if (NT_STATUS_IS_OK(resolve_wins(name,0x20,&ret,count))) {
63                 if ( *count == 0 )
64                         return NULL;
65                 return_ss = TALLOC_ARRAY(mem_ctx, struct sockaddr_storage,
66                                          *count);
67                 if (return_ss == NULL ) {
68                         free( ret );
69                         return NULL;
70                 }
71
72                 /* copy the IP addresses */
73                 for ( i=0; i<(*count); i++ )
74                         return_ss[i] = ret[i].ss;
75
76                 free( ret );
77                 return return_ss;
78         }
79
80         /* uggh, we have to broadcast to each interface in turn */
81         for (j=iface_count() - 1;
82              j >= 0;
83              j--) {
84                 const struct sockaddr_storage *bcast_ss = iface_n_bcast(j);
85                 if (!bcast_ss) {
86                         continue;
87                 }
88                 status = name_query(name, 0x20, True, True,bcast_ss,
89                                     mem_ctx, &return_ss, count, NULL);
90                 if (NT_STATUS_IS_OK(status)) {
91                         break;
92                 }
93         }
94
95         return return_ss;
96 }
97
98 /* Get hostname from IP  */
99
100 void winbindd_wins_byip(struct winbindd_cli_state *state)
101 {
102         fstring response;
103         int i, count, maxlen, size;
104         struct node_status *status;
105
106         /* Ensure null termination */
107         state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
108
109         DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state->pid,
110                 state->request->data.winsreq));
111
112         *response = '\0';
113         maxlen = sizeof(response) - 1;
114
115         if ((status = lookup_byaddr_backend(
116                      state->mem_ctx, state->request->data.winsreq, &count))) {
117             size = strlen(state->request->data.winsreq);
118             if (size > maxlen) {
119                 TALLOC_FREE(status);
120                 request_error(state);
121                 return;
122             }
123             fstrcat(response,state->request->data.winsreq);
124             fstrcat(response,"\t");
125             for (i = 0; i < count; i++) {
126                 /* ignore group names */
127                 if (status[i].flags & 0x80) continue;
128                 if (status[i].type == 0x20) {
129                         size = sizeof(status[i].name) + strlen(response);
130                         if (size > maxlen) {
131                             TALLOC_FREE(status);
132                             request_error(state);
133                             return;
134                         }
135                         fstrcat(response, status[i].name);
136                         fstrcat(response, " ");
137                 }
138             }
139             /* make last character a newline */
140             response[strlen(response)-1] = '\n';
141             TALLOC_FREE(status);
142         }
143         strlcpy(state->response->data.winsresp,
144                         response,
145                         sizeof(state->response->data.winsresp));
146         request_ok(state);
147 }
148
149 /* Get IP from hostname */
150
151 void winbindd_wins_byname(struct winbindd_cli_state *state)
152 {
153         struct sockaddr_storage *ip_list = NULL;
154         int i, count, maxlen, size;
155         fstring response;
156         char addr[INET6_ADDRSTRLEN];
157
158         /* Ensure null termination */
159         state->request->data.winsreq[sizeof(state->request->data.winsreq)-1]='\0';
160
161         DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state->pid,
162                 state->request->data.winsreq));
163
164         *response = '\0';
165         maxlen = sizeof(response) - 1;
166
167         ip_list = lookup_byname_backend(
168                 state->mem_ctx, state->request->data.winsreq, &count);
169         if (ip_list != NULL){
170                 for (i = count; i ; i--) {
171                         print_sockaddr(addr, sizeof(addr), &ip_list[i-1]);
172                         size = strlen(addr);
173                         if (size > maxlen) {
174                                 TALLOC_FREE(ip_list);
175                                 request_error(state);
176                                 return;
177                         }
178                         if (i != 0) {
179                                 /* Clear out the newline character */
180                                 /* But only if there is something in there,
181                                 otherwise we clobber something in the stack */
182                                 if (strlen(response)) {
183                                         response[strlen(response)-1] = ' ';
184                                 }
185                         }
186                         strlcat(response,addr,sizeof(response));
187                         strlcat(response,"\t",sizeof(response));
188                 }
189                 size = strlen(state->request->data.winsreq) + strlen(response);
190                 if (size > maxlen) {
191                     TALLOC_FREE(ip_list);
192                     request_error(state);
193                     return;
194                 }
195                 strlcat(response,state->request->data.winsreq,sizeof(response));
196                 strlcat(response,"\n",sizeof(response));
197                 TALLOC_FREE(ip_list);
198         } else {
199                 request_error(state);
200                 return;
201         }
202
203         strlcpy(state->response->data.winsresp,
204                 response,
205                 sizeof(state->response->data.winsresp));
206
207         request_ok(state);
208 }