pax_global_header00006660000000000000000000000064136272035520014517gustar00rootroot0000000000000052 comment=90d7bbdbc8eeeb9a92fc10038d2b64e7b90cd601 uml-utilities-20070815.4/000077500000000000000000000000001362720355200147375ustar00rootroot00000000000000uml-utilities-20070815.4/COPYING000066400000000000000000000431271362720355200160010ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. uml-utilities-20070815.4/CVS/000077500000000000000000000000001362720355200153725ustar00rootroot00000000000000uml-utilities-20070815.4/CVS/Entries000066400000000000000000000005651362720355200167340ustar00rootroot00000000000000/COPYING/1.1/Tue Oct 9 02:22:36 2001// D/gdbbot//// D/mconsole//// D/moo//// D/net//// D/redhat//// D/rpm//// D/test//// D/transformiix//// D/uml_net//// D/uml_router//// D/umn//// D/port-helper//// D/jailtest//// D/tunctl//// D/umlgdb//// D/watchdog//// D/jail//// D/honeypot//// /Makefile/1.13/Sat Feb 8 04:01:52 2003// /uml_util.spec.in/1.4/Wed Jan 14 09:56:31 2004// uml-utilities-20070815.4/CVS/Repository000066400000000000000000000000061362720355200174700ustar00rootroot00000000000000tools uml-utilities-20070815.4/CVS/Root000066400000000000000000000001031362720355200162320ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/Changelog000066400000000000000000000017101362720355200165500ustar00rootroot00000000000000BB - shebangs added where needed fixed the command line handling in honeypot.pl the jail Makefile installs things now jail_uml handles user names as well as uids and sets its group CFLAGS is now settable at the top level cleanups in uml_mconsole 64-bit fixes, fixing of broken 64-bit COW files, and other cleanups in uml_moo bug fix in uml_net's handling of a SLIP interface going down moved the uml_router directory to uml_switch cleanups in uml_switch added a spec file Me - BB's tarball contained a tools-$DATE with a tools subdirectory - the subdirectory had newer stuff, so I started with that. Cleaned up the honeypot.pl changes Spurious tab in jail/Makefile Ignored BB's redeclaration of jailtest main - I prefer correct declarations even when the arguments aren't used. Removed uml_util.spec, which is generated Added a lib directory which contains send_fd() Added umlfs and umlfs support to uml_mconsole From dgraves - mconsole int uml-utilities-20070815.4/Makefile000066400000000000000000000013351362720355200164010ustar00rootroot00000000000000TUNCTL = $(shell [ -e /usr/include/linux/if_tun.h ] && echo tunctl) SUBDIRS = lib jail jailtest humfsify mconsole moo port-helper $(TUNCTL) \ uml_net uml_switch watchdog umlfs UMLVER = $(shell date +%Y%m%d) TARBALL = uml_utilities_$(UMLVER).tar.bz2 BIN_DIR = /usr/bin LIB_DIR = /usr/lib/uml CFLAGS = -g -Wall #CFLAGS = -g -O2 -Wall export BIN_DIR LIB_DIR CFLAGS all install: set -e ; for dir in $(SUBDIRS); do $(MAKE) -C $$dir $@; done tarball : clean spec cd .. ; \ mv tools tools-$(UMLVER); \ tar cjf $(TARBALL) tools-$(UMLVER); \ mv tools-$(UMLVER) tools clean: rm -rf *~ set -e ; for dir in $(SUBDIRS); do $(MAKE) -C $$dir $@; done spec: sed -e 's/__UMLVER__/$(UMLVER)/' < uml_util.spec.in > uml_util.spec uml-utilities-20070815.4/gdb/000077500000000000000000000000001362720355200154735ustar00rootroot00000000000000uml-utilities-20070815.4/gdb/gdbinit000066400000000000000000000024151362720355200170400ustar00rootroot00000000000000#Give this a task like #uml-task-info current_task #it will dump info about it define uml-task-info set $tstate = "Unknown" if $arg0->state == 0 set $tstate = "R" end if $arg0->state == 1 set $tstate = "S" end if $arg0->state == 2 set $tstate = "D" end if $arg0->state == 4 set $tstate = "Z" end if $arg0->state == 8 set $tstate = "T" end printf "PID(ext): %d(%d)\tState: %s UID(E): %d(%d) \tCmd: %10s @ 0x%x\n", $arg0->pid, $arg0->thread.extern_pid, $tstate, \ $arg0->uid, $arg0->euid, $arg0->comm, $arg0 end #dump out the info for the files the task has open define uml-task-files set $files = $arg0->files set $max = $files->max_fds set $i = 0 if $max > 32 set $max = 32 end while $i < $max if $files->fd_array[$i] != 0 printf " File fd = %d Name: %s\n", $i, $files->fd_array[$i]->f_dentry.d_iname end set $i = $i + 1 end end #walk though the task list and print info about them define uml-tasks set $current_task = (struct task_struct *)cpu_tasks[0].task uml-task-info $current_task set $p = $current_task.next_task while $p != $current_task uml-task-info $p if $uml_task > 1 uml-task-files $p end set $p = $p->next_task end end #Vars depend on how much info we dump #$uml_task = (* ) always dump some info #$uml_task = (> 0) dump open files set $uml_task = 0 uml-utilities-20070815.4/gdbbot/000077500000000000000000000000001362720355200162005ustar00rootroot00000000000000uml-utilities-20070815.4/gdbbot/CVS/000077500000000000000000000000001362720355200166335ustar00rootroot00000000000000uml-utilities-20070815.4/gdbbot/CVS/Entries000066400000000000000000000000541362720355200201660ustar00rootroot00000000000000/gdbbot.pl/1.3/Thu Jul 5 19:18:58 2001// D uml-utilities-20070815.4/gdbbot/CVS/Repository000066400000000000000000000000151362720355200207310ustar00rootroot00000000000000tools/gdbbot uml-utilities-20070815.4/gdbbot/CVS/Root000066400000000000000000000001031362720355200174730ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/gdbbot/gdbbot.pl000066400000000000000000000045331362720355200200030ustar00rootroot00000000000000use IO::Pty; use IPC::Open2; use Net::IRC; use POSIX; use Fcntl; use strict; my $irc = new Net::IRC; my $pty = new IO::Pty; my $pid; my $key; my %trust = (); my $conn = $irc->newconn(Server => 'irc.openprojects.net', Port => 6667, Nick => 'gdbbot', Ircname => 'Vlad the Debugger', Username => 'gdb') or die "gdbbot: Can't connect to IRC server.\n"; sub start_gdb { my $cmd = shift; $pid = fork(); !defined($pid) and die "Couldn't fork : $!"; if($pid == 0){ POSIX::setsid() || warn "Couldn't perform setsid $!\n"; my $tty = $pty->slave(); my $name = $tty->ttyname(); # close($pty); close STDIN; close STDOUT; close STDERR; open(STDIN,"<&". $tty->fileno()) || die "Couldn't reopen ". $name ." for reading, $!"; open(STDOUT,">&". $tty->fileno()) || die "Couldn't reopen ". $name ." for writing, $!"; open(STDERR,">&". fileno(STDOUT)) || die "Couldn't redirect STDERR, $!"; exec ($cmd); die "Couldn't exec : $!"; } else { fcntl($pty, Fcntl::F_SETFL, Fcntl::O_NONBLOCK); print "gdb pid is $pid\n"; return $pty; } } sub on_connect { my $self = shift; print "Joining \#umldebug...\n"; $self->join("#umldebug"); $key = rand(1<<31); print "Your secret is $key\n"; my $cmd = "gdb linux"; my $pty = start_gdb($cmd); $irc->addfh($pty->fileno(), \&gdb_output, "rw"); } sub on_public { my ($self, $event) = @_; my @to = $event->to; my ($nick, $mynick) = ($event->nick, $self->nick); my ($arg) = ($event->args); $arg !~ /^gdbbot/ and return; $arg =~ s/^gdbbot:\s*//; if($arg =~ /\^C/){ kill 2, $pid; } elsif(defined($key) && ($arg eq $key)){ $self->me("#umldebug", "trusts $nick"); $trust{$nick} = 1; $key = undef; } elsif($trust{$nick} == 1){ if($arg =~ /trust (.*)/){ $trust{$1} = 1; $self->me("#umldebug", "trusts $1"); } else { print $pty "$arg\n"; } } else { $self->me("#umldebug", "doesn't trust $nick"); } } sub gdb_output { while(<$pty>){ $conn->privmsg("#umldebug", "$_"); } } sub on_msg { my ($self, $event) = @_; my $nick = $event->nick; $self->privmsg($nick, "Sorry, I don't understand messages yet."); } $conn->add_global_handler(376, \&on_connect); $conn->add_handler('msg', \&on_msg); $conn->add_handler('public', \&on_public); $irc->start; uml-utilities-20070815.4/honeypot/000077500000000000000000000000001362720355200166045ustar00rootroot00000000000000uml-utilities-20070815.4/honeypot/CVS/000077500000000000000000000000001362720355200172375ustar00rootroot00000000000000uml-utilities-20070815.4/honeypot/CVS/CVS/000077500000000000000000000000001362720355200176725ustar00rootroot00000000000000uml-utilities-20070815.4/honeypot/CVS/CVS/Entries000066400000000000000000000000021362720355200212160ustar00rootroot00000000000000D uml-utilities-20070815.4/honeypot/CVS/CVS/Repository000066400000000000000000000000231362720355200217670ustar00rootroot00000000000000tools/honeypot/CVS uml-utilities-20070815.4/honeypot/CVS/CVS/Root000066400000000000000000000001031362720355200205320ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/honeypot/CVS/Entries000066400000000000000000000002131362720355200205670ustar00rootroot00000000000000D/CVS//// /hppfs.pm/1.3/Wed Jan 22 20:40:26 2003// /honeypot.pl/1.6/Thu Jul 31 18:10:52 2003// /hppfslib.pm/1.4/Thu Jul 31 18:12:39 2003// uml-utilities-20070815.4/honeypot/CVS/Repository000066400000000000000000000000171362720355200213370ustar00rootroot00000000000000tools/honeypot uml-utilities-20070815.4/honeypot/CVS/Root000066400000000000000000000001031362720355200200770ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/honeypot/honeypot.pl000066400000000000000000000022511362720355200210060ustar00rootroot00000000000000#!/usr/bin/perl # # Copyright (C) 2002, 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # use hppfs; use hppfslib; use strict; my $dir; @ARGV and $dir = $ARGV[0] or die "Not enough arguments - pass dir where UML will be running"; # !mkdir $dir and warn "Couldn't create '$dir' : $!"; !mkdir "$dir/proc" and warn "Couldn't create '$dir/proc' : $!"; my $hppfs = hppfs->new($dir); my $remove_filesystems = remove_lines("hppfs", "hostfs"); # Need to be able to add directories, i.e. driver, bus/pci # partitions needs work # slabinfo if UML ever uses the slab cache for anything $hppfs->add("cmdline" => host_proc("cmdline"), "cpuinfo" => host_proc("cpuinfo"), "dma" => host_proc("dma"), "devices" => remove_lines("ubd"), "exitcode" => "remove", "filesystems" => $remove_filesystems, "interrupts" => host_proc("interrupts"), "iomem" => host_proc("iomem"), "ioports" => host_proc("ioports"), "mounts" => $remove_filesystems, "pid/mounts" => $remove_filesystems, "stat" => host_proc("stat"), "uptime" => host_proc("uptime"), "version" => host_proc("version"), dup_proc_dir("bus", $dir) ); $hppfs->handler(); uml-utilities-20070815.4/honeypot/hppfs.pm000066400000000000000000000050601362720355200202630ustar00rootroot00000000000000# # Copyright (C) 2002 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # package hppfs; use Socket; use IO::Select; use strict; sub new { my $class = shift; my $base = shift; !defined($base) and $base = "."; my $me = { files => { }, handles => { }, base => $base }; bless($me, $class); return($me); } sub add { my $me = shift; while(@_){ my $file = shift; my $handler = shift; $me->{files}->{$file} = $handler; } } sub prepare { my $me = shift; my $dir = shift; my $file = shift; my $full = $me->{base} . "/proc/$dir"; if(! -d $full){ unlink $full; my $out = `mkdir -p $full 2>&1`; $? and die "mkdir '$full' failed : $out"; } my $out = `chmod 755 $full 2>&1`; $? and die "chmod 755 $full failed : $out"; defined($file) and unlink "$full/$file"; return("$full/$file"); } sub setup_sock { my ($me, $file, undef, $mode) = @_; my $full = $me->prepare($file, $mode); my $sock = sockaddr_un($full); !defined($sock) and die "sockaddr_un of '$sock' failed : $!"; !defined(socket(my $fh, AF_UNIX, SOCK_STREAM, 0)) and die "socket failed : $!"; !defined(bind($fh, $sock)) and die "bind failed : $!"; my $out = `chmod 777 $full 2>&1`; $? ne 0 and die "'chmod 777 $full' failed : $!"; !defined(listen($fh, 5)) and die "listen failed : $!"; $me->{select}->add(\*$fh); $me->{handles}->{fileno(\*$fh)} = $file; } sub setup_remove { my ($me, $file) = @_; my $full = $me->prepare($file); my $out = `touch $full/remove 2>&1`; $? != 0 and die "touch $full/remove failed : $out"; } sub handler { my $me = shift; $me->{select} = IO::Select->new(); foreach my $file (keys(%{$me->{files}})){ my $handler = $me->{files}->{$file}; if(ref($handler) eq "ARRAY"){ $me->setup_sock($file, @$handler); } elsif($handler eq "remove"){ $me->setup_remove($file); } else { die "Bad handler for '$file'"; } } while(1){ my @ready = $me->{select}->can_read(); foreach my $sock (@ready){ my $file = $me->{handles}->{fileno($sock)}; !defined($file) and die "Couldn't map from socket to file"; !accept(CONN, $sock) and die "accept failed : $!"; my ($handler, $mode) = @{$me->{files}->{$file}}; (!defined($handler) || !defined($mode)) and die "Couldn't map from file to handler"; my $output; if($mode eq "rw"){ my $input = join("", ); $output = $handler->($input); } else { $output = $handler->(); } print CONN $output; close CONN; } } } 1; uml-utilities-20070815.4/honeypot/hppfslib.pm000066400000000000000000000022241362720355200207510ustar00rootroot00000000000000# # Copyright (C) 2002, 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # package hppfslib; use Exporter (); use vars qw(@ISA @EXPORT); use strict; @ISA = qw(Exporter); @EXPORT = qw(&remove_lines &host &host_proc &dup_proc_dir); sub remove_lines { my @remove = @_; return( [ sub { my $input = shift; foreach my $str (@remove){ $input =~ s/^.*$str.*\n//mg; } return($input) }, "rw" ] ); } sub host { my $file = shift; return( [ sub { return(`cat $file`); }, "r" ] ); } sub host_proc { my $file = shift; return(host("/proc/$file")); } sub dup_proc_dir { my $to = shift; my $root = shift; my $new = "$root/proc/$to"; -e $new and `rm -rf $new`; !mkdir $new and warn "Couldn't create '$new' : $!"; my @dirs = `cd /proc/$to ; find . -type d -print`; chomp @dirs; foreach my $dir (@dirs){ $dir eq "." and next; my $new_dir = "$new/$dir"; !mkdir $new_dir and warn "Couldn't create '$new_dir' : $!"; } my @files = `cd /proc ; find $to -type f -print`; chomp @files; return(map { $_ => host_proc($_) } @files); } 1; uml-utilities-20070815.4/humfsify/000077500000000000000000000000001362720355200165715ustar00rootroot00000000000000uml-utilities-20070815.4/humfsify/Makefile000066400000000000000000000001771362720355200202360ustar00rootroot00000000000000BIN = humfsify all: install: install -d $(DESTDIR)$(BIN_DIR) install -o 0755 $(BIN) $(DESTDIR)$(BIN_DIR) clean: rm -f *~ uml-utilities-20070815.4/humfsify/humfsify000066400000000000000000000234201362720355200203470ustar00rootroot00000000000000#!/usr/bin/perl use strict; my $VERSION = 2; if(@ARGV[0] eq "-r"){ unhumfsify(); } else { meta_fs(@ARGV); } exit 0; sub meta_fs { my ($user, $group, $size) = @_; if(!defined($user) || !defined($group) || !defined($size)){ print "Usage: $0 user group size\n"; exit(1); } my $save_user = $user; $user !~ /^[0-9]+$/ and $user = getpwnam($user); if(!defined($user)){ print "User '$save_user' not present in the password file\n"; exit(1); } my $save_group = $group; $group !~ /^[0-9]+$/ and $group = getgrnam($group); if(!defined($group)){ print "Group '$save_group' not present in the groups file\n"; exit(1); } my %mult = ( K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024 ); my $sizeb = $size; $sizeb =~ s/^([0-9]+)([KMG])$/$1*$mult{$2}/e; if ($sizeb !~ /^[0-9]+$/) { print "Bad filesystem size - '$size'\n"; exit(1); } # Do this here so we bomb out quickly if there are any problems writing # the superblock file open SUPER, "> superblock" || die "Couldn't open superblock : $!"; my $used = `du -sk data`; chomp $used; $used =~ s/^([0-9]+)\s*data$/$1*1024/e; if($used > $sizeb){ print "Current disk usage greater than the requested size - " . "$used vs $sizeb\n"; exit(1); } my $pwd = `pwd`; chomp $pwd; $pwd !~ /\/$/ and $pwd .= "/"; chdir "data"; if(!(open FILES, "find . | ")){ print "Couldn't start a find in 'data' - error = $!\n"; exit(1); } my %links = (); while(){ my $file = $_; chomp $file; my ($mode, $uid, $gid, $dev, $links, $inode, $fsdev) = (lstat($file))[2, 4, 5, 6, 3, 1, 0 ]; my $which = (! -l $file && -d $file ? "dir" : "file"); $mode &= 0777; my @segments = split(/\//, $file); my $last = pop @segments; my $dir = join("/", @segments); my $meta_dir = "${pwd}${which}_metadata/$dir"; my $meta = "$meta_dir/$last"; if($which eq "dir"){ my $out = `mkdir -p "${pwd}file_metadata/$dir/$last" 2>&1`; if($? != 0){ print "mkdir -p '${pwd}file_metadata/$dir/$last' failed, " . "exit status = $?, output = '$out'\n"; exit(1); } $meta_dir = $meta; $meta .= "/metadata"; } my $extra = ""; my $out = `mkdir -p "$meta_dir" 2>&1`; if($? != 0){ print "mkdir -p '$meta_dir' failed, exit status = $?, " . "output = '$out'\n"; exit(1); } chown $user, $group, $meta_dir; if($links > 1){ if(defined($links{"$inode $fsdev"})){ my $from = "${pwd}${which}_metadata/$file"; my $to = "${pwd}${which}_metadata/" . $links{"$inode $fsdev"}; !link $to, $from and warn "linking '$from' to '$to' failed - $!"; next; } else { $links{"$inode $fsdev"} = $file; } } if(-c $file || -b $file){ my $major = $dev >> 8; my $minor = $dev % 0x100; $extra = (-c $file) ? " c" : " b"; $extra .= " $major $minor"; } ! -l $file and chmod 0755, $file; chown_file($file, $user, $group); open META, ">$meta" or die "Can't open metadata file '$meta': $!"; print META "$mode $uid $gid$extra\n"; close META; chown $user, $group, $meta; } chdir $pwd; my $out = `find file_metadata -type d | xargs chown $user.$group 2>&1`; if($? != 0){ print "'find file_metadata -type d | xargs chown $user.$group' " . "failed, exit status = $?, output = '$out'\n"; exit(1); } print SUPER <){ my $line = $_; if($line =~ /^version\s+([0-9]+)$/){ $version = $1; } elsif($line =~ /used\s+([0-9]+)$/){ $used = $1; } elsif($line =~ /total\s+([0-9]+)$/){ $total = $1; } elsif($line =~ /metadata\s+([\w-]+)$/){ $metadata = $1; } } ($version == 1) && !defined($metadata) and $metadata = "shadow_fs"; !defined($version) and die "Bogus superblock - version not defined"; !defined($metadata) and die "Bogus superblock - metadata not defined"; !defined($used) and die "Bogus superblock - used not defined"; !defined($total) and die "Bogus superblock - total not defined"; $used > $total and die "Bogus superblock - used > total"; if($version == 1){ $metadata ne "shadow_fs" and die "Metadata format '$metadata' isn't supported by version 1"; unhumfsify_shadow_fs_v1(); } elsif($version == 2){ $metadata ne "shadow_fs" and die "Metadata format '$metadata' isn't supported by version 2"; unhumfsify_shadow_fs_v2(); } else { die "Unsupported version - $version"; } } sub validate_number { my $number = shift; my $name = shift; my $file = shift; $number =~ /[0-9]+/ and return(0); warn "Bogus $name ('$number') in $file"; return(1); } sub unhumfsify_shadow_fs_v1 { my $problems = 0; my $pwd = `pwd`; chomp $pwd; $pwd !~ /\/$/ and $pwd .= "/"; chdir "metadata"; if(!(open FILES, "find . | ")){ print "Couldn't start a find in 'data' - error = $!\n"; exit(1); } while(){ my $bad = 0; my $file = $_; chomp $file; $file =~ /\/metadata$/ and next; my $meta = "${pwd}metadata/$file"; if(-d $file){ $meta .= "/metadata"; } open META, "<$meta" or die "Can't open metadata file '$meta': $!"; my @fields = split(" ", ); my ($uid, $gid, $major, $type, $major, $minor) = @fields; close META; $bad += validate_number($uid, "uid", $meta); $bad += validate_number($gid, "gid", $meta); if(($type eq "c") || ($type eq "b")){ $bad += validate_number($major, "major", $meta); $bad += validate_number($minor, "minor", $meta); } elsif(defined($type) && ($type ne "s")){ warn "Bogus file type ('$type') in '$meta'"; $bad++; } $problems += $bad; $bad != 0 and next; my $data = "${pwd}/data/$file"; if(($type eq "c") || ($type eq "b")){ if(!unlink $data){ warn "Couldn't delete '$data' - $!"; $problems++; } my $out = `mknod $data $type $major $minor 2>&1`; if($? != 0){ warn "Couldn't 'mknod $data $type $major $minor' - " . "exit status = $?, output = '$out'\n"; $problems++; } } elsif($type eq "s"){ warn "Can't make Unix socket '$data' from perl"; $problems++; } else { my $out = `chown -h $uid.$gid $data 2>&1`; if($? != 0){ warn "Couldn't chown '$data' to $uid, $gid - " . "exit status $?, output - '$out'"; $problems++; } } } if($problems != 0){ print <&1`; $? != 0 and die "Couldn't remove the metadata directory - " . "exit status of rm = $?, output = '$out'"; } } sub chown_file { my $file = shift; my $uid = shift; my $gid = shift; my $out = `chown -h $uid.$gid $file 2>&1`; if($? != 0){ warn "Couldn't chown '$file' to $uid, $gid - exit status $?, " . "output - '$out'"; return(1); } return(0); } sub unhumfsify_shadow_fs_v2 { my $problems = 0; my $pwd = `pwd`; chomp $pwd; $pwd !~ /\/$/ and $pwd .= "/"; chdir "dir_metadata"; if(!(open FILES, "find . \! -type d | ")){ print "Couldn't start a find in 'data' - error = $!\n"; exit(1); } while(){ my $bad = 0; my $file = $_; chomp $file; open META, "<$file" or die "Can't open metadata file '$file': $!"; my @fields = split(" ", ); my ($mode, $uid, $gid) = @fields; close META; $bad += validate_number($mode, "mode", $file); $bad += validate_number($uid, "uid", $file); $bad += validate_number($gid, "gid", $file); $problems += $bad; $bad != 0 and next; my $data = `dirname ${pwd}/data/$file`; chomp $data; $problems += chown_file($data, $uid, $gid); $problems += chmod $data, $mode; } chdir "../file_metadata"; if(!(open FILES, "find . \! -type d | ")){ print "Couldn't start a find in 'data' - error = $!\n"; exit(1); } while(){ my $bad = 0; my $file = $_; chomp $file; my $meta = "${pwd}file_metadata/$file"; open META, "<$meta" or die "Can't open metadata file '$meta': $!"; my @fields = split(" ", ); my ($mode, $uid, $gid, $type, $major, $minor) = @fields; close META; $bad += validate_number($mode, "mode", $meta); $bad += validate_number($uid, "uid", $meta); $bad += validate_number($gid, "gid", $meta); if(($type eq "c") || ($type eq "b")){ $bad += validate_number($major, "major", $meta); $bad += validate_number($minor, "minor", $meta); } elsif(defined($type) && ($type ne "s")){ warn "Bogus file type ('$type') in '$meta'"; $bad++; } $problems += $bad; $bad != 0 and next; my $data = "${pwd}/data/$file"; if(($type eq "c") || ($type eq "b")){ if(!unlink $data){ warn "Couldn't delete '$data' - $!"; $problems++; } my $out = `mknod $data $type $major $minor 2>&1`; if($? != 0){ warn "Couldn't 'mknod $data $type $major $minor' - " . "exit status = $?, output = '$out'\n"; $problems++; } } elsif($type eq "s"){ warn "Can't make Unix socket '$data' from perl"; $problems++; } $problems += chown_file($data, $uid, $gid); ! -l $data and chmod $mode, $data; } if($problems != 0){ print <&1`; $? != 0 and die "Couldn't remove the metadata directories - " . "exit status of rm = $?, output = '$out'"; } } uml-utilities-20070815.4/jail/000077500000000000000000000000001362720355200156565ustar00rootroot00000000000000uml-utilities-20070815.4/jail/CVS/000077500000000000000000000000001362720355200163115ustar00rootroot00000000000000uml-utilities-20070815.4/jail/CVS/Entries000066400000000000000000000005771362720355200176560ustar00rootroot00000000000000/cell.tar/1.1/Fri Jun 14 01:08:18 2002// /jail_uml.c/1.2/Mon Jul 22 17:37:43 2002// /Makefile/1.3/Wed Jan 22 18:23:54 2003// /tty_log.pl/1.2/Wed Jan 29 18:14:40 2003// /tty_log.pm/1.3/Thu Jun 12 17:10:50 2003// /db_out.pm/1.1/Wed Nov 19 10:46:34 2003// /playlog.pl/1.4/Wed Nov 19 10:47:41 2003// /tty_out.pm/1.1/Wed Nov 19 10:46:28 2003// /jailer.pl/1.7/Thu Sep 25 20:43:37 2003// D uml-utilities-20070815.4/jail/CVS/Repository000066400000000000000000000000131362720355200204050ustar00rootroot00000000000000tools/jail uml-utilities-20070815.4/jail/CVS/Root000066400000000000000000000001031362720355200171510ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/jail/Makefile000066400000000000000000000004551362720355200173220ustar00rootroot00000000000000OBJS = jail_uml.o BIN = jail_uml CFLAGS ?= -g -Wall SBIN_DIR ?= /usr/sbin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) install : $(BIN) install -d $(DESTDIR)$(SBIN_DIR) install -s $(BIN) $(DESTDIR)$(SBIN_DIR) clean : rm -rf *~ $(BIN) $(OBJS) cell[0-9]* core* tty_log_cell* uml-utilities-20070815.4/jail/cell.tar000066400000000000000000000240001362720355200173010ustar00rootroot00000000000000./0040766000076400007640000000000007502240740010167 5ustar jdikejdike./dev/0040755000076400007640000000000007502176371010753 5ustar jdikejdike./dev/net/0040755000076400007640000000000007502224315011531 5ustar jdikejdike./dev/net/tun0020666000000000000000000000000007502176436013266 3ustar rootroot00000120000310./tmp/0040755000076400007640000000000007502227510010765 5ustar jdikejdikeuml-utilities-20070815.4/jail/db_out.pm000066400000000000000000000101031362720355200174630ustar00rootroot00000000000000# # Copyright (C) 2003 George Bakos, ISTS at Dartmouth College and # Jeff Dike (jdike@addtoit.com) # Licensed under the GPL # package db_out; use strict; INIT { my %options = ( dbh => undef, table => undef ); main::register_output("sql", sub { my $options = shift; check_option($options, \%options); }, sub { my $record = shift; output($record, \%options); }, 0, " -d enables logging to a database. You will need to pass additional parameters so that playlog knows how to connect to the database. The syntax is name=value,name=value,name=value, etc. Examples: perl playlog.pl -d host=loghost.my.org,db=uml,user=gbakos,pass=foobar,table=cell0_log tty_log_cell0 perl playlog.pl -f -d db=uml tty_log_cell0 The only mandatory parameter is: db - database name The following default values will be used if not otherwise specified: parameter default value description ---------- ----------------- --------------------------------------------- host localhost Hostname or IP address of database server port 3306 TCP port of the database server user current user name Database account name passwd '' (null) Password for authenticating with the database table ttylog Name of the database table to create and/or append NOTE: Database must already exist and the user must have appropriate rights. See http://www.mysql.com/doc for additional information. "); } sub check_option { my $options = shift; my $arg = shift; my $option = shift @$options; my $ret = 1; if($option eq "-d"){ my $db_args = shift @$options; ($arg->{dbh}, $arg->{table}) = db_intercon_connect($db_args); db_intercon_create($arg->{dbh}, $arg->{table}); } else { unshift @$options, $option; $ret = 0; } return($ret); } sub output { my $record = shift; my $arg = shift; db_intercon_append($record, $arg->{dbh}, $arg->{table}); } my $dbhost = "localhost"; my $dbport = 3306; my $dbuser = (getpwuid $>)[0]; my $dbpasswd = ""; my $dbtable = "ttylog"; sub db_intercon_connect { my $args = shift; my @db = split(",", $args); my %dbparams = (); print STDERR "Connecting to MySqld with params: @db:\t"; map { my ($key,$val) = split("=", $_) or Usage(); $dbparams{$key} = $val; } @db; !$dbparams{host} and $dbparams{host} = $dbhost; !$dbparams{port} and $dbparams{port} = $dbport; !$dbparams{user} and $dbparams{user} = $dbuser; !$dbparams{passwd} and $dbparams{passwd} = $dbpasswd; !$dbparams{table} and $dbparams{table} = $dbtable; require DBD::mysql; my $connect = "DBI:mysql:$dbparams{db}:$dbparams{host}:$dbparams{port}"; my $dbh = DBI->connect($connect, $dbparams{user}, $dbparams{pass}) or die "\nFatal: can't connect to database $!\n"; print STDERR "success\n"; return($dbh, $dbparams{table}); } sub db_intercon_create { my $dbh = shift; my $table = shift; $dbh->do(" CREATE TABLE IF NOT EXISTS $table ( string BLOB null, timeusecs BIGINT UNSIGNED not null, old_tty INT UNSIGNED null, tty INT UNSIGNED null, op TINYTEXT not null, direction TINYTEXT null ) ") or die "\nFatal: can't access/create table $!\n"; } sub db_intercon_append { my $op = shift; my $dbh = shift; my $table = shift; # Uncomment the following to send logging to STDERR as well as the database # foreach my $key(sort keys %$op){ # print STDERR "$key=$$op{$key},"; # } # print STDERR "\n"; my $string = $dbh->quote("$op->{string}"); my $secs = $op->{secs}; my $usecs = $op->{usecs}; my $old_tty = $dbh->quote("$op->{old_tty}"); my $tty = $dbh->quote("$op->{tty}"); my $direction = $dbh->quote("$op->{direction}"); my $op = $dbh->quote("$op->{op}"); my $timeusecs = (($secs * 1000000) + $usecs); $dbh->do("INSERT INTO $table(string, timeusecs, old_tty, tty, " . "op, direction) VALUES ($string, $timeusecs, $old_tty, $tty, $op, $direction)"); } 1; uml-utilities-20070815.4/jail/jail_uml.c000066400000000000000000000026711362720355200176240ustar00rootroot00000000000000/* jail a uml into a directory. */ #include #include #include #include #include #include #include static void Usage(void) { fprintf(stderr, "Usage : jail_uml jail-directory user " "uml-command-line ...\n"); fprintf(stderr, " or: jail_uml jail-directory uid " "uml-command-line ...\n\n"); fprintf(stderr, "If the user is not found, it's assumed to be a uid.\n"); exit(1); } int main(int argc, char **argv) { char *dir, *end; char *user; struct passwd *pw; int uid, gid=99; gid_t gidset[1]; gidset[0]=gid; if(geteuid() != 0){ fprintf(stderr, "jail_uml must be run as root\n"); exit(1); } if(argc < 3) Usage(); dir = argv[1]; user = argv[2]; // get users password information pw = getpwnam (user); if (pw == NULL){ uid = strtoul(argv[2], &end, 0); if(*end != '\0') Usage(); setgroups(1, gidset); } else { // try to init groups initgroups (pw->pw_name, pw->pw_gid); uid = pw->pw_uid; gid = pw->pw_gid; } // if(*end != '\0') Usage(); argc -= 3; argv += 3; if(chdir(dir)){ perror("chdir"); exit(1); } if(chroot(".")){ perror("chroot"); exit(1); } if(setgid(gid)){ perror("setgid"); exit(1); } if(setuid(uid)){ perror("setuid"); exit(1); } execv(argv[0], argv); fprintf(stderr, "execve of %s failed : ", argv[0]); perror(""); exit(1); } uml-utilities-20070815.4/jail/jailer.pl000066400000000000000000000161721362720355200174700ustar00rootroot00000000000000#!/usr/bin/perl use strict; use English; # Make sure things like ifconfig and route are accessible $ENV{PATH} .= ":/sbin:/usr/sbin"; sub Usage { print "Usage : jailer.pl uml-binary root-filesystem uid [ -n ] " . "[ -v ]\n"; print "\t[ -net 0.0.0.0 uml-ip -bridge device,device,... ]\n"; print "\t[ -net host-ip uml-ip] [ -tty-log ] [ -r cell ] [ -p ]\n"; print "[ more uml arguments ... ]\n"; print "Required arguments:\n"; print "\tuml-binary is the path of the UML 'linux' executable\n\n"; print "\troot-filesystem is the path of the filesystem that it will " . "boot on.\n"; print "\tThis will be copied into the jail, and the copy will be " . "booted\n\n"; print "\tuid is the user id that the UML will run as - an otherwise " . "unused uid\n"; print "\tis a good idea\n\n"; print "Optional arguments:\n"; print "\t-n prints its commands without executing them\n\n"; print "\t-v prints its commands as well as executing them\n\n"; print "\t-net configures a network - with a non-bridged configuration, " . "IP\n"; print "\taddresses for both ends of the TAP device are required. With \n"; print "\t-bridge, the new TAP device will be bridged with the devices\n"; print "\tspecified. The host-ip address will assigned to the bridge.\n"; print "\t-tty-log enables logging of tty traffic and process execs\n\n"; print "\t-r specifies a cell to reuse\n"; print "\t-p the cell will be persistent (not be removed after the UML\n"; print "\texits)\n"; print "Any further arguments will simply be appended to the UML command " . "line\n"; exit 1; } my ($uml, $rootfs, $uid, @uml_args) = @ARGV; my ($tap, $host_ip, $uml_ip); my @net_cmds = (); my $tty_log = 0; my $verbose = 0; my @bridge_devices = (); my $dry_run = 0; my $persistent = 0; my $reuse; while(1){ if($uml_args[0] eq "-net"){ (undef, $host_ip, $uml_ip, @uml_args) = @uml_args; (!defined($host_ip) || !defined($uml_ip)) and Usage(); push @net_cmds, ( "tunctl", "ifconfig", "route" ); } elsif($uml_args[0] eq "-bridge"){ shift @uml_args; push @bridge_devices, split(",", shift @uml_args); push @net_cmds, "brctl"; } elsif($uml_args[0] eq "-tty-log"){ shift @uml_args; $tty_log = 1; } elsif($uml_args[0] eq "-v"){ shift @uml_args; $verbose = 1; } elsif($uml_args[0] eq "-n"){ shift @uml_args; $dry_run = 1; } elsif($uml_args[0] eq "-r"){ (undef, $reuse, @uml_args) = @uml_args; ! -d $reuse and die "'$reuse' is not a directory"; } elsif($uml_args[0] eq "-p"){ shift @uml_args; $persistent = 1; } else { last; } } (!defined($uml) || !defined($rootfs) || !defined($uid)) and Usage(); my $status = run_output_status("ldd $uml"); $status == 0 and die "The UML binary should be statically linked - enable " . "CONFIG_STATIC_LINK in the UML build"; if(@bridge_devices && !defined($uml_ip)){ print "-bridge requires -net\n"; Usage(); } my @cmds = ("tar", "cp", "basename", "rm", @net_cmds ); !defined($reuse) and push @cmds, "mkdir"; my $sudo = ""; if($UID != 0){ $sudo = "sudo"; push @cmds, $sudo; } my @dont_have = (); foreach my $cmd (@cmds){ `which $cmd 2>&1 > /dev/null`; $? != 0 and push @dont_have, $cmd; } if(@dont_have){ print "Can't find the following utilities: " . join(" ", @dont_have) . "\n"; exit 1; } my $out; my @more_args = (); sub run_output_status { my $cmd = shift; $verbose || $dry_run and print "$cmd\n"; $dry_run and return("", 0); my $out = `$cmd 2>&1`; my $status = $?; $verbose and print "$out\n"; return($out, $status); } sub run_output { my $cmd = shift; my ($output, $status) = run_output_status($cmd); $status ne 0 and die "Running '$cmd' failed : output = '$out'"; return($output); } sub run_status { my $cmd = shift; my (undef, $status) = run_output_status($cmd); return($status); } sub run { my $cmd = shift; my ($output, $status) = run_output_status($cmd); $status ne 0 and die "Running '$cmd' failed : output = '$output'"; } if(defined($uml_ip)){ $out = run_output("tunctl -u $uid"); if($out =~ /(tap\d+)/){ $tap = $1; push @more_args, "eth0=tuntap,$tap"; my $tap_ifconfig = "$sudo ifconfig $tap up"; !@bridge_devices and $tap_ifconfig .= " $host_ip"; run($tap_ifconfig); if(@bridge_devices){ my @non_existant = map { run_status("ifconfig $_") ? $_ : () } @bridge_devices; my @bridges = map { run_status("sudo brctl showstp $_") ? () : $_ } @bridge_devices; @non_existant + @bridges != 1 and die "-bridge must specify one already-existing bridge or " . "non-existant device"; my $create = @non_existant; my $bridge = shift @{ [@non_existant, @bridges] }; if($create){ run("sudo brctl addbr $bridge"); run("sudo brctl stp $bridge off"); } map { if($_ ne $bridge){ run("sudo brctl addif $bridge $_"); run("sudo ifconfig $_ 0.0.0.0 promisc up"); } } (@bridge_devices, $tap); run("sudo ifconfig $bridge $host_ip"); } else { defined($host_ip) and run("$sudo route add -host $uml_ip dev $tap"); run("$sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'"); } } else { die "Couldn't find tap device name in '$out'"; } } if($tty_log == 1){ push @more_args, "tty_log_fd=3" } push @more_args, "uml_dir=/tmp"; my $cell; if(!defined($reuse)){ my $n = 0; while(-e "cell$n"){ $n++; } $cell = "cell$n"; } else { $cell = $reuse; } print "New inmate assigned to '$cell'\n"; print " UML image : $uml\n"; print " Root filesystem : $rootfs\n"; if(defined($tap)){ if(defined($host_ip)){ print " Network : routing through $tap, host = $host_ip, " . "uml = $uml_ip\n"; } else { print " Network : bridging $tap, uml = $uml_ip\n"; } } else { print " No network configured\n"; } if($tty_log == 1){ print " TTY logging to tty_log_$cell\n"; push @more_args, "3>tty_log_$cell"; } print " Extra arguments : '" . join(" ", @uml_args) . "'\n"; print "\n"; if(!defined($reuse)){ run("mkdir $cell"); run("chmod 755 $cell"); my $cell_tar = "cell.tar"; run("cd $cell ; $sudo tar xpf ../$cell_tar"); run("$sudo chown $uid $cell/tmp"); run("$sudo chmod 777 $cell/tmp"); print "Copying '$uml' and '$rootfs' to '$cell'..."; run("cp $uml $rootfs $cell"); print "done\n\n"; if(-e "/proc/mm"){ run("mkdir $cell/proc"); run("chmod 755 $cell/proc"); run("touch $cell/proc/mm"); run("$sudo mount --bind /proc/mm $cell/proc/mm"); } } $uml = `basename $uml`; chomp $uml; $rootfs = `basename $rootfs`; chomp $rootfs; run("$sudo chmod 666 $cell/$rootfs"); run("$sudo chmod 755 $cell/$uml"); my @args = ( "bash", "-c", "./jail_uml $cell $uid /$uml ubd0=/$rootfs " . join(" ", @uml_args, @more_args) ); $sudo ne "" and unshift @args, $sudo; if(!$dry_run){ system @args; } else { print join(" ", @args) . "\n"; } if(!$persistent){ -e "$cell/proc/mm" and run("$sudo umount $cell/proc/mm"); run("$sudo rm -rf $cell"); } if(defined($tap)){ run("$sudo ifconfig $tap down"); run("$sudo tunctl -d $tap"); } exit 0; uml-utilities-20070815.4/jail/playlog.pl000066400000000000000000000047741362720355200176760ustar00rootroot00000000000000# # Copyright (C) 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # # Translated from playlog.py, by Upi Tamminen my @option_handlers; use tty_log; use tty_out; use db_out; use IO::Handle; use strict; my $usage_string = "Usage : perl playlog.pl [ options ] log-file [tty-id] -f - follow the log, similar to 'tail -f' "; sub Usage { print $usage_string; foreach my $handler (@option_handlers){ print $handler->{usage}; } exit(1); } sub register_output { my $name = shift; my $option_proc = shift; my $output_proc = shift; my $one_tty = shift; my $usage = shift; push @option_handlers, { active => 0, name => $name, option => $option_proc, output => $output_proc, one_tty => $one_tty, usage => $usage }; } my $follow = 0; LOOP: while(@ARGV){ my $arg = shift @ARGV; if($arg eq "-f"){ $follow = 1; } else { unshift @ARGV, $arg; foreach my $handler (@option_handlers){ if($handler->{option}(\@ARGV)){ $handler->{active} = 1; next LOOP; } } last; } } !@ARGV and Usage(); if(!map { $_->{active} ? 1 : () } @option_handlers){ my @tty_out = map { $_->{name} eq "tty" ? $_ : () } @option_handlers; !@tty_out and die "No output handlers active and no tty output handler defined"; $tty_out[0]->{active} = 1; } my $file = shift @ARGV; @ARGV > 1 and Usage(); my $tty_id; @ARGV and my $tty_id = $ARGV[0]; open FILE, "<$file" or die "Couldn't open $file : $!"; binmode(FILE); my @ops = (); while(1){ my $op = read_log_line($file, \*FILE, 0); !defined($op) and last; push @ops, $op; } my @ttys = map { $_->{op} eq "open" && (($_->{old_tty} == 0) || ($_->{old_tty} == $_->{tty})) ? $_->{tty} : () } @ops; my %unique_ttys = (); foreach my $tty (@ttys){ $unique_ttys{$tty} = 1; } @ttys = keys(%unique_ttys); my @need_tty = map { $_->{active} && $_->{one_tty} ? $_->{name} : () } @option_handlers; if((@ttys > 1) && !defined($tty_id) && @need_tty){ print join(" and ", @need_tty) . " output(s) need a tty id to follow\n"; print "You have the following ttys to choose from:\n"; print join(" ", @ttys) . "\n"; exit(0); } !defined($tty_id) and $tty_id = $ttys[0]; foreach my $op (@ops){ foreach my $handler (@option_handlers){ if($handler->{active} && (!$handler->{one_tty} || ($op->{tty} == $tty_id))){ $handler->{output}->($op); } } } !$follow and exit 0; while(1){ my $op = read_log_line($file, \*FILE, 1); } uml-utilities-20070815.4/jail/tty_log.pl000066400000000000000000000014451362720355200177000ustar00rootroot00000000000000# # Copyright (C) 2002, 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # use tty_log; use strict; !@ARGV and die "Usage : perl tty_log.pl log-file"; my $file = $ARGV[0]; my @ops = read_log($file); foreach my $op (@ops){ if($op->{op} eq "open"){ printf("Opening new tty 0x%x from tty 0x%x\n", $op->{tty}, $op->{old_tty}); } elsif($op->{op} eq "close"){ printf("Closing tty 0x%x\n", $op->{tty}); } elsif($op->{op} eq "write"){ if($op->{direction} eq "read"){ printf("Read from tty 0x%x - '%s'\n", $op->{tty}, $op->{string}); } elsif($op->{direction} eq "write"){ printf("Write to tty 0x%x - '%s'\n", $op->{tty}, $op->{string}); } else { die "Bad direction - '$op->{direction}'"; } } else { die "Bad op - " . $op->{op}; } } uml-utilities-20070815.4/jail/tty_log.pm000066400000000000000000000050051362720355200176750ustar00rootroot00000000000000# # Copyright (C) 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # package tty_log; use File::stat; use Time::HiRes qw(usleep); use Exporter (); use vars qw(@ISA @EXPORT); use strict; @ISA = qw(Exporter); @EXPORT = qw(&read_log &read_log_line); my $TTY_LOG_OPEN = 1; my $TTY_LOG_CLOSE = 2; my $TTY_LOG_WRITE = 3; my $TTY_LOG_EXEC = 4; my $TTY_READ = 1; my $TTY_WRITE = 2; my %op_names = ($TTY_READ => "read", $TTY_WRITE => "write"); sub read_log_item { my $filename = shift; my $handle = shift; my $len = shift; my $wait = shift; my $offset = 0; my $data; while(1){ my $size = stat($filename)->size(); my $n = sysread($handle, $data, $len, $offset); $n == $len and return($data); if(!$wait){ !defined($n) and die "read failed - $!"; $n == 0 and return(undef); die "Short file - expected $len bytes, got $n"; } $offset += $n; while(1){ usleep(100 * 1000); my $new_size = stat($filename)->size(); $new_size != $size and last; } } } sub read_log_line { my $filename = shift; my $handle = shift; my $wait = shift; my $record_len = length(pack("iIiiII", "0" x 6)); my $record = read_log_item($filename, $handle, $record_len, $wait); !defined($record) and return(undef); my ($op, $tty, $len, $direction, $sec, $usecs) = unpack("iIiiIIa*", $record); my $data; $len != 0 and $data = read_log_item($filename, $handle, $len, $wait); if($op == $TTY_LOG_OPEN){ my ($old_tty) = unpack("I", $data); return( { op => "open", tty => $tty, old_tty => $old_tty, secs => $sec, usecs => $usecs } ); } elsif($op == $TTY_LOG_CLOSE){ return( { op => "close", tty => $tty, secs => $sec, usecs => $usecs } ); } elsif($op == $TTY_LOG_WRITE){ my $op_name = $op_names{$direction}; !defined($op_name) and die "Bad direction - '$direction'"; return( { op => "write", tty => $tty, string => $data, direction => $op_name, secs => $sec, usecs => $usecs } ); } elsif($op == $TTY_LOG_EXEC){ my @cmd = split("\0", $data); my $string = join(" ", @cmd); return( { op => "exec", tty => $tty, string => $string, secs => $sec, usecs => $usecs } ); } else { die "Bad tty_log op - $op"; } } sub read_log { my $file = shift; open FILE, "<$file" or die "Couldn't open $file : $!"; binmode(FILE); my @ops = (); while(1){ my $op = read_log_line($file, \*FILE, 0); !defined($op) and last; push @ops, $op; } close FILE; return(@ops); } 1; uml-utilities-20070815.4/jail/tty_out.pm000066400000000000000000000051361362720355200177300ustar00rootroot00000000000000# # Copyright (C) 2003 Jeff Dike (jdike@karaya.com) # Licensed under the GPL # package tty_out; use Time::HiRes qw(usleep); use strict; INIT { my %options = ( fast => 0, all => 0, execs => 0, first_record => 1, last_time => undef ); main::register_output("tty", sub { my $options = shift; check_option($options, \%options); }, sub { my $record = shift; output($record, \%options); }, 1, " -n - full-speed playback, without mimicing the original timing -a - all traffic, including both tty reads and writes -e - print out logged execs By default, playlog will retain the original timing in the log. -n will just dump the log out without that timing. Also by default, only tty writes will be output. This will provide an authentic view of what the original user saw, but it will omit non-echoed characters, such as passwords. -a will output ttys reads as well, but this has the side-effect of duplicating all normal, echoed, user input. The -e option prints out logged execs. This option exists because it's possible to run commands on a system without anything allocating a terminal. In this situation, tty logging is useless because no data flows through a terminal. However, execs will be logged, and the -e switch will print them out, allowing you to see everything that an intruder did without a terminal. "); } sub check_option { my $options = shift; my $arg = shift; my $option = shift @$options; my $ret = 1; if($option eq "-n"){ $arg->{fast} = 1; } elsif($option eq "-a"){ $arg->{all} = 1; } elsif($option eq "-e"){ $arg->{execs} = 1; } else { unshift @$options, $option; $ret = 0; } return($ret); } sub output { my $record = shift; my $arg = shift; if($arg->{first_record}){ STDOUT->autoflush(1); $arg->{first_record} = 0; } !defined($arg->{last_time}) and $arg->{last_time} = $record->{secs} * 1000 * 1000 + $record->{usecs}; my $next = $record->{secs} * 1000 * 1000 + $record->{usecs}; !$arg->{fast} and usleep($next - $arg->{last_time}); print_op($record, $arg, $arg); $arg->{last_time} = $next; } sub print_op { my $op = shift; my $arg = shift; if(($op->{op} eq "exec") && !$arg->{execs}){ return; } elsif($arg->{execs} and ($op->{op} ne "exec")){ return; } elsif(($op->{op} eq "open") || ($op->{op} eq "close")){ return; } elsif($op->{op} eq "write"){ ($op->{direction} ne "write") && !$arg->{all} and return; } print $op->{string}; $op->{op} eq "exec" and print "\n"; } 1; uml-utilities-20070815.4/jailtest/000077500000000000000000000000001362720355200165565ustar00rootroot00000000000000uml-utilities-20070815.4/jailtest/CVS/000077500000000000000000000000001362720355200172115ustar00rootroot00000000000000uml-utilities-20070815.4/jailtest/CVS/CVS/000077500000000000000000000000001362720355200176445ustar00rootroot00000000000000uml-utilities-20070815.4/jailtest/CVS/CVS/Entries000066400000000000000000000000021362720355200211700ustar00rootroot00000000000000D uml-utilities-20070815.4/jailtest/CVS/CVS/Repository000066400000000000000000000000231362720355200217410ustar00rootroot00000000000000tools/jailtest/CVS uml-utilities-20070815.4/jailtest/CVS/CVS/Root000066400000000000000000000001031362720355200205040ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/jailtest/CVS/Entries000066400000000000000000000001361362720355200205450ustar00rootroot00000000000000D/CVS//// /jailtest.c/1.1/Fri Jan 25 19:59:48 2002// /Makefile/1.2/Sat Feb 8 04:10:27 2003// uml-utilities-20070815.4/jailtest/CVS/Repository000066400000000000000000000000171362720355200213110ustar00rootroot00000000000000tools/jailtest uml-utilities-20070815.4/jailtest/CVS/Root000066400000000000000000000001031362720355200200510ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/jailtest/Makefile000066400000000000000000000004121362720355200202130ustar00rootroot00000000000000OBJS = jailtest.o BIN = jailtest CFLAGS ?= -g -Wall BIN_DIR ?= /usr/bin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/jailtest/jailtest.c000066400000000000000000000027321362720355200205450ustar00rootroot00000000000000#include #include #include #include #include #include #include #define KERNEL_ADDRESS ((unsigned long *) (0xa0800000)) int main(int argc, char **argv) { int pid, status, fd, n; printf("Trying to directly write %p...", KERNEL_ADDRESS); fflush(stdout); if((pid = fork()) != 0){ if(waitpid(pid, &status, 0) < 0){ perror("waitpid"); exit(1); } if(!WIFSIGNALED(status) || (WTERMSIG(status) != SIGSEGV)) printf("No segv (status = 0x%x) - BAD!\n", status); else printf("Segv - GOOD\n"); } else { *KERNEL_ADDRESS = 0xdeadbeef; exit(1); } printf("Trying to write %p via read...", KERNEL_ADDRESS); if((fd = open("/etc/passwd", O_RDONLY)) < 0){ perror("open"); exit(1); } n = read(fd, (char *) KERNEL_ADDRESS, sizeof(*KERNEL_ADDRESS)); if((n > 0) || (errno != EFAULT)) printf("Didn't return EFAULT - BAD!\n"); else printf("Returned EFAULT - GOOD\n"); printf("Trying to open /dev/mem..."); fd = open("/dev/mem", O_WRONLY); if((fd >= 0) || (errno != EPERM)) printf("Didn't fail with EPERM (fd = %d, errno = %d) - BAD!\n", fd, errno); else printf("Failed with EPERM - GOOD\n"); printf("Trying to open /dev/kmem..."); fd = open("/dev/kmem", O_WRONLY); if((fd >= 0) || (errno != EPERM)) printf("Didn't fail with EPERM (fd = %d, errno = %d) - BAD!\n", fd, errno); else printf("Failed with EPERM - GOOD\n"); return(0); } uml-utilities-20070815.4/lib/000077500000000000000000000000001362720355200155055ustar00rootroot00000000000000uml-utilities-20070815.4/lib/Makefile000066400000000000000000000002121362720355200171400ustar00rootroot00000000000000LIB = libuml.a OBJS = rights.o CFLAGS = -g -Wall all : $(LIB) $(LIB) : $(OBJS) $(AR) r $@ $< clean : rm -f $(LIB) $(OBJS) install : uml-utilities-20070815.4/lib/rights.c000066400000000000000000000022401362720355200171470ustar00rootroot00000000000000#include #include #include /* pass the fd over an fd that is already connected to a socket */ int send_fd(int fd, int target, struct sockaddr *to, int to_len, void *msg, int msg_len) { /* File descriptors are specific to a process and normally only sharable with another process by inheritence with fork(). The alternative is to use sendmsg with a special flag that says "I'm knowingly giving another process information from my private file descriptor table" (SCM_RIGHTS) */ char anc[CMSG_SPACE(sizeof(fd))]; struct msghdr hdr; struct cmsghdr *cmsg; struct iovec iov; int *fd_ptr; hdr.msg_name = to; hdr.msg_namelen = to_len; iov = ((struct iovec) { iov_base : msg, iov_len : msg_len }); hdr.msg_iov = &iov; hdr.msg_iovlen = 1; hdr.msg_flags = 0; hdr.msg_control = anc; hdr.msg_controllen = sizeof(anc); cmsg = CMSG_FIRSTHDR(&hdr); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); fd_ptr = (int *) CMSG_DATA(cmsg); *fd_ptr = fd; hdr.msg_controllen = cmsg->cmsg_len; if(sendmsg(target, &hdr, 0) < 0) return -errno; return 0; } uml-utilities-20070815.4/mconsole/000077500000000000000000000000001362720355200165565ustar00rootroot00000000000000uml-utilities-20070815.4/mconsole/.cvsignore000066400000000000000000000000151362720355200205520ustar00rootroot00000000000000uml_mconsole uml-utilities-20070815.4/mconsole/CVS/000077500000000000000000000000001362720355200172115ustar00rootroot00000000000000uml-utilities-20070815.4/mconsole/CVS/Entries000066400000000000000000000002601362720355200205430ustar00rootroot00000000000000/.cvsignore/1.1/Fri Jan 11 05:21:31 2002// /notify.pl/1.1/Tue Feb 4 20:11:39 2003// /Makefile/1.6/Sat Feb 8 04:10:08 2003// /uml_mconsole.c/1.14/Fri Aug 29 20:47:14 2003// D uml-utilities-20070815.4/mconsole/CVS/Repository000066400000000000000000000000171362720355200213110ustar00rootroot00000000000000tools/mconsole uml-utilities-20070815.4/mconsole/CVS/Root000066400000000000000000000001031362720355200200510ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/mconsole/Makefile000066400000000000000000000004661362720355200202240ustar00rootroot00000000000000BIN = uml_mconsole OBJS = $(BIN).o CFLAGS ?= -g -Wall LIBS = ../lib/libuml.a -lreadline BIN_DIR ?= /usr/bin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) $(LIBS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/mconsole/notify.pl000066400000000000000000000023031362720355200204210ustar00rootroot00000000000000use Socket; use strict; my $MCONSOLE_SOCKET = 0; my $MCONSOLE_PANIC = 1; my $MCONSOLE_HANG = 2; my $MCONSOLE_USER_NOTIFY = 3; my @types = ( "socket", "panic", "hang", "user notification" ); !defined(socket(SOCK, AF_UNIX, SOCK_DGRAM, 0)) and die "socket() failed : $!\n"; # XXX the -u causes mktemp to unlink the file - this is needed because # we don't want a normal file, we want a socket. my $file = `mktemp -u /tmp/mconsole.XXXXXX`; $? != 0 and die "mktemp failed with status $?\n"; chomp $file; print "The notification socket is '$file'\n"; !defined(bind(SOCK, sockaddr_un($file))) and die "binding '$file' failed : $!\n"; print "Run UML with\n\tmconsole=notify:$file\non the command line.\n"; print "Then, inside UML, write messages into /proc/mconsole\n"; while(1){ my $data; !defined(recv(SOCK, $data, 4096, 0)) and die "recv from '$file' failed : $!"; my ($magic, $version, $type, $len, $message) = unpack("LiiiA*", $data); print "Received message -\n"; printf "\tmagic = 0x%x\n", $magic; print "\tversion = $version\n"; print "\tmessage type = $type (" . $types[$type] . ")\n"; print "\tmessage length = $len\n"; print "\tmessage = '$message'\n\n"; } uml-utilities-20070815.4/mconsole/uml_mconsole.c000066400000000000000000000313141362720355200214200ustar00rootroot00000000000000/* (c) Copyright 2001-2004 Jeff Dike and others * Licensed under the GPL, see file COPYING * * This is uml_console version 2, a tool for querying a User Mode Linux * instance over a local pipe connection. This tool needs to be in sync * with the version of the UML kernel. * * There are a very few local commands that this program knows * about, but by default everything gets processed by UML. * * The uml_mconsole documentation distributed with covers all mconsole * commands, so the docs have to be kept in sync with the kernel. * In future it should be possible for the docs to come from (or be * in common with) something over in the kernel source. * * If you are looking for the command implementation, go to the * files mconsole_kern.c in the Linux kernel source under arch/um. * * The program exits with error values of: * * 0 No error * 1 Error (need better breakdown of error type in future) * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static char uml_name[11]; static struct sockaddr_un sun; static long uml_pid; static int do_switch(char *dir, char *file, char *name) { struct stat buf; char pid_path[MAXPATHLEN + 1]; FILE *fp; char pid[sizeof("12345\0")], *end; int n, x = -1; if(stat(file, &buf) == -1){ fprintf(stderr, "Warning: couldn't stat file: %s - ", file); perror(""); return 1; } sun.sun_family = AF_UNIX; strncpy(sun.sun_path, file, sizeof(sun.sun_path)); strncpy(uml_name, name, sizeof(uml_name)); /* Open and read PID file */ uml_pid = -1; snprintf(pid_path, sizeof(pid_path), "%s/%s/pid", dir, name); fp = fopen(pid_path, "r"); if(fp == NULL) return 0; while (!feof(fp) && !ferror(fp)) fread(&pid[++x], sizeof(char), 1, fp); /* Convert read PID to number, or set PID to known error number */ if (feof(fp) && !ferror(fp)){ n = strtol(pid, &end, 10); if(end != pid) uml_pid = n; } return 0; } static int switch_common(char *name) { char file[MAXPATHLEN + 1], dir[MAXPATHLEN + 1], tmp[MAXPATHLEN + 1]; char *home; int try_file = 1; if((home = getenv("HOME")) != NULL){ snprintf(dir, sizeof(dir), "%s/.uml", home); snprintf(file, sizeof(file), "%s/%s/mconsole", dir, name); if(strncmp(name, dir, strlen(dir))){ if(!do_switch(dir, file, name)) return(0); try_file = 0; } } snprintf(dir, sizeof(dir), "/tmp/uml/%s", name); snprintf(tmp, sizeof(tmp), "/tmp/uml/%s/mconsole", name); if(strncmp(name, "/tmp/uml/", strlen("/tmp/uml/"))){ if(!do_switch(dir, tmp, name)) return 0; } snprintf(dir, sizeof(dir), "./"); if(!do_switch(dir, name, name)) return 0; if(!try_file) return -1; return do_switch(dir, file, name); } #define MCONSOLE_MAGIC (0xcafebabe) #define MCONSOLE_MAX_DATA (512) #define MCONSOLE_VERSION (2) struct mconsole_request { uint32_t magic; uint32_t version; uint32_t len; char data[MCONSOLE_MAX_DATA]; }; struct mconsole_reply { uint32_t err; uint32_t more; uint32_t len; char data[MCONSOLE_MAX_DATA]; }; static char *absolutize(char *to, size_t size, char *from) { char save_cwd[MAXPATHLEN + 1], *slash; size_t remaining; if(getcwd(save_cwd, sizeof(save_cwd)) == NULL) { perror("absolutize : unable to get cwd"); return(NULL); } slash = strrchr(from, '/'); if(slash != NULL){ *slash = '\0'; if(chdir(from)){ fprintf(stderr, "absolutize : Can't cd to '%s', ", from); *slash = '/'; perror(""); return(NULL); } *slash = '/'; if(getcwd(to, size) == NULL){ fprintf(stderr, "absolutize : unable to get cwd of '%s'", from); perror(""); return(NULL); } remaining = size - strlen(to); if(strlen(slash) + 1 > remaining){ fprintf(stderr, "absolutize : unable to fit '%s' into %zd chars\n", from, size); return(NULL); } strcat(to, slash); } else { if(strlen(save_cwd) + 1 + strlen(from) + 1 > size){ fprintf(stderr, "absolutize : unable to fit '%s' into %zd chars\n", from, size); return(NULL); } strcpy(to, save_cwd); strcat(to, "/"); strcat(to, from); } chdir(save_cwd); return(to); } static int fix_filenames(char **cmd_ptr) { char *cow_file, *backing_file, *equal, *new, *ptr = *cmd_ptr; char full_backing[MAXPATHLEN + 1], full_cow[MAXPATHLEN + 1]; int len; if(strncmp(ptr, "config", strlen("config"))) return(0); ptr += strlen("config"); while(isspace(*ptr) && (*ptr != '\0')) ptr++; if(*ptr == '\0') return(0); if(strncmp(ptr, "ubd", strlen("ubd"))) return(0); while((*ptr != '=') && (*ptr != '\0')) ptr++; if(*ptr == '\0') return(0); equal = ptr; cow_file = ptr + 1; while((*ptr != ',') && (*ptr != '\0')) ptr++; if(*ptr == '\0'){ backing_file = cow_file; cow_file = NULL; } else { *ptr = '\0'; backing_file = ptr + 1; } ptr = absolutize(full_backing, sizeof(full_backing), backing_file); backing_file = ptr ? ptr : backing_file; if(cow_file != NULL){ ptr = absolutize(full_cow, sizeof(full_cow), cow_file); cow_file = ptr ? ptr : cow_file; } len = equal - *cmd_ptr; len += strlen("=") + strlen(backing_file) + 1; if(cow_file != NULL){ len += strlen(",") + strlen(cow_file); } new = malloc(len * sizeof(char)); if(new == NULL) return(0); strncpy(new, *cmd_ptr, equal - *cmd_ptr); ptr = new + (equal - *cmd_ptr); *ptr++ = '='; if(cow_file != NULL){ sprintf(ptr, "%s,", cow_file); ptr += strlen(ptr); } strcpy(ptr, backing_file); *cmd_ptr = new; return(1); } static int rcv_output(int fd, char **output) { struct mconsole_reply reply; int n, err = 0, first = 1, len = 0; do { n = recvfrom(fd, &reply, sizeof(reply), 0, NULL, 0); if(n < 0){ perror("recvmsg"); return(1); } err = reply.err ? 1 : err; *output = realloc(*output, len + strlen(reply.data) + 1); if(*output == NULL){ perror("realloc failed"); return 1; } if(first) **output = '\0'; strcat(*output, reply.data); len += strlen(reply.data); first = 0; } while(reply.more); return reply.err; } static void format_cmd(char *command, struct mconsole_request *request) { *request = ((struct mconsole_request) { .magic = MCONSOLE_MAGIC, .version = MCONSOLE_VERSION, .len = MIN(strlen(command), sizeof(request->data) - 1) }); strncpy(request->data, command, request->len); request->data[request->len] = '\0'; } static int send_cmd(int fd, char *command, char **output) { struct mconsole_request request; format_cmd(command, &request); if(sendto(fd, &request, sizeof(request), 0, (struct sockaddr *) &sun, sizeof(sun)) < 0){ fprintf(stderr, "Sending command to '%s' : ", sun.sun_path); perror(""); return 1; } return rcv_output(fd, output); } static int default_cmd(int fd, char *command) { char name[128], *output = NULL; int free_command, err; if((sscanf(command, "%128[^: \f\n\r\t\v]:", name) == 1) && (*(name + 1) == ':')){ if(switch_common(name)) return(1); command = strchr(command, ':'); *command++ = '\0'; while(isspace(*command)) command++; } free_command = fix_filenames(&command); err = send_cmd(fd, command, &output); if(output != NULL){ printf("%s %s\n", err ? "ERR" : "OK", output); free(output); } if(free_command) free(command); return err; } char *local_help = "Additional local mconsole commands:\n\ quit - Quit mconsole\n\ switch - Switch control to the given machine\n\ log -f - use contents of as UML log messages\n\ mconsole-version - version of this mconsole program\n\ int - Interrupt UML session\n"; static int help_cmd(int fd, char *command) { default_cmd(fd, command); printf("%s", local_help); return(0); } static int switch_cmd(int fd, char *command) { char *ptr; ptr = &command[strlen("switch")]; while(isspace(*ptr)) ptr++; if(switch_common(ptr)) return(1); printf("Switched to '%s'\n", ptr); return(0); } static int log_cmd(int fd, char *command) { int len, max, chunk, input_fd, newline = 0; char *ptr, buf[sizeof(((struct mconsole_request *) NULL)->data)]; ptr = &command[strlen("log")]; while(isspace(*ptr)) ptr++; max = sizeof(((struct mconsole_request *) NULL)->data) - sizeof("log ") - 1; if(!strncmp(ptr, "-f", strlen("-f"))){ ptr = &ptr[strlen("-f")]; while(isspace(*ptr)) ptr++; input_fd = open(ptr, O_RDONLY); if(input_fd < 0){ perror("opening file"); exit(1); } strcpy(buf, "log "); ptr = buf + strlen(buf); while((len = read(input_fd, ptr, max)) > 0){ ptr[len] = '\0'; default_cmd(fd, buf); newline = (ptr[len - 1] == '\n'); } if(len < 0){ perror("reading file"); exit(1); } } else { len = strlen(ptr); while(len > 0){ chunk = MIN(len, max); sprintf(buf, "log %.*s", chunk, ptr); default_cmd(fd, buf); newline = (ptr[chunk - 1] == '\n'); len -= chunk; ptr += chunk; } } if(!newline){ sprintf(buf, "log \n"); default_cmd(fd, buf); } return(0); } extern int send_fd(int fd, int target, struct sockaddr *to, int to_len, void *msg, int msg_len); static int umlfs_cmd(int fd, char *command) { struct mconsole_request request; char *end, *output, *fd_ptr; int fuse_fd, err; fd_ptr = command; fd_ptr += strlen("umlfs"); while(isspace(*fd_ptr)) fd_ptr++; fuse_fd = strtoul(fd_ptr, &end, 0); if(*end != '\0'){ output = "Failed to parse file descriptor"; err = -EINVAL; goto out; } format_cmd(command, &request); err = send_fd(fuse_fd, fd, (struct sockaddr *) &sun, sizeof(sun), &request, sizeof(request)); if(err){ output = "Failed to send FUSE file descriptor"; goto out; } err = rcv_output(fd, &output); out: printf("%s %s\n", err ? "ERR" : "OK", output); return err; } static int quit_cmd(int fd, char *command) { exit(0); } static int mversion_cmd(int fd, char *command) { printf("uml_mconsole client version %d\n", MCONSOLE_VERSION); return(0); } static int int_cmd(int fd, char *command) { if(uml_pid == -1){ printf("Cannot determine the PID of your UML session, not " "sending signal.\n"); return 0; } kill(uml_pid, SIGINT); return 0; } struct cmd { char *command; int (*proc)(int, char *); }; static struct cmd cmds[] = { { "quit", quit_cmd }, { "help", help_cmd }, { "switch", switch_cmd }, { "log", log_cmd }, { "umlfs", umlfs_cmd }, { "mconsole-version", mversion_cmd }, { "int", int_cmd }, { NULL, default_cmd } /* default_cmd means "send it to the UML" */ }; /* sends a command */ int issue_command(int fd, char *command) { char *ptr; unsigned int i = 0; /* Trim trailing spaces left by readline's filename completion */ ptr = &command[strlen(command) - 1]; while(isspace(*ptr)) *ptr-- = '\0'; for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++){ if((cmds[i].command == NULL) || !strncmp(cmds[i].command, command, strlen(cmds[i].command))){ return((*cmds[i].proc)(fd, command)); } } /* Should never get here, considering the NULL test above will match the last * entry of cmds */ return(0); } /* sends a command in argv style array */ int issue_commandv(int fd, char **argv) { char *command; int len = -1, i = 0, status; len = 1; /* space for trailing null */ for(i = 0; argv[i] != NULL; i++) len += strlen(argv[i]) + 1; /* space for space */ command = malloc(len); if(command == NULL){ perror("issue_command"); return(1); } command[0] = '\0'; for(i = 0; argv[i] != NULL; i++) { strcat(command, argv[i]); if(argv[i+1] != NULL) strcat(command, " "); } status = issue_command(fd, command); free(command); return(status); } static void Usage(void) { fprintf(stderr, "Usage : uml_mconsole socket-name [command]\n"); exit(1); } int main(int argc, char **argv) { struct sockaddr_un here; char *sock; int fd; if(argc < 2) Usage(); strcpy(uml_name, "[None]"); sock = argv[1]; switch_common(sock); if((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){ perror("socket"); exit(1); } here.sun_family = AF_UNIX; memset(here.sun_path, 0, sizeof(here.sun_path)); sprintf(&here.sun_path[1], "%5d", getpid()); if(bind(fd, (struct sockaddr *) &here, sizeof(here)) < 0){ perror("bind"); exit(1); } if(argc>2) exit(issue_commandv(fd, argv+2)); while(1){ char *command, prompt[1 + sizeof(uml_name) + 2 + 1]; sprintf(prompt, "(%s) ", uml_name); command = readline(prompt); if(command == NULL) break; if(*command) add_history(command); issue_command(fd, command); free(command); } printf("\n"); exit(0); } uml-utilities-20070815.4/moo/000077500000000000000000000000001362720355200155315ustar00rootroot00000000000000uml-utilities-20070815.4/moo/.cvsignore000066400000000000000000000000101362720355200175200ustar00rootroot00000000000000uml_moo uml-utilities-20070815.4/moo/CVS/000077500000000000000000000000001362720355200161645ustar00rootroot00000000000000uml-utilities-20070815.4/moo/CVS/Entries000066400000000000000000000004431362720355200175210ustar00rootroot00000000000000/.cvsignore/1.1/Fri Jan 11 05:21:31 2002// /Makefile/1.7/Thu Jun 26 16:27:00 2003// /cow.c/1.2/Wed Sep 3 16:35:07 2003// /cow.h/1.2/Wed Sep 3 16:35:52 2003// /cow_sys.h/1.2/Thu Aug 28 01:44:05 2003// /uml_mkcow.c/1.2/Mon Sep 8 21:20:29 2003// /uml_moo.c/1.10/Mon Sep 8 21:39:46 2003// D uml-utilities-20070815.4/moo/CVS/Repository000066400000000000000000000000121362720355200202570ustar00rootroot00000000000000tools/moo uml-utilities-20070815.4/moo/CVS/Root000066400000000000000000000001031362720355200170240ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/moo/Makefile000066400000000000000000000010661362720355200171740ustar00rootroot00000000000000BIN = uml_moo uml_mkcow uml_moo_OBJS = uml_moo.o cow.o uml_mkcow_OBJS = uml_mkcow.o cow.o CFLAGS ?= -g -Wall override CFLAGS += -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_GNU_SOURCE BIN_DIR ?= /usr/bin all : $(BIN) uml_moo : $(uml_moo_OBJS) $(CC) $(CFLAGS) -o $@ $($@_OBJS) uml_mkcow : $(uml_mkcow_OBJS) $(CC) $(CFLAGS) -o $@ $($@_OBJS) $(uml_moo_OBJS) $(uml_mkcow_OBJS) : cow.h cow_sys.h clean : rm -f $(foreach bin,$(BIN),$($(bin)_OBJS)) $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/moo/cow.c000066400000000000000000000275351362720355200165010ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include #include #include "cow.h" #include "cow_sys.h" #define PATH_LEN_V1 256 typedef __u32 time32_t; struct cow_header_v1 { __s32 magic; __s32 version; char backing_file[PATH_LEN_V1]; time32_t mtime; __u64 size; __s32 sectorsize; } __attribute__((packed)); /* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in * case other systems have different values for MAXPATHLEN. * * The same must hold for V2 - we want file format compatibility, not anything * else. */ #define PATH_LEN_V3 4096 #define PATH_LEN_V2 PATH_LEN_V3 struct cow_header_v2 { __u32 magic; __u32 version; char backing_file[PATH_LEN_V2]; time32_t mtime; __u64 size; __s32 sectorsize; } __attribute__((packed)); /* Changes from V2 - * PATH_LEN_V3 as described above * Explicitly specify field bit lengths for systems with different * lengths for the usual C types. Not sure whether char or * time_t should be changed, this can be changed later without * breaking compatibility * Add alignment field so that different alignments can be used for the * bitmap and data * Add cow_format field to allow for the possibility of different ways * of specifying the COW blocks. For now, the only value is 0, * for the traditional COW bitmap. * Move the backing_file field to the end of the header. This allows * for the possibility of expanding it into the padding required * by the bitmap alignment. * The bitmap and data portions of the file will be aligned as specified * by the alignment field. This is to allow COW files to be * put on devices with restrictions on access alignments, such as * /dev/raw, with a 512 byte alignment restriction. This also * allows the data to be more aligned more strictly than on * sector boundaries. This is needed for ubd-mmap, which needs * the data to be page aligned. * Fixed (finally!) the rounding bug */ /* Until Dec2005, __attribute__((packed)) was left out from the below * definition, leading on 64-bit systems to 4 bytes of padding after mtime, to * align size to 8-byte alignment. This shifted all fields above (no padding * was present on 32-bit, no other padding was added). * * However, this _can be detected_: it means that cow_format (always 0 until * now) is shifted onto the first 4 bytes of backing_file, where it is otherwise * impossible to find 4 zeros. -bb */ struct cow_header_v3 { __u32 magic; __u32 version; time32_t mtime; __u64 size; __u32 sectorsize; __u32 alignment; __u32 cow_format; char backing_file[PATH_LEN_V3]; } __attribute__((packed)); /* This is the broken layout used by some 64-bit binaries. */ struct cow_header_v3_broken { __u32 magic; __u32 version; __s64 mtime; __u64 size; __u32 sectorsize; __u32 alignment; __u32 cow_format; char backing_file[PATH_LEN_V3]; }; /* COW format definitions - for now, we have only the usual COW bitmap */ #define COW_BITMAP 0 union cow_header { struct cow_header_v1 v1; struct cow_header_v2 v2; struct cow_header_v3 v3; struct cow_header_v3_broken v3_b; }; #define COW_MAGIC 0x4f4f4f4d /* MOOO */ #define COW_VERSION 3 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len)) #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align) void cow_sizes(int version, __u64 size, int sectorsize, int align, int bitmap_offset, unsigned long *bitmap_len_out, int *data_offset_out) { if(version < 3){ *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize); *data_offset_out = bitmap_offset + *bitmap_len_out; *data_offset_out = (*data_offset_out + sectorsize - 1) / sectorsize; *data_offset_out *= sectorsize; } else { *bitmap_len_out = DIV_ROUND(size, sectorsize); *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8); *data_offset_out = bitmap_offset + *bitmap_len_out; *data_offset_out = ROUND_UP(*data_offset_out, align); } } static int absolutize(char *to, size_t size, char *from) { char save_cwd[256], *slash; size_t remaining; if(getcwd(save_cwd, sizeof(save_cwd)) == NULL) { cow_printf("absolutize : unable to get cwd - errno = %d\n", errno); return(-1); } slash = strrchr(from, '/'); if(slash != NULL){ *slash = '\0'; if(chdir(from)){ *slash = '/'; cow_printf("absolutize : Can't cd to '%s' - " "errno = %d\n", from, errno); return(-1); } *slash = '/'; if(getcwd(to, size) == NULL){ cow_printf("absolutize : unable to get cwd of '%s' - " "errno = %d\n", from, errno); return(-1); } remaining = size - strlen(to); if(strlen(slash) + 1 > remaining){ cow_printf("absolutize : unable to fit '%s' into %zd " "chars\n", from, size); return(-1); } strcat(to, slash); } else { if(strlen(save_cwd) + 1 + strlen(from) + 1 > size){ cow_printf("absolutize : unable to fit '%s' into %zd " "chars\n", from, size); return(-1); } strcpy(to, save_cwd); strcat(to, "/"); strcat(to, from); } chdir(save_cwd); return(0); } int write_cow_header(char *cow_file, int fd, char *backing_file, int sectorsize, int alignment, unsigned long long *size) { struct cow_header_v3 *header; struct stat64 buf; int err; err = cow_seek_file(fd, 0); if(err < 0){ cow_printf("write_cow_header - lseek failed, err = %d\n", -err); goto out; } err = -ENOMEM; header = cow_malloc(sizeof(*header)); if(header == NULL){ cow_printf("write_cow_header - failed to allocate COW V3 header\n"); goto out; } header->magic = htonl(COW_MAGIC); header->version = htonl(COW_VERSION); err = -EINVAL; if(strlen(backing_file) > sizeof(header->backing_file) - 1){ /* Below, %zd is for a size_t value */ cow_printf("Backing file name \"%s\" is too long - names are " "limited to %zd characters\n", backing_file, sizeof(header->backing_file) - 1); goto out_free; } if(absolutize(header->backing_file, sizeof(header->backing_file), backing_file)) goto out_free; err = stat64(header->backing_file, &buf); if(err < 0){ cow_printf("write_cow_header - backing file '%s' mtime " "request failed, err = %d\n", header->backing_file, -err); goto out_free; } err = cow_file_size(header->backing_file, size); if(err < 0){ cow_printf("write_cow_header - couldn't get size of " "backing file '%s', err = %d\n", header->backing_file, -err); goto out_free; } header->mtime = htonl(buf.st_mtime); header->size = htonll(*size); header->sectorsize = htonl(sectorsize); header->alignment = htonl(alignment); header->cow_format = COW_BITMAP; err = cow_write_file(fd, header, sizeof(*header)); if(err != sizeof(*header)){ cow_printf("write_cow_header - write of header to " "new COW file '%s' failed, err = %d\n", cow_file, -err); goto out_free; } err = 0; out_free: cow_free(header); out: return(err); } int file_reader(__u64 offset, char *buf, int len, void *arg) { int fd = *((int *) arg); return(pread(fd, buf, len, offset)); } /* XXX Need to sanity-check the values read from the header */ int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg, __u32 *version_out, char **backing_file_out, time_t *mtime_out, __u64 *size_out, int *sectorsize_out, __u32 *align_out, int *bitmap_offset_out) { union cow_header *header; char *file; int err, n; unsigned long version, magic; header = cow_malloc(sizeof(*header)); if(header == NULL){ cow_printf("read_cow_header - Failed to allocate header\n"); return(-ENOMEM); } err = -EINVAL; n = (*reader)(0, (char *) header, sizeof(*header), arg); if(n < offsetof(typeof(header->v1), backing_file)){ cow_printf("read_cow_header - short header\n"); goto out; } magic = header->v1.magic; if(magic == COW_MAGIC) { version = header->v1.version; } else if(magic == ntohl(COW_MAGIC)){ version = ntohl(header->v1.version); } /* No error printed because the non-COW case comes through here */ else goto out; *version_out = version; if(version == 1){ if(n < sizeof(header->v1)){ cow_printf("read_cow_header - failed to read V1 " "header\n"); goto out; } *mtime_out = header->v1.mtime; *size_out = header->v1.size; *sectorsize_out = header->v1.sectorsize; *bitmap_offset_out = sizeof(header->v1); *align_out = *sectorsize_out; file = header->v1.backing_file; } else if(version == 2){ if(n < sizeof(header->v2)){ cow_printf("read_cow_header - failed to read V2 " "header\n"); goto out; } *mtime_out = ntohl(header->v2.mtime); *size_out = ntohll(header->v2.size); *sectorsize_out = ntohl(header->v2.sectorsize); *bitmap_offset_out = sizeof(header->v2); *align_out = *sectorsize_out; file = header->v2.backing_file; } /* This is very subtle - see above at union cow_header definition */ else if(version == 3 && (*((int*)header->v3.backing_file) != 0)){ if(n < sizeof(header->v3)){ cow_printf("read_cow_header - failed to read V3 " "header\n"); goto out; } *mtime_out = ntohl(header->v3.mtime); *size_out = ntohll(header->v3.size); *sectorsize_out = ntohl(header->v3.sectorsize); *align_out = ntohl(header->v3.alignment); if (*align_out == 0) { cow_printf("read_cow_header - invalid COW header, " "align == 0\n"); } *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out); file = header->v3.backing_file; } else if(version == 3){ cow_printf("read_cow_header - broken V3 file with" " 64-bit layout - recovering content.\n"); if(n < sizeof(header->v3_b)){ cow_printf("read_cow_header - failed to read V3 " "header\n"); goto out; } /* this was used until Dec2005 - 64bits are needed to represent * 2038+. I.e. we can safely do this truncating cast. * * Additionally, we must use ntohl() instead of ntohll(), since * the program used to use the former (tested - I got mtime * mismatch "0 vs whatever"). * * Ever heard about bug-to-bug-compatibility ? ;-) */ *mtime_out = (time32_t) ntohl(header->v3_b.mtime); *size_out = ntohll(header->v3_b.size); *sectorsize_out = ntohl(header->v3_b.sectorsize); *align_out = ntohl(header->v3_b.alignment); if (*align_out == 0) { cow_printf("read_cow_header - invalid COW header, " "align == 0\n"); } *bitmap_offset_out = ROUND_UP(sizeof(header->v3_b), *align_out); file = header->v3_b.backing_file; } else { cow_printf("read_cow_header - invalid COW version\n"); goto out; } err = -ENOMEM; *backing_file_out = cow_strdup(file); if(*backing_file_out == NULL){ cow_printf("read_cow_header - failed to allocate backing " "file\n"); goto out; } err = 0; out: cow_free(header); return(err); } int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize, int alignment, int *bitmap_offset_out, unsigned long *bitmap_len_out, int *data_offset_out) { unsigned long long size, offset; char zero = 0; int err; err = write_cow_header(cow_file, fd, backing_file, sectorsize, alignment, &size); if(err) goto out; *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment); cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out, bitmap_len_out, data_offset_out); offset = *data_offset_out + size - sizeof(zero); err = cow_seek_file(fd, offset); if(err < 0){ cow_printf("cow bitmap lseek failed : err = %d\n", -err); goto out; } /* does not really matter how much we write it is just to set EOF * this also sets the entire COW bitmap * to zero without having to allocate it */ err = cow_write_file(fd, &zero, sizeof(zero)); if(err != sizeof(zero)){ cow_printf("Write of bitmap to new COW file '%s' failed, " "err = %d\n", cow_file, -err); if (err >= 0) err = -EINVAL; goto out; } return(0); out: return(err); } /* * --------------------------------------------------------------------------- * Local variables: * c-file-style: "linux" * End: */ uml-utilities-20070815.4/moo/cow.h000066400000000000000000000023451362720355200164760ustar00rootroot00000000000000#ifndef __COW_H__ #define __COW_H__ #include #if __BYTE_ORDER == __BIG_ENDIAN # define ntohll(x) (x) # define htonll(x) (x) #elif __BYTE_ORDER == __LITTLE_ENDIAN # define ntohll(x) bswap_64(x) # define htonll(x) bswap_64(x) #else #error "__BYTE_ORDER not defined" #endif extern int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize, int alignment, int *bitmap_offset_out, unsigned long *bitmap_len_out, int *data_offset_out); extern int file_reader(__u64 offset, char *buf, int len, void *arg); extern int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg, __u32 *version_out, char **backing_file_out, time_t *mtime_out, __u64 *size_out, int *sectorsize_out, __u32 *align_out, int *bitmap_offset_out); extern int write_cow_header(char *cow_file, int fd, char *backing_file, int sectorsize, int alignment, unsigned long long *size); extern void cow_sizes(int version, __u64 size, int sectorsize, int align, int bitmap_offset, unsigned long *bitmap_len_out, int *data_offset_out); #endif /* * --------------------------------------------------------------------------- * Local variables: * c-file-style: "linux" * End: */ uml-utilities-20070815.4/moo/cow_sys.h000066400000000000000000000030361362720355200173720ustar00rootroot00000000000000#ifndef __COW_SYS_H__ #define __COW_SYS_H__ #include #include #include #include #include #include #include #include static inline void *cow_malloc(int size) { return(malloc(size)); } static inline void cow_free(void *ptr) { free(ptr); } #define cow_printf printf static inline char *cow_strdup(char *str) { return(strdup(str)); } static inline int cow_seek_file(int fd, __u64 offset) { __u64 actual; actual = lseek64(fd, offset, SEEK_SET); if(actual != offset) return(-errno); return(0); } static inline int cow_file_size(char *file, __u64 *size_out) { struct stat64 buf; if (stat64(file, &buf) == -1) { cow_printf("Couldn't stat \"%s\" : errno = %d\n", file, errno); return(-errno); } if(S_ISBLK(buf.st_mode)){ int fd, blocks; if((fd = open(file, O_RDONLY)) < 0){ cow_printf("Couldn't open \"%s\", errno = %d\n", file, errno); return(-errno); } if(ioctl(fd, BLKGETSIZE, &blocks) < 0){ cow_printf("Couldn't get the block size of \"%s\", " "errno = %d\n", file, errno); close(fd); return(-errno); } *size_out = ((long long) blocks) * 512; close(fd); return(0); } *size_out = buf.st_size; return(0); } static inline int cow_write_file(int fd, void *buf, int size) { int ret = write(fd, buf, size); if (ret == -1) return(-errno); return(ret); } #endif /* * --------------------------------------------------------------------------- * Local variables: * c-file-style: "linux" * End: */ uml-utilities-20070815.4/moo/uml_mkcow.c000066400000000000000000000025411362720355200176740ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include "cow.h" void usage(const char progname[]) { fprintf(stderr, "%s usage: \n", progname); fprintf(stderr, "\t%s [-f] \n", progname); fprintf(stderr, "\t-f forces overwrite of an existing COW file\n"); exit(2); } int main(int argc, char *argv[]) { int fd; int err; char *cow_file; char *backing_file; int bitmap_offset_out; unsigned long bitmap_len_out; int data_offset_out; int flags; mode_t mode; char *progname; progname = argv[0]; argv++; argc--; if(argc < 1) usage(progname); flags = O_RDWR | O_CREAT | O_LARGEFILE; mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; if(strcmp("-f", argv[0]) != 0){ /* not forced, creating as exclusive */ flags |= O_EXCL; } else { argv++; argc--; } if (argc < 2) usage(progname); cow_file = argv[0]; backing_file = argv[1]; if((fd = open(cow_file, flags, mode)) == -1) { perror("open COW file"); exit(1); } if((err = init_cow_file(fd, cow_file, backing_file, 512, sysconf(_SC_PAGESIZE), &bitmap_offset_out, &bitmap_len_out, &data_offset_out))){ errno = -err; perror("write_cow_header"); exit(1); } return (0); } /* vim: ts=4 tw=74 sw=2 */ uml-utilities-20070815.4/moo/uml_moo.c000066400000000000000000000137141362720355200173520ustar00rootroot00000000000000/* Copyrighted (C) 2001 RidgeRun,Inc (glonnon@ridgerun.com) * With modifications by Jeff Dike, James McMechan, and Steve Schmidtke. * Licensed under the GPL */ #define _XOPEN_SOURCE 500 #include #include #include #include #include #include #include #include #include #include #include #include "cow.h" static inline int ubd_test_bit(__u64 bit, unsigned long *data) { int bits, off; __u64 n; bits = sizeof(data[0]) * 8; n = bit / bits; off = bit % bits; //1UL to prevent sign-extension, which turns 0x8000000 into //0xffffffff8000000 return((data[n] & (1UL << off)) != 0); } static inline void ubd_set_bit(__u64 bit, unsigned long *data) { int bits, off; __u64 n; bits = sizeof(data[0]) * 8; n = bit / bits; off = bit % bits; //1UL to prevent sign-extension, which turns 0x8000000 into //0xffffffff8000000 data[n] |= (1UL << off); } int create_backing_file(char *in, char *out, char *actual_backing) { int cow_fd, back_fd, in_fd, out_fd; struct stat buf; unsigned long *bitmap; unsigned long bitmap_len; unsigned sectorsize, sectors; void *sector, *zeros; __u64 size, offset, i; /* i is u64 to prevent 32 bit overflow */ __u32 version, alignment; int data_offset; char *backing_file; time_t mtime; int bitmap_offset, perms, n; if((cow_fd = open(in, O_RDONLY)) < 0){ perror("COW file open"); exit(1); } if(read_cow_header(file_reader, &cow_fd, &version, &backing_file, &mtime, &size, §orsize, &alignment, &bitmap_offset)){ fprintf(stderr, "Reading COW header failed\n"); exit(1); } if(actual_backing != NULL) backing_file = actual_backing; if(stat(backing_file, &buf) < 0) { perror("Stating backing file"); exit(1); } if(buf.st_size != size){ fprintf(stderr,"Size mismatch (%ld vs %ld) of COW header " "vs backing file \"%s\"\n", (long int) size, (long int) buf.st_size, backing_file); exit(1); } if(buf.st_mtime != mtime) { fprintf(stderr,"mtime mismatch (%ld vs %ld) of COW " "header vs backing file \"%s\"\n", mtime, buf.st_mtime, backing_file); exit(1); } perms = (out != NULL) ? O_RDONLY : O_RDWR; if((back_fd = open(backing_file, perms)) < 0){ perror("Opening backing file"); exit(1); } if(out != NULL){ if((out_fd = creat(out, 0644)) < 0){ perror("Output file open"); exit(1); } } else out_fd = back_fd; cow_sizes(version, size, sectorsize, alignment, bitmap_offset, &bitmap_len, &data_offset); bitmap = (unsigned long *) malloc(bitmap_len); if(bitmap == NULL) { perror("Can't allocate bitmap"); exit(1); } if(pread(cow_fd, bitmap, bitmap_len, bitmap_offset) != bitmap_len){ perror("Reading COW bitmap"); exit(1); } sectors = size / sectorsize; sector = malloc(sectorsize); zeros = malloc(sectorsize); if((sector == NULL) || (zeros == NULL)){ perror("Malloc of buffers"); exit(1); } memset(zeros, 0, sectorsize); for(i = 0; i < sectors; i++){ offset = i * sectorsize; if(ubd_test_bit(i, bitmap)){ offset += data_offset; in_fd = cow_fd; } else in_fd = back_fd; /* If we're doing a destructive merge, and the backing file * sector is up to date, then there's nothing to do. */ if((out_fd == back_fd) && (in_fd == back_fd)) continue; if(pread(in_fd, sector, sectorsize, offset) != sectorsize){ perror("Reading data"); exit(1); } /* Sparse file creation - if the sector is all zeros and it's * not the last sector (which always gets written out in order * to make the file size right), maybe it doesn't need to be * written out. */ if((i < sectors - 1) && !memcmp(sector, zeros, sectorsize)){ /* If we're doing a non-destructive merge, then zero * sectors can just be skipped. */ if(out_fd != back_fd) continue; /* Otherwise, we are doing a destructive merge, and * we need to check the backing file for a zero * sector. */ n = pread(back_fd, sector, sectorsize, i * sectorsize); if(n != sectorsize){ perror("Checking backing file for zeros"); exit(1); } if(!memcmp(sector, zeros, sectorsize)) continue; /* The backing file sector isn't zeros, so the sector * of zeros in the COW file needs to be written out. */ memset(sector, 0, sectorsize); } /* The offset can't be 'offset' because that's used as an * input offset, which may be offset by the COW header. */ n = pwrite(out_fd, sector, sectorsize, i * sectorsize); if(n != sectorsize){ perror("Writing data"); exit(1); } } free(bitmap); free(sector); free(zeros); close(cow_fd); close(out_fd); close(back_fd); return(0); } static char *usage_string = "%s usage:\n" "\t%s [ -b ] \n" "\t%s [ -b ] -d \n" "Creates a new filesystem image from the COW file and its backing file.\n" "Specifying -d will cause a destructive, in-place merge of the COW file into\n" "its current backing file\n" "Specifying -b overrides the backing_file specified in the COW file. This is\n" "needed when dealing with a COW file that was created inside a chroot jail.\n" "%s supports version 1,2 and 3 COW files.\n" ""; static int Usage(char *prog) { fprintf(stderr, usage_string, prog, prog, prog, prog); exit(1); } int main(int argc, char **argv) { char *prog = argv[0]; char *actual_backing_file = NULL; int in_place = 0; argv++; argc--; if(argc == 0) Usage(prog); if(!strcmp(argv[0], "-d")){ in_place = 1; argv++; argc--; } if(!strcmp(argv[0], "-b")){ actual_backing_file = argv[1]; argv += 2; argc -= 2; } if(in_place){ if(argc != 1) Usage(prog); create_backing_file(argv[0], NULL, actual_backing_file); } else { if(argc != 2) Usage(prog); create_backing_file(argv[0], argv[1], actual_backing_file); } return 0; } /* * --------------------------------------------------------------------------- * Local variables: * c-file-style: "linux" * End: */ uml-utilities-20070815.4/port-helper/000077500000000000000000000000001362720355200172005ustar00rootroot00000000000000uml-utilities-20070815.4/port-helper/CVS/000077500000000000000000000000001362720355200176335ustar00rootroot00000000000000uml-utilities-20070815.4/port-helper/CVS/CVS/000077500000000000000000000000001362720355200202665ustar00rootroot00000000000000uml-utilities-20070815.4/port-helper/CVS/CVS/Entries000066400000000000000000000000021362720355200216120ustar00rootroot00000000000000D uml-utilities-20070815.4/port-helper/CVS/CVS/Repository000066400000000000000000000000261362720355200223660ustar00rootroot00000000000000tools/port-helper/CVS uml-utilities-20070815.4/port-helper/CVS/CVS/Root000066400000000000000000000001031362720355200211260ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/port-helper/CVS/Entries000066400000000000000000000001411362720355200211630ustar00rootroot00000000000000D/CVS//// /port-helper.c/1.6/Thu Jul 25 01:27:37 2002// /Makefile/1.3/Sat Feb 8 04:05:31 2003// uml-utilities-20070815.4/port-helper/CVS/Repository000066400000000000000000000000221362720355200217270ustar00rootroot00000000000000tools/port-helper uml-utilities-20070815.4/port-helper/CVS/Root000066400000000000000000000001031362720355200204730ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/port-helper/Makefile000066400000000000000000000004631362720355200206430ustar00rootroot00000000000000OBJS = port-helper.o BIN = port-helper CFLAGS ?= -g -Wall LIBS = ../lib/libuml.a LIB_DIR ?= /usr/lib/uml all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) $(LIBS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(LIB_DIR) install -s $(BIN) $(DESTDIR)$(LIB_DIR) uml-utilities-20070815.4/port-helper/port-helper.c000066400000000000000000000033721362720355200216120ustar00rootroot00000000000000/* port-helper Used by the port and xterm console channels for User Mode Linux. Tells UML "here is a file descriptor for my stdin as given to me by xterm or telnetd". Once UML has that (with os_rcv_fd()) UML opens it for read and write, and the console is functional. (c) Jeff Dike 2002-2004 */ #include #include #include #include #include #include #include #include #include extern void send_fd(int fd, int target, struct sockaddr *to, int to_len, void *msg, int msg_len); /* for xterm we don't have an open socket, we only have the name of a file used as a Unix socket by UML. So we need to open it. */ static int open_socket(char *name) { struct sockaddr_un sock; int fd; if((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){ perror("socket"); exit(1); } sock.sun_family = AF_UNIX; memset(sock.sun_path, 0, sizeof(sock.sun_path)); sprintf(&sock.sun_path[1], "%5d", getpid()); if(bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0){ perror("bind"); exit(1); } snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name); if(connect(fd, (struct sockaddr *) &sock, sizeof(sock))){ perror("connect"); exit(1); } return(fd); } int main(int argc, char **argv) { int fd, pid; if((argc > 1) && !strcmp(argv[1], "-uml-socket")) { /* inherited a filename not an open fd */ fd = open_socket(argv[2]); } else { /* inherited fd of the listening TCP socket */ fd = 3; } signal(SIGHUP, SIG_IGN); if(ioctl(0, TIOCNOTTY, 0) < 0) perror("TIOCNOTTY failed in port-helper, check UML's exec call for xterm or telnetd"); pid = getpid(); send_fd(0, fd, NULL, 0, &pid, sizeof(pid)); pause(); return(0); } uml-utilities-20070815.4/tunctl/000077500000000000000000000000001362720355200162505ustar00rootroot00000000000000uml-utilities-20070815.4/tunctl/CVS/000077500000000000000000000000001362720355200167035ustar00rootroot00000000000000uml-utilities-20070815.4/tunctl/CVS/Entries000066400000000000000000000001241362720355200202340ustar00rootroot00000000000000/tunctl.c/1.4/Tue Sep 10 22:18:06 2002// /Makefile/1.2/Sat Feb 8 04:05:04 2003// D uml-utilities-20070815.4/tunctl/CVS/Repository000066400000000000000000000000151362720355200210010ustar00rootroot00000000000000tools/tunctl uml-utilities-20070815.4/tunctl/CVS/Root000066400000000000000000000001031362720355200175430ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/tunctl/Makefile000066400000000000000000000004061362720355200177100ustar00rootroot00000000000000OBJS = tunctl.o BIN = tunctl CFLAGS ?= -g -Wall BIN_DIR ?= /usr/bin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/tunctl/tunctl.c000066400000000000000000000064171362720355200177350ustar00rootroot00000000000000/* Copyright 2002 Jeff Dike * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include #include #include /* TUNSETGROUP appeared in 2.6.23 */ #ifndef TUNSETGROUP #define TUNSETGROUP _IOW('T', 206, int) #endif static void Usage(char *name) { fprintf(stderr, "Create: %s [-b] [-u owner] [-g group] [-t device-name] " "[-f tun-clone-device]\n", name); fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n", name); fprintf(stderr, "The default tun clone device is /dev/net/tun - some systems" " use\n/dev/misc/net/tun instead\n\n"); fprintf(stderr, "-b will result in brief output (just the device name)\n"); exit(1); } int main(int argc, char **argv) { struct ifreq ifr; struct passwd *pw; struct group *gr; uid_t owner = -1; gid_t group = -1; int tap_fd, opt, delete = 0, brief = 0; char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end; while((opt = getopt(argc, argv, "bd:f:t:u:g:")) > 0){ switch(opt) { case 'b': brief = 1; break; case 'd': delete = 1; tun = optarg; break; case 'f': file = optarg; break; case 'u': pw = getpwnam(optarg); if(pw != NULL){ owner = pw->pw_uid; break; } owner = strtol(optarg, &end, 0); if(*end != '\0'){ fprintf(stderr, "'%s' is neither a username nor a numeric uid.\n", optarg); Usage(name); } break; case 'g': gr = getgrnam(optarg); if(gr != NULL){ group = gr->gr_gid; break; } group = strtol(optarg, &end, 0); if(*end != '\0'){ fprintf(stderr, "'%s' is neither a groupname nor a numeric group.\n", optarg); Usage(name); } break; case 't': tun = optarg; break; case 'h': default: Usage(name); } } argv += optind; argc -= optind; if(argc > 0) Usage(name); if((tap_fd = open(file, O_RDWR)) < 0){ fprintf(stderr, "Failed to open '%s' : ", file); perror(""); exit(1); } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; strncpy(ifr.ifr_name, tun, sizeof(ifr.ifr_name) - 1); if(ioctl(tap_fd, TUNSETIFF, (void *) &ifr) < 0){ perror("TUNSETIFF"); exit(1); } if(delete){ if(ioctl(tap_fd, TUNSETPERSIST, 0) < 0){ perror("TUNSETPERSIST"); exit(1); } printf("Set '%s' nonpersistent\n", ifr.ifr_name); } else { if(ioctl(tap_fd, TUNSETPERSIST, 1) < 0){ perror("TUNSETPERSIST"); exit(1); } /* emulate behaviour prior to TUNSETGROUP */ if(owner == -1 && group == -1) { owner = geteuid(); } if(owner != -1) { if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){ perror("TUNSETPERSIST"); exit(1); } } if(group != -1) { if(ioctl(tap_fd, TUNSETGROUP, group) < 0){ perror("TUNSETPERSIST"); exit(1); } } if(brief) printf("%s\n", ifr.ifr_name); else { printf("Set '%s' persistent and owned by", ifr.ifr_name); if(owner != -1) printf(" uid %d", owner); if(group != -1) printf(" gid %d", group); printf("\n"); } } return(0); } uml-utilities-20070815.4/uml_net/000077500000000000000000000000001362720355200164025ustar00rootroot00000000000000uml-utilities-20070815.4/uml_net/.cvsignore000066400000000000000000000000111362720355200203720ustar00rootroot00000000000000uml_net uml-utilities-20070815.4/uml_net/CVS/000077500000000000000000000000001362720355200170355ustar00rootroot00000000000000uml-utilities-20070815.4/uml_net/CVS/Entries000066400000000000000000000006341362720355200203740ustar00rootroot00000000000000/output.h/1.1/Tue Nov 13 05:25:26 2001// /host.h/1.3/Mon Dec 10 04:36:26 2001// /.cvsignore/1.1/Fri Jan 11 05:21:31 2002// /host.c/1.6/Sun Apr 28 13:26:07 2002// /output.c/1.2/Sun Apr 28 13:38:44 2002// /slip.c/1.5/Sun Apr 28 19:47:35 2002// /Makefile/1.8/Sat Feb 8 04:04:55 2003// /uml_net.c/1.19/Tue May 27 02:52:21 2003// /ethertap.c/1.9/Sat Nov 8 13:15:25 2003// /tuntap.c/1.6/Sat Nov 8 13:04:58 2003// D uml-utilities-20070815.4/uml_net/CVS/Repository000066400000000000000000000000161362720355200211340ustar00rootroot00000000000000tools/uml_net uml-utilities-20070815.4/uml_net/CVS/Root000066400000000000000000000001031362720355200176750ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/uml_net/Makefile000066400000000000000000000007041362720355200200430ustar00rootroot00000000000000TUNTAP = $(shell [ -e /usr/include/linux/if_tun.h ] && echo -DTUNTAP) BIN = uml_net CFLAGS ?= -g -Wall override CFLAGS += $(TUNTAP) BIN_DIR ?= /usr/bin OBJS = ethertap.o host.o output.o slip.o uml_net.o ifneq ($(TUNTAP),) OBJS += tuntap.o endif all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s -m 04755 $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/uml_net/ethertap.c000066400000000000000000000156461362720355200203760ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include #include #include #include "host.h" #include "output.h" static void fail(int fd) { char c = 0; if(write(fd, &c, sizeof(c)) != sizeof(c)) perror("Writing failure byte"); } void output_fail(struct output *output, int fd) { fail(fd); write_output(fd, output); exit(1); } /* Shouldn't there be some other way to figure out the major and minor * number of the tap device other than hard-wiring it here? Does FreeBSD * have a 'tap' device? Should we have some kind of #ifdef here? */ #define TAP_MAJOR 36 #define TAP_MINOR 16 /* plus whatever tap device it was. */ static int maybe_insmod(char *dev, struct output *output) { struct ifreq ifr; int fd, unit; char unit_buf[sizeof("unit=nnn\0")]; char ethertap_buf[sizeof("ethertapnnn\0")]; char *ethertap_argv[] = { "modprobe", "ethertap", unit_buf, "-o", ethertap_buf, NULL }; char *netlink_argv[] = { "modprobe", "netlink_dev", NULL }; char buf[256]; if((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0){ output_errno(output, "socket"); return(-1); } strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1); ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; if(ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) return(0); if(errno != ENODEV){ output_errno(output, "SIOCGIFFLAGS on tap device"); return(-1); } if(sscanf(dev, "tap%d", &unit) != 1){ snprintf(buf, sizeof(buf), "failed to get unit number from '%s'\n", dev); add_output(output, buf, -1); return(-1); } snprintf(unit_buf, sizeof(unit_buf) - 1, "unit=%d", unit); snprintf(ethertap_buf, sizeof(ethertap_buf) - 1, "ethertap%d", unit); do_exec(netlink_argv, 0, output); return(do_exec(ethertap_argv, 0, output)); } #define BUF_SIZE 1500 #define max(i, j) (((i) > (j)) ? (i) : (j)) static void ethertap(char *dev, int data_fd, int control_fd, char *gate, char *remote, int collect_output, int (*control_cb)(int, char *, struct output *)) { struct output output = INIT_OUTPUT, *o = NULL; int ip[4]; char *ifconfig_argv[] = { "ifconfig", dev, "arp", "mtu", "1500", gate, "netmask", "255.255.255.255", "up", NULL }; char *down_argv[] = { "ifconfig", dev, "0.0.0.0", "down", NULL }; char dev_file[sizeof("/dev/tapxxxx\0")], buf[256], c; int tap, minor; if(collect_output) o = &output; if(setreuid(0, 0) < 0){ output_errno(o, "setreuid"); output_fail(o, control_fd); } if(gate != NULL){ sscanf(gate, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); if(maybe_insmod(dev, o)) output_fail(o, control_fd); if(do_exec(ifconfig_argv, 1, o)) output_fail(o, control_fd); if((remote != NULL) && route_and_arp(dev, remote, NULL, 1, o)) output_fail(o, control_fd); forward_ip(o); } if(!is_a_device(dev)){ add_output(o, "Device doesn't contain only alphanumeric characters\n", -1); output_fail(o, control_fd); } snprintf(dev_file, sizeof(dev_file) - 1, "/dev/%.7s", dev); /* do a mknod on it in case it doesn't exist. */ if(sscanf(dev_file, "/dev/tap%d", &minor) != 1){ snprintf(buf, sizeof(buf), "failed to get unit number from '%s'\n", dev_file); add_output(o, buf, -1); output_fail(o, control_fd); } minor += TAP_MINOR; mk_node(dev_file, TAP_MAJOR, minor); if((tap = open(dev_file, O_RDWR | O_NONBLOCK)) < 0){ output_errno(o, "open"); output_fail(o, control_fd); } c = 1; if(write(control_fd, &c, sizeof(c)) != sizeof(c)){ output_errno(o, "write"); output_fail(o, control_fd); } write_output(control_fd, o); while(1){ fd_set fds, except; char buf[BUF_SIZE]; int n, max_fd; FD_ZERO(&fds); FD_SET(tap, &fds); FD_SET(data_fd, &fds); FD_SET(control_fd, &fds); except = fds; max_fd = max(max(tap, data_fd), control_fd) + 1; if(select(max_fd, &fds, NULL, &except, NULL) < 0){ perror("select"); continue; } if(FD_ISSET(tap, &fds)){ n = read(tap, buf, sizeof(buf)); if(n == 0) break; else if(n < 0){ perror("read"); continue; } n = send(data_fd, buf, n, 0); if((n < 0) && (errno != EAGAIN)){ perror("send"); break; } } else if(FD_ISSET(data_fd, &fds)){ n = recvfrom(data_fd, buf, sizeof(buf), 0, NULL, NULL); if(n == 0) break; else if(n < 0) perror("recvfrom"); n = write(tap, buf, n); if(n < 0) perror("write"); } else if(FD_ISSET(control_fd, &fds)){ if((*control_cb)(control_fd, dev, o)) break; } } if(gate != NULL) do_exec(down_argv, 0, NULL); if(remote != NULL) no_route_and_arp(dev, remote, NULL, NULL); } struct addr_change_v1_v3 { enum change_type what; unsigned char addr[4]; }; static int control_v1_v3(int fd, char *dev, struct output *output) { struct addr_change_v1_v3 change; int n; n = read(fd, &change, sizeof(change)); if(n == sizeof(change)) address_change(change.what, change.addr, dev, NULL, output); else if(n == 0) return(1); else { fprintf(stderr, "read from UML failed, n = %d, errno = %d\n", n, errno); return(1); } if(output) write_output(fd, output); return(0); } struct addr_change_v4 { enum change_type what; unsigned char addr[4]; unsigned char netmask[4]; }; static int control_v4(int fd, char *dev, struct output *output) { struct addr_change_v4 change; int n; n = read(fd, &change, sizeof(change)); if(n == sizeof(change)) address_change(change.what, change.addr, dev, change.netmask, output); else if(n == 0) return(1); else { fprintf(stderr, "read from UML failed, n = %d, errno = %d\n", n, errno); return(1); } if(output) write_output(fd, output); return(0); } void ethertap_v0(int argc, char **argv) { char *dev = argv[0]; int fd = atoi(argv[1]); char *gate_addr = NULL; char *remote_addr = NULL; if(argc > 2){ gate_addr = argv[2]; remote_addr = argv[3]; } ethertap(dev, fd, -1, gate_addr, remote_addr, 0, NULL); } void ethertap_v1_v2(int argc, char **argv) { char *dev = argv[0]; int data_fd = atoi(argv[1]); int control_fd = atoi(argv[2]); char *gate_addr = NULL; if(argc > 3) gate_addr = argv[3]; ethertap(dev, data_fd, control_fd, gate_addr, NULL, 0, control_v1_v3); } void ethertap_v3(int argc, char **argv) { char *dev = argv[0]; int data_fd = atoi(argv[1]); char *gate = argv[2]; ethertap(dev, data_fd, 1, gate, NULL, 1, control_v1_v3); } void ethertap_v4(int argc, char **argv) { char *dev, *gate; int data_fd; struct output output = INIT_OUTPUT; if(argc < 2){ add_output(&output, "uml_net : Too few arguments to ethertap_v4\n", -1); output_fail(&output, 1); } dev = argv[0]; data_fd = atoi(argv[1]); gate = argc >= 3 ? argv[2] : NULL; ethertap(dev, data_fd, 1, gate, NULL, 1, control_v4); } uml-utilities-20070815.4/uml_net/host.c000066400000000000000000000155731362720355200175360ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include "output.h" #include "host.h" #include int do_exec(char **args, int need_zero, struct output *output) { int pid, status, fds[2], n; char buf[256], **arg; if(output){ add_output(output, "*", -1); for(arg = args; *arg; arg++){ add_output(output, " ", -1); add_output(output, *arg, -1); } add_output(output, "\n", -1); if(pipe(fds) < 0){ perror("Creating pipe"); output = NULL; } } if((pid = fork()) == 0){ if(output){ close(fds[0]); if((dup2(fds[1], 1) < 0) || (dup2(fds[1], 2) < 0)) perror("dup2"); } execvp(args[0], args); fprintf(stderr, "Failed to exec '%s'", args[0]); perror(""); exit(1); } else if(pid < 0){ output_errno(output, "fork failed"); return(-1); } if(output){ close(fds[1]); while((n = read(fds[0], buf, sizeof(buf))) > 0) add_output(output, buf, n); if(n < 0) output_errno(output, "Reading command output"); } if(waitpid(pid, &status, 0) < 0){ output_errno(output, "waitpid"); return(-1); } if(need_zero && (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))){ sprintf(buf, "'%s' didn't exit with status 0\n", args[0]); add_output(output, buf, -1); return(-1); } return(0); } static void local_net_do(char **argv, int if_index, char *addr_str, char *netmask_str, char *skip_dev, struct output *output) { FILE *fp; unsigned long addr, netmask, if_addr; int a[4], n[4]; char buf[1024], iface[32], out[1024]; if(sscanf(addr_str, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4){ add_output(output, "local_net_do didn't parse address", -1); return; } addr = (a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]; if(sscanf(netmask_str, "%d.%d.%d.%d", &n[0], &n[1], &n[2], &n[3]) != 4){ add_output(output, "local_net_do didn't parse netmask", -1); return; } netmask = (n[3] << 24) | (n[2] << 16) | (n[1] << 8) | n[0]; argv[if_index] = iface; if((fp = fopen("/proc/net/route", "r")) == NULL){ output_errno(output, "Couldn't open /proc/net/route"); return; } if(fgets(buf, sizeof(buf), fp) == NULL) return; buf[0] = '\0'; while(fgets(buf, sizeof(buf), fp) != NULL){ int n; n = sscanf(buf, "%32s %lx %*x %*x %*x %*x %*x", iface, &if_addr); if(n != 2){ sprintf(out, "Didn't parse /proc/net/route, got %d\n on '%s'", n, buf); add_output(output, out, -1); return; } if(strcmp(skip_dev, iface) && ((addr & netmask) == (if_addr & netmask))) do_exec(argv, 0, output); buf[0] = '\0'; } fclose(fp); } int is_a_device(char *dev) { unsigned i; for(i = 0; i < strlen(dev); i++){ if(!isalnum(dev[i])) return(0); } return(1); } int route_and_arp(char *dev, char *addr, char *netmask, int need_route, struct output *output) { char echo[sizeof("echo 1 > /proc/sys/net/ipv4/conf/XXXXXXXXX/proxy_arp")]; char *echo_argv[] = { "bash", "-c", echo, NULL }; char *route_argv[] = { "route", "add", "-host", addr, "dev", dev, NULL }; char *arp_argv[] = { "arp", "-Ds", addr, "eth0", "pub", NULL }; if(!is_a_device(dev)){ add_output(output, "Device doesn't contain only alphanumeric characters\n", -1); return(-1); } if(do_exec(route_argv, need_route, output)) return(-1); snprintf(echo, sizeof(echo) - 1, "echo 1 > /proc/sys/net/ipv4/conf/%.9s/proxy_arp", dev); do_exec(echo_argv, 0, output); if(netmask) local_net_do(arp_argv, 3, addr, netmask, dev, output); else do_exec(arp_argv, 0, output); return(0); } int no_route_and_arp(char *dev, char *addr, char *netmask, struct output *output) { char echo[sizeof("echo 0 > /proc/sys/net/ipv4/conf/XXXXXXXXX/proxy_arp")]; char *no_echo_argv[] = { "bash", "-c", echo, NULL }; char *no_route_argv[] = { "route", "del", "-host", addr, "dev", dev, NULL }; char *no_arp_argv[] = { "arp", "-i", "eth0", "-d", addr, "pub", NULL }; if(!is_a_device(dev)){ add_output(output, "Device doesn't contain only alphanumeric characters\n", -1); return(-1); } do_exec(no_route_argv, 0, output); snprintf(echo, sizeof(echo) - 1, "echo 0 > /proc/sys/net/ipv4/conf/%.9s/proxy_arp", dev); do_exec(no_echo_argv, 0, output); if(netmask) local_net_do(no_arp_argv, 2, addr, netmask, dev, output); else do_exec(no_arp_argv, 0, output); return(0); } void forward_ip(struct output *output) { char *forw_argv[] = { "bash", "-c", "echo 1 > /proc/sys/net/ipv4/ip_forward", NULL }; do_exec(forw_argv, 0, output); } void address_change(enum change_type what, unsigned char *addr_str, char *dev, unsigned char *netmask_str, struct output *output) { char addr[sizeof("255.255.255.255\0")]; char netmask[sizeof("255.255.255.255\0")], *n = NULL; snprintf(addr, sizeof(addr) - 1, "%d.%d.%d.%d", addr_str[0], addr_str[1], addr_str[2], addr_str[3]); if(netmask_str != NULL){ snprintf(netmask, sizeof(netmask) - 1, "%d.%d.%d.%d", netmask_str[0], netmask_str[1], netmask_str[2], netmask_str[3]); n = netmask; } switch(what){ case ADD_ADDR: route_and_arp(dev, addr, n, 0, output); break; case DEL_ADDR: no_route_and_arp(dev, addr, n, output); break; default: fprintf(stderr, "address_change - bad op : %d\n", what); return; } } /* This is a routine to do a 'mknod' on the /dev/tap if possible: * Return: 0 is ok, -1=already open, etc. */ int mk_node(char *devname, int major, int minor) { struct stat statval; int retval; /* first do a stat on the node to see whether it exists and we * had some other reason to fail: */ retval = stat(devname, &statval); if(retval == 0) return(0); else if(errno != ENOENT){ /* it does exist. We are just going to return -1, 'cause there * was some other problem in the open :-(. */ return -1; } /* It doesn't exist. We can create it. */ return(mknod(devname, S_IFCHR|S_IREAD|S_IWRITE, makedev(major, minor))); } void add_address_v4(int argc, char **argv) { struct output output = INIT_OUTPUT; if(setreuid(0, 0) < 0){ output_errno(&output, "setreuid"); exit(1); } route_and_arp(argv[0], argv[1], argv[2], 0, &output); write_output(1, &output); } void del_address_v4(int argc, char **argv) { struct output output = INIT_OUTPUT; if(setreuid(0, 0) < 0){ output_errno(&output, "setreuid"); exit(1); } no_route_and_arp(argv[0], argv[1], argv[2], &output); write_output(1, &output); } void change_addr(char *op, char *dev, char *address, char *netmask, struct output *output) { if(setreuid(0, 0) < 0){ output_errno(output, "setreuid"); exit(1); } if(!strcmp(op, "add")) route_and_arp(dev, address, netmask, 0, output); else no_route_and_arp(dev, address, netmask, output); } uml-utilities-20070815.4/uml_net/host.h000066400000000000000000000015001362720355200175240ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike * Licensed under the GPL */ #ifndef __HOST_H #define __HOST_H #include "output.h" enum change_type { ADD_ADDR, DEL_ADDR }; extern int do_exec(char **args, int need_zero, struct output *output); extern int route_and_arp(char *dev, char *addr, char *netmask, int need_route, struct output *output); extern int no_route_and_arp(char *dev, char *addr, char *netmask, struct output *output); extern void forward_ip(struct output *output); extern void address_change(enum change_type what, unsigned char *addr_str, char *dev, unsigned char *netmask, struct output *output); extern int mk_node(char *devname, int major, int minor); extern void change_addr(char *op, char *dev, char *address, char *netmask, struct output *output); extern int is_a_device(char *dev); #endif uml-utilities-20070815.4/uml_net/output.c000066400000000000000000000033121362720355200201050ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include "output.h" void write_output(int fd, struct output *output) { if(output == NULL) return; if(write(fd, &output->used, sizeof(output->used)) != sizeof(output->used)){ perror("write_output : write of length failed"); exit(1); } if(write(fd, output->buffer, output->used) != output->used){ perror("write_output : write of data failed"); exit(1); } output->used = 0; } void add_output(struct output *output, char *new, int len) { int n, start; if(output == NULL){ fprintf(stderr, "%s", new); return; } if(len == -1) len = strlen(new); n = len; if(output->used == 0) n++; if(output->used + n > output->total){ output->total = output->used + n; output->total = (output->total + 4095) & ~4095; if(output->buffer == NULL){ if((output->buffer = malloc(output->total)) == NULL){ perror("mallocing new output buffer"); *output = ((struct output) { 0, 0, NULL}); return; } } else if((output->buffer = realloc(output->buffer, output->total)) == NULL){ perror("reallocing new output buffer"); *output = ((struct output) { 0, 0, NULL}); return; } } if(output->used == 0) start = 0; else start = output->used - 1; strncpy(&output->buffer[start], new, len); output->used += n; output->buffer[output->used - 1] = '\0'; } void output_errno(struct output *output, char *str) { if(output == NULL) perror(str); else { add_output(output, str, -1); add_output(output, ": ", -1); add_output(output, strerror(errno), -1); add_output(output, "\n", -1); } } uml-utilities-20070815.4/uml_net/output.h000066400000000000000000000006161362720355200201160ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #ifndef __OUTPUT_H #define __OUTPUT_H struct output { int total; int used; char *buffer; }; #define INIT_OUTPUT { 0, 0, NULL } extern void write_output(int fd, struct output *output); extern void add_output(struct output *output, char *new, int len); extern void output_errno(struct output *output, char *str); #endif uml-utilities-20070815.4/uml_net/slip.c000066400000000000000000000072661362720355200175300ustar00rootroot00000000000000/* Copyright 2001, 2002 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include #include #include "host.h" #include "output.h" static void slip_up(int fd, char *gate_addr, char *remote_addr, char *netmask, struct output *output) { char slip_name[sizeof("slxxxx\0")]; char *up_argv[] = { "ifconfig", slip_name, gate_addr, "mtu", "1500", "up", NULL }; int disc, sencap, n; disc = N_SLIP; if((n = ioctl(fd, TIOCSETD, &disc)) < 0){ output_errno(output, "Setting slip line discipline"); write_output(1, output); exit(1); } sencap = 0; if(ioctl(fd, SIOCSIFENCAP, &sencap) < 0){ output_errno(output, "Setting slip encapsulation"); write_output(1, output); exit(1); } snprintf(slip_name, sizeof(slip_name) - 1, "sl%d", n); if(do_exec(up_argv, 1, output)){ write_output(1, output); exit(1); } forward_ip(output); if(remote_addr != NULL) route_and_arp(slip_name, remote_addr, netmask, 0, output); } static void slip_down(char *dev, char *remote_addr, char *netmask, struct output *output) { char *down_argv[] = { "ifconfig", dev, "0.0.0.0", "down", NULL }; if(remote_addr != NULL) no_route_and_arp(dev, remote_addr, netmask, output); if(do_exec(down_argv, 1, output)){ write_output(1, output); exit(1); } } static void slip_name(int fd, char *name, struct output *output) { if(ioctl(fd, SIOCGIFNAME, name) < 0){ output_errno(output, "Getting slip line discipline"); write_output(1, output); exit(1); } } void slip_v0_v2(int argc, char **argv) { char *op = argv[0], dev[sizeof("slnnnnn\0")]; if(setreuid(0, 0) < 0){ perror("slip - setreuid failed"); exit(1); } if(!strcmp(op, "up")) slip_up(atoi(argv[1]), argv[2], argv[3], NULL, NULL); else if(!strcmp(op, "down")){ slip_name(atoi(argv[1]), dev, NULL); slip_down(dev, argv[2], NULL, NULL); } else { printf("slip - Unknown op '%s'\n", op); exit(1); } } void slip_v3(int argc, char **argv) { struct output output = INIT_OUTPUT; char *op = argv[0], dev[sizeof("slnnnnn\0")]; if(setreuid(0, 0) < 0){ output_errno(&output, "slip - setreuid failed"); exit(1); } if(!strcmp(op, "up")) slip_up(atoi(argv[1]), argv[2], argv[3], NULL, &output); else if(!strcmp(op, "down")){ slip_name(atoi(argv[1]), dev, &output); slip_down(dev, argv[2], NULL, &output); } else { printf("slip - Unknown op '%s'\n", op); exit(1); } write_output(1, &output); } void slip_v4(int argc, char **argv) { struct output output = INIT_OUTPUT; char *op, dev[sizeof("slnnnnn\0")]; if(setreuid(0, 0) < 0){ output_errno(&output, "slip - setreuid failed"); exit(1); } if(argc < 1){ add_output(&output, "uml_net : too few arguments to slip_v4\n", -1); write_output(1, &output); exit(1); } op = argv[0]; if(argc < 2){ add_output(&output, "uml_net : too few arguments to slip_v4\n", -1); write_output(1, &output); exit(1); } if(!strcmp(op, "up")){ slip_up(0, argv[1], NULL, NULL, &output); } else if(!strcmp(op, "down")){ /* We now pass the interface to the "down" command as an open FD, rather * than as a name, because the latter allowed anybody to use uml_net to down * a interface he didn't own. * * Instead, as Steve Schmidtke said, an open FD is a proof that you own the * interface, so this way we should be safe. */ slip_name(0, dev, &output); slip_down(dev, NULL, NULL, &output); } else { printf("slip - Unknown op '%s'\n", op); exit(1); } write_output(1, &output); } uml-utilities-20070815.4/uml_net/tuntap.c000066400000000000000000000076211362720355200200670ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include #include #include #include #include "host.h" #include "output.h" static int do_tuntap_up(char *gate_addr, struct output *output, struct ifreq *ifr) { int tap_fd; char *ifconfig_argv[] = { "ifconfig", ifr->ifr_name, gate_addr, "netmask", "255.255.255.255", "up", NULL }; char *insmod_argv[] = { "modprobe", "tun", NULL }; if(setreuid(0, 0) < 0){ output_errno(output, "setreuid to root failed : "); return(-1); } /* XXX hardcoded dir name and perms */ umask(0); mkdir("/dev/net", 0755); /* #includes for MISC_MAJOR and TUN_MINOR bomb in userspace */ mk_node("/dev/net/tun", 10, 200); do_exec(insmod_argv, 0, output); if((tap_fd = open("/dev/net/tun", O_RDWR)) < 0){ output_errno(output, "Opening /dev/net/tun failed : "); return(-1); } memset(ifr, 0, sizeof(*ifr)); ifr->ifr_flags = IFF_TAP | IFF_NO_PI; ifr->ifr_name[0] = '\0'; if(ioctl(tap_fd, TUNSETIFF, (void *) ifr) < 0){ output_errno(output, "TUNSETIFF : "); return(-1); } if((*gate_addr != '\0') && do_exec(ifconfig_argv, 1, output)) return(-1); forward_ip(output); return(tap_fd); } static void tuntap_up(int fd, int argc, char **argv) { struct ifreq ifr; struct msghdr msg; struct cmsghdr *cmsg; struct output output = INIT_OUTPUT; struct iovec iov[2]; int tap_fd, *fd_ptr; char anc[CMSG_SPACE(sizeof(tap_fd))]; char *gate_addr; msg.msg_control = NULL; msg.msg_controllen = 0; iov[0].iov_base = NULL; iov[0].iov_len = 0; if(argc < 1){ add_output(&output, "Too few arguments to tuntap_up\n", -1); goto out; } gate_addr = argv[0]; if((tap_fd = do_tuntap_up(gate_addr, &output, &ifr)) > 0){ iov[0].iov_base = ifr.ifr_name; iov[0].iov_len = IFNAMSIZ; msg.msg_control = anc; msg.msg_controllen = sizeof(anc); cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(tap_fd)); msg.msg_controllen = cmsg->cmsg_len; fd_ptr = (int *) CMSG_DATA(cmsg); *fd_ptr = tap_fd; } out: iov[1].iov_base = output.buffer; iov[1].iov_len = output.used; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = iov; msg.msg_iovlen = sizeof(iov)/sizeof(iov[0]); msg.msg_flags = 0; if(sendmsg(fd, &msg, 0) < 0){ perror("sendmsg"); exit(1); } } void tuntap_v2(int argc, char **argv) { char *op = argv[0]; if(!strcmp(op, "up")) tuntap_up(atoi(argv[2]), argc - 3, argv + 3); else if(!strcmp(op, "add") || !strcmp(op, "del")) change_addr(argv[1], argv[2], argv[3], NULL, NULL); else { fprintf(stderr, "Bad tuntap op : '%s'\n", op); exit(1); } } void tuntap_v3(int argc, char **argv) { char *op = argv[0]; struct output output = INIT_OUTPUT; if(!strcmp(op, "up")) tuntap_up(1, argc - 2, argv + 2); else if(!strcmp(op, "add") || !strcmp(op, "del")) change_addr(op, argv[1], argv[2], NULL, &output); else { fprintf(stderr, "Bad tuntap op : '%s'\n", op); exit(1); } write_output(1, &output); } void tuntap_v4(int argc, char **argv) { char *op; struct output output = INIT_OUTPUT; if(argc < 1){ add_output(&output, "uml_net : too few arguments to tuntap_v4\n", -1); write_output(1, &output); exit(1); } op = argv[0]; if(!strcmp(op, "up")) tuntap_up(1, argc - 1, argv + 1); else if(!strcmp(op, "add") || !strcmp(op, "del")){ if(argc < 4) add_output(&output, "uml_net : too few arguments to tuntap " "change_addr\n", -1); else change_addr(op, argv[1], argv[2], argv[3], &output); write_output(1, &output); } else { fprintf(stderr, "Bad tuntap op : '%s'\n", op); exit(1); } } uml-utilities-20070815.4/uml_net/uml_net.c000066400000000000000000000060031362720355200202100ustar00rootroot00000000000000/* Copyright 2001 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #define CURRENT_VERSION (4) extern void ethertap_v0(int argc, char **argv); extern void ethertap_v1_v2(int argc, char **argv); extern void ethertap_v1_v2(int argc, char **argv); extern void ethertap_v3(int argc, char **argv); extern void ethertap_v4(int argc, char **argv); void (*ethertap_handlers[])(int argc, char **argv) = { ethertap_v0, ethertap_v1_v2, ethertap_v1_v2, ethertap_v3, ethertap_v4, }; extern void slip_v0_v2(int argc, char **argv); extern void slip_v3(int argc, char **argv); extern void slip_v4(int argc, char **argv); void (*slip_handlers[])(int argc, char **argv) = { slip_v0_v2, slip_v0_v2, slip_v0_v2, slip_v3, slip_v4, }; #ifdef TUNTAP extern void tuntap_v2(int argc, char **argv); extern void tuntap_v3(int argc, char **argv); extern void tuntap_v4(int argc, char **argv); void (*tuntap_handlers[])(int argc, char **argv) = { NULL, NULL, tuntap_v2, tuntap_v3, tuntap_v4, }; #endif extern void add_address_v4(int argc, char **argv); void (*add_handlers[])(int argc, char **argv) = { NULL, NULL, NULL, NULL, add_address_v4, }; extern void del_address_v4(int argc, char **argv); void (*del_handlers[])(int argc, char **argv) = { NULL, NULL, NULL, NULL, del_address_v4, }; static void handler(int sig) { } int main(int argc, char **argv) { char *version = argv[1]; char *transport = argv[2]; void (**handlers)(int, char **); struct sigaction sa; char *out; int n = 3; unsigned int v; if(sigaction(SIGCHLD, NULL, &sa) < 0){ perror("sigaction"); exit(1); } sa.sa_handler = handler; sa.sa_flags &= ~SA_NOCLDWAIT; sa.sa_flags |= SA_RESTART; if(sigaction(SIGCHLD, &sa, NULL) < 0){ perror("sigaction"); exit(1); } if(argc < 3){ fprintf(stderr, "uml_net : bad argument list - if you're running uml_net by\n" "hand, don't bother. uml_net is run automatically by UML when\n" "necessary.\n"); exit(1); } setenv("PATH", "/bin:/usr/bin:/sbin:/usr/sbin", 1); v = strtoul(version, &out, 0); if(out != version){ if(v > CURRENT_VERSION){ fprintf(stderr, "Version mismatch - requested version %d, uml_net " "supports up to version %d\n", v, CURRENT_VERSION); exit(1); } } else { v = 0; transport = version; n = 2; } if(!strcmp(transport, "ethertap")) handlers = ethertap_handlers; else if(!strcmp(transport, "slip")) handlers = slip_handlers; #ifdef TUNTAP else if(!strcmp(transport, "tuntap")) handlers = tuntap_handlers; #endif else if(!strcmp(transport, "add")) handlers = add_handlers; else if(!strcmp(transport, "del")) handlers = del_handlers; else { printf("Unknown transport : '%s'\n", transport); exit(1); } if(handlers[v] != NULL) (*handlers[v])(argc - n, &argv[n]); else { printf("No version #%d handler for '%s'\n", v, transport); exit(1); } return(0); } uml-utilities-20070815.4/uml_switch/000077500000000000000000000000001362720355200171155ustar00rootroot00000000000000uml-utilities-20070815.4/uml_switch/.cvsignore000066400000000000000000000000141362720355200211100ustar00rootroot00000000000000uml_switch uml-utilities-20070815.4/uml_switch/CVS/000077500000000000000000000000001362720355200175505ustar00rootroot00000000000000uml-utilities-20070815.4/uml_switch/CVS/Entries000066400000000000000000000006341362720355200211070ustar00rootroot00000000000000/.cvsignore/1.1/Fri Jan 11 05:21:31 2002// /switch.h/1.1/Sun Apr 7 17:57:12 2002// /tuntap.h/1.1/Wed Apr 10 11:21:56 2002// /port.h/1.1/Wed Apr 10 13:07:02 2002// /hash.h/1.2/Tue Apr 9 14:38:23 2002// /hash.c/1.3/Sat Apr 27 15:42:04 2002// /tuntap.c/1.2/Wed Apr 24 00:13:30 2002// /Makefile/1.10/Sat Feb 8 04:04:39 2003// /port.c/1.4/Wed Mar 12 15:19:03 2003// /uml_switch.c/1.11/Fri Aug 15 19:03:58 2003// D uml-utilities-20070815.4/uml_switch/CVS/Repository000066400000000000000000000000211362720355200216430ustar00rootroot00000000000000tools/uml_router uml-utilities-20070815.4/uml_switch/CVS/Root000066400000000000000000000001031362720355200204100ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/uml_switch/Makefile000066400000000000000000000006551362720355200205630ustar00rootroot00000000000000TUNTAP = $(shell [ -e /usr/include/linux/if_tun.h ] && echo -DTUNTAP) OBJS = hash.o port.o uml_switch.o BIN = uml_switch CFLAGS ?= -g -Wall override CFLAGS += $(TUNTAP) BIN_DIR ?= /usr/bin ifneq ($(TUNTAP),) OBJS += tuntap.o endif all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/uml_switch/hash.c000066400000000000000000000066621362720355200202160ustar00rootroot00000000000000/* Copyright 2002 Yon Uriarte and Jeff Dike * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include #include #include "switch.h" #include "hash.h" #define HASH_SIZE 128 #define HASH_MOD 11 struct hash_entry { struct hash_entry *next; struct hash_entry *prev; time_t last_seen; void *port; unsigned char dst[ETH_ALEN]; }; static struct hash_entry *h[HASH_SIZE]; static int calc_hash(unsigned char *src) { return ((*(u_int32_t *) &src[0] % HASH_MOD) ^ src[4] ^ src[5] ) % HASH_SIZE ; } static struct hash_entry *find_entry(unsigned char *dst) { struct hash_entry *e; int k = calc_hash(dst); for(e = h[k]; e; e = e->next){ if(!memcmp(&e->dst, dst, ETH_ALEN)) return(e); } return(NULL); } void *find_in_hash(unsigned char *dst) { struct hash_entry *e = find_entry(dst); if(e == NULL) return(NULL); return(e->port); } void insert_into_hash(unsigned char *src, void *port) { struct hash_entry *new; int k = calc_hash(src); new = find_in_hash(src); if(new != NULL) return; new = malloc(sizeof(*new)); if(new == NULL){ perror("Failed to malloc hash entry"); return; } memcpy(&new->dst, src, ETH_ALEN ); if(h[k] != NULL) h[k]->prev = new; new->next = h[k]; new->prev = NULL; new->port = port; new->last_seen = 0; h[k] = new; } void update_entry_time(unsigned char *src) { struct hash_entry *e; e = find_entry(src); if(e == NULL) return; e->last_seen = time(NULL); } static void delete_hash_entry(struct hash_entry *old) { int k = calc_hash(old->dst); if(old->prev != NULL) old->prev->next = old->next; if(old->next != NULL) old->next->prev = old->prev; if(h[k] == old) h[k] = old->next; free(old); } void delete_hash(unsigned char *dst) { struct hash_entry *old = find_entry(dst); if(old == NULL) return; delete_hash_entry(old); } static void for_all_hash(void (*f)(struct hash_entry *, void *), void *arg) { int i; struct hash_entry *e, *next; for(i = 0; i < HASH_SIZE; i++){ for(e = h[i]; e; e = next){ next = e->next; (*f)(e, arg); } } } struct printer { time_t now; char *(*port_id)(void *); }; static void print_hash_entry(struct hash_entry *e, void *arg) { struct printer *p = arg; printf("Hash: %d Addr: %02x:%02x:%02x:%02x:%02x:%02x to port: %s " "age %ld secs\n", calc_hash(e->dst), e->dst[0], e->dst[1], e->dst[2], e->dst[3], e->dst[4], e->dst[5], (*p->port_id)(e->port), (int) p->now - e->last_seen); } void print_hash(char *(*port_id)(void *)) { struct printer p = ((struct printer) { now : time(NULL), port_id : port_id }); for_all_hash(print_hash_entry, &p); } #define GC_INTERVAL 2 #define GC_EXPIRE 100 static void gc(struct hash_entry *e, void *now) { time_t t = *(time_t *) now; if(e->last_seen + GC_EXPIRE < t) delete_hash_entry(e); } static void sig_alarm(int sig) { struct itimerval it; time_t t = time(NULL); for_all_hash(&gc, &t); it.it_value.tv_sec = GC_INTERVAL; it.it_value.tv_usec = 0 ; it.it_interval.tv_sec = 0; it.it_interval.tv_usec = 0 ; setitimer(ITIMER_REAL, &it, NULL); } void hash_init(void) { struct sigaction sa; sa.sa_handler = sig_alarm; sa.sa_flags = SA_RESTART; if(sigaction(SIGALRM, &sa, NULL) < 0){ perror("Setting handler for SIGALRM"); return; } kill(getpid(), SIGALRM); } uml-utilities-20070815.4/uml_switch/hash.h000066400000000000000000000006261362720355200202150ustar00rootroot00000000000000/* Copyright 2002 Yon Uriarte and Jeff Dike * Licensed under the GPL */ #ifndef __HASH_H__ #define __HASH_H__ extern void *find_in_hash(unsigned char *dst); extern void insert_into_hash(unsigned char *src, void *port); extern void delete_hash(unsigned char *dst); extern void print_hash(char *(*port_id)(void *)); extern void update_entry_time(unsigned char *src); extern void hash_init(void); #endif uml-utilities-20070815.4/uml_switch/port.c000066400000000000000000000136011362720355200202460ustar00rootroot00000000000000#include #include #include #include #include #include #include "switch.h" #include "hash.h" #include "port.h" struct packet { struct { unsigned char dest[ETH_ALEN]; unsigned char src[ETH_ALEN]; unsigned char proto[2]; } header; unsigned char data[1500]; }; struct port { struct port *next; struct port *prev; int control; void *data; int data_len; unsigned char src[ETH_ALEN]; void (*sender)(int fd, void *packet, int len, void *data); }; static struct port *head = NULL; #define IS_BROADCAST(addr) ((addr[0] & 1) == 1) static void free_port(struct port *port) { if(port->prev) port->prev->next = port->next; else head = port->next; if(port->next) port->next->prev = port->prev; free(port); } void close_port(int fd) { struct port *port; for(port = head; port != NULL; port = port->next){ if(port->control == fd) break; } if(port == NULL){ fprintf(stderr, "No port associated with descriptor %d\n", fd); return; } delete_hash(port->src); free_port(port); } static void update_src(struct port *port, struct packet *p) { struct port *last; /* We don't like broadcast source addresses */ if(IS_BROADCAST(p->header.src)) return; last = find_in_hash(p->header.src); if(port != last){ /* old value differs from actual input port */ printf(" Addr: %02x:%02x:%02x:%02x:%02x:%02x New port %d", p->header.src[0], p->header.src[1], p->header.src[2], p->header.src[3], p->header.src[4], p->header.src[5], port->control); if(last != NULL){ printf(" old port %d", last->control); delete_hash(p->header.src); } printf("\n"); memcpy(port->src, p->header.src, sizeof(port->src)); insert_into_hash(p->header.src, port); } update_entry_time(p->header.src); } static void send_dst(struct port *port, struct packet *packet, int len, int hub) { struct port *target, *p; target = find_in_hash(packet->header.dest); if((target == NULL) || IS_BROADCAST(packet->header.dest) || hub){ if((target == NULL) && !IS_BROADCAST(packet->header.dest)){ printf("unknown Addr: %02x:%02x:%02x:%02x:%02x:%02x from port ", packet->header.src[0], packet->header.src[1], packet->header.src[2], packet->header.src[3], packet->header.src[4], packet->header.src[5]); if(port == NULL) printf("UNKNOWN\n"); else printf("%d\n", port->control); } /* no cache or broadcast/multicast == all ports */ for(p = head; p != NULL; p = p->next){ /* don't send it back the port it came in */ if(p == port) continue; (*p->sender)(p->control, packet, len, p->data); } } else (*target->sender)(target->control, packet, len, target->data); } static void handle_data(int fd, int hub, struct packet *packet, int len, void *data, int (*matcher)(int port_fd, int data_fd, void *port_data, int port_data_len, void *data)) { struct port *p; for(p = head; p != NULL; p = p->next){ if((*matcher)(p->control, fd, p->data, p->data_len, data)) break; } /* if we have an incoming port (we should) */ if(p != NULL) update_src(p, packet); else printf("Unknown connection for packet, shouldn't happen.\n"); send_dst(p, packet, len, hub); } static int match_tap(int port_fd, int data_fd, void *port_data, int port_data_len, void *data) { return(port_fd == data_fd); } void handle_tap_data(int fd, int hub) { struct packet packet; int len; len = read(fd, &packet, sizeof(packet)); if(len < 0){ if(errno != EAGAIN) perror("Reading tap data"); return; } handle_data(fd, hub, &packet, len, NULL, match_tap); } int setup_port(int fd, void (*sender)(int fd, void *packet, int len, void *data), void *data, int data_len) { struct port *port; port = malloc(sizeof(struct port)); if(port == NULL){ perror("malloc"); return(-1); } port->next = head; if(head) head->prev = port; port->prev = NULL; port->control = fd; port->data = data; port->data_len = data_len; port->sender = sender; head = port; printf("New connection\n"); return(0); } struct sock_data { int fd; struct sockaddr_un sock; }; static void send_sock(int fd, void *packet, int len, void *data) { struct sock_data *mine = data; int err; err = sendto(mine->fd, packet, len, 0, (struct sockaddr *) &mine->sock, sizeof(mine->sock)); if(err != len){ fprintf(stderr, "send_sock sending to fd %d ", mine->fd); perror(""); } } static int match_sock(int port_fd, int data_fd, void *port_data, int port_data_len, void *data) { struct sock_data *mine = data; struct sock_data *port = port_data; if(port_data_len != sizeof(*mine)) return(0); return(!memcmp(&port->sock, &mine->sock, sizeof(mine->sock))); } void handle_sock_data(int fd, int hub) { struct packet packet; struct sock_data data; int len; unsigned int socklen = sizeof(data.sock); len = recvfrom(fd, &packet, sizeof(packet), 0, (struct sockaddr *) &data.sock, &socklen); if(len < 0){ if(errno != EAGAIN) perror("handle_sock_data"); return; } data.fd = fd; handle_data(fd, hub, &packet, len, &data, match_sock); } int setup_sock_port(int fd, struct sockaddr_un *name, int data_fd) { struct sock_data *data; data = malloc(sizeof(*data)); if(data == NULL){ perror("setup_sock_port"); return(-1); } *data = ((struct sock_data) { fd : data_fd, sock : *name }); return(setup_port(fd, send_sock, data, sizeof(*data))); } static void service_port(struct port *port) { int n; char c; n = read(port->control, &c, sizeof(c)); if(n < 0) perror("Reading request"); else if(n == 0) printf("Disconnect\n"); else printf("Bad request\n"); } int handle_port(int fd) { struct port *p; for(p = head; p != NULL; p = p->next){ if(p->control == fd){ service_port(p); return(0); } } return(1); } uml-utilities-20070815.4/uml_switch/port.h000066400000000000000000000010071362720355200202500ustar00rootroot00000000000000/* Copyright 2002 Jeff Dike * Licensed under the GPL */ #ifndef __PORT_H__ #define __PORT_H__ #include #include extern int handle_port(int fd); extern void close_port(int fd); extern int setup_sock_port(int fd, struct sockaddr_un *name, int data_fd); extern int setup_port(int fd, void (*sender)(int fd, void *packet, int len, void *data), void *data, int data_len); extern void handle_tap_data(int fd, int hub); extern void handle_sock_data(int fd, int hub); #endif uml-utilities-20070815.4/uml_switch/switch.h000066400000000000000000000002011362720355200205600ustar00rootroot00000000000000/* Copyright 2002 Jeff Dike * Licensed under the GPL */ #ifndef __SWITCH_H__ #define __SWITCH_H__ #define ETH_ALEN 6 #endif uml-utilities-20070815.4/uml_switch/tuntap.c000066400000000000000000000015301362720355200205730ustar00rootroot00000000000000#include #include #include #include #include #include #include #include #include "port.h" static void send_tap(int fd, void *packet, int len, void *unused) { int n; n = write(fd, packet, len); if(n != len){ if(errno != EAGAIN) perror("send_tap"); } } int open_tap(char *dev) { struct ifreq ifr; int fd, err; if((fd = open("/dev/net/tun", O_RDWR)) < 0){ perror("Failed to open /dev/net/tun"); return(-1); } memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name) - 1); if(ioctl(fd, TUNSETIFF, (void *) &ifr) < 0){ perror("TUNSETIFF failed"); close(fd); return(-1); } err = setup_port(fd, send_tap, NULL, 0); if(err) return(err); return(fd); } uml-utilities-20070815.4/uml_switch/tuntap.h000066400000000000000000000002671362720355200206060ustar00rootroot00000000000000/* Copyright 2002 Jeff Dike * Licensed under the GPL */ #ifndef __TUNTAP_H__ #define __TUNTAP_H__ extern int open_tap(char *dev); extern void handle_tap(int fd, int hub); #endif uml-utilities-20070815.4/uml_switch/uml_switch.c000066400000000000000000000310301362720355200214340ustar00rootroot00000000000000/* Copyright 2001, 2002 Jeff Dike and others * Licensed under the GPL */ #include #include #include #include #include #include #include #include #include #include #include #include "switch.h" #include "port.h" #include "hash.h" #ifdef TUNTAP #include "tuntap.h" #endif #ifdef notdef #include #endif static int hub = 0; static int compat_v0 = 0; enum request_type { REQ_NEW_CONTROL }; struct request_v0 { enum request_type type; union { struct { unsigned char addr[ETH_ALEN]; struct sockaddr_un name; } new_control; } u; }; #define SWITCH_MAGIC 0xfeedface struct request_v1 { uint32_t magic; enum request_type type; union { struct { unsigned char addr[ETH_ALEN]; struct sockaddr_un name; } new_control; } u; }; struct request_v2 { uint32_t magic; uint32_t version; enum request_type type; struct sockaddr_un sock; }; struct reply_v2 { unsigned char mac[ETH_ALEN]; struct sockaddr_un sock; }; struct request_v3 { uint32_t magic; uint32_t version; enum request_type type; struct sockaddr_un sock; }; union request { struct request_v0 v0; struct request_v1 v1; struct request_v2 v2; struct request_v3 v3; }; static char *ctl_socket = "/tmp/uml.ctl"; static char *data_socket = NULL; static struct sockaddr_un data_sun; static void cleanup(void) { if(unlink(ctl_socket) < 0){ printf("Couldn't remove control socket '%s' : ", ctl_socket); perror(""); } if((data_socket != NULL) && (unlink(data_socket) < 0)){ printf("Couldn't remove data socket '%s' : ", data_socket); perror(""); } } static struct pollfd *fds = NULL; static int maxfds = 0; static int nfds = 0; static void add_fd(int fd) { struct pollfd *p; if(nfds == maxfds){ maxfds = maxfds ? 2 * maxfds : 4; if((fds = realloc(fds, maxfds * sizeof(struct pollfd))) == NULL){ perror("realloc"); cleanup(); exit(1); } } p = &fds[nfds++]; p->fd = fd; p->events = POLLIN; } static void remove_fd(int fd) { int i; for(i = 0; i < nfds; i++){ if(fds[i].fd == fd) break; } if(i == nfds){ fprintf(stderr, "remove_fd : Couldn't find descriptor %d\n", fd); } memmove(&fds[i], &fds[i + 1], (maxfds - i - 1) * sizeof(struct pollfd)); nfds--; } static void sig_handler(int sig) { printf("Caught signal %d, cleaning up and exiting\n", sig); cleanup(); signal(sig, SIG_DFL); kill(getpid(), sig); } static void close_descriptor(int fd) { remove_fd(fd); close(fd); close_port(fd); } static void new_port_v0(int fd, struct request_v0 *req, int data_fd) { switch(req->type){ case REQ_NEW_CONTROL: setup_sock_port(fd, &req->u.new_control.name, data_fd); break; default: printf("Bad request type : %d\n", req->type); close_descriptor(fd); } } static void new_port_v1_v3(int fd, enum request_type type, struct sockaddr_un *sock, int data_fd) { int n, err; switch(type){ case REQ_NEW_CONTROL: err = setup_sock_port(fd, sock, data_fd); if(err) return; n = write(fd, &data_sun, sizeof(data_sun)); if(n != sizeof(data_sun)){ perror("Sending data socket name"); close_descriptor(fd); } break; default: printf("Bad request type : %d\n", type); close_descriptor(fd); } } static void new_port_v2(int fd) { fprintf(stderr, "Version 2 is not supported\n"); close_descriptor(fd); } static void new_port(int fd, int data_fd) { union request req; int len; len = read(fd, &req, sizeof(req)); if(len < 0){ if(errno != EAGAIN){ perror("Reading request"); close_descriptor(fd); } return; } else if(len == 0){ printf("EOF from new port\n"); close_descriptor(fd); return; } if(req.v1.magic == SWITCH_MAGIC){ if(req.v2.version == 2) new_port_v2(fd); if(req.v3.version == 3) new_port_v1_v3(fd, req.v3.type, &req.v3.sock, data_fd); else if(req.v2.version > 2) fprintf(stderr, "Request for a version %d port, which this " "uml_switch doesn't support\n", req.v2.version); else new_port_v1_v3(fd, req.v1.type, &req.v1.u.new_control.name, data_fd); } else new_port_v0(fd, &req.v0, data_fd); } void accept_connection(int fd) { struct sockaddr addr; unsigned int len; int new; len = sizeof(addr); new = accept(fd, &addr, &len); if(new < 0){ perror("accept"); return; } if(fcntl(new, F_SETFL, O_NONBLOCK) < 0){ perror("fcntl - setting O_NONBLOCK"); close(new); return; } add_fd(new); } int still_used(struct sockaddr_un *sun) { int test_fd, ret = 1; if((test_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0){ perror("socket"); exit(1); } if(connect(test_fd, (struct sockaddr *) sun, sizeof(*sun)) < 0){ if(errno == ECONNREFUSED){ if(unlink(sun->sun_path) < 0){ fprintf(stderr, "Failed to removed unused socket '%s': ", sun->sun_path); perror(""); } ret = 0; } else perror("connect"); } close(test_fd); return(ret); } int bind_socket(int fd, const char *name, struct sockaddr_un *sock_out) { struct sockaddr_un sun; sun.sun_family = AF_UNIX; strncpy(sun.sun_path, name, sizeof(sun.sun_path)); if(bind(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0){ if((errno == EADDRINUSE) && still_used(&sun)) return(EADDRINUSE); else if(bind(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0){ perror("bind"); return(EPERM); } } if(sock_out != NULL) *sock_out = sun; return(0); } static char *prog; void bind_sockets_v0(int ctl_fd, const char *ctl_name, int data_fd, const char *data_name) { int ctl_err, ctl_present = 0, ctl_used = 0; int data_err, data_present = 0, data_used = 0; int try_remove_ctl, try_remove_data; ctl_err = bind_socket(ctl_fd, ctl_name, NULL); if(ctl_err != 0) ctl_present = 1; if(ctl_err == EADDRINUSE) ctl_used = 1; data_err = bind_socket(data_fd, data_name, &data_sun); if(data_err != 0) data_present = 1; if(data_err == EADDRINUSE) data_used = 1; if(!ctl_err && !data_err){ return; } try_remove_ctl = ctl_present; try_remove_data = data_present; if(ctl_present && ctl_used){ fprintf(stderr, "The control socket '%s' has another server " "attached to it\n", ctl_name); try_remove_ctl = 0; } else if(ctl_present && !ctl_used) fprintf(stderr, "The control socket '%s' exists, isn't used, but couldn't " "be removed\n", ctl_name); if(data_present && data_used){ fprintf(stderr, "The data socket '%s' has another server " "attached to it\n", data_name); try_remove_data = 0; } else if(data_present && !data_used) fprintf(stderr, "The data socket '%s' exists, isn't used, but couldn't " "be removed\n", data_name); if(try_remove_ctl || try_remove_data){ fprintf(stderr, "You can either\n"); if(try_remove_ctl && !try_remove_data) fprintf(stderr, "\tremove '%s'\n", ctl_socket); else if(!try_remove_ctl && try_remove_data) fprintf(stderr, "\tremove '%s'\n", data_socket); else fprintf(stderr, "\tremove '%s' and '%s'\n", ctl_socket, data_socket); fprintf(stderr, "\tor rerun with different, unused filenames for " "sockets:\n"); fprintf(stderr, "\t\t%s -unix \n", prog); fprintf(stderr, "\t\tand run the UMLs with " "'eth0=daemon,,unix,,\n"); exit(1); } else { fprintf(stderr, "You should rerun with different, unused filenames for " "sockets:\n"); fprintf(stderr, "\t%s -unix \n", prog); fprintf(stderr, "\tand run the UMLs with " "'eth0=daemon,,unix,,'\n"); exit(1); } } void bind_data_socket(int fd, struct sockaddr_un *sun) { struct { char zero; int pid; int usecs; } name; struct timeval tv; name.zero = 0; name.pid = getpid(); gettimeofday(&tv, NULL); name.usecs = tv.tv_usec; sun->sun_family = AF_UNIX; memcpy(sun->sun_path, &name, sizeof(name)); if(bind(fd, (struct sockaddr *) sun, sizeof(*sun)) < 0){ perror("Binding to data socket"); exit(1); } } void bind_sockets(int ctl_fd, const char *ctl_name, int data_fd) { int err, used = 0; err = bind_socket(ctl_fd, ctl_name, NULL); if(err == 0){ bind_data_socket(data_fd, &data_sun); return; } else if(err == EADDRINUSE) used = 1; if(used){ fprintf(stderr, "The control socket '%s' has another server " "attached to it\n", ctl_name); fprintf(stderr, "You can either\n"); fprintf(stderr, "\tremove '%s'\n", ctl_name); fprintf(stderr, "\tor rerun with a different, unused filename for a " "socket\n"); } else fprintf(stderr, "The control socket '%s' exists, isn't used, but couldn't " "be removed\n", ctl_name); exit(1); } static void Usage(void) { char *tap_str = ""; #ifdef TUNTAP tap_str = "[ -tap tap-device ]"; #endif fprintf(stderr, "Usage : %s [ -unix control-socket ] [ -hub ] %s\n" "or : %s -compat-v0 [ -unix control-socket data-socket ] " "[ -hub ] %s\n", prog, tap_str, prog, tap_str); exit(1); } int main(int argc, char **argv) { int connect_fd, data_fd, n, i, new, one = 1, daemonize = 0; char *tap_dev = NULL; #ifdef TUNTAP int tap_fd = -1; #endif prog = argv[0]; argv++; argc--; while(argc > 0){ if(!strcmp(argv[0], "-unix")){ if(argc < 2) Usage(); ctl_socket = argv[1]; argc -= 2; argv += 2; if(!compat_v0) continue; if(argc < 1) Usage(); data_socket = argv[0]; argc--; argv++; } else if(!strcmp(argv[0], "-tap")){ #ifdef TUNTAP if(argc < 2) Usage(); tap_dev = argv[1]; argv += 2; argc -= 2; #else fprintf(stderr, "-tap isn't supported since TUNTAP isn't enabled\n"); Usage(); #endif } else if(!strcmp(argv[0], "-hub")){ printf("%s will be a hub instead of a switch\n", prog); hub = 1; argc--; argv++; } else if(!strcmp(argv[0], "-compat-v0")){ printf("Control protocol 0 compatibility\n"); compat_v0 = 1; data_socket = "/tmp/uml.data"; argc--; argv++; } else if(!strcmp(argv[0], "-daemon")){ daemonize = 1; argc--; argv++; } else Usage(); } if((connect_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0){ perror("socket"); exit(1); } if(setsockopt(connect_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) < 0){ perror("setsockopt"); exit(1); } if(fcntl(connect_fd, F_SETFL, O_NONBLOCK) < 0){ perror("Setting O_NONBLOCK on connection fd"); exit(1); } if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0){ perror("socket"); exit(1); } if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0){ perror("Setting O_NONBLOCK on data fd"); exit(1); } if(compat_v0) bind_sockets_v0(connect_fd, ctl_socket, data_fd, data_socket); else bind_sockets(connect_fd, ctl_socket, data_fd); if(listen(connect_fd, 15) < 0){ perror("listen"); exit(1); } if(signal(SIGINT, sig_handler) == SIG_ERR) perror("Setting handler for SIGINT"); hash_init(); if(compat_v0) printf("%s attached to unix sockets '%s' and '%s'", prog, ctl_socket, data_socket); else printf("%s attached to unix socket '%s'", prog, ctl_socket); #ifdef TUNTAP if(tap_dev != NULL) printf(" tap device '%s'", tap_dev); #endif printf("\n"); if(isatty(0)) add_fd(0); add_fd(connect_fd); add_fd(data_fd); #ifdef TUNTAP if(tap_dev != NULL) tap_fd = open_tap(tap_dev); if(tap_fd > -1) add_fd(tap_fd); #endif if (daemonize && daemon(0, 1)) { perror("daemon"); exit(1); } while(1){ char buf[128]; n = poll(fds, nfds, -1); if(n < 0){ if(errno == EINTR) continue; perror("poll"); break; } for(i = 0; i < nfds; i++){ if(fds[i].revents == 0) continue; if(fds[i].fd == 0){ if(fds[i].revents & POLLHUP){ printf("EOF on stdin, cleaning up and exiting\n"); goto out; } n = read(0, buf, sizeof(buf)); if(n < 0){ perror("Reading from stdin"); break; } else if(n == 0){ printf("EOF on stdin, cleaning up and exiting\n"); goto out; } } else if(fds[i].fd == connect_fd){ if(fds[i].revents & POLLHUP){ printf("Error on connection fd\n"); continue; } accept_connection(connect_fd); } else if(fds[i].fd == data_fd) handle_sock_data(data_fd, hub); #ifdef TUNTAP else if(fds[i].fd == tap_fd) handle_tap_data(tap_fd, hub); #endif else { new = handle_port(fds[i].fd); if(new) new_port(fds[i].fd, data_fd); else close_descriptor(fds[i].fd); } } } out: cleanup(); return 0; } uml-utilities-20070815.4/uml_util.spec.in000066400000000000000000000023231362720355200200520ustar00rootroot00000000000000# Generated by RPM Workshop. v0.1 # Macro definitions %define ver __UMLVER__ %define rel 1 Name: uml_utilities Version: %{ver} Release: %{rel} Copyright: GPL Packager: Someone URL: http://user-mode-linux.sourceforge.net/index.html Source: http://prdownloads.sourceforge.net/user-mode-linux/uml_utilities_%{ver}.tar.bz2 BuildRoot: %{_tmppath}/%{name}-%{ver}-root Group: System Summary: Utilities for user-mode linux kernel %description This package contains the utilities for user-mode linux for networking, COW, etc. %prep %setup -n tools %build make CFLAGS="-g -Wall -O2" DESTDIR=$RPM_BUILD_ROOT %install make install DESTDIR=$RPM_BUILD_ROOT %files %attr(755,root,root) %{_bindir}/jailtest #%attr(755,root,root) %{_bindir}/mkrootfs %attr(755,root,root) %{_bindir}/tunctl %attr(755,root,root) %{_bindir}/uml_mconsole %attr(755,root,root) %{_bindir}/uml_moo %attr(4755,root,root) %{_bindir}/uml_net %attr(755,root,root) %{_bindir}/uml_switch %attr(755,root,root) %dir %{_libdir}/uml %attr(755,root,root) %{_bindir}/uml_mkcow %attr(755,root,root) %{_bindir}/uml_watchdog #%attr(755,root,root) %{_libdir}/uml/functions %attr(755,root,root) %{_libdir}/uml/port-helper %clean uml-utilities-20070815.4/umlfs/000077500000000000000000000000001362720355200160655ustar00rootroot00000000000000uml-utilities-20070815.4/umlfs/Makefile000066400000000000000000000005721362720355200175310ustar00rootroot00000000000000BIN = uml_mount OBJS = $(BIN).o # I have the FUSE headers in /usr/local/include CFLAGS = -g -Wall -D_FILE_OFFSET_BITS=64 -I/usr/local/include LIBS = -lfuse BIN_DIR ?= /usr/bin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) $(LIBS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/umlfs/uml_mount.c000066400000000000000000000013341362720355200202510ustar00rootroot00000000000000#include #include #include #include #include static int init_fuse(int argc, char **argv) { struct fuse_args args = FUSE_ARGS_INIT(argc, argv); char *mountpoint; if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) == -1) return -EINVAL; return fuse_mount(mountpoint, &args); } int main(int argc, char **argv) { char *umid, fd_arg[sizeof("1234567890\0")]; int fd; umid = argv[1]; argv[1] = argv[0]; argv++; argc--; fd = init_fuse(argc, argv); if(fd < 0) return 1; snprintf(fd_arg, sizeof(fd_arg), "%d", fd); execlp("uml_mconsole", "uml_mconsole", umid, "umlfs", fd_arg, NULL); perror("Failed to exec uml_mconsole\n"); return 1; } uml-utilities-20070815.4/umlgdb/000077500000000000000000000000001362720355200162115ustar00rootroot00000000000000uml-utilities-20070815.4/umlgdb/CVS/000077500000000000000000000000001362720355200166445ustar00rootroot00000000000000uml-utilities-20070815.4/umlgdb/CVS/CVS/000077500000000000000000000000001362720355200172775ustar00rootroot00000000000000uml-utilities-20070815.4/umlgdb/CVS/CVS/Entries000066400000000000000000000000021362720355200206230ustar00rootroot00000000000000D uml-utilities-20070815.4/umlgdb/CVS/CVS/Repository000066400000000000000000000000211362720355200213720ustar00rootroot00000000000000tools/umlgdb/CVS uml-utilities-20070815.4/umlgdb/CVS/CVS/Root000066400000000000000000000001031362720355200201370ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/umlgdb/CVS/Entries000066400000000000000000000001321362720355200201740ustar00rootroot00000000000000D/CVS//// /Makefile/1.1/Sun Apr 14 20:37:39 2002// /umlgdb/1.3/Sat Nov 8 13:20:29 2003// uml-utilities-20070815.4/umlgdb/CVS/Repository000066400000000000000000000000151362720355200207420ustar00rootroot00000000000000tools/umlgdb uml-utilities-20070815.4/umlgdb/CVS/Root000066400000000000000000000001031362720355200175040ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/umlgdb/Makefile000066400000000000000000000001611362720355200176470ustar00rootroot00000000000000all : install : install -d $(DESTDIR)$(BIN_DIR) install -m 755 umlgdb $(DESTDIR)$(BIN_DIR) clean : rm -f *~ uml-utilities-20070815.4/umlgdb/umlgdb000066400000000000000000000177451362720355200174240ustar00rootroot00000000000000#!/usr/bin/expect ####################################################################### # Name: umlgdl # # Description: # umlgdb is a expect shell for gdb, which is specialised # to handle re-loading symbols of kernel modules as they # are reloaded using rmmod and insmod. # # It spawns GDB and prints out its pid which should be # be used to start the UML session as follows. # # ./linux debug gdb-pid= # # Once the kernel starts booting, proceed with the gdb and enter 'att 1' # to attach to the kernel and 'c' to continue. # # When any module is loaded with an insmod command, # gdb breaks automatically at sys_init_module # and executes the appropriate gdb commands to reload all the symbols # for the kernel and the symbols for the recently laoded module. # # It then passes control to the user who can proceed with their debugging # as usual. # # umlgdb session is very much like a session started with the normal gdb # except for the special handling of breakpoints at sys_init_modle. # # Its very easy to extend the behaviour opf umlgdb to handle different # debugging situations that need automation. # # Author: Chandan Kudige, April 11, 2002 ####################################################################### ####################################################################### # Customisation: most of the defaults here should suffice. But feel # free to change anything to suit custom requirements. ####################################################################### ## # GDB program name ## set PROG "gdb" ## # Kernel path relative to current directory. ## set ARG "./linux" ## # GDB prompt - Anyway valid TCL regex. Defaults to '(gdb)' ## set GDB {\(gdb\)} ## # Regex pattern printed out by the gdb when 'sys_init_module' # breakpoint is hit. # The name of the module is matched by the regex. ## set RE_BREAKPOINT {Breakpoint 1, sys_init_module \(name_user=[x0-9a-f]+ "(.+)"} ## # Special character for quitting the UML session. # Type the actual character (using your editor's escape) ## set QUIT_CHAR "" ### # Module paths: # You can add paths for modules that are not in the gdb load-path. # This is basically a list with alternating module name and module path. # # When a module is loaded, umlgdb tries to load the symbols from the # path given here. If the module is not listed then no symbols are loaded. ### set MODULE_PATHS { "hostfs" "/home/jdike/linux/2.4/um/arch/um/fs/hostfs/hostfs.o" } ####################################################################### # Script starts here. ####################################################################### log_user 0 system stty -echo raw set timeout -1 proc get_module_path {modname} { global MODULE_PATHS ARG env set idx [lsearch $MODULE_PATHS $modname] if {$idx == -1} { set version "[exec ./linux --version]" set path "[exec find $env(PWD)/lib/modules/$version/ -name ${modname}.o ]" if {$path != ""} { return $path } return 0 } incr idx return [lindex $MODULE_PATHS $idx] } ####################################################################### # do_insmod() hook is invoked when umlgdb detects that the breakpoint # at sys_init_module has been hit. # # Args: # name - name of the module being loaded # # Returns: None ####################################################################### proc do_insmod {name} { global GDB ARG set timeout 5 ## # Let the sys_init_module return # since module_list struct is initialised in this routine. ## prompt_wait $GDB send "finish\r" prompt_wait $GDB ## # find the start address of the module. ## set tmp "p/x (int)module_list + module_list->size_of_struct\r" send $tmp ## # gdb prints the expression values as # $ = 0x # Capture the hex value into modstart ## set buf [prompt_wait {= (0x[0-9a-f]+)}] set succ [regexp "= (.+)" $buf tmp modstart] set modpath [get_module_path $name] ## # Reload symbols only if we have a module path ## if {$modpath != 0} { ## # Reload all kernel symbols ## prompt_wait $GDB send "symbol-file $ARG\r" prompt_wait "y or n" send "y\r" ## # Load the symbols for our module ## prompt_wait $GDB send "add-symbol-file $modpath $modstart\r" prompt_wait "y or n" send "y\r" ## # print the module_list head to make sure init() and cleanup() # are valid addresses ## prompt_wait $GDB send "p *module_list\r" prompt_wait $GDB puts "\r\r>> Finished loading symbols for $name ...\r" send " \r" } prompt_wait $GDB ## # Back to user ## set timeout -1 } ####################################################################### # shell() is the main dispatch loop. # # - Passes user # # Args: # name - name of the module being loaded # # Returns: None ####################################################################### proc shell {} { global spawn_id, user_spawn_id global RE_BREAKPOINT global QUIT_CHAR while 1 { expect { ## # gracefully exit when the gdb exits. ## eof { return } ## # Hooks for gdb output. # Note: order is important ## -re $RE_BREAKPOINT { set modname $expect_out(1,string); set bpline $expect_out(buffer); puts "\r *** Module $modname loaded *** \r" send_user -raw -- $bpline do_insmod $modname } ## # Catch all from gdb and pass it to the user ## -re ".+" { send_user -raw -- $expect_out(buffer); } ## # Catch user QUIT sequence. # Currently should be a single character. ## -i $user_spawn_id $QUIT_CHAR {return} ## # Catch all from user and pass it to gdb ## -i $user_spawn_id -re .+ { send -- $expect_out(buffer); } } } } ####################################################################### # prompt_wait() : Can be called anytime we wait for a prompt. # Respects user input while waiting for the prompt. ####################################################################### proc prompt_wait {prompt} { global user_spawn_id global QUIT_CHAR set buf "" expect { -re $prompt { set buf $expect_out(buffer); send_user -raw -- $buf } -re ".+" { set buf $expect_out(buffer); send_user -raw -- $buf exp_continue; } timeout { puts "\rTIMEDOUT on prompt $prompt!!!\r" return 0; } -i $user_spawn_id $QUIT_CHAR {return} -i $user_spawn_id -re .+ { send -- $expect_out(buffer); } } return $buf } ## # Setup a breakpoint at sys_init_module # Prepare to enter "att 1" ## proc initgdb {pid} { global GDB ## # user can press the 'enter' once they start the UML kernel. ## prompt_wait $GDB send "att 1" prompt_wait $GDB send "b sys_init_module\r" prompt_wait $GDB send "b panic\r" prompt_wait $GDB send "b stop\r" prompt_wait $GDB send "handle SIGWINCH nostop noprint pass\r" prompt_wait $GDB send "b start_kernel\r" prompt_wait $GDB send "c\r" } ## # Spawn our gdb with linux ## set pid [spawn $PROG $ARG] puts "\r\n\n ******** GDB pid is $pid ********\r" puts "Start UML as: $ARG debug gdb-pid=$pid\r\n\r\n\r\n" ## # Initialise gdb ## initgdb $pid ## # Main dispatch loop ## shell ## # Cleanup ## system stty sane puts "\r----- umlgdb finished----\r" uml-utilities-20070815.4/watchdog/000077500000000000000000000000001362720355200165375ustar00rootroot00000000000000uml-utilities-20070815.4/watchdog/CVS/000077500000000000000000000000001362720355200171725ustar00rootroot00000000000000uml-utilities-20070815.4/watchdog/CVS/Entries000066400000000000000000000001321362720355200205220ustar00rootroot00000000000000/uml_watchdog.c/1.1/Tue Jun 4 02:53:57 2002// /Makefile/1.2/Sat Feb 8 04:04:25 2003// D uml-utilities-20070815.4/watchdog/CVS/Repository000066400000000000000000000000171362720355200212720ustar00rootroot00000000000000tools/watchdog uml-utilities-20070815.4/watchdog/CVS/Root000066400000000000000000000001031362720355200200320ustar00rootroot00000000000000jdike@cvs.user-mode-linux.sourceforge.net:/cvsroot/user-mode-linux uml-utilities-20070815.4/watchdog/Makefile000066400000000000000000000004141362720355200201760ustar00rootroot00000000000000BIN = uml_watchdog OBJS = $(BIN).o CFLAGS ?= -g -Wall BIN_DIR ?= /usr/bin all : $(BIN) $(BIN) : $(OBJS) $(CC) $(CFLAGS) -o $(BIN) $(OBJS) clean : rm -f $(BIN) $(OBJS) *~ install : $(BIN) install -d $(DESTDIR)$(BIN_DIR) install -s $(BIN) $(DESTDIR)$(BIN_DIR) uml-utilities-20070815.4/watchdog/uml_watchdog.c000066400000000000000000000034511362720355200213630ustar00rootroot00000000000000#include #include #include #include #include #include #include #define MCONSOLE_MAGIC (0xcafebabe) #define MCONSOLE_MAX_DATA (512) #define MCONSOLE_VERSION 2 struct mconsole_notify { unsigned long magic; int version; enum { MCONSOLE_SOCKET, MCONSOLE_PANIC, MCONSOLE_HANG } type; int len; char data[MCONSOLE_MAX_DATA]; }; static char *sock_name = NULL; static void notify(int sig) { struct sockaddr_un target; struct mconsole_notify packet; int sock, len, n; sock = socket(PF_UNIX, SOCK_DGRAM, 0); if(sock < 0){ perror("socket"); exit(1); } target.sun_family = AF_UNIX; strcpy(target.sun_path, sock_name); packet.magic = MCONSOLE_MAGIC; packet.version = MCONSOLE_VERSION; packet.type = MCONSOLE_HANG; packet.len = 0; len = sizeof(packet) + packet.len - sizeof(packet.data); n = sendto(sock, &packet, len, 0, (struct sockaddr *) &target, sizeof(target)); if(n < 0) perror("sendto"); exit(1); } static int tracing_pid = -1; static void kill_tracer(int sig) { kill(tracing_pid, SIGINT); sleep(1); kill(tracing_pid, SIGINT); sleep(1); kill(tracing_pid, SIGKILL); exit(1); } int main(int argc, char **argv) { void (*handler)(int); int n; char c, *end; if(argc < 3) exit(1); if(!strcmp(argv[1], "-pid")){ tracing_pid = strtol(argv[2], &end, 0); if(*end != '\0') exit(1); if(kill(tracing_pid, 0)) exit(1); handler = kill_tracer; } else if(!strcmp(argv[1], "-mconsole")){ sock_name = argv[2]; handler = notify; } else exit(1); if(signal(SIGALRM, handler)) exit(1); if(write(1, &c, sizeof(c)) != sizeof(c)) exit(1); while(1){ alarm(60); n = read(0, &c, sizeof(c)); if(n == 0) exit(0); else if(n < 0) exit(1); } }