2 Unix SMB/CIFS implementation.
4 Winbind daemon - WINS related functions
6 Copyright (C) Andrew Tridgell 1999
7 Copyright (C) Herb Lewis 2002
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.
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.
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/>.
27 #define DBGC_CLASS DBGC_WINBIND
29 /* Use our own create socket code so we don't recurse.... */
31 static int wins_lookup_open_socket_in(void)
33 struct sockaddr_in sock
;
37 memset((char *)&sock
,'\0',sizeof(sock
));
39 #ifdef HAVE_SOCK_SIN_LEN
40 sock
.sin_len
= sizeof(sock
);
43 sock
.sin_family
= AF_INET
;
44 sock
.sin_addr
.s_addr
= interpret_addr("0.0.0.0");
45 res
= socket(AF_INET
, SOCK_DGRAM
, 0);
49 setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&val
,sizeof(val
));
51 setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,(char *)&val
,sizeof(val
));
52 #endif /* SO_REUSEPORT */
54 /* now we've got a socket - we need to bind it */
56 if (bind(res
, (struct sockaddr
* ) &sock
,sizeof(sock
)) < 0) {
61 set_socket_options(res
,"SO_BROADCAST");
67 static NODE_STATUS_STRUCT
*lookup_byaddr_backend(const char *addr
, int *count
)
70 struct sockaddr_storage ss
;
71 struct nmb_name nname
;
72 NODE_STATUS_STRUCT
*status
;
74 fd
= wins_lookup_open_socket_in();
78 make_nmb_name(&nname
, "*", 0);
79 if (!interpret_string_addr(&ss
, addr
, AI_NUMERICHOST
)) {
82 status
= node_status_query(fd
, &nname
, &ss
, count
, NULL
);
88 static struct sockaddr_storage
*lookup_byname_backend(const char *name
,
92 struct ip_service
*ret
= NULL
;
93 struct sockaddr_storage
*return_ss
= NULL
;
98 /* always try with wins first */
99 if (NT_STATUS_IS_OK(resolve_wins(name
,0x20,&ret
,count
))) {
102 if ( (return_ss
= SMB_MALLOC_ARRAY(struct sockaddr_storage
, *count
)) == NULL
) {
107 /* copy the IP addresses */
108 for ( i
=0; i
<(*count
); i
++ )
109 return_ss
[i
] = ret
[i
].ss
;
115 fd
= wins_lookup_open_socket_in();
120 /* uggh, we have to broadcast to each interface in turn */
121 for (j
=iface_count() - 1;
124 const struct sockaddr_storage
*bcast_ss
= iface_n_bcast(j
);
128 return_ss
= name_query(fd
,name
,0x20,True
,True
,bcast_ss
,count
, &flags
, NULL
);
138 /* Get hostname from IP */
140 void winbindd_wins_byip(struct winbindd_cli_state
*state
)
143 int i
, count
, maxlen
, size
;
144 NODE_STATUS_STRUCT
*status
;
146 /* Ensure null termination */
147 state
->request
.data
.winsreq
[sizeof(state
->request
.data
.winsreq
)-1]='\0';
149 DEBUG(3, ("[%5lu]: wins_byip %s\n", (unsigned long)state
->pid
,
150 state
->request
.data
.winsreq
));
153 maxlen
= sizeof(response
) - 1;
155 if ((status
= lookup_byaddr_backend(state
->request
.data
.winsreq
, &count
))){
156 size
= strlen(state
->request
.data
.winsreq
);
159 request_error(state
);
162 fstrcat(response
,state
->request
.data
.winsreq
);
163 fstrcat(response
,"\t");
164 for (i
= 0; i
< count
; i
++) {
165 /* ignore group names */
166 if (status
[i
].flags
& 0x80) continue;
167 if (status
[i
].type
== 0x20) {
168 size
= sizeof(status
[i
].name
) + strlen(response
);
171 request_error(state
);
174 fstrcat(response
, status
[i
].name
);
175 fstrcat(response
, " ");
178 /* make last character a newline */
179 response
[strlen(response
)-1] = '\n';
182 fstrcpy(state
->response
.data
.winsresp
,response
);
186 /* Get IP from hostname */
188 void winbindd_wins_byname(struct winbindd_cli_state
*state
)
190 struct sockaddr_storage
*ip_list
= NULL
;
191 int i
, count
, maxlen
, size
;
193 char addr
[INET6_ADDRSTRLEN
];
195 /* Ensure null termination */
196 state
->request
.data
.winsreq
[sizeof(state
->request
.data
.winsreq
)-1]='\0';
198 DEBUG(3, ("[%5lu]: wins_byname %s\n", (unsigned long)state
->pid
,
199 state
->request
.data
.winsreq
));
202 maxlen
= sizeof(response
) - 1;
204 if ((ip_list
= lookup_byname_backend(state
->request
.data
.winsreq
,&count
))){
205 for (i
= count
; i
; i
--) {
206 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
-1]);
210 request_error(state
);
214 /* Clear out the newline character */
215 /* But only if there is something in there,
216 otherwise we clobber something in the stack */
217 if (strlen(response
)) {
218 response
[strlen(response
)-1] = ' ';
221 fstrcat(response
,addr
);
222 fstrcat(response
,"\t");
224 size
= strlen(state
->request
.data
.winsreq
) + strlen(response
);
227 request_error(state
);
230 fstrcat(response
,state
->request
.data
.winsreq
);
231 fstrcat(response
,"\n");
234 request_error(state
);
238 fstrcpy(state
->response
.data
.winsresp
,response
);