1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <unistd.h> 5#include <sys/types.h> 6#include <sys/stat.h> 7#include <fcntl.h> 8#include <errno.h> 9 10static void print_help() 11{ 12 printf("ebtablesu v"PROGVERSION" ("PROGDATE")\n"); 13 printf( 14"Usage:\n" 15"ebtablesu open table : copy the kernel table\n" 16"ebtablesu fopen table file : copy the table from the specified file\n" 17"ebtablesu free table : remove the table from memory\n" 18"ebtablesu commit table : commit the table to the kernel\n" 19"ebtablesu fcommit table file : commit the table to the specified file\n\n" 20"ebtablesu <ebtables options> : the ebtables specifications\n" 21" use spaces only to separate options and commands\n" 22"For the ebtables options, see\n# ebtables -h\nor\n# man ebtables\n" 23 ); 24} 25int main(int argc, char *argv[]) 26{ 27 char *arguments, *pos; 28 int i, writefd, len = 0; 29 30 if (argc > EBTD_ARGC_MAX) { 31 fprintf(stderr, "ebtablesd accepts at most %d arguments, %d " 32 "arguments were specified. If you need this many " 33 "arguments, recompile this tool with a higher value for" 34 " EBTD_ARGC_MAX.\n", EBTD_ARGC_MAX - 1, argc - 1); 35 return -1; 36 } else if (argc == 1) { 37 fprintf(stderr, "At least one argument is needed.\n"); 38 print_help(); 39 exit(0); 40 } 41 42 for (i = 0; i < argc; i++) 43 len += strlen(argv[i]); 44 /* Don't forget '\0' */ 45 len += argc; 46 if (len > EBTD_CMDLINE_MAXLN) { 47 fprintf(stderr, "ebtablesd has a maximum command line argument " 48 "length of %d, an argument length of %d was received. " 49 "If a smaller length is unfeasible, recompile this tool " 50 "with a higher value for EBTD_CMDLINE_MAXLN.\n", 51 EBTD_CMDLINE_MAXLN, len); 52 return -1; 53 } 54 55 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { 56 if (argc != 2) { 57 fprintf(stderr, "%s does not accept options.\n", argv[1]); 58 return -1; 59 } 60 print_help(); 61 exit(0); 62 } 63 64 if (!(arguments = (char *)malloc(len))) { 65 fprintf(stderr, "ebtablesu: out of memory.\n"); 66 return -1; 67 } 68 69 if ((writefd = open(EBTD_PIPE, O_WRONLY, 0)) == -1) { 70 fprintf(stderr, "Could not open the pipe, perhaps ebtablesd is " 71 "not running or you don't have write permission (try " 72 "running as root).\n"); 73 return -1; 74 } 75 76 pos = arguments; 77 for (i = 0; i < argc; i++) { 78 strcpy(pos, argv[i]); 79 pos += strlen(argv[i]); 80 *(pos++) = ' '; 81 } 82 83 *(pos-1) = '\n'; 84 if (write(writefd, arguments, len) == -1) { 85 perror("write"); 86 return -1; 87 } 88 return 0; 89} 90