2 * VDETELWEB: VDE telnet and WEB interface
6 * Copyright 2005,2008 Virtual Square Team University of Bologna - Italy
7 * 2005 written by Renzo Davoli
8 * --pidfile/-p and cleanup management by Mattia Belletti (C) 2004
9 * (copied from vde_switch code).
10 * 2008 updated Renzo Davoli
11 * 2008 sha1sum by Marco Dalla Via
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/select.h>
45 #include <sys/utsname.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
53 #include "vdetelweb.h"
65 static char *pidfile
= NULL
;
66 static char pidfile_path
[_POSIX_PATH_MAX
];
67 struct stack
*lwipstack
;
72 struct pollfd pfd
[MAXFD
];
76 #define ROOTCONFFILE "/etc/vde/vdetelwebrc"
78 void printlog(int priority
, const char *format
, ...)
82 va_start (arg
, format
);
85 vsyslog(priority
,format
,arg
);
87 fprintf(stderr
,"%s: ",progname
);
88 vfprintf(stderr
,format
,arg
);
95 static void cleanup(void)
97 lwip_stack_free(lwipstack
);
98 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
99 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
103 int sha1passwdok(const char *pw
) {
104 char buf
[HASH_SIZE
+ 1];
116 dup2(pfd_fc
[0], STDIN_FILENO
);
117 dup2(pfd_cf
[1], STDOUT_FILENO
);
119 execl("/usr/bin/sha1sum", "/usr/bin/sha1sum", NULL
);
125 write(pfd_fc
[1], pw
, strlen(pw
));
127 read(pfd_cf
[0], buf
, sizeof(buf
));
130 waitpid(pid
, NULL
, 0);
131 return (strncmp(buf
,passwd
,strlen(passwd
))==0);
135 static void sig_handler(int sig
)
138 signal(sig
, SIG_DFL
);
142 static void setsighandlers()
144 /* setting signal handlers.
145 * * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
146 * * ignores all the others signals which could cause termination. */
147 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
148 { SIGHUP
, "SIGHUP", 0 },
149 { SIGINT
, "SIGINT", 0 },
150 { SIGPIPE
, "SIGPIPE", 1 },
151 { SIGALRM
, "SIGALRM", 1 },
152 { SIGTERM
, "SIGTERM", 0 },
153 { SIGUSR1
, "SIGUSR1", 1 },
154 { SIGUSR2
, "SIGUSR2", 1 },
155 { SIGPOLL
, "SIGPOLL", 1 },
156 { SIGPROF
, "SIGPROF", 1 },
157 { SIGVTALRM
, "SIGVTALRM", 1 },
158 { SIGSTKFLT
, "SIGSTKFLT", 1 },
159 { SIGIO
, "SIGIO", 1 },
160 { SIGPWR
, "SIGPWR", 1 },
161 { SIGUNUSED
, "SIGUNUSED", 1 },
166 for(i
= 0; signals
[i
].sig
!= 0; i
++)
167 if(signal(signals
[i
].sig
,
168 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
169 perror("Setting handler");
172 static void usage(char *progname
) {
173 fprintf (stderr
,"Usage: %s [-w] [-t] [-d] [-n nodename] [-p pidfile] mgmt_socket\n"
174 " %s [--web] [--telnet] [--daemon] [--nodename nodename] [--pidfile pidfile] mgmt_socket\n",progname
,progname
);
178 void setprompt(char *ctrl
,char *nodename
)
181 if (nodename
==NULL
) {
184 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",un
.nodename
,ctrl
);
186 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",nodename
,ctrl
);
192 struct sockaddr_un sun
;
195 sun
.sun_family
=PF_UNIX
;
196 snprintf(sun
.sun_path
,UNIX_PATH_MAX
,"%s",mgmt
);
197 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
198 if (connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) < 0) {
199 printlog(LOG_ERR
,"mgmt extra connect %s",strerror(errno
));
202 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
203 printlog(LOG_ERR
,"banner %s",strerror(errno
));
209 int openvdem(char *mgmt
,char *progname
, struct netif
**nif
,char *nodename
)
211 struct sockaddr_un sun
;
213 char buf
[BUFSIZE
+1],*line2
,*ctrl
;
214 sun
.sun_family
=PF_UNIX
;
215 snprintf(sun
.sun_path
,UNIX_PATH_MAX
,"%s",mgmt
);
216 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
217 if (connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) < 0) {
218 printlog(LOG_ERR
,"mgmt connect %s",strerror(errno
));
221 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
222 printlog(LOG_ERR
,"banner %s",strerror(errno
));
226 if ((ctrl
=rindex(buf
,'\n')) != NULL
)
229 write(fd
,"ds/showinfo\n",13);
230 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
231 printlog(LOG_ERR
,"read ctl socket %s",strerror(errno
));
235 if ((line2
=index(buf
,'\n')) == NULL
) {
236 printlog(LOG_ERR
,"read ctl socket parse error 1");
240 if (strncmp(line2
,"ctl dir ",8) != 0) {
241 printlog(LOG_ERR
,"read ctl socket parse error");
244 for(ctrl
=line2
+8;*ctrl
!='\n' && ctrl
<buf
+n
;ctrl
++)
248 setprompt(ctrl
,nodename
);
250 *nif
=lwip_vdeif_add(lwipstack
,ctrl
);
252 printlog(LOG_ERR
,"cannot connect to the switch");
260 static void bitno2mask(unsigned char *addr
,int bitno
,int len
)
263 for(i
=0;i
<len
;i
++,bitno
-= 8) {
269 addr
[i
]=256 - (1<<(8-bitno
));
273 static void sockaddr2ip_6addr(struct ip_addr
*ipaddrp
,unsigned char *addr
)
276 (addr
[0]<<8)|addr
[1],
277 (addr
[2]<<8)|addr
[3],
278 (addr
[4]<<8)|addr
[5],
279 (addr
[6]<<8)|addr
[7],
280 (addr
[8]<<8)|addr
[9],
281 (addr
[10]<<8)|addr
[11],
282 (addr
[12]<<8)|addr
[13],
283 (addr
[14]<<8)|addr
[15]);
286 static void readip(char *arg
,struct netif
*nif
,int af
)
288 char *bit
=rindex(arg
,'/');
290 printlog(LOG_ERR
,"ip addresses must include the netmask i.e. addr/maskbits");
292 int bitno
=atoi(bit
+1);
294 struct addrinfo
*res
,hint
;
295 struct ip_addr ipaddr
,netmask
;
297 memset(&hint
,0,sizeof(hint
));
299 if ((err
=getaddrinfo(arg
,NULL
,&hint
,&res
))!=0)
300 printlog(LOG_ERR
,"ip address %s error %s",arg
,gai_strerror(err
));
302 switch(res
->ai_family
) {
304 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
305 int addrh
=ntohl(in
->sin_addr
.s_addr
);
306 unsigned char i
,addr
[4];
307 for (i
=0;i
<4;i
++,addrh
>>=8)
309 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
310 bitno2mask(addr
,bitno
,4);
311 IP64_MASKADDR(&netmask
, addr
[0],addr
[1],addr
[2],addr
[3]);
312 lwip_add_addr(nif
,&ipaddr
,&netmask
);
316 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
317 unsigned char *addr
=in
->sin6_addr
.s6_addr
;
318 sockaddr2ip_6addr(&ipaddr
,addr
);
319 bitno2mask(addr
,bitno
,16);
320 sockaddr2ip_6addr(&netmask
,addr
);
321 lwip_add_addr(nif
,&ipaddr
,&netmask
);
325 printlog(LOG_ERR
,"unsupported Address Family: %s",arg
);
332 static void readdefroute(char *arg
,struct netif
*nif
,int af
)
334 struct addrinfo
*res
,hint
;
335 struct ip_addr ipaddr
;
337 memset(&hint
,0,sizeof(hint
));
339 if ((err
=getaddrinfo(arg
,NULL
,&hint
,&res
))!=0)
340 printlog(LOG_ERR
,"ip address %s error %s",arg
,gai_strerror(err
));
342 switch(res
->ai_family
) {
344 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
345 int addrh
=ntohl(in
->sin_addr
.s_addr
);
346 unsigned char i
,addr
[4];
347 for (i
=0;i
<4;i
++,addrh
>>=8)
349 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
350 lwip_add_route(lwipstack
,IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
354 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
355 sockaddr2ip_6addr(&ipaddr
,in
->sin6_addr
.s6_addr
);
356 lwip_add_route(lwipstack
,IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
360 printlog(LOG_ERR
,"unsupported Address Family: %s",arg
);
366 static void readpassword(char *arg
,int unused
)
376 {"ip4",readip
,PF_INET
},
377 {"ip6",readip
,PF_INET6
},
379 {"defroute4",readdefroute
,PF_INET
},
380 {"defroute6",readdefroute
,PF_INET6
},
381 {"defroute",readdefroute
,0},
382 {"password",readpassword
,0},
385 int readconffile(char *path
,struct netif
*nif
)
388 char buf
[BUFSIZE
],*s
;
389 if (path
== NULL
&& geteuid() == 0)
393 if((f
=fopen(path
,"r"))==NULL
)
395 while (fgets(buf
,BUFSIZE
,f
) != NULL
) {
396 if ((s
=rindex(buf
,'\n')) != NULL
)
398 for(s
=buf
;*s
== ' ' || *s
== '\t';s
++)
402 for (scf
=cft
;scf
->tag
!= NULL
;scf
++)
403 if(strncmp(s
,scf
->tag
,strlen(scf
->tag
)) == 0){
405 for(;*s
== ' ' || *s
== '\t';s
++)
408 for(;*s
== ' ' || *s
== '\t';s
++)
410 scf
->f(s
,nif
,scf
->arg
);
413 if (scf
->tag
== NULL
) {
414 printlog(LOG_ERR
,"rc file syntax error: %s",buf
);
421 int addpfd(int fd
,voidfun cb
)
425 pfd
[npfd
].events
=POLLIN
|POLLHUP
;
436 for (i
=fn
;i
<npfd
-1;i
++) {
439 status
[i
]=status
[i
+1];
444 int pfdsearch(int fd
)
447 for (i
=0;i
<npfd
&& pfd
[i
].fd
!=fd
;i
++)
452 int setfds(fd_set
*rds
, fd_set
*exc
)
457 for (i
=0;i
<npfd
;i
++) {
458 FD_SET(pfd
[i
].fd
,rds
);
459 FD_SET(pfd
[i
].fd
,exc
);
460 if (pfd
[i
].fd
>max
) max
=pfd
[i
].fd
;
465 static void save_pidfile()
467 if(pidfile
[0] != '/')
468 strncat(pidfile_path
, pidfile
, _POSIX_PATH_MAX
- strlen(pidfile_path
));
470 strcpy(pidfile_path
, pidfile
);
472 int fd
= open(pidfile_path
,
473 O_WRONLY
| O_CREAT
| O_EXCL
,
474 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
478 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
482 if((f
= fdopen(fd
, "w")) == NULL
) {
483 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
487 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
488 printlog(LOG_ERR
, "Error in writing pidfile");
495 int main(int argc
, char *argv
[])
505 int option_index
= 0;
507 static struct option long_options
[] = {
508 {"daemon", 0, 0, 'd'},
510 {"telnet", 0, 0, 't'},
514 {"nodename",1,0,'n'},
515 {"pidfile", 1, 0, 'p'},
518 c
= getopt_long_only (argc
, argv
, "hdwtM:f:n:",
519 long_options
, &option_index
);
528 conffile
=strdup(optarg
);
531 nodename
=strdup(optarg
);
543 pidfile
=strdup(optarg
);
546 usage(argv
[0]); //implies exit
550 if (optind
< argc
&& mgmt
==NULL
)
554 printlog(LOG_ERR
,"mgmt_socket not defined");
557 if (telnet
==0 && web
==0) {
558 printlog(LOG_ERR
,"at least one service option (-t -w) must be specified");
564 openlog(basename(argv
[0]), LOG_PID
, 0);
566 syslog(LOG_INFO
,"VDETELWEB started");
569 /* saves current path in pidfile_path, because otherwise with daemonize() we
571 if(getcwd(pidfile_path
, _POSIX_PATH_MAX
-1) == NULL
) {
572 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
575 strcat(pidfile_path
, "/");
577 if (daemonize
&& daemon(0, 1)) {
578 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
581 /* once here, we're sure we're the true process which will continue as a
582 * server: save PID file if needed */
583 if(pidfile
) save_pidfile();
585 lwipstack
=lwip_stack_new();
586 lwip_stack_set(lwipstack
);
588 vdefd
=openvdem(mgmt
,argv
[0],&nif
,nodename
);
589 if (readconffile(conffile
,nif
) < 0) {
590 printlog(LOG_ERR
,"configuration file not found");
602 int max
=setfds(&rds
,&exc
);
603 m
=lwip_select(max
,&rds
,NULL
,&exc
,NULL
);
604 for(i
=0; m
>0 && i
<max
; i
++) {
605 if (FD_ISSET(i
,&rds
) || FD_ISSET(i
,&exc
)) {