Initial commit.
This commit is contained in:
13
ndhc/AUTHORS.udhcp
Normal file
13
ndhc/AUTHORS.udhcp
Normal file
@@ -0,0 +1,13 @@
|
||||
udhcp server/client package
|
||||
-----------------------
|
||||
|
||||
Russ Dill <Russ.Dill@asu.edu>
|
||||
Matthew Ramsay <matthewr@moreton.com.au>
|
||||
Chris Trew <christ@moreton.com.au>
|
||||
|
||||
Other Credits:
|
||||
--------------
|
||||
Moreton Bay (http://www.moretonbay.com/)
|
||||
Lineo (http://opensource.lineo.com)
|
||||
|
||||
|
339
ndhc/COPYING
Normal file
339
ndhc/COPYING
Normal file
@@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
675 Mass Ave, Cambridge, MA 02139, 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
|
||||
|
||||
Appendix: 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.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) 19yy <name of author>
|
||||
|
||||
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., 675 Mass Ave, Cambridge, MA 02139, 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.
|
||||
|
||||
<signature of Ty Coon>, 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.
|
43
ndhc/MODIFICATIONS
Normal file
43
ndhc/MODIFICATIONS
Normal file
@@ -0,0 +1,43 @@
|
||||
All modifications are to udhcp 0.9.8
|
||||
All bugs should be blamed on Nicholas Kain, not the udhcp folks!
|
||||
|
||||
Great thanks go to the authors of udhcp (primarily Russ Dill), from which
|
||||
ndhc is adapted.
|
||||
|
||||
CHANGES
|
||||
-------
|
||||
|
||||
C99 compiler required
|
||||
|
||||
removed dhcp server support (use mainline udhcp)
|
||||
removed sample scripts
|
||||
added chroot support
|
||||
added capability support
|
||||
added setuid/setgid support
|
||||
run_script now takes int argument (from enum)
|
||||
script.c heavily modified to call ifchd
|
||||
stripped command line option for custom script
|
||||
bounds check all string functions
|
||||
removed pidfile support: backgrounding model + chroot preclude it
|
||||
manpage updated
|
||||
removed busybox compat wrapper
|
||||
convert all strncpy -> strlcpy
|
||||
u_int*_t -> uint*_t (C99)
|
||||
add_simple_option no longer does unspeakable things to the C language
|
||||
character signedness issues fixed
|
||||
casting bug [dhcpc.c:549 sizeof(signal) should be sizeof sig]
|
||||
- won't bite except on an architecture where sizeof fnptr != sizeof int
|
||||
LOG and DEBUG are abolished - functions are used instead, see log.c/log.h
|
||||
C99 structure initializers used exclusively (C99)
|
||||
Linux kernel headers no longer used under any circumstances.
|
||||
- this is likely to break prehistoric systems running <glibc 2.1
|
||||
exit() return codes now use EXIT_(SUCCESS|FAILURE) #defines and make more sense
|
||||
close fd leak on failure in read_interface()
|
||||
close fd leak on failure in kernel_packet()
|
||||
char buffer[16] in perform_release() should not be static
|
||||
don't bother to use a signal pipe - instead use static sig_atomic_t bools
|
||||
convert lots of #defines to enums
|
||||
break apart main client loop into maintainable functions
|
||||
more discerning error handling
|
||||
boundary check end_option()
|
||||
|
69
ndhc/Makefile
Normal file
69
ndhc/Makefile
Normal file
@@ -0,0 +1,69 @@
|
||||
# ndhc makefile
|
||||
|
||||
prefix=/usr
|
||||
SBINDIR=/sbin
|
||||
USRSBINDIR=${prefix}/sbin
|
||||
USRBINDIR=${prefix}/bin
|
||||
USRSHAREDIR=${prefix}/share
|
||||
|
||||
# Uncomment this for extra output and to compile with debugging symbols
|
||||
#DEBUG=1
|
||||
|
||||
#CROSS_COMPILE=arm-uclibc-
|
||||
CC = $(CROSS_COMPILE)gcc
|
||||
LD = $(CROSS_COMPILE)gcc
|
||||
INSTALL = install
|
||||
|
||||
VER := 0.9.8
|
||||
|
||||
|
||||
OBJS_SHARED = nstrl.o log.o options.o socket.o packet.o rootcap.o
|
||||
DHCPC_OBJS = dhcpc.o clientpacket.o script.o
|
||||
|
||||
EXEC2 = ndhc
|
||||
OBJS2 = -lcap $(DHCPC_OBJS) $(OBJS_SHARED)
|
||||
|
||||
BOOT_PROGRAMS = ndhc
|
||||
|
||||
ifdef SYSLOG
|
||||
CFLAGS += -DSYSLOG
|
||||
endif
|
||||
|
||||
CFLAGS += -W -Wall -Wstrict-prototypes -DVERSION='"$(VER)"'
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -g -DDEBUG
|
||||
STRIP=true
|
||||
else
|
||||
CFLAGS += -Os -fomit-frame-pointer
|
||||
STRIP=$(CROSS_COMPILE)strip
|
||||
endif
|
||||
|
||||
all: $(EXEC2)
|
||||
$(STRIP) --remove-section=.note --remove-section=.comment $(EXEC2)
|
||||
|
||||
$(OBJS2) $(OBJS3): *.h Makefile
|
||||
$(EXEC2): Makefile
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $<
|
||||
|
||||
$(EXEC2): $(OBJS2)
|
||||
$(LD) $(LDFLAGS) $(OBJS2) -o $(EXEC2)
|
||||
|
||||
tags:
|
||||
-ctags -f tags *.[ch]
|
||||
-cscope -b
|
||||
|
||||
install: all
|
||||
|
||||
$(INSTALL) $(BOOT_PROGRAMS) $(USRSBINDIR)
|
||||
mkdir -p $(USRSHAREDIR)/man/man8
|
||||
$(INSTALL) ndhc.8 ndhc.8 $(USRSHAREDIR)/man/man8
|
||||
|
||||
clean:
|
||||
-rm -f ndhc *.o core
|
||||
|
||||
distclean:
|
||||
-rm -f ndhc *.o tags cscope.out
|
||||
|
54
ndhc/README
Normal file
54
ndhc/README
Normal file
@@ -0,0 +1,54 @@
|
||||
ndhc client
|
||||
--------------------
|
||||
|
||||
The ndhc client negotiates a lease with the DHCP server and notifies
|
||||
ifchd when a leases is obtained or lost.
|
||||
|
||||
|
||||
command line options
|
||||
-------------------
|
||||
|
||||
The command line options for the ndhc client are:
|
||||
|
||||
-c, --clientid=CLIENTID Client identifier
|
||||
-H, --hostname=HOSTNAME Client hostname
|
||||
-h, Alias for -H
|
||||
-f, --foreground Do not fork after getting lease
|
||||
-b, --background Fork to background if lease cannot be
|
||||
immediately negotiated.
|
||||
-i, --interface=INTERFACE Interface to use (default: eth0)
|
||||
-n, --now Exit with failure if lease cannot be
|
||||
immediately negotiated.
|
||||
-q, --quit Quit after obtaining lease
|
||||
-r, --request=IP IP address to request (default: none)
|
||||
-v, --version Display version
|
||||
|
||||
|
||||
If the requested IP address cannot be obtained, the client accepts the
|
||||
address that the server offers.
|
||||
|
||||
|
||||
note on ndhc's random seed
|
||||
---------------------------
|
||||
|
||||
ndhc will seed its random number generator (used for generating xid's)
|
||||
by reading /dev/urandom. If you have a lot of embedded systems on the same
|
||||
network, with no entropy, you can either seed /dev/urandom by a method of
|
||||
your own, or doing the following on startup:
|
||||
|
||||
ifconfig eth0 > /dev/urandom
|
||||
|
||||
in order to seed /dev/urandom with some data (mac address) unique to your
|
||||
system. If reading /dev/urandom fails, ndhc will fall back to its old
|
||||
behavior of seeding with time(0).
|
||||
|
||||
|
||||
signals accepted by ndhc
|
||||
-------------------------
|
||||
|
||||
ndhc also responds to SIGUSR1 and SIGUSR2. SIGUSR1 will force a renew state,
|
||||
and SIGUSR2 will force a release of the current lease, and cause ndhc to
|
||||
go into an inactive state (until it is killed, or receives a SIGUSR1). You do
|
||||
not need to sleep between sending signals, as signals received are processed
|
||||
sequencially in the order they are received.
|
||||
|
7
ndhc/TODO
Normal file
7
ndhc/TODO
Normal file
@@ -0,0 +1,7 @@
|
||||
TODO
|
||||
----
|
||||
+ using time(0) breaks if the system clock changes, find a portable solution
|
||||
+ make failure of reading functions revert to previous value, not the default
|
||||
+ sanity code for option[OPT_LEN]
|
||||
+ fix aliasing (ie: eth0:0)
|
||||
|
256
ndhc/clientpacket.c
Normal file
256
ndhc/clientpacket.c
Normal file
@@ -0,0 +1,256 @@
|
||||
/* clientpacket.c
|
||||
*
|
||||
* Packet generation and dispatching functions for the DHCP client.
|
||||
*
|
||||
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||
*
|
||||
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <features.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "dhcpd.h"
|
||||
#include "packet.h"
|
||||
#include "options.h"
|
||||
#include "dhcpc.h"
|
||||
#include "log.h"
|
||||
|
||||
/* Create a random xid */
|
||||
unsigned long random_xid(void)
|
||||
{
|
||||
static int initialized;
|
||||
if (!initialized) {
|
||||
int fd;
|
||||
unsigned long seed;
|
||||
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1 || read(fd, &seed, sizeof(seed)) < 0) {
|
||||
log_line(LOG_WARNING, "Could not load seed from /dev/urandom: %s\n",
|
||||
strerror(errno));
|
||||
seed = time(0);
|
||||
}
|
||||
if (fd != -1)
|
||||
close(fd);
|
||||
srand(seed);
|
||||
initialized++;
|
||||
}
|
||||
return rand();
|
||||
}
|
||||
|
||||
|
||||
/* initialize a packet with the proper defaults */
|
||||
static void init_packet(struct dhcpMessage *packet, char type)
|
||||
{
|
||||
struct vendor {
|
||||
char vendor;
|
||||
char length;
|
||||
char str[sizeof("ndhc "VERSION)];
|
||||
} vendor_id = { DHCP_VENDOR, sizeof("ndhc "VERSION) - 1, "ndhc "VERSION};
|
||||
|
||||
init_header(packet, type);
|
||||
memcpy(packet->chaddr, client_config.arp, 6);
|
||||
add_option_string(packet->options, client_config.clientid);
|
||||
if (client_config.hostname)
|
||||
add_option_string(packet->options, client_config.hostname);
|
||||
add_option_string(packet->options, (unsigned char *)&vendor_id);
|
||||
}
|
||||
|
||||
|
||||
/* Add a paramater request list for stubborn DHCP servers. Pull the data
|
||||
* from the struct in options.c. Don't do bounds checking here because it
|
||||
* goes towards the head of the packet. */
|
||||
static void add_requests(struct dhcpMessage *packet)
|
||||
{
|
||||
int end = end_option(packet->options);
|
||||
int i, len = 0;
|
||||
|
||||
packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
|
||||
for (i = 0; options[i].code; i++)
|
||||
if (options[i].flags & OPTION_REQ)
|
||||
packet->options[end + OPT_DATA + len++] = options[i].code;
|
||||
packet->options[end + OPT_LEN] = len;
|
||||
packet->options[end + OPT_DATA + len] = DHCP_END;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* Broadcast a DHCP discover packet to the network, with an optionally
|
||||
* requested IP */
|
||||
int send_discover(unsigned long xid, unsigned long requested)
|
||||
{
|
||||
struct dhcpMessage packet;
|
||||
|
||||
init_packet(&packet, DHCPDISCOVER);
|
||||
packet.xid = xid;
|
||||
if (requested)
|
||||
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
||||
|
||||
add_requests(&packet);
|
||||
log_line(LOG_DEBUG, "Sending discover...\n");
|
||||
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
||||
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||
}
|
||||
|
||||
|
||||
/* Broadcasts a DHCP request message */
|
||||
int send_selecting(unsigned long xid, unsigned long server,
|
||||
unsigned long requested)
|
||||
{
|
||||
struct dhcpMessage packet;
|
||||
struct in_addr addr;
|
||||
|
||||
init_packet(&packet, DHCPREQUEST);
|
||||
packet.xid = xid;
|
||||
|
||||
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
|
||||
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
||||
|
||||
add_requests(&packet);
|
||||
addr.s_addr = requested;
|
||||
log_line(LOG_DEBUG, "Sending select for %s...\n", inet_ntoa(addr));
|
||||
return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
||||
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||
}
|
||||
|
||||
|
||||
/* Unicasts or broadcasts a DHCP renew message */
|
||||
int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
|
||||
{
|
||||
struct dhcpMessage packet;
|
||||
int ret = 0;
|
||||
|
||||
init_packet(&packet, DHCPREQUEST);
|
||||
packet.xid = xid;
|
||||
packet.ciaddr = ciaddr;
|
||||
|
||||
add_requests(&packet);
|
||||
log_line(LOG_DEBUG, "Sending renew...\n");
|
||||
if (server)
|
||||
ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
||||
else
|
||||
ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
|
||||
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* Unicasts a DHCP release message */
|
||||
int send_release(unsigned long server, unsigned long ciaddr)
|
||||
{
|
||||
struct dhcpMessage packet;
|
||||
|
||||
init_packet(&packet, DHCPRELEASE);
|
||||
packet.xid = random_xid();
|
||||
packet.ciaddr = ciaddr;
|
||||
|
||||
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
|
||||
add_simple_option(packet.options, DHCP_SERVER_ID, server);
|
||||
|
||||
log_line(LOG_DEBUG, "Sending release...\n");
|
||||
return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
|
||||
}
|
||||
|
||||
|
||||
/* return -1 on errors that are fatal for the socket,
|
||||
* -2 for those that aren't */
|
||||
int get_raw_packet(struct dhcpMessage *payload, int fd)
|
||||
{
|
||||
int bytes;
|
||||
struct udp_dhcp_packet packet;
|
||||
uint32_t source, dest;
|
||||
uint16_t check;
|
||||
|
||||
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
|
||||
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
|
||||
if (bytes < 0) {
|
||||
debug(LOG_INFO, "couldn't read on raw listening socket -- ignoring\n");
|
||||
usleep(500000); /* possible down interface, looping condition */
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
|
||||
debug(LOG_INFO, "message too short, ignoring\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (bytes < ntohs(packet.ip.tot_len)) {
|
||||
debug(LOG_INFO, "Truncated packet\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* ignore any extra garbage bytes */
|
||||
bytes = ntohs(packet.ip.tot_len);
|
||||
|
||||
/* Make sure its the right packet for us, and that it passes
|
||||
* sanity checks */
|
||||
if (packet.ip.protocol != IPPROTO_UDP
|
||||
|| packet.ip.version != IPVERSION
|
||||
|| packet.ip.ihl != sizeof(packet.ip) >> 2
|
||||
|| packet.udp.dest != htons(CLIENT_PORT)
|
||||
|| bytes > (int)sizeof(struct udp_dhcp_packet)
|
||||
|| ntohs(packet.udp.len) != (short)(bytes - sizeof(packet.ip))) {
|
||||
debug(LOG_INFO, "unrelated/bogus packet\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* check IP checksum */
|
||||
check = packet.ip.check;
|
||||
packet.ip.check = 0;
|
||||
if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
|
||||
debug(LOG_INFO, "bad IP header checksum, ignoring\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* verify the UDP checksum by replacing the header with a psuedo header */
|
||||
source = packet.ip.saddr;
|
||||
dest = packet.ip.daddr;
|
||||
check = packet.udp.check;
|
||||
packet.udp.check = 0;
|
||||
memset(&packet.ip, 0, sizeof(packet.ip));
|
||||
|
||||
packet.ip.protocol = IPPROTO_UDP;
|
||||
packet.ip.saddr = source;
|
||||
packet.ip.daddr = dest;
|
||||
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
|
||||
if (check && check != checksum(&packet, bytes)) {
|
||||
debug(LOG_ERR, "packet with bad UDP checksum received, ignoring\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
memcpy(payload, &(packet.data),
|
||||
bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
|
||||
|
||||
if (ntohl(payload->cookie) != DHCP_MAGIC) {
|
||||
log_line(LOG_ERR, "received bogus message (bad magic) -- ignoring\n");
|
||||
return -2;
|
||||
}
|
||||
debug(LOG_INFO, "oooooh!!! got some!\n");
|
||||
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
|
||||
|
||||
}
|
||||
|
12
ndhc/clientpacket.h
Normal file
12
ndhc/clientpacket.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef _CLIENTPACKET_H
|
||||
#define _CLIENTPACKET_H
|
||||
|
||||
unsigned long random_xid(void);
|
||||
int send_discover(unsigned long xid, unsigned long requested);
|
||||
int send_selecting(unsigned long xid, unsigned long server, unsigned long requested);
|
||||
int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr);
|
||||
int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr);
|
||||
int send_release(unsigned long server, unsigned long ciaddr);
|
||||
int get_raw_packet(struct dhcpMessage *payload, int fd);
|
||||
|
||||
#endif
|
594
ndhc/dhcpc.c
Normal file
594
ndhc/dhcpc.c
Normal file
@@ -0,0 +1,594 @@
|
||||
/* dhcpc.c
|
||||
*
|
||||
* ndhc DHCP client
|
||||
*
|
||||
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||
* Nicholas Kain <njk@-N0SPaM-.kain.us> 2004
|
||||
*
|
||||
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/file.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <errno.h>
|
||||
#include <sys/capability.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#include "dhcpd.h"
|
||||
#include "dhcpc.h"
|
||||
#include "options.h"
|
||||
#include "clientpacket.h"
|
||||
#include "packet.h"
|
||||
#include "script.h"
|
||||
#include "socket.h"
|
||||
#include "log.h"
|
||||
#include "rootcap.h"
|
||||
#include "nstrl.h"
|
||||
|
||||
static unsigned long requested_ip, server_addr, timeout;
|
||||
static unsigned long lease, t1, t2, xid, start;
|
||||
static int state, packet_num, fd, listen_mode;
|
||||
static sig_atomic_t pending_exit, pending_renew, pending_release;
|
||||
|
||||
enum {
|
||||
LISTEN_NONE,
|
||||
LISTEN_KERNEL,
|
||||
LISTEN_RAW
|
||||
};
|
||||
|
||||
struct client_config_t client_config = {
|
||||
/* Default options. */
|
||||
.abort_if_no_lease = 0,
|
||||
.foreground = 0,
|
||||
.quit_after_lease = 0,
|
||||
.background_if_no_lease = 0,
|
||||
.interface = "eth0",
|
||||
.script = "none",
|
||||
.clientid = NULL,
|
||||
.hostname = NULL,
|
||||
.ifindex = 0,
|
||||
.arp = "\0",
|
||||
};
|
||||
|
||||
static void show_usage(void)
|
||||
{
|
||||
printf(
|
||||
"Usage: ndhc [OPTIONS]\n\n"
|
||||
" -c, --clientid=CLIENTID Client identifier\n"
|
||||
" -H, --hostname=HOSTNAME Client hostname\n"
|
||||
" -h Alias for -H\n"
|
||||
" -f, --foreground Do not fork after getting lease\n"
|
||||
" -b, --background Fork to background if lease cannot be\n"
|
||||
" immediately negotiated.\n"
|
||||
" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
|
||||
" -n, --now Exit with failure if lease cannot be\n"
|
||||
" immediately negotiated.\n"
|
||||
" -q, --quit Quit after obtaining lease\n"
|
||||
" -r, --request=IP IP address to request (default: none)\n"
|
||||
" -u, --user Change privileges to this user\n"
|
||||
" -C, --chroot Directory to which udhcp should chroot\n"
|
||||
" -v, --version Display version\n"
|
||||
);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/* just a little helper */
|
||||
static void change_mode(int new_mode)
|
||||
{
|
||||
debug(LOG_INFO, "entering %s listen mode",
|
||||
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
|
||||
close(fd);
|
||||
fd = -1;
|
||||
listen_mode = new_mode;
|
||||
}
|
||||
|
||||
/* perform a renew */
|
||||
static void perform_renew(void)
|
||||
{
|
||||
log_line(LOG_INFO, "Performing a DHCP renew...\n");
|
||||
switch (state) {
|
||||
case BOUND:
|
||||
change_mode(LISTEN_KERNEL);
|
||||
case RENEWING:
|
||||
case REBINDING:
|
||||
state = RENEW_REQUESTED;
|
||||
break;
|
||||
case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
case REQUESTING:
|
||||
case RELEASED:
|
||||
change_mode(LISTEN_RAW);
|
||||
state = INIT_SELECTING;
|
||||
break;
|
||||
case INIT_SELECTING:
|
||||
break;
|
||||
}
|
||||
|
||||
/* start things over */
|
||||
packet_num = 0;
|
||||
|
||||
/* Kill any timeouts because the user wants this to hurry along */
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
|
||||
/* perform a release */
|
||||
static void perform_release(void)
|
||||
{
|
||||
char buf[16];
|
||||
struct in_addr temp_addr;
|
||||
|
||||
buf[16] = '\0';
|
||||
|
||||
/* send release packet */
|
||||
if (state == BOUND || state == RENEWING || state == REBINDING) {
|
||||
temp_addr.s_addr = server_addr;
|
||||
snprintf(buf, sizeof buf, "%s", inet_ntoa(temp_addr));
|
||||
temp_addr.s_addr = requested_ip;
|
||||
log_line(LOG_INFO, "Unicasting a release of %s to %s.\n",
|
||||
inet_ntoa(temp_addr), buf);
|
||||
send_release(server_addr, requested_ip); /* unicast */
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
}
|
||||
log_line(LOG_INFO, "Entering released state.\n");
|
||||
|
||||
change_mode(LISTEN_NONE);
|
||||
state = RELEASED;
|
||||
timeout = 0x7fffffff;
|
||||
}
|
||||
|
||||
static void signal_handler(int sig)
|
||||
{
|
||||
switch (sig) {
|
||||
case SIGUSR1:
|
||||
pending_renew = 1;
|
||||
break;
|
||||
case SIGUSR2:
|
||||
pending_release = 1;
|
||||
break;
|
||||
case SIGTERM:
|
||||
pending_exit = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void background(void)
|
||||
{
|
||||
if (daemon(0, 0) == -1) {
|
||||
perror("fork");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
client_config.foreground = 1; /* Do not fork again. */
|
||||
}
|
||||
|
||||
static void handle_timeout(void)
|
||||
{
|
||||
time_t now = time(0);
|
||||
|
||||
/* timeout dropped to zero */
|
||||
switch (state) {
|
||||
case INIT_SELECTING:
|
||||
if (packet_num < 3) {
|
||||
if (packet_num == 0)
|
||||
xid = random_xid();
|
||||
|
||||
/* send discover packet */
|
||||
send_discover(xid, requested_ip); /* broadcast */
|
||||
|
||||
timeout = now + ((packet_num == 2) ? 4 : 2);
|
||||
packet_num++;
|
||||
} else {
|
||||
if (client_config.background_if_no_lease) {
|
||||
log_line(LOG_INFO, "No lease, going to background.\n");
|
||||
background();
|
||||
} else if (client_config.abort_if_no_lease) {
|
||||
log_line(LOG_INFO, "No lease, failing.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* wait to try again */
|
||||
packet_num = 0;
|
||||
timeout = now + 60;
|
||||
}
|
||||
break;
|
||||
case RENEW_REQUESTED:
|
||||
case REQUESTING:
|
||||
if (packet_num < 3) {
|
||||
/* send request packet */
|
||||
if (state == RENEW_REQUESTED)
|
||||
/* unicast */
|
||||
send_renew(xid, server_addr, requested_ip);
|
||||
else
|
||||
/* broadcast */
|
||||
send_selecting(xid, server_addr, requested_ip);
|
||||
timeout = now + ((packet_num == 2) ? 10 : 2);
|
||||
packet_num++;
|
||||
} else {
|
||||
/* timed out, go back to init state */
|
||||
if (state == RENEW_REQUESTED)
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
state = INIT_SELECTING;
|
||||
timeout = now;
|
||||
packet_num = 0;
|
||||
change_mode(LISTEN_RAW);
|
||||
}
|
||||
break;
|
||||
case BOUND:
|
||||
/* Lease is starting to run out, time to enter renewing state */
|
||||
state = RENEWING;
|
||||
change_mode(LISTEN_KERNEL);
|
||||
debug(LOG_INFO, "Entering renew state.\n");
|
||||
/* fall right through */
|
||||
case RENEWING:
|
||||
/* Either set a new T1, or enter REBINDING state */
|
||||
if ((t2 - t1) <= (lease / 14400 + 1)) {
|
||||
/* timed out, enter rebinding state */
|
||||
state = REBINDING;
|
||||
timeout = now + (t2 - t1);
|
||||
debug(LOG_INFO, "Entering rebinding state.\n");
|
||||
} else {
|
||||
/* send a request packet */
|
||||
send_renew(xid, server_addr, requested_ip); /* unicast */
|
||||
|
||||
t1 = ((t2 - t1) >> 1) + t1;
|
||||
timeout = t1 + start;
|
||||
}
|
||||
break;
|
||||
case REBINDING:
|
||||
/* Either set a new T2, or enter INIT state */
|
||||
if ((lease - t2) <= (lease / 14400 + 1)) {
|
||||
/* timed out, enter init state */
|
||||
state = INIT_SELECTING;
|
||||
log_line(LOG_INFO, "Lease lost, entering init state.\n");
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
timeout = now;
|
||||
packet_num = 0;
|
||||
change_mode(LISTEN_RAW);
|
||||
} else {
|
||||
/* send a request packet */
|
||||
send_renew(xid, 0, requested_ip); /* broadcast */
|
||||
|
||||
t2 = ((lease - t2) >> 1) + t2;
|
||||
timeout = t2 + start;
|
||||
}
|
||||
break;
|
||||
case RELEASED:
|
||||
/* yah, I know, *you* say it would never happen */
|
||||
timeout = 0x7fffffff;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_packet(void)
|
||||
{
|
||||
unsigned char *temp = NULL, *message = NULL;
|
||||
int len;
|
||||
time_t now = time(0);
|
||||
struct in_addr temp_addr;
|
||||
struct dhcpMessage packet;
|
||||
|
||||
debug(LOG_INFO, "got a packet\n");
|
||||
|
||||
if (listen_mode == LISTEN_KERNEL)
|
||||
len = get_packet(&packet, fd);
|
||||
else
|
||||
len = get_raw_packet(&packet, fd);
|
||||
|
||||
if (len == -1 && errno != EINTR) {
|
||||
debug(LOG_INFO, "error on read, %s, reopening socket.\n",
|
||||
strerror(errno));
|
||||
change_mode(listen_mode); /* just close and reopen */
|
||||
}
|
||||
|
||||
if (len < 0)
|
||||
return;
|
||||
|
||||
if (packet.xid != xid) {
|
||||
debug(LOG_INFO, "Ignoring XID %lx (our xid is %lx).\n",
|
||||
(unsigned long) packet.xid, xid);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
|
||||
debug(LOG_ERR, "couldnt get option from packet -- ignoring\n");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case INIT_SELECTING:
|
||||
/* Must be a DHCPOFFER to one of our xid's */
|
||||
if (*message == DHCPOFFER) {
|
||||
if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
|
||||
memcpy(&server_addr, temp, 4);
|
||||
xid = packet.xid;
|
||||
requested_ip = packet.yiaddr;
|
||||
|
||||
/* enter requesting state */
|
||||
state = REQUESTING;
|
||||
timeout = now;
|
||||
packet_num = 0;
|
||||
} else {
|
||||
debug(LOG_ERR, "No server ID in message\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RENEW_REQUESTED:
|
||||
case REQUESTING:
|
||||
case RENEWING:
|
||||
case REBINDING:
|
||||
if (*message == DHCPACK) {
|
||||
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
|
||||
log_line(LOG_ERR,
|
||||
"No lease time received, assuming 1h.\n");
|
||||
lease = 60 * 60;
|
||||
} else {
|
||||
memcpy(&lease, temp, 4);
|
||||
lease = ntohl(lease);
|
||||
}
|
||||
|
||||
/* enter bound state */
|
||||
t1 = lease >> 1;
|
||||
|
||||
/* little fixed point for n * .875 */
|
||||
t2 = (lease * 0x7) >> 3;
|
||||
temp_addr.s_addr = packet.yiaddr;
|
||||
log_line(LOG_INFO,
|
||||
"Lease of %s obtained, lease time %ld.\n",
|
||||
inet_ntoa(temp_addr), lease);
|
||||
start = now;
|
||||
timeout = t1 + start;
|
||||
requested_ip = packet.yiaddr;
|
||||
run_script(&packet,
|
||||
((state == RENEWING || state == REBINDING)
|
||||
? SCRIPT_RENEW : SCRIPT_BOUND));
|
||||
|
||||
state = BOUND;
|
||||
change_mode(LISTEN_NONE);
|
||||
if (client_config.quit_after_lease)
|
||||
exit(EXIT_SUCCESS);
|
||||
if (!client_config.foreground)
|
||||
background();
|
||||
|
||||
} else if (*message == DHCPNAK) {
|
||||
/* return to init state */
|
||||
log_line(LOG_INFO, "Received DHCP NAK.\n");
|
||||
run_script(&packet, SCRIPT_NAK);
|
||||
if (state != REQUESTING)
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
state = INIT_SELECTING;
|
||||
timeout = now;
|
||||
requested_ip = 0;
|
||||
packet_num = 0;
|
||||
change_mode(LISTEN_RAW);
|
||||
sleep(3); /* avoid excessive network traffic */
|
||||
}
|
||||
break;
|
||||
case BOUND:
|
||||
case RELEASED:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static int do_work(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
fd_set rfds;
|
||||
for (;;) {
|
||||
|
||||
/* Handle signals asynchronously. */
|
||||
if (pending_renew)
|
||||
perform_renew();
|
||||
if (pending_release)
|
||||
perform_release();
|
||||
if (pending_exit) {
|
||||
log_line(LOG_INFO, "Received SIGTERM. Exiting gracefully.\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
tv.tv_sec = timeout - time(0);
|
||||
tv.tv_usec = 0;
|
||||
|
||||
if (listen_mode != LISTEN_NONE && fd < 0) {
|
||||
if (listen_mode == LISTEN_KERNEL)
|
||||
fd = listen_socket(INADDR_ANY, CLIENT_PORT,
|
||||
client_config.interface);
|
||||
else
|
||||
fd = raw_socket(client_config.ifindex);
|
||||
|
||||
if (fd < 0) {
|
||||
log_line(LOG_ERR, "FATAL: couldn't listen on socket: %s.\n",
|
||||
strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (tv.tv_sec <= 0) {
|
||||
handle_timeout();
|
||||
continue;
|
||||
}
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
if (fd >= 0)
|
||||
FD_SET(fd, &rfds);
|
||||
debug(LOG_INFO, "Waiting on select...\n");
|
||||
if (select(fd + 1, &rfds, NULL, NULL, &tv) == -1) {
|
||||
switch (errno) {
|
||||
case EBADF:
|
||||
fd = -1;
|
||||
default:
|
||||
debug(LOG_ERR, "Error: \"%s\" on select!\n",
|
||||
strerror(errno));
|
||||
case EINTR: /* Signal received, go back to top. */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
debug(LOG_INFO, "select suceeded\n");
|
||||
|
||||
if (listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds))
|
||||
handle_packet();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *chroot_dir = NULL;
|
||||
int c, len;
|
||||
struct passwd *pwd;
|
||||
uid_t uid = 0;
|
||||
gid_t gid = 0;
|
||||
static struct option arg_options[] = {
|
||||
{"clientid", required_argument, 0, 'c'},
|
||||
{"foreground", no_argument, 0, 'f'},
|
||||
{"background", no_argument, 0, 'b'},
|
||||
{"hostname", required_argument, 0, 'H'},
|
||||
{"hostname", required_argument, 0, 'h'},
|
||||
{"interface", required_argument, 0, 'i'},
|
||||
{"now", no_argument, 0, 'n'},
|
||||
{"quit", no_argument, 0, 'q'},
|
||||
{"request", required_argument, 0, 'r'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
{"user", required_argument, 0, 'u'},
|
||||
{"chroot", required_argument, 0, 'C'},
|
||||
{"help", no_argument, 0, '?'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
/* get options */
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
c = getopt_long(argc, argv, "c:fbH:h:i:np:qr:u:C:v", arg_options,
|
||||
&option_index);
|
||||
if (c == -1) break;
|
||||
|
||||
switch (c) {
|
||||
case 'c':
|
||||
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
|
||||
if (client_config.clientid)
|
||||
free(client_config.clientid);
|
||||
client_config.clientid = malloc(len + 2);
|
||||
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
|
||||
client_config.clientid[OPT_LEN] = len;
|
||||
client_config.clientid[OPT_DATA] = '\0';
|
||||
strlcpy((char *)client_config.clientid + OPT_DATA, optarg, len);
|
||||
break;
|
||||
case 'f':
|
||||
client_config.foreground = 1;
|
||||
break;
|
||||
case 'b':
|
||||
client_config.background_if_no_lease = 1;
|
||||
break;
|
||||
case 'h':
|
||||
case 'H':
|
||||
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
|
||||
if (client_config.hostname)
|
||||
free(client_config.hostname);
|
||||
client_config.hostname = malloc(len + 2);
|
||||
client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
|
||||
client_config.hostname[OPT_LEN] = len;
|
||||
strlcpy((char*)client_config.hostname + 2, optarg, len);
|
||||
break;
|
||||
case 'i':
|
||||
client_config.interface = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
client_config.abort_if_no_lease = 1;
|
||||
break;
|
||||
case 'q':
|
||||
client_config.quit_after_lease = 1;
|
||||
break;
|
||||
case 'r':
|
||||
requested_ip = inet_addr(optarg);
|
||||
break;
|
||||
case 'u':
|
||||
pwd = getpwnam(optarg);
|
||||
if (pwd) {
|
||||
uid = (int)pwd->pw_uid;
|
||||
gid = (int)pwd->pw_gid;
|
||||
} else {
|
||||
printf("Bad username provided.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
case 'C':
|
||||
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
|
||||
chroot_dir = malloc(len + 2);
|
||||
memset(chroot_dir, '\0', len + 2);
|
||||
strlcpy(chroot_dir, optarg, len);
|
||||
break;
|
||||
case 'v':
|
||||
printf("ndhc, version %s\n\n", VERSION);
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
default:
|
||||
show_usage();
|
||||
}
|
||||
}
|
||||
|
||||
log_line(LOG_INFO, "ndhc client (v%s) started.\n", VERSION);
|
||||
|
||||
if (read_interface(client_config.interface, &client_config.ifindex,
|
||||
NULL, client_config.arp) < 0)
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
if (!client_config.clientid) {
|
||||
client_config.clientid = malloc(6 + 3);
|
||||
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
|
||||
client_config.clientid[OPT_LEN] = 7;
|
||||
client_config.clientid[OPT_DATA] = 1;
|
||||
memcpy(client_config.clientid + 3, client_config.arp, 6);
|
||||
}
|
||||
|
||||
/* setup signal handlers */
|
||||
signal(SIGUSR1, signal_handler);
|
||||
signal(SIGUSR2, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
|
||||
if (chdir(chroot_dir)) {
|
||||
printf("Failed to chdir(%s)!\n", chroot_dir);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (chroot(chroot_dir)) {
|
||||
printf("Failed to chroot(%s)!\n", chroot_dir);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
drop_root(uid, gid,
|
||||
"cap_net_bind_service,cap_net_broadcast,cap_net_raw=ep");
|
||||
|
||||
state = INIT_SELECTING;
|
||||
run_script(NULL, SCRIPT_DECONFIG);
|
||||
change_mode(LISTEN_RAW);
|
||||
|
||||
do_work();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
31
ndhc/dhcpc.h
Normal file
31
ndhc/dhcpc.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _DHCPC_H
|
||||
#define _DHCPC_H
|
||||
|
||||
enum {
|
||||
INIT_SELECTING,
|
||||
REQUESTING,
|
||||
BOUND,
|
||||
RENEWING,
|
||||
REBINDING,
|
||||
INIT_REBOOT,
|
||||
RENEW_REQUESTED,
|
||||
RELEASED
|
||||
};
|
||||
|
||||
struct client_config_t {
|
||||
char foreground; /* Do not fork */
|
||||
char quit_after_lease; /* Quit after obtaining lease */
|
||||
char abort_if_no_lease; /* Abort if no lease */
|
||||
char background_if_no_lease; /* Fork to background if no lease */
|
||||
char *interface; /* The name of the interface to use */
|
||||
char *script; /* User script to run at dhcp events */
|
||||
unsigned char *clientid; /* Optional client id to use */
|
||||
unsigned char *hostname; /* Optional hostname to use */
|
||||
int ifindex; /* Index number of the interface to use */
|
||||
unsigned char arp[6]; /* Our arp address */
|
||||
};
|
||||
|
||||
extern struct client_config_t client_config;
|
||||
|
||||
#endif
|
||||
|
132
ndhc/dhcpd.h
Normal file
132
ndhc/dhcpd.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/* dhcpd.h */
|
||||
#ifndef _DHCPD_H
|
||||
#define _DHCPD_H
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/udp.h>
|
||||
|
||||
#include "leases.h"
|
||||
|
||||
/************************************/
|
||||
/* Defaults _you_ may want to tweak */
|
||||
/************************************/
|
||||
|
||||
/* the period of time the client is allowed to use that address */
|
||||
#define LEASE_TIME (60*60*24*10) /* 10 days of seconds */
|
||||
|
||||
/*****************************************************************/
|
||||
/* Do not modify below here unless you know what you are doing!! */
|
||||
/*****************************************************************/
|
||||
|
||||
/* DHCP protocol -- see RFC 2131 */
|
||||
#define SERVER_PORT 67
|
||||
#define CLIENT_PORT 68
|
||||
|
||||
#define DHCP_MAGIC 0x63825363
|
||||
|
||||
/* DHCP option codes (partial list) */
|
||||
#define DHCP_PADDING 0x00
|
||||
#define DHCP_SUBNET 0x01
|
||||
#define DHCP_TIME_OFFSET 0x02
|
||||
#define DHCP_ROUTER 0x03
|
||||
#define DHCP_TIME_SERVER 0x04
|
||||
#define DHCP_NAME_SERVER 0x05
|
||||
#define DHCP_DNS_SERVER 0x06
|
||||
#define DHCP_LOG_SERVER 0x07
|
||||
#define DHCP_COOKIE_SERVER 0x08
|
||||
#define DHCP_LPR_SERVER 0x09
|
||||
#define DHCP_HOST_NAME 0x0c
|
||||
#define DHCP_BOOT_SIZE 0x0d
|
||||
#define DHCP_DOMAIN_NAME 0x0f
|
||||
#define DHCP_SWAP_SERVER 0x10
|
||||
#define DHCP_ROOT_PATH 0x11
|
||||
#define DHCP_IP_TTL 0x17
|
||||
#define DHCP_MTU 0x1a
|
||||
#define DHCP_BROADCAST 0x1c
|
||||
#define DHCP_NTP_SERVER 0x2a
|
||||
#define DHCP_WINS_SERVER 0x2c
|
||||
#define DHCP_REQUESTED_IP 0x32
|
||||
#define DHCP_LEASE_TIME 0x33
|
||||
#define DHCP_OPTION_OVER 0x34
|
||||
#define DHCP_MESSAGE_TYPE 0x35
|
||||
#define DHCP_SERVER_ID 0x36
|
||||
#define DHCP_PARAM_REQ 0x37
|
||||
#define DHCP_MESSAGE 0x38
|
||||
#define DHCP_MAX_SIZE 0x39
|
||||
#define DHCP_T1 0x3a
|
||||
#define DHCP_T2 0x3b
|
||||
#define DHCP_VENDOR 0x3c
|
||||
#define DHCP_CLIENT_ID 0x3d
|
||||
#define DHCP_END 0xFF
|
||||
|
||||
enum {
|
||||
BOOTREQUEST = 1,
|
||||
BOOTREPLY = 2
|
||||
};
|
||||
|
||||
#define ETH_10MB 1
|
||||
#define ETH_10MB_LEN 6
|
||||
|
||||
enum {
|
||||
DHCPDISCOVER = 1,
|
||||
DHCPOFFER = 2,
|
||||
DHCPREQUEST = 3,
|
||||
DHCPDECLINE = 4,
|
||||
DHCPACK = 5,
|
||||
DHCPNAK = 6,
|
||||
DHCPRELEASE = 7,
|
||||
DHCPINFORM = 8
|
||||
};
|
||||
|
||||
#define BROADCAST_FLAG 0x8000
|
||||
|
||||
enum {
|
||||
OPTION_FIELD = 0,
|
||||
FILE_FIELD = 1,
|
||||
SNAME_FIELD = 2
|
||||
};
|
||||
|
||||
#define MAC_BCAST_ADDR (unsigned char *) "\xff\xff\xff\xff\xff\xff"
|
||||
|
||||
enum {
|
||||
OPT_CODE = 0,
|
||||
OPT_LEN = 1,
|
||||
OPT_DATA = 2
|
||||
};
|
||||
|
||||
struct option_set {
|
||||
unsigned char *data;
|
||||
struct option_set *next;
|
||||
};
|
||||
|
||||
struct server_config_t {
|
||||
uint32_t server; /* Our IP, in network order */
|
||||
uint32_t start; /* Start address of leases, network order */
|
||||
uint32_t end; /* End of leases, network order */
|
||||
struct option_set *options; /* List of DHCP options loaded from the config file */
|
||||
char *interface; /* The name of the interface to use */
|
||||
int ifindex; /* Index number of the interface to use */
|
||||
unsigned char arp[6]; /* Our arp address */
|
||||
unsigned long lease; /* lease time in seconds (host order) */
|
||||
unsigned long max_leases; /* maximum number of leases (including reserved address) */
|
||||
char remaining; /* should the lease file be interpreted as lease time remaining, or
|
||||
* as the time the lease expires */
|
||||
unsigned long auto_time; /* how long should udhcpd wait before writing a config file.
|
||||
* if this is zero, it will only write one on SIGUSR1 */
|
||||
unsigned long decline_time; /* how long an address is reserved if a client returns a
|
||||
* decline message */
|
||||
unsigned long conflict_time; /* how long an arp conflict offender is leased for */
|
||||
unsigned long offer_time; /* how long an offered address is reserved */
|
||||
unsigned long min_lease; /* minimum lease a client can request*/
|
||||
char *lease_file;
|
||||
char *notify_file; /* What to run whenever leases are written */
|
||||
uint32_t siaddr; /* next server bootp option */
|
||||
char *sname; /* bootp server name */
|
||||
char *boot_file; /* bootp boot file option */
|
||||
};
|
||||
|
||||
extern struct server_config_t server_config;
|
||||
extern struct dhcpOfferedAddr *leases;
|
||||
|
||||
|
||||
#endif
|
24
ndhc/leases.h
Normal file
24
ndhc/leases.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/* leases.h */
|
||||
#ifndef _LEASES_H
|
||||
#define _LEASES_H
|
||||
|
||||
|
||||
struct dhcpOfferedAddr {
|
||||
uint8_t chaddr[16];
|
||||
uint32_t yiaddr; /* network order */
|
||||
uint32_t expires; /* host order */
|
||||
};
|
||||
|
||||
extern unsigned char blank_chaddr[];
|
||||
|
||||
void clear_lease(uint8_t *chaddr, uint32_t yiaddr);
|
||||
struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease);
|
||||
int lease_expired(struct dhcpOfferedAddr *lease);
|
||||
struct dhcpOfferedAddr *oldest_expired_lease(void);
|
||||
struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr);
|
||||
struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr);
|
||||
uint32_t find_address(int check_expired);
|
||||
int check_ip(uint32_t addr);
|
||||
|
||||
|
||||
#endif
|
20
ndhc/log.c
Normal file
20
ndhc/log.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include <stdarg.h>
|
||||
#include <syslog.h>
|
||||
|
||||
void log_line(int level, char *format, ...) {
|
||||
va_list argp;
|
||||
|
||||
if (format == NULL) return;
|
||||
|
||||
va_start(argp, format);
|
||||
vfprintf(stderr, format, argp);
|
||||
va_end(argp);
|
||||
openlog("ndhc", 0, 0);
|
||||
va_start(argp, format);
|
||||
vsyslog(level, format, argp);
|
||||
va_end(argp);
|
||||
closelog();
|
||||
}
|
||||
|
11
ndhc/log.h
Normal file
11
ndhc/log.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef H_LOG_H__
|
||||
#define H_LOG_H__
|
||||
#include <syslog.h>
|
||||
void log_line(int level, char *format, ...);
|
||||
#ifdef DEBUG
|
||||
#define debug log_line
|
||||
#else
|
||||
#define debug(...)
|
||||
#endif
|
||||
#endif
|
||||
|
56
ndhc/ndhc.8
Normal file
56
ndhc/ndhc.8
Normal file
@@ -0,0 +1,56 @@
|
||||
.TH NDHC 8 2004-02-25 Linux "Linux Administrator's Manual"
|
||||
.SH NAME
|
||||
ndhc \- secure DHCP client
|
||||
.SH SYNOPSIS
|
||||
.B ndhc
|
||||
.RI [ OPTION ]...
|
||||
.SH DESCRIPTION
|
||||
The ndhc client negotiates a lease with the DHCP server and
|
||||
informs ifchd of the change when it is obtained or lost.
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.BI \-c\ CLIENTID ,\ \-\-clientid= CLIENTID
|
||||
Send the client identifier
|
||||
.IR CLIENTID .
|
||||
.TP
|
||||
.BR -f ,\ \-\-foreground
|
||||
Do not fork after obtaining a lease.
|
||||
.TP
|
||||
.BI \-H\ HOSTNAME ,\ \-\-hostname= HOSTNAME
|
||||
Send the client hostname
|
||||
.IR HOSTNAME .
|
||||
.TP
|
||||
.BI \-h\ HOSTNAME
|
||||
Alias for -H
|
||||
.IR HOSTNAME .
|
||||
.TP
|
||||
.BI \-i\ INTERFACE ,\ \-\-interface= INTERFACE
|
||||
Configure
|
||||
.IR INTERFACE .
|
||||
.TP
|
||||
.BR -n ,\ \-\-now
|
||||
Exit with failure if a lease cannot be obtained.
|
||||
.TP
|
||||
.BR -q ,\ \-\-quit
|
||||
Exit after obtaining a lease.
|
||||
.TP
|
||||
.BI \-r\ ADDRESS ,\ \-\-request= ADDRESS
|
||||
Request IP address
|
||||
.IR ADDRESS .
|
||||
.TP
|
||||
.BR -v ,\ \-\-version
|
||||
Display version.
|
||||
.SH NOTES
|
||||
.B ndhc
|
||||
responds to the following signals:
|
||||
.TP
|
||||
.B SIGUSR1
|
||||
This signal causes
|
||||
.B ndhc
|
||||
to renew the current lease or, if it does not have one, obtain a
|
||||
new lease.
|
||||
.TP
|
||||
.B SIGUSR2
|
||||
This signal caused
|
||||
.B ndhc
|
||||
to release the current lease.
|
48
ndhc/nstrl.c
Normal file
48
ndhc/nstrl.c
Normal file
@@ -0,0 +1,48 @@
|
||||
/* nstrl.c - strlcpy/strlcat implementation
|
||||
Time-stamp: <2003-05-28 02:35:13 njk>
|
||||
|
||||
(C) 2003 Nicholas Jay Kain <njk@aerifal.cx>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef HAVE_STRLCPY
|
||||
|
||||
size_t strlcpy (char *dest, char *src, size_t size)
|
||||
{
|
||||
register char *d = dest, *s = src;
|
||||
|
||||
for (; *s != '\0' && size > 0; size--, d++, s++)
|
||||
*d = *s;
|
||||
|
||||
*d = '\0';
|
||||
return (d - dest) + (s - src);
|
||||
}
|
||||
|
||||
size_t strlcat (char *dest, char *src, size_t size)
|
||||
{
|
||||
register char *d = dest, *s = src;
|
||||
|
||||
for (; size > 0 && *d != '\0'; size--, d++);
|
||||
|
||||
for (; *s != '\0' && size > 0; size--, d++, s++)
|
||||
*d = *s;
|
||||
|
||||
*d = '\0';
|
||||
return (d - dest) + (s - src);
|
||||
}
|
||||
|
||||
#endif
|
25
ndhc/nstrl.h
Normal file
25
ndhc/nstrl.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/* nstrl.h - header file for strlcpy/strlcat implementation
|
||||
Time-stamp: <2003-05-28 02:34:47 njk>
|
||||
|
||||
(C) 2003 Nicholas Jay Kain <njk@aerifal.cx>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
#ifndef NJK_HAVE_STRL_
|
||||
size_t strlcpy (char *dest, char *src, size_t size);
|
||||
size_t strlcat (char *dest, char *src, size_t size);
|
||||
#endif
|
||||
|
||||
#define NJK_HAVE_STRL_ 1
|
228
ndhc/options.c
Normal file
228
ndhc/options.c
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* options.c -- DHCP server option packet tools
|
||||
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||
* Fixes and hardening: Nicholas Kain <njk@-n0xZpam-.kain.us>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "dhcpd.h"
|
||||
#include "options.h"
|
||||
#include "leases.h"
|
||||
|
||||
|
||||
/* supported options are easily added here */
|
||||
struct dhcp_option options[] = {
|
||||
/* name[10] flags code */
|
||||
{"subnet", OPTION_IP | OPTION_REQ, 0x01},
|
||||
{"timezone", OPTION_S32, 0x02},
|
||||
{"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03},
|
||||
{"timesvr", OPTION_IP | OPTION_LIST, 0x04},
|
||||
{"namesvr", OPTION_IP | OPTION_LIST, 0x05},
|
||||
{"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06},
|
||||
{"logsvr", OPTION_IP | OPTION_LIST, 0x07},
|
||||
{"cookiesvr", OPTION_IP | OPTION_LIST, 0x08},
|
||||
{"lprsvr", OPTION_IP | OPTION_LIST, 0x09},
|
||||
{"hostname", OPTION_STRING | OPTION_REQ, 0x0c},
|
||||
{"bootsize", OPTION_U16, 0x0d},
|
||||
{"domain", OPTION_STRING | OPTION_REQ, 0x0f},
|
||||
{"swapsvr", OPTION_IP, 0x10},
|
||||
{"rootpath", OPTION_STRING, 0x11},
|
||||
{"ipttl", OPTION_U8, 0x17},
|
||||
{"mtu", OPTION_U16, 0x1a},
|
||||
{"broadcast", OPTION_IP | OPTION_REQ, 0x1c},
|
||||
{"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a},
|
||||
{"wins", OPTION_IP | OPTION_LIST, 0x2c},
|
||||
{"requestip", OPTION_IP, 0x32},
|
||||
{"lease", OPTION_U32, 0x33},
|
||||
{"dhcptype", OPTION_U8, 0x35},
|
||||
{"serverid", OPTION_IP, 0x36},
|
||||
{"message", OPTION_STRING, 0x38},
|
||||
{"tftp", OPTION_STRING, 0x42},
|
||||
{"bootfile", OPTION_STRING, 0x43},
|
||||
{"", 0x00, 0x00}
|
||||
};
|
||||
|
||||
/* Lengths of the different option types */
|
||||
int option_lengths[] = {
|
||||
[OPTION_IP] = 4,
|
||||
[OPTION_IP_PAIR] = 8,
|
||||
[OPTION_BOOLEAN] = 1,
|
||||
[OPTION_STRING] = 1,
|
||||
[OPTION_U8] = 1,
|
||||
[OPTION_U16] = 2,
|
||||
[OPTION_S16] = 2,
|
||||
[OPTION_U32] = 4,
|
||||
[OPTION_S32] = 4
|
||||
};
|
||||
|
||||
|
||||
/* get an option with bounds checking (warning, not aligned). */
|
||||
unsigned char *get_option(struct dhcpMessage *packet, int code)
|
||||
{
|
||||
int i = 0, length = 308;
|
||||
unsigned char *optionptr;
|
||||
int over = 0, done = 0, curr = OPTION_FIELD;
|
||||
|
||||
optionptr = packet->options;
|
||||
while (!done) {
|
||||
if (i >= length) {
|
||||
log_line(LOG_WARNING, "bogus packet, option fields too long.\n");
|
||||
return NULL;
|
||||
}
|
||||
if (optionptr[i + OPT_CODE] == code) {
|
||||
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
|
||||
log_line(LOG_WARNING, "bogus packet, option fields too long.\n");
|
||||
return NULL;
|
||||
}
|
||||
return optionptr + i + 2;
|
||||
}
|
||||
switch (optionptr[i + OPT_CODE]) {
|
||||
case DHCP_PADDING:
|
||||
i++;
|
||||
break;
|
||||
case DHCP_OPTION_OVER:
|
||||
if (i + 1 + optionptr[i + OPT_LEN] >= length) {
|
||||
log_line(LOG_WARNING,
|
||||
"bogus packet, option fields too long.\n");
|
||||
return NULL;
|
||||
}
|
||||
over = optionptr[i + 3];
|
||||
i += optionptr[OPT_LEN] + 2;
|
||||
break;
|
||||
case DHCP_END:
|
||||
if (curr == OPTION_FIELD && over & FILE_FIELD) {
|
||||
optionptr = packet->file;
|
||||
i = 0;
|
||||
length = 128;
|
||||
curr = FILE_FIELD;
|
||||
} else if (curr == FILE_FIELD && over & SNAME_FIELD) {
|
||||
optionptr = packet->sname;
|
||||
i = 0;
|
||||
length = 64;
|
||||
curr = SNAME_FIELD;
|
||||
} else done = 1;
|
||||
break;
|
||||
default:
|
||||
i += optionptr[OPT_LEN + i] + 2;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* return the position of the 'end' option */
|
||||
int end_option(unsigned char *optionptr)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (i < 308 && optionptr[i] != DHCP_END) {
|
||||
if (optionptr[i] == DHCP_PADDING)
|
||||
++i;
|
||||
else
|
||||
i += optionptr[i + OPT_LEN] + 2;
|
||||
}
|
||||
return (i < 308 ? i : 308);
|
||||
}
|
||||
|
||||
|
||||
/* add an option string to the options (an option string contains an option
|
||||
* code, length, then data) */
|
||||
int add_option_string(unsigned char *optionptr, unsigned char *string)
|
||||
{
|
||||
int end = end_option(optionptr);
|
||||
|
||||
/* end position + string length + option code/length + end option */
|
||||
if (end + string[OPT_LEN] + 2 + 1 >= 308) {
|
||||
log_line(LOG_ERR, "Option 0x%02x did not fit into the packet!\n",
|
||||
string[OPT_CODE]);
|
||||
return 0;
|
||||
}
|
||||
debug(LOG_INFO, "adding option 0x%02x\n", string[OPT_CODE]);
|
||||
memcpy(optionptr + end, string, string[OPT_LEN] + 2);
|
||||
optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
|
||||
return string[OPT_LEN] + 2;
|
||||
}
|
||||
|
||||
int add_simple_option(unsigned char *optionptr, unsigned char code,
|
||||
uint32_t data)
|
||||
{
|
||||
int i, length = 0;
|
||||
unsigned char option[2 + 4];
|
||||
|
||||
for (i = 0; options[i].code; i++)
|
||||
if (options[i].code == code) {
|
||||
length = option_lengths[options[i].flags & TYPE_MASK];
|
||||
}
|
||||
|
||||
option[OPT_CODE] = code;
|
||||
option[OPT_LEN] = (unsigned char)length;
|
||||
|
||||
if (!length) {
|
||||
debug(LOG_ERR, "Could not add option 0x%02x\n", code);
|
||||
return 0;
|
||||
} else if (length == 1) {
|
||||
uint8_t t = (uint8_t)data;
|
||||
memcpy(option + 2, &t, 1);
|
||||
} else if (length == 2) {
|
||||
uint16_t t = (uint16_t)data;
|
||||
memcpy(option + 2, &t, 2);
|
||||
} else if (length == 4) {
|
||||
uint32_t t = (uint32_t)data;
|
||||
memcpy(option + 2, &t, 4);
|
||||
}
|
||||
return add_option_string(optionptr, option);
|
||||
}
|
||||
|
||||
/* find option 'code' in opt_list */
|
||||
struct option_set *find_option(struct option_set *opt_list, char code)
|
||||
{
|
||||
while (opt_list && opt_list->data[OPT_CODE] < code)
|
||||
opt_list = opt_list->next;
|
||||
|
||||
if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* add an option to the opt_list */
|
||||
void attach_option(struct option_set **opt_list, struct dhcp_option *option,
|
||||
char *buffer, int length)
|
||||
{
|
||||
struct option_set *existing, *new, **curr;
|
||||
|
||||
/* add it to an existing option */
|
||||
if ((existing = find_option(*opt_list, option->code))) {
|
||||
debug(LOG_INFO, "Attaching option %s to existing member of list\n",
|
||||
option->name);
|
||||
if (option->flags & OPTION_LIST) {
|
||||
if (existing->data[OPT_LEN] + length <= 255) {
|
||||
existing->data = realloc(existing->data,
|
||||
existing->data[OPT_LEN] + length + 2);
|
||||
memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer,
|
||||
length);
|
||||
existing->data[OPT_LEN] += length;
|
||||
} /* else, ignore the data; we could put this in a second option
|
||||
in the future */
|
||||
} /* else, ignore the new data */
|
||||
} else {
|
||||
debug(LOG_INFO, "Attaching option %s to list\n", option->name);
|
||||
|
||||
/* make a new option */
|
||||
new = malloc(sizeof(struct option_set));
|
||||
new->data = malloc(length + 2);
|
||||
new->data[OPT_CODE] = option->code;
|
||||
new->data[OPT_LEN] = length;
|
||||
memcpy(new->data + 2, buffer, length);
|
||||
|
||||
curr = opt_list;
|
||||
while (*curr && (*curr)->data[OPT_CODE] < option->code)
|
||||
curr = &(*curr)->next;
|
||||
|
||||
new->next = *curr;
|
||||
*curr = new;
|
||||
}
|
||||
}
|
40
ndhc/options.h
Normal file
40
ndhc/options.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* options.h */
|
||||
#ifndef _OPTIONS_H
|
||||
#define _OPTIONS_H
|
||||
|
||||
#include "packet.h"
|
||||
|
||||
#define TYPE_MASK 0x0F
|
||||
|
||||
enum {
|
||||
OPTION_IP=1,
|
||||
OPTION_IP_PAIR,
|
||||
OPTION_STRING,
|
||||
OPTION_BOOLEAN,
|
||||
OPTION_U8,
|
||||
OPTION_U16,
|
||||
OPTION_S16,
|
||||
OPTION_U32,
|
||||
OPTION_S32
|
||||
};
|
||||
|
||||
#define OPTION_REQ 0x10 /* have the client request this option */
|
||||
#define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */
|
||||
|
||||
struct dhcp_option {
|
||||
char name[10];
|
||||
char flags;
|
||||
unsigned char code;
|
||||
};
|
||||
|
||||
extern struct dhcp_option options[];
|
||||
extern int option_lengths[];
|
||||
|
||||
unsigned char *get_option(struct dhcpMessage *packet, int code);
|
||||
int end_option(unsigned char *optionptr);
|
||||
int add_option_string(unsigned char *optionptr, unsigned char *string);
|
||||
int add_simple_option(unsigned char *optionptr, unsigned char code, uint32_t data);
|
||||
struct option_set *find_option(struct option_set *opt_list, char code);
|
||||
void attach_option(struct option_set **opt_list, struct dhcp_option *option, char *buffer, int length);
|
||||
|
||||
#endif
|
203
ndhc/packet.c
Normal file
203
ndhc/packet.c
Normal file
@@ -0,0 +1,203 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <features.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "packet.h"
|
||||
#include "log.h"
|
||||
#include "dhcpd.h"
|
||||
#include "options.h"
|
||||
|
||||
|
||||
void init_header(struct dhcpMessage *packet, char type)
|
||||
{
|
||||
memset(packet, 0, sizeof(struct dhcpMessage));
|
||||
switch (type) {
|
||||
case DHCPDISCOVER:
|
||||
case DHCPREQUEST:
|
||||
case DHCPRELEASE:
|
||||
case DHCPINFORM:
|
||||
packet->op = BOOTREQUEST;
|
||||
break;
|
||||
case DHCPOFFER:
|
||||
case DHCPACK:
|
||||
case DHCPNAK:
|
||||
packet->op = BOOTREPLY;
|
||||
}
|
||||
packet->htype = ETH_10MB;
|
||||
packet->hlen = ETH_10MB_LEN;
|
||||
packet->cookie = htonl(DHCP_MAGIC);
|
||||
packet->options[0] = DHCP_END;
|
||||
add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
|
||||
}
|
||||
|
||||
|
||||
/* read a packet from socket fd, return -1 on read error, -2 on packet error */
|
||||
int get_packet(struct dhcpMessage *packet, int fd)
|
||||
{
|
||||
int bytes;
|
||||
int i;
|
||||
const char broken_vendors[][8] = {
|
||||
"MSFT 98",
|
||||
""
|
||||
};
|
||||
unsigned char *vendor;
|
||||
|
||||
memset(packet, 0, sizeof(struct dhcpMessage));
|
||||
bytes = read(fd, packet, sizeof(struct dhcpMessage));
|
||||
if (bytes < 0) {
|
||||
debug(LOG_INFO, "couldn't read on listening socket, ignoring\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ntohl(packet->cookie) != DHCP_MAGIC) {
|
||||
log_line(LOG_ERR, "received bogus message, ignoring.\n");
|
||||
return -2;
|
||||
}
|
||||
debug(LOG_INFO, "Received a packet\n");
|
||||
|
||||
if (packet->op == BOOTREQUEST
|
||||
&& (vendor = get_option(packet, DHCP_VENDOR)))
|
||||
{
|
||||
for (i = 0; broken_vendors[i][0]; i++) {
|
||||
if (vendor[OPT_LEN - 2] == (unsigned char)strlen(broken_vendors[i])
|
||||
&& !strncmp((char *)vendor, broken_vendors[i],
|
||||
vendor[OPT_LEN - 2]))
|
||||
{
|
||||
debug(LOG_INFO, "broken client (%s), forcing broadcast\n",
|
||||
broken_vendors[i]);
|
||||
packet->flags |= htons(BROADCAST_FLAG);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
uint16_t checksum(void *addr, int count)
|
||||
{
|
||||
/* Compute Internet Checksum for "count" bytes
|
||||
* beginning at location "addr".
|
||||
*/
|
||||
register int32_t sum = 0;
|
||||
uint16_t *source = (uint16_t *)addr;
|
||||
|
||||
while (count > 1) {
|
||||
sum += *source++;
|
||||
count -= 2;
|
||||
}
|
||||
|
||||
/* Add left-over byte, if any */
|
||||
if (count > 0) {
|
||||
/* Make sure that the left-over byte is added correctly both
|
||||
* with little and big endian hosts */
|
||||
uint16_t tmp = 0;
|
||||
*(unsigned char *) (&tmp) = * (unsigned char *) source;
|
||||
sum += tmp;
|
||||
}
|
||||
/* Fold 32-bit sum to 16 bits */
|
||||
while (sum >> 16)
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
|
||||
return ~sum;
|
||||
}
|
||||
|
||||
|
||||
/* Constuct a ip/udp header for a packet, and specify the source and dest
|
||||
* hardware address */
|
||||
int raw_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
||||
int source_port, uint32_t dest_ip, int dest_port,
|
||||
unsigned char *dest_arp, int ifindex)
|
||||
{
|
||||
int fd, result = -1;
|
||||
struct sockaddr_ll dest;
|
||||
struct udp_dhcp_packet packet;
|
||||
|
||||
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(&dest, 0, sizeof(dest));
|
||||
memset(&packet, 0, sizeof(packet));
|
||||
|
||||
dest.sll_family = AF_PACKET;
|
||||
dest.sll_protocol = htons(ETH_P_IP);
|
||||
dest.sll_ifindex = ifindex;
|
||||
dest.sll_halen = 6;
|
||||
memcpy(dest.sll_addr, dest_arp, 6);
|
||||
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
|
||||
debug(LOG_ERR, "bind call failed: %s\n", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
packet.ip.protocol = IPPROTO_UDP;
|
||||
packet.ip.saddr = source_ip;
|
||||
packet.ip.daddr = dest_ip;
|
||||
packet.udp.source = htons(source_port);
|
||||
packet.udp.dest = htons(dest_port);
|
||||
/* cheat on the psuedo-header */
|
||||
packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage));
|
||||
packet.ip.tot_len = packet.udp.len;
|
||||
memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
|
||||
packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet));
|
||||
|
||||
packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
|
||||
packet.ip.ihl = sizeof(packet.ip) >> 2;
|
||||
packet.ip.version = IPVERSION;
|
||||
packet.ip.ttl = IPDEFTTL;
|
||||
packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip));
|
||||
|
||||
result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
|
||||
(struct sockaddr *)&dest, sizeof dest);
|
||||
if (result <= 0) {
|
||||
debug(LOG_ERR, "write on socket failed: %s\n",
|
||||
strerror(errno));
|
||||
}
|
||||
out_fd:
|
||||
close(fd);
|
||||
out:
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* Let the kernel do all the work for packet generation */
|
||||
int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
||||
int source_port, uint32_t dest_ip, int dest_port)
|
||||
{
|
||||
int n = 1, fd, result = -1;
|
||||
struct sockaddr_in client;
|
||||
|
||||
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
|
||||
goto out;
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1)
|
||||
goto out_fd;
|
||||
|
||||
memset(&client, 0, sizeof(client));
|
||||
client.sin_family = AF_INET;
|
||||
client.sin_port = htons(source_port);
|
||||
client.sin_addr.s_addr = source_ip;
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
|
||||
goto out_fd;
|
||||
|
||||
memset(&client, 0, sizeof(client));
|
||||
client.sin_family = AF_INET;
|
||||
client.sin_port = htons(dest_port);
|
||||
client.sin_addr.s_addr = dest_ip;
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
|
||||
goto out_fd;
|
||||
|
||||
result = write(fd, payload, sizeof(struct dhcpMessage));
|
||||
out_fd:
|
||||
close(fd);
|
||||
out:
|
||||
return result;
|
||||
}
|
||||
|
41
ndhc/packet.h
Normal file
41
ndhc/packet.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef _PACKET_H
|
||||
#define _PACKET_H
|
||||
|
||||
#include <netinet/udp.h>
|
||||
#include <netinet/ip.h>
|
||||
|
||||
struct dhcpMessage {
|
||||
uint8_t op;
|
||||
uint8_t htype;
|
||||
uint8_t hlen;
|
||||
uint8_t hops;
|
||||
uint32_t xid;
|
||||
uint16_t secs;
|
||||
uint16_t flags;
|
||||
uint32_t ciaddr;
|
||||
uint32_t yiaddr;
|
||||
uint32_t siaddr;
|
||||
uint32_t giaddr;
|
||||
uint8_t chaddr[16];
|
||||
uint8_t sname[64];
|
||||
uint8_t file[128];
|
||||
uint32_t cookie;
|
||||
uint8_t options[308]; /* 312 - cookie */
|
||||
};
|
||||
|
||||
struct udp_dhcp_packet {
|
||||
struct iphdr ip;
|
||||
struct udphdr udp;
|
||||
struct dhcpMessage data;
|
||||
};
|
||||
|
||||
void init_header(struct dhcpMessage *packet, char type);
|
||||
int get_packet(struct dhcpMessage *packet, int fd);
|
||||
uint16_t checksum(void *addr, int count);
|
||||
int raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
|
||||
uint32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex);
|
||||
int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port,
|
||||
uint32_t dest_ip, int dest_port);
|
||||
|
||||
|
||||
#endif
|
67
ndhc/rootcap.c
Normal file
67
ndhc/rootcap.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/capability.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <grp.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
static void set_cap(uid_t uid, gid_t gid, char *captxt)
|
||||
{
|
||||
cap_t caps;
|
||||
|
||||
if (!captxt) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: captxt == NULL\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (prctl(PR_SET_KEEPCAPS, 1)) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: prctl() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (setgroups(0, NULL) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: setgroups() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (setegid(gid) == -1 || seteuid(uid) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: seteuid() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
caps = cap_from_text(captxt);
|
||||
if (!caps) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: cap_from_text() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (cap_set_proc(caps) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - set_cap: cap_set_proc() failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
cap_free(caps);
|
||||
}
|
||||
|
||||
void drop_root(uid_t uid, gid_t gid, char *captxt)
|
||||
{
|
||||
if (!captxt) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: captxt == NULL\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (uid == 0 || gid == 0) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: attempt to drop root to root?\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
set_cap(uid, gid, captxt);
|
||||
|
||||
if (setregid(gid, gid) == -1 || setreuid(uid, uid) == -1) {
|
||||
log_line(LOG_ERR, "FATAL - drop_root: failed to drop root!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
2
ndhc/rootcap.h
Normal file
2
ndhc/rootcap.h
Normal file
@@ -0,0 +1,2 @@
|
||||
void drop_root(uid_t uid, gid_t gid, char *captxt);
|
||||
|
258
ndhc/script.c
Normal file
258
ndhc/script.c
Normal file
@@ -0,0 +1,258 @@
|
||||
/* script.c
|
||||
*
|
||||
* Functions to call the interface change daemon
|
||||
*
|
||||
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||
* Nicholas Kain <njk@nozspamz.aerifal.cx> 2004
|
||||
*
|
||||
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "options.h"
|
||||
#include "dhcpd.h"
|
||||
#include "dhcpc.h"
|
||||
#include "packet.h"
|
||||
#include "options.h"
|
||||
#include "log.h"
|
||||
#include "script.h"
|
||||
|
||||
static int snprintip(char *dest, size_t size, unsigned char *ip) {
|
||||
if (!dest) return -1;
|
||||
return snprintf(dest, size, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||
}
|
||||
|
||||
static int sprintip(char *dest, size_t size, char *pre, unsigned char *ip) {
|
||||
if (!dest) return -1;
|
||||
return snprintf(dest, size, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
|
||||
}
|
||||
|
||||
/* Fill dest with the text of option 'option'. */
|
||||
static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p, unsigned int maxlen)
|
||||
{
|
||||
int type, optlen;
|
||||
uint16_t val_u16;
|
||||
int16_t val_s16;
|
||||
uint32_t val_u32;
|
||||
int32_t val_s32;
|
||||
int len = option[OPT_LEN - 2];
|
||||
char *odest;
|
||||
|
||||
odest = dest;
|
||||
|
||||
dest += snprintf(dest, maxlen, "%s=", type_p->name);
|
||||
|
||||
type = type_p->flags & TYPE_MASK;
|
||||
optlen = option_lengths[type];
|
||||
for(;;) {
|
||||
switch (type) {
|
||||
case OPTION_IP_PAIR:
|
||||
dest += sprintip(dest, maxlen - (dest - odest), "", option);
|
||||
*(dest++) = '/';
|
||||
option += 4;
|
||||
optlen = 4;
|
||||
case OPTION_IP: /* Works regardless of host byte order. */
|
||||
dest += sprintip(dest, maxlen - (dest - odest), "", option);
|
||||
break;
|
||||
case OPTION_BOOLEAN:
|
||||
dest += snprintf(dest, maxlen - (dest - odest), *option ? "yes " : "no ");
|
||||
break;
|
||||
case OPTION_U8:
|
||||
dest += snprintf(dest, maxlen - (dest - odest), "%u ", *option);
|
||||
break;
|
||||
case OPTION_U16:
|
||||
memcpy(&val_u16, option, 2);
|
||||
dest += snprintf(dest, maxlen - (dest - odest), "%u ", ntohs(val_u16));
|
||||
break;
|
||||
case OPTION_S16:
|
||||
memcpy(&val_s16, option, 2);
|
||||
dest += snprintf(dest, maxlen - (dest - odest), "%d ", ntohs(val_s16));
|
||||
break;
|
||||
case OPTION_U32:
|
||||
memcpy(&val_u32, option, 4);
|
||||
dest += snprintf(dest, maxlen - (dest - odest), "%lu ", (unsigned long) ntohl(val_u32));
|
||||
break;
|
||||
case OPTION_S32:
|
||||
memcpy(&val_s32, option, 4);
|
||||
dest += snprintf(dest, maxlen - (dest - odest), "%ld ", (long) ntohl(val_s32));
|
||||
break;
|
||||
case OPTION_STRING:
|
||||
if ( (maxlen - (dest - odest)) < (unsigned)len) return;
|
||||
memcpy(dest, option, len);
|
||||
dest[len] = '\0';
|
||||
return; /* Short circuit this case */
|
||||
}
|
||||
option += optlen;
|
||||
len -= optlen;
|
||||
if (len <= 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
static int open_ifch(void) {
|
||||
int sockfd, ret;
|
||||
struct sockaddr_un address =
|
||||
{
|
||||
.sun_family = AF_UNIX,
|
||||
.sun_path = "ifchange"
|
||||
};
|
||||
|
||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
|
||||
|
||||
if (ret == -1) {
|
||||
log_line(LOG_ERR, "unable to connect to ifchd!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
static void sockwrite(int fd, const void *buf, size_t count)
|
||||
{
|
||||
int ret;
|
||||
|
||||
sockwrite_again:
|
||||
ret = write(fd, buf, count);
|
||||
if (ret == -1) {
|
||||
if (errno == EAGAIN)
|
||||
goto sockwrite_again;
|
||||
log_line(LOG_ERR, "error while writing to unix socket!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (ret < 0) ret = 0;
|
||||
if ((unsigned int)ret < strlen(buf)) {
|
||||
log_line(LOG_ERR, "incomplete write!\n");
|
||||
}
|
||||
debug(LOG_INFO, "writing: %s\n", (char *)buf);
|
||||
}
|
||||
|
||||
static void deconfig_if(void)
|
||||
{
|
||||
int sockfd;
|
||||
char buf[256];
|
||||
|
||||
memset(buf, '\0', sizeof buf);
|
||||
|
||||
sockfd = open_ifch();
|
||||
|
||||
snprintf(buf, sizeof buf, "interface:%s:",
|
||||
client_config.interface);
|
||||
sockwrite(sockfd, buf, strlen(buf));
|
||||
|
||||
snprintf(buf, sizeof buf, "ip:0.0.0.0:");
|
||||
sockwrite(sockfd, buf, strlen(buf));
|
||||
|
||||
close(sockfd);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static void translate_option(int sockfd, struct dhcpMessage *packet, int opt) {
|
||||
char buf[256], buf2[256];
|
||||
unsigned char *p;
|
||||
int i;
|
||||
|
||||
if (!packet) return;
|
||||
|
||||
memset(buf, '\0', sizeof(buf));
|
||||
memset(buf2, '\0', sizeof(buf2));
|
||||
|
||||
p = get_option(packet, options[opt].code);
|
||||
fill_options(buf2, p, &options[opt], sizeof(buf2) - 1);
|
||||
snprintf(buf, sizeof buf, "%s:", buf2);
|
||||
for (i=0; i<256; i++) {
|
||||
if (buf[i] == '\0') break;
|
||||
if (buf[i] == '=') {
|
||||
buf[i] = ':';
|
||||
break;
|
||||
}
|
||||
}
|
||||
sockwrite(sockfd, buf, strlen(buf));
|
||||
}
|
||||
|
||||
static void bound_if(struct dhcpMessage *packet)
|
||||
{
|
||||
int sockfd;
|
||||
char buf[256], buf2[256];
|
||||
char ip[32];
|
||||
|
||||
if (!packet) return;
|
||||
|
||||
memset(buf, '\0', sizeof(buf));
|
||||
memset(ip, '\0', sizeof(ip));
|
||||
memset(buf2, '\0', sizeof(buf2));
|
||||
|
||||
sockfd = open_ifch();
|
||||
|
||||
snprintf(buf, sizeof buf, "interface:%s:", client_config.interface);
|
||||
sockwrite(sockfd, buf, strlen(buf));
|
||||
|
||||
snprintip(ip, sizeof ip, (unsigned char *) &packet->yiaddr);
|
||||
snprintf(buf, sizeof buf, "ip:%s:", ip);
|
||||
sockwrite(sockfd, buf, strlen(buf));
|
||||
|
||||
translate_option(sockfd, packet, 0);
|
||||
translate_option(sockfd, packet, 2);
|
||||
translate_option(sockfd, packet, 5);
|
||||
translate_option(sockfd, packet, 9);
|
||||
translate_option(sockfd, packet, 11);
|
||||
translate_option(sockfd, packet, 15);
|
||||
translate_option(sockfd, packet, 16);
|
||||
translate_option(sockfd, packet, 17);
|
||||
|
||||
close(sockfd);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void run_script(struct dhcpMessage *packet, int mode)
|
||||
{
|
||||
int pid;
|
||||
|
||||
pid = fork();
|
||||
if (pid) {
|
||||
waitpid(pid, NULL, 0);
|
||||
return;
|
||||
} else if (pid == 0) {
|
||||
switch (mode) {
|
||||
case SCRIPT_DECONFIG:
|
||||
deconfig_if();
|
||||
break;
|
||||
case SCRIPT_BOUND:
|
||||
bound_if(packet);
|
||||
break;
|
||||
case SCRIPT_RENEW:
|
||||
bound_if(packet);
|
||||
break;
|
||||
case SCRIPT_NAK:
|
||||
deconfig_if();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log_line(LOG_ERR, "invalid script mode: %d\n", mode);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
13
ndhc/script.h
Normal file
13
ndhc/script.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef _SCRIPT_H
|
||||
#define _SCRIPT_H
|
||||
|
||||
enum {
|
||||
SCRIPT_DECONFIG = 0,
|
||||
SCRIPT_BOUND = 1,
|
||||
SCRIPT_RENEW = 2,
|
||||
SCRIPT_NAK = 4
|
||||
};
|
||||
|
||||
void run_script(struct dhcpMessage *packet, int mode);
|
||||
|
||||
#endif
|
155
ndhc/socket.c
Normal file
155
ndhc/socket.c
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* socket.c -- DHCP server client/server socket creation
|
||||
*
|
||||
* udhcp client/server
|
||||
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
|
||||
* Chris Trew <ctrew@moreton.com.au>
|
||||
*
|
||||
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
||||
*
|
||||
* Cleanup and fixes, Nicholas Kain <njk@n0sPaM.kain.us> 2004
|
||||
*
|
||||
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <errno.h>
|
||||
#include <features.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include "log.h"
|
||||
#include "nstrl.h"
|
||||
|
||||
int read_interface(char *interface, int *ifindex, uint32_t *addr,
|
||||
unsigned char *arp)
|
||||
{
|
||||
int fd, ret = -1;
|
||||
struct ifreq ifr;
|
||||
struct sockaddr_in *our_ip;
|
||||
|
||||
memset(&ifr, 0, sizeof(struct ifreq));
|
||||
if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
|
||||
log_line(LOG_ERR, "socket failed!: %s\n", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
|
||||
|
||||
if (addr) {
|
||||
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
|
||||
log_line(LOG_ERR, "Couldn't get IP for %s.\n", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
|
||||
*addr = our_ip->sin_addr.s_addr;
|
||||
debug(LOG_INFO, "%s (our ip) = %s\n", ifr.ifr_name,
|
||||
inet_ntoa(our_ip->sin_addr));
|
||||
}
|
||||
|
||||
if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
|
||||
log_line(LOG_ERR, "SIOCGIFINDEX failed!: %s\n", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
debug(LOG_INFO, "adapter index %d\n", ifr.ifr_ifindex);
|
||||
*ifindex = ifr.ifr_ifindex;
|
||||
|
||||
if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
|
||||
log_line(LOG_ERR, "Couldn't get MAC for %s\n", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
|
||||
debug(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
|
||||
ret = 0;
|
||||
out_fd:
|
||||
close(fd);
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int listen_socket(unsigned int ip, int port, char *inf)
|
||||
{
|
||||
struct ifreq interface;
|
||||
int fd;
|
||||
struct sockaddr_in addr;
|
||||
int n = 1;
|
||||
|
||||
debug(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf);
|
||||
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = ip;
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof n) == -1)
|
||||
goto out_fd;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *)&n, sizeof n) == -1)
|
||||
goto out_fd;
|
||||
|
||||
strlcpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ);
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
|
||||
(char *)&interface, sizeof interface) < 0)
|
||||
goto out_fd;
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)
|
||||
goto out_fd;
|
||||
|
||||
return fd;
|
||||
out_fd:
|
||||
close(fd);
|
||||
out:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int raw_socket(int ifindex)
|
||||
{
|
||||
int fd;
|
||||
struct sockaddr_ll sock;
|
||||
|
||||
debug(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex);
|
||||
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
|
||||
debug(LOG_ERR, "socket call failed: %s\n", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
sock.sll_family = AF_PACKET;
|
||||
sock.sll_protocol = htons(ETH_P_IP);
|
||||
sock.sll_ifindex = ifindex;
|
||||
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
|
||||
debug(LOG_ERR, "bind call failed: %s\n", strerror(errno));
|
||||
goto out_fd;
|
||||
}
|
||||
|
||||
return fd;
|
||||
out_fd:
|
||||
close(fd);
|
||||
out:
|
||||
return -1;
|
||||
}
|
||||
|
9
ndhc/socket.h
Normal file
9
ndhc/socket.h
Normal file
@@ -0,0 +1,9 @@
|
||||
/* socket.h */
|
||||
#ifndef _SOCKET_H
|
||||
#define _SOCKET_H
|
||||
|
||||
int read_interface(char *interface, int *ifindex, uint32_t *addr, unsigned char *arp);
|
||||
int listen_socket(unsigned int ip, int port, char *inf);
|
||||
int raw_socket(int ifindex);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user