2 Unix SMB/Netbios implementation.
4 SMB agent/socket plugin
5 Copyright (C) Andrew Tridgell 1999
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.
25 #define SECURITY_MASK 0
26 #define SECURITY_SET 0
28 /* this forces non-unicode */
29 #define CAPABILITY_MASK CAP_UNICODE
30 #define CAPABILITY_SET 0
32 /* and non-unicode for the client too */
33 #define CLI_CAPABILITY_MASK CAP_UNICODE
34 #define CLI_CAPABILITY_SET 0
36 extern int DEBUGLEVEL
;
38 static int ClientNMB
= -1;
40 /****************************************************************************
41 terminate sockent connection
42 ****************************************************************************/
43 static void free_sock(void *sock
)
51 static void filter_reply(struct packet_struct
*p
, int tr_id
)
53 p
->packet
.nmb
.header
.name_trn_id
= tr_id
;
56 static BOOL
process_cli_sock(struct sock_redir
**socks
,
58 struct sock_redir
*sock
)
60 struct packet_struct
*p
;
61 struct nmb_state
*nmb
;
62 static uint16 trn_id
= 0x0;
64 p
= receive_packet(sock
->c
, NMB_SOCK_PACKET
, 0);
67 DEBUG(0,("client closed connection\n"));
71 nmb
= (struct nmb_state
*)malloc(sizeof(struct nmb_state
));
80 sock
->c_id
= p
->packet
.nmb
.header
.name_trn_id
;
89 DEBUG(10,("new trn_id: %d\n", trn_id
));
91 filter_reply(p
, sock
->s_id
);
97 p
->packet_type
= NMB_PACKET
;
101 DEBUG(0,("server is dead\n"));
109 static BOOL
process_srv_sock(struct sock_redir
**socks
,
117 struct packet_struct
*p
;
119 p
= receive_packet(fd
, NMB_PACKET
, 0);
126 if (!p
->packet
.nmb
.header
.response
)
128 DEBUG(10,("skipping response packet\n"));
134 nmb_id
= p
->packet
.nmb
.header
.name_trn_id
;
135 DEBUG(10,("process_srv_sock:\tnmb_id:\t%d\n", nmb_id
));
137 for (i
= 0; i
< num_socks
; i
++)
139 if (socks
[i
] == NULL
)
144 tr_id
= socks
[i
]->s_id
;
146 DEBUG(10,("list:\tfd:\t%d\tc_id:\t%d\ttr_id:\t%d\n",
156 filter_reply(p
, socks
[i
]->c_id
);
158 p
->packet_type
= NMB_SOCK_PACKET
;
162 DEBUG(0,("client is dead\n"));
170 static int get_agent_sock(char *id
)
176 slprintf(dir
, sizeof(dir
)-1, "/tmp/.nmb");
177 slprintf(path
, sizeof(path
)-1, "%s/agent", dir
);
179 s
= create_pipe_socket(dir
, 0777, path
, 0777);
183 /* ready to listen */
184 if (listen(s
, 5) == -1) {
185 DEBUG(0,("listen: %s\n", strerror(errno
)));
192 static void start_nmb_agent(void)
194 struct vagent_ops va
=
210 /******************************************************************************
211 open the socket communication
212 *****************************************************************************/
213 static BOOL
open_sockets(BOOL isdaemon
, int port
)
215 /* The sockets opened here will be used to receive broadcast
216 packets *only*. Interface specific sockets are opened in
217 make_subnet() in namedbsubnet.c. Thus we bind to the
218 address "0.0.0.0". The parameter 'socket address' is
223 ClientNMB
= open_socket_in(SOCK_DGRAM
, port
,0,0,True
);
227 if ( ClientNMB
== -1 )
230 /* we are never interested in SIGPIPE */
231 BlockSignals(True
,SIGPIPE
);
233 set_socket_options( ClientNMB
, "SO_BROADCAST" );
235 DEBUG( 3, ( "open_sockets: Broadcast sockets opened.\n" ) );
239 /****************************************************************************
241 ****************************************************************************/
242 static void usage(char *pname
)
244 printf("Usage: %s [-D]", pname
);
246 printf("\nVersion %s\n",VERSION
);
247 printf("\t-D run as a daemon\n");
248 printf("\t-h usage\n");
252 int main(int argc
, char *argv
[])
255 BOOL is_daemon
= False
;
257 extern pstring debugf
;
258 int global_nmb_port
= NMB_PORT
;
262 pstrcpy(configfile
,CONFIGFILE
);
264 while ((opt
= getopt(argc
, argv
, "Dh")) != EOF
)
282 slprintf(debugf
, sizeof(debugf
)-1, "log.%s", argv
[0]);
283 setup_logging(argv
[0], !is_daemon
);
285 charset_initialise();
287 if (!lp_load(configfile
,True
,False
,False
))
289 DEBUG(0,("Unable to load config file\n"));
294 DEBUG(0,("%s: becoming daemon\n", argv
[0]));
298 if (!open_sockets(True
, global_nmb_port
))