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"
66 static char *pidfile
= NULL
;
67 static char pidfile_path
[_POSIX_PATH_MAX
];
68 struct stack
*lwipstack
;
73 struct pollfd pfd
[MAXFD
];
77 #define ROOTCONFFILE "/etc/vde/vdetelwebrc"
79 /* This will be prefixed by getenv("HOME") */
80 #define USERCONFFILE "/.vde/vdetelwebrc"
82 void printlog(int priority
, const char *format
, ...)
86 va_start (arg
, format
);
89 vsyslog(priority
,format
,arg
);
91 fprintf(stderr
,"%s: ",progname
);
92 vfprintf(stderr
,format
,arg
);
99 static void cleanup(void)
102 lwip_stack_free(lwipstack
);
103 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
104 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
108 int sha1passwdok(const char *pw
) {
109 char buf
[HASH_SIZE
+ 1];
121 dup2(pfd_fc
[0], STDIN_FILENO
);
122 dup2(pfd_cf
[1], STDOUT_FILENO
);
124 execl("/usr/bin/sha1sum", "/usr/bin/sha1sum", NULL
);
130 write(pfd_fc
[1], pw
, strlen(pw
));
132 read(pfd_cf
[0], buf
, sizeof(buf
));
135 waitpid(pid
, NULL
, 0);
136 return (strncmp(buf
,passwd
,strlen(passwd
))==0);
140 static void sig_handler(int sig
)
143 signal(sig
, SIG_DFL
);
147 static void setsighandlers(void)
149 /* setting signal handlers.
150 * * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
151 * * ignores all the others signals which could cause termination. */
152 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
153 { SIGHUP
, "SIGHUP", 0 },
154 { SIGINT
, "SIGINT", 0 },
155 { SIGPIPE
, "SIGPIPE", 1 },
156 { SIGALRM
, "SIGALRM", 1 },
157 { SIGTERM
, "SIGTERM", 0 },
158 { SIGUSR1
, "SIGUSR1", 1 },
159 { SIGUSR2
, "SIGUSR2", 1 },
160 { SIGPOLL
, "SIGPOLL", 1 },
161 { SIGPROF
, "SIGPROF", 1 },
162 { SIGVTALRM
, "SIGVTALRM", 1 },
164 { SIGSTKFLT
, "SIGSTKFLT", 1 },
166 { SIGIO
, "SIGIO", 1 },
167 { SIGPWR
, "SIGPWR", 1 },
169 { SIGUNUSED
, "SIGUNUSED", 1 },
175 for(i
= 0; signals
[i
].sig
!= 0; i
++)
176 if(signal(signals
[i
].sig
,
177 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
178 perror("Setting handler");
181 static void usage(char *progname
) {
182 fprintf (stderr
,"Usage: %s [-w] [-t] [-d] [-n nodename] [-p pidfile] mgmt_socket\n"
183 " %s [--web] [--telnet] [--daemon] [--nodename nodename] [--pidfile pidfile] mgmt_socket\n",progname
,progname
);
187 void setprompt(char *ctrl
,char *nodename
)
190 if (nodename
==NULL
) {
193 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",un
.nodename
,ctrl
);
195 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",nodename
,ctrl
);
199 int openextravdem(void)
201 struct sockaddr_un sun
;
204 sun
.sun_family
=PF_UNIX
;
205 snprintf(sun
.sun_path
,UNIX_PATH_MAX
,"%s",mgmt
);
206 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
207 if (connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) < 0) {
208 printlog(LOG_ERR
,"Error connecting to the management socket '%s': %s", mgmt
, strerror(errno
));
211 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
212 printlog(LOG_ERR
,"banner %s",strerror(errno
));
218 int openvdem(char *mgmt
,char *progname
, struct netif
**nif
,char *nodename
)
220 struct sockaddr_un sun
;
222 char buf
[BUFSIZE
+1],*line2
,*ctrl
;
223 sun
.sun_family
=PF_UNIX
;
224 snprintf(sun
.sun_path
,UNIX_PATH_MAX
,"%s",mgmt
);
225 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
226 if (connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) < 0) {
227 printlog(LOG_ERR
,"Error connecting to the management socket '%s': %s", mgmt
, strerror(errno
));
230 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
231 printlog(LOG_ERR
,"Error reading banner from VDE switch: %s",strerror(errno
));
235 if ((ctrl
=rindex(buf
,'\n')) != NULL
)
238 write(fd
,"ds/showinfo\n",13);
239 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
240 printlog(LOG_ERR
,"Error reading ctl socket from VDE switch: %s",strerror(errno
));
244 if ((line2
=index(buf
,'\n')) == NULL
) {
245 printlog(LOG_ERR
,"Error parsing first line of ctl socket information");
249 if (strncmp(line2
,"ctl dir ",8) != 0) {
250 printlog(LOG_ERR
,"Error parsing ctl socket information");
253 for(ctrl
=line2
+8;*ctrl
!='\n' && ctrl
<buf
+n
;ctrl
++)
257 setprompt(ctrl
,nodename
);
259 *nif
=lwip_vdeif_add(lwipstack
,ctrl
);
261 printlog(LOG_ERR
,"Cannot connect to the VDE switch");
269 static void bitno2mask(unsigned char *addr
,int bitno
,int len
)
272 for(i
=0;i
<len
;i
++,bitno
-= 8) {
278 addr
[i
]=256 - (1<<(8-bitno
));
282 static void sockaddr2ip_6addr(struct ip_addr
*ipaddrp
,unsigned char *addr
)
285 (addr
[0]<<8)|addr
[1],
286 (addr
[2]<<8)|addr
[3],
287 (addr
[4]<<8)|addr
[5],
288 (addr
[6]<<8)|addr
[7],
289 (addr
[8]<<8)|addr
[9],
290 (addr
[10]<<8)|addr
[11],
291 (addr
[12]<<8)|addr
[13],
292 (addr
[14]<<8)|addr
[15]);
295 static void readip(char *arg
,struct netif
*nif
,int af
)
297 char *bit
=rindex(arg
,'/');
299 printlog(LOG_ERR
,"IP addresses must include the netmask i.e. addr/maskbits");
301 int bitno
=atoi(bit
+1);
303 struct addrinfo
*res
,hint
;
304 struct ip_addr ipaddr
,netmask
;
306 memset(&hint
,0,sizeof(hint
));
308 if ((err
=getaddrinfo(arg
,NULL
,&hint
,&res
))!=0)
309 printlog(LOG_ERR
,"IP address %s error %s",arg
,gai_strerror(err
));
311 switch(res
->ai_family
) {
314 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
315 int addrh
=ntohl(in
->sin_addr
.s_addr
);
316 unsigned char i
,addr
[4];
317 for (i
=0;i
<4;i
++,addrh
>>=8)
319 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
320 bitno2mask(addr
,bitno
,4);
321 IP64_MASKADDR(&netmask
, addr
[0],addr
[1],addr
[2],addr
[3]);
322 lwip_add_addr(nif
,&ipaddr
,&netmask
);
327 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
328 unsigned char *addr
=in
->sin6_addr
.s6_addr
;
329 sockaddr2ip_6addr(&ipaddr
,addr
);
330 bitno2mask(addr
,bitno
,16);
331 sockaddr2ip_6addr(&netmask
,addr
);
332 lwip_add_addr(nif
,&ipaddr
,&netmask
);
336 printlog(LOG_ERR
,"Unsupported Address Family: %s",arg
);
343 static void readdefroute(char *arg
,struct netif
*nif
,int af
)
345 struct addrinfo
*res
,hint
;
346 struct ip_addr ipaddr
;
348 memset(&hint
,0,sizeof(hint
));
350 if ((err
=getaddrinfo(arg
,NULL
,&hint
,&res
))!=0)
351 printlog(LOG_ERR
,"IP address %s error %s",arg
,gai_strerror(err
));
353 switch(res
->ai_family
) {
356 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
357 int addrh
=ntohl(in
->sin_addr
.s_addr
);
358 unsigned char i
,addr
[4];
359 for (i
=0;i
<4;i
++,addrh
>>=8)
361 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
362 lwip_add_route(lwipstack
,IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
367 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
368 sockaddr2ip_6addr(&ipaddr
,in
->sin6_addr
.s6_addr
);
369 lwip_add_route(lwipstack
,IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
373 printlog(LOG_ERR
,"Unsupported Address Family: %s",arg
);
379 static void readpassword(char *arg
,int unused
)
389 {"ip4",readip
,PF_INET
},
390 {"ip6",readip
,PF_INET6
},
392 {"defroute4",readdefroute
,PF_INET
},
393 {"defroute6",readdefroute
,PF_INET6
},
394 {"defroute",readdefroute
,0},
395 {"password",readpassword
,0},
398 int readconffile(char *path
,struct netif
*nif
)
401 char buf
[BUFSIZE
],*s
;
406 if((f
=fopen(path
,"r"))==NULL
)
408 while (fgets(buf
,BUFSIZE
,f
) != NULL
)
412 if ((s
=rindex(buf
,'\n')) != NULL
)
415 for(s
=buf
;*s
== ' ' || *s
== '\t';s
++);
417 if (*s
!= '#' && *s
!= '\n' && *s
!= '\0')
420 for (scf
=cft
;scf
->tag
!= NULL
;scf
++)
421 if(strncmp(s
,scf
->tag
,strlen(scf
->tag
)) == 0)
424 for(;*s
== ' ' || *s
== '\t';s
++);
427 for(;*s
== ' ' || *s
== '\t';s
++);
428 scf
->f(s
,nif
,scf
->arg
);
431 if (scf
->tag
== NULL
)
432 printlog(LOG_ERR
,"Error parsing configuration file: line %d: %s", line
, buf
);
438 int addpfd(int fd
,voidfun cb
)
442 pfd
[npfd
].events
=POLLIN
|POLLHUP
;
453 for (i
=fn
;i
<npfd
-1;i
++) {
456 status
[i
]=status
[i
+1];
461 int pfdsearch(int fd
)
464 for (i
=0;i
<npfd
&& pfd
[i
].fd
!=fd
;i
++)
469 int setfds(fd_set
*rds
, fd_set
*exc
)
474 for (i
=0;i
<npfd
;i
++) {
475 FD_SET(pfd
[i
].fd
,rds
);
476 FD_SET(pfd
[i
].fd
,exc
);
477 if (pfd
[i
].fd
>max
) max
=pfd
[i
].fd
;
482 static void save_pidfile(void)
484 if(pidfile
[0] != '/')
485 strncat(pidfile_path
, pidfile
, _POSIX_PATH_MAX
- strlen(pidfile_path
));
487 strcpy(pidfile_path
, pidfile
);
489 int fd
= open(pidfile_path
,
490 O_WRONLY
| O_CREAT
| O_EXCL
,
491 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
495 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
499 if((f
= fdopen(fd
, "w")) == NULL
) {
500 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
504 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
505 printlog(LOG_ERR
, "Error in writing pidfile");
512 /* this custom version of daemon(3) continue to receive stderr messages
513 * until the end of the startup phase, the foreground process terminates
514 * when stderr gets closed*/
515 static int special_daemon(void)
532 while ((n
=read(errorpipe
[0],buf
,128)) > 0) {
533 write(STDERR_FILENO
,buf
,n
);
544 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
545 (void)dup2(fd
, STDIN_FILENO
);
546 (void)dup2(fd
, STDOUT_FILENO
);
547 (void)dup2(errorpipe
[1], STDERR_FILENO
);
555 int main(int argc
, char *argv
[])
566 int option_index
= 0;
568 static struct option long_options
[] = {
569 {"daemon", 0, 0, 'd'},
571 {"telnet", 0, 0, 't'},
575 {"nodename",1,0,'n'},
576 {"pidfile", 1, 0, 'p'},
579 c
= getopt_long_only (argc
, argv
, "hdwtM:f:n:",
580 long_options
, &option_index
);
589 conffile
=strdup(optarg
);
592 nodename
=strdup(optarg
);
604 pidfile
=strdup(optarg
);
607 usage(argv
[0]); //implies exit
611 if (optind
< argc
&& mgmt
==NULL
)
615 printlog(LOG_ERR
,"mgmt_socket not defined");
618 if (telnet
==0 && web
==0) {
619 printlog(LOG_ERR
,"at least one service option (-t -w) must be specified");
626 /* saves current path in pidfile_path, because otherwise with daemonize() we
628 if(getcwd(pidfile_path
, _POSIX_PATH_MAX
-1) == NULL
) {
629 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
632 strcat(pidfile_path
, "/");
634 /* call daemon before starting the stack otherwise the stack threads
635 * does not get inherited by the forked process */
636 if (daemonize
&& special_daemon()) {
637 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
641 lwipstack
=lwip_stack_new();
642 lwip_stack_set(lwipstack
);
644 vdefd
= openvdem(mgmt
, argv
[0], &nif
, nodename
);
646 /* If rcfile is specified, try it and nothing else */
647 if (conffile
&& readconffile(conffile
,nif
) < 0)
649 printlog(LOG_ERR
, "Error reading configuration file '%s': %s", conffile
, strerror(errno
));
652 /* Else try default ones */
656 char *homedir
= getenv("HOME");
659 int len
= strlen(homedir
) + strlen(USERCONFFILE
) + 1;
660 conffile
= malloc(len
);
661 snprintf(conffile
, len
, "%s%s", homedir
, USERCONFFILE
);
662 if ((rv
= readconffile(conffile
, nif
)) >= 0)
665 if (!homedir
|| rv
< 0)
666 rv
= readconffile(conffile
= ROOTCONFFILE
, nif
);
670 printlog(LOG_ERR
, "Error reading configuration file '%s': %s", conffile
, strerror(errno
));
675 /* once here, we're sure we're the true process which will continue as a
676 * server: save PID file if needed */
677 if(pidfile
) save_pidfile();
686 if ((fd
=open("/dev/null",O_RDWR
)) >= 0) {
687 close(STDERR_FILENO
);
688 dup2(fd
,STDERR_FILENO
);
690 openlog(basename(argv
[0]), LOG_PID
, 0);
693 printlog(LOG_INFO
,"VDETELWEB started");
700 int max
=setfds(&rds
,&exc
);
701 m
=lwip_select(max
,&rds
,NULL
,&exc
,NULL
);
702 for(i
=0; m
>0 && i
<max
; i
++) {
703 if (FD_ISSET(i
,&rds
) || FD_ISSET(i
,&exc
)) {