├── .gitignore
├── .travis.yml
├── LICENSE
├── Makefile
├── README.md
├── auth.c
├── auth.h
├── configparse.c
├── configparse.h
├── daemon.c
├── daemon.h
├── debug.h
├── eapol.c
├── eapol.h
├── keepalive.c
├── keepalive.h
├── libs
├── common.c
├── common.h
├── md4.c
├── md4.h
├── md5.c
├── md5.h
├── sha1.c
└── sha1.h
├── main.c
├── sample-d.conf
└── sample-p.conf
/.gitignore:
--------------------------------------------------------------------------------
1 | test
2 | drcom.conf
3 | test.conf
4 | dogcom
5 | main
6 | .vscode
7 |
8 | # Prerequisites
9 | *.d
10 |
11 | # Object files
12 | *.o
13 | *.ko
14 | *.obj
15 | *.elf
16 |
17 | # Linker output
18 | *.ilk
19 | *.map
20 | *.exp
21 |
22 | # Precompiled Headers
23 | *.gch
24 | *.pch
25 |
26 | # Libraries
27 | *.lib
28 | *.a
29 | *.la
30 | *.lo
31 |
32 | # Shared objects (inc. Windows DLLs)
33 | *.dll
34 | *.so
35 | *.so.*
36 | *.dylib
37 |
38 | # Executables
39 | *.exe
40 | *.out
41 | *.app
42 | *.i*86
43 | *.x86_64
44 | *.hex
45 |
46 | # Debug files
47 | *.dSYM/
48 | *.su
49 | *.idb
50 | *.pdb
51 |
52 | # Kernel Module Compile Results
53 | *.mod*
54 | *.cmd
55 | modules.order
56 | Module.symvers
57 | Mkfile.old
58 | dkms.conf
59 | *.stackdump
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: c
2 | os:
3 | - linux
4 | - osx
5 | compiler:
6 | - gcc
7 | - clang
8 |
9 | script:
10 | - make test=y
11 | - ./dogcom -m dhcp -c sample-d.conf
12 | - ./dogcom -m pppoe -c sample-p.conf
13 |
14 | branches:
15 | only:
16 | - master
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | TARGET = dogcom
3 | INSTALL_DIR = /usr/bin/
4 |
5 | ifeq ($(debug), y)
6 | CFLAGS += -DDEBUG -g
7 | endif
8 |
9 | ifeq ($(win32), y)
10 | CFLAGS += -lws2_32
11 | # TARGET = dogcom.exe
12 | endif
13 |
14 | ifeq ($(static), y)
15 | CFLAGS += -static
16 | endif
17 |
18 | ifeq ($(strip), y)
19 | CFLAGS += -Os -s -Wno-unused-result
20 | endif
21 |
22 | ifeq ($(force_encrypt), y)
23 | CFLAGS += -DFORCE_ENCRYPT
24 | endif
25 |
26 | ifeq ($(test), y)
27 | CFLAGS += -std=gnu99 -Werror -DTEST
28 | else
29 | CFLAGS += -std=gnu99 -Werror
30 | endif
31 |
32 | SOURCES = $(wildcard *.c) $(wildcard libs/*.c)
33 | OBJS = $(patsubst %.c, %.o, $(SOURCES))
34 |
35 | $(TARGET): $(OBJS)
36 | $(CC) $(DEBUG) $(TEST) $(OBJS) $(CFLAGS) -o $(TARGET)
37 |
38 | all: $(TARGET)
39 |
40 | install: $(TARGET)
41 | cp $(TARGET) $(INSTALL_DIR)
42 |
43 | clean:
44 | rm -f $(OBJS)
45 | rm -f $(TARGET)
46 |
47 | distclean: clean
48 |
49 | .PHONY: all clean distclean install
50 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # dogcom [](https://travis-ci.org/mchome/dogcom) [](https://github.com/mchome/dogcom) [](https://github.com/mchome/dogcom/tree/v1.6.2)
2 |
3 | [Drcom-generic](https://github.com/drcoms/drcom-generic) implementation in C.
4 |
5 | ```
6 | Usage:
7 | dogcom -m -c [options ]...
8 |
9 | Options:
10 | --mode , -m set your dogcom mode
11 | --conf , -c import configuration file
12 | --bindip , -b bind your ip address(default is 0.0.0.0)
13 | --log , -l specify log file
14 | --802.1x, -x enable 802.1x
15 | --daemon, -d set daemon flag
16 | --eternal, -e set eternal flag
17 | --verbose, -v set verbose flag
18 | --help, -h display this help
19 | ```
20 |
21 | Config file is compatible with [drcom-generic](https://github.com/drcoms/drcom-generic).
22 |
23 | #### Example:
24 |
25 | ```bash
26 | $ dogcom -m dhcp -c dogcom.conf
27 | $ dogcom -m dhcp -c dogcom.conf -l /tmp/dogcom.log -v
28 | $ dogcom -m dhcp -c dogcom.conf -d # (PS: only on Linux build)
29 | $ dogcom -m pppoe -c dogcom.conf -x # (PS: only on Linux build)
30 | $ dogcom -m pppoe -c dogcom.conf -e # eternal dogcoming (default times is 5)
31 | $ dogcom -m pppoe -c dogcom.conf -v
32 | $ dogcom -m dhcp -c dogcom.conf -b 10.2.3.12 -v
33 | ```
34 |
35 | #### To build:
36 |
37 | ```bash
38 | $ make # Linux
39 | $ make win32=y # Windows(MinGW)
40 | $ make test=y # For testing purposes
41 | $ make force_encrypt=y # Force open encrypt mode in PPPoE version
42 | ```
43 |
44 | #### Openwrt-package
45 | [https://github.com/mchome/openwrt-dogcom](https://github.com/mchome/openwrt-dogcom)
46 |
47 | #### Tutorial
48 | [](https://asciinema.org/a/9j7cj1s61jiczx2s0206tosjr)
49 |
50 | ### Thanks:
51 | - [gdut-drcom](https://github.com/chenhaowen01/gdut-drcom 'chenhaowen01')
52 | - [jlu-drcom-client](https://github.com/drcoms/jlu-drcom-client/tree/master/C-version 'feix')
53 | - [leetking](https://github.com/leetking 'leetking')
54 |
55 | ### Special thanks:
56 | - [Drcom-generic](https://github.com/drcoms/drcom-generic 'ly0')
57 |
58 | ### License:
59 | 
60 |
--------------------------------------------------------------------------------
/auth.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #ifdef WIN32
9 | #include
10 | typedef int socklen_t;
11 | #else
12 | #include
13 | #include
14 | #include
15 | #endif
16 |
17 | #include "auth.h"
18 | #include "configparse.h"
19 | #include "debug.h"
20 | #include "keepalive.h"
21 | #include "libs/md4.h"
22 | #include "libs/md5.h"
23 | #include "libs/sha1.h"
24 |
25 | #define BIND_PORT 61440
26 | #define DEST_PORT 61440
27 |
28 | int dhcp_challenge(int sockfd, struct sockaddr_in addr, unsigned char seed[]) {
29 | unsigned char challenge_packet[20], recv_packet[1024];
30 | memset(challenge_packet, 0, 20);
31 | challenge_packet[0] = 0x01;
32 | challenge_packet[1] = 0x02;
33 | challenge_packet[2] = rand() & 0xff;
34 | challenge_packet[3] = rand() & 0xff;
35 | challenge_packet[4] = drcom_config.AUTH_VERSION[0];
36 |
37 | sendto(sockfd, challenge_packet, 20, 0, (struct sockaddr *)&addr, sizeof(addr));
38 |
39 | if (verbose_flag) {
40 | print_packet("[Challenge sent] ", challenge_packet, 20);
41 | }
42 | if (logging_flag) {
43 | logging("[Challenge sent] ", challenge_packet, 20);
44 | }
45 | #ifdef TEST
46 | unsigned char test[4] = {0x52, 0x6c, 0xe4, 0x00};
47 | memcpy(seed, test, 4);
48 | print_packet("[TEST MODE] ", seed, 4);
49 | return 0;
50 | #endif
51 |
52 | socklen_t addrlen = sizeof(addr);
53 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
54 | #ifdef WIN32
55 | get_lasterror("Failed to recv data");
56 | #else
57 | perror("Failed to recv data");
58 | #endif
59 | return 1;
60 | }
61 |
62 | if (verbose_flag) {
63 | print_packet("[Challenge recv] ", recv_packet, 76);
64 | }
65 | if (logging_flag) {
66 | logging("[Challenge recv] ", recv_packet, 76);
67 | }
68 |
69 | if (recv_packet[0] != 0x02) {
70 | printf("Bad challenge response received.\n");
71 | return 1;
72 | }
73 |
74 | memcpy(seed, &recv_packet[4], 4);
75 | #ifdef DEBUG
76 | print_packet(" ", seed, 4);
77 | #endif
78 |
79 | return 0;
80 | }
81 |
82 | int dhcp_login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[], int try_JLUversion) {
83 | unsigned int login_packet_size;
84 | unsigned int length_padding = 0;
85 | int JLU_padding = 0;
86 |
87 | if (strlen(drcom_config.password) > 8) {
88 | length_padding = strlen(drcom_config.password) - 8 + (length_padding % 2);
89 | if (try_JLUversion) {
90 | printf("Start JLU mode.\n");
91 | if (logging_flag) {
92 | logging("Start JLU mode.", NULL, 0);
93 | }
94 | if (strlen(drcom_config.password) != 16) {
95 | JLU_padding = strlen(drcom_config.password) / 4;
96 | }
97 | length_padding = 28 + (strlen(drcom_config.password) - 8) + JLU_padding;
98 | }
99 | }
100 | if (drcom_config.ror_version) {
101 | login_packet_size = 338 + length_padding;
102 | } else {
103 | login_packet_size = 330;
104 | }
105 | unsigned char login_packet[login_packet_size], recv_packet[1024], MD5A[16], MACxorMD5A[6], MD5B[16], checksum1[8], checksum2[4];
106 | memset(login_packet, 0, login_packet_size);
107 | memset(recv_packet, 0, 100);
108 |
109 | // build login-packet
110 | login_packet[0] = 0x03;
111 | login_packet[1] = 0x01;
112 | login_packet[2] = 0x00;
113 | login_packet[3] = strlen(drcom_config.username) + 20;
114 | int MD5A_len = 6 + strlen(drcom_config.password);
115 | unsigned char MD5A_str[MD5A_len];
116 | MD5A_str[0] = 0x03;
117 | MD5A_str[1] = 0x01;
118 | memcpy(MD5A_str + 2, seed, 4);
119 | memcpy(MD5A_str + 6, drcom_config.password, strlen(drcom_config.password));
120 | MD5(MD5A_str, MD5A_len, MD5A);
121 | memcpy(login_packet + 4, MD5A, 16);
122 | memcpy(login_packet + 20, drcom_config.username, strlen(drcom_config.username));
123 | memcpy(login_packet + 56, &drcom_config.CONTROLCHECKSTATUS, 1);
124 | memcpy(login_packet + 57, &drcom_config.ADAPTERNUM, 1);
125 | uint64_t sum = 0;
126 | uint64_t mac = 0;
127 | // unpack
128 | for (int i = 0; i < 6; i++) {
129 | sum = (int)MD5A[i] + sum * 256;
130 | }
131 | // unpack
132 | for (int i = 0; i < 6; i++) {
133 | mac = (int)drcom_config.mac[i] + mac * 256;
134 | }
135 | sum ^= mac;
136 | // pack
137 | for (int i = 6; i > 0; i--) {
138 | MACxorMD5A[i - 1] = (unsigned char)(sum % 256);
139 | sum /= 256;
140 | }
141 | memcpy(login_packet + 58, MACxorMD5A, sizeof(MACxorMD5A));
142 | int MD5B_len = 9 + strlen(drcom_config.password);
143 | unsigned char MD5B_str[MD5B_len];
144 | memset(MD5B_str, 0, MD5B_len);
145 | MD5B_str[0] = 0x01;
146 | memcpy(MD5B_str + 1, drcom_config.password, strlen(drcom_config.password));
147 | memcpy(MD5B_str + strlen(drcom_config.password) + 1, seed, 4);
148 | MD5(MD5B_str, MD5B_len, MD5B);
149 | memcpy(login_packet + 64, MD5B, 16);
150 | login_packet[80] = 0x01;
151 | unsigned char host_ip[4];
152 | sscanf(drcom_config.host_ip, "%hhd.%hhd.%hhd.%hhd",
153 | &host_ip[0],
154 | &host_ip[1],
155 | &host_ip[2],
156 | &host_ip[3]);
157 | memcpy(login_packet + 81, host_ip, 4);
158 | unsigned char checksum1_str[101], checksum1_tmp[4] = {0x14, 0x00, 0x07, 0x0b};
159 | memcpy(checksum1_str, login_packet, 97);
160 | memcpy(checksum1_str + 97, checksum1_tmp, 4);
161 | MD5(checksum1_str, 101, checksum1);
162 | memcpy(login_packet + 97, checksum1, 8);
163 | memcpy(login_packet + 105, &drcom_config.IPDOG, 1);
164 | memcpy(login_packet + 110, &drcom_config.host_name, strlen(drcom_config.host_name));
165 | unsigned char PRIMARY_DNS[4];
166 | sscanf(drcom_config.PRIMARY_DNS, "%hhd.%hhd.%hhd.%hhd",
167 | &PRIMARY_DNS[0],
168 | &PRIMARY_DNS[1],
169 | &PRIMARY_DNS[2],
170 | &PRIMARY_DNS[3]);
171 | memcpy(login_packet + 142, PRIMARY_DNS, 4);
172 | unsigned char dhcp_server[4];
173 | sscanf(drcom_config.dhcp_server, "%hhd.%hhd.%hhd.%hhd",
174 | &dhcp_server[0],
175 | &dhcp_server[1],
176 | &dhcp_server[2],
177 | &dhcp_server[3]);
178 | memcpy(login_packet + 146, dhcp_server, 4);
179 | unsigned char OSVersionInfoSize[4] = {0x94};
180 | unsigned char OSMajor[4] = {0x05};
181 | unsigned char OSMinor[4] = {0x01};
182 | unsigned char OSBuild[4] = {0x28, 0x0a};
183 | unsigned char PlatformID[4] = {0x02};
184 | if (try_JLUversion) {
185 | OSVersionInfoSize[0] = 0x94;
186 | OSMajor[0] = 0x06;
187 | OSMinor[0] = 0x02;
188 | OSBuild[0] = 0xf0;
189 | OSBuild[1] = 0x23;
190 | PlatformID[0] = 0x02;
191 | unsigned char ServicePack[40] = {0x33, 0x64, 0x63, 0x37, 0x39, 0x66, 0x35, 0x32, 0x31, 0x32, 0x65, 0x38, 0x31, 0x37, 0x30, 0x61, 0x63, 0x66, 0x61, 0x39, 0x65, 0x63, 0x39, 0x35, 0x66, 0x31, 0x64, 0x37, 0x34, 0x39, 0x31, 0x36, 0x35, 0x34, 0x32, 0x62, 0x65, 0x37, 0x62, 0x31};
192 | unsigned char hostname[9] = {0x44, 0x72, 0x43, 0x4f, 0x4d, 0x00, 0xcf, 0x07, 0x68};
193 | memcpy(login_packet + 182, hostname, 9);
194 | memcpy(login_packet + 246, ServicePack, 40);
195 | }
196 | memcpy(login_packet + 162, OSVersionInfoSize, 4);
197 | memcpy(login_packet + 166, OSMajor, 4);
198 | memcpy(login_packet + 170, OSMinor, 4);
199 | memcpy(login_packet + 174, OSBuild, 4);
200 | memcpy(login_packet + 178, PlatformID, 4);
201 | if (!try_JLUversion) {
202 | memcpy(login_packet + 182, &drcom_config.host_os, strlen(drcom_config.host_os));
203 | }
204 | memcpy(login_packet + 310, drcom_config.AUTH_VERSION, 2);
205 | int counter = 312;
206 | unsigned int ror_padding = 0;
207 | if (strlen(drcom_config.password) <= 8) {
208 | ror_padding = 8 - strlen(drcom_config.password);
209 | } else {
210 | if ((strlen(drcom_config.password) - 8) % 2) {
211 | ror_padding = 1;
212 | }
213 | if (try_JLUversion) {
214 | ror_padding = JLU_padding;
215 | }
216 | }
217 | if (drcom_config.ror_version) {
218 | MD5(MD5A_str, MD5A_len, MD5A);
219 | login_packet[counter + 1] = strlen(drcom_config.password);
220 | counter += 2;
221 | for (int i = 0, x = 0; i < strlen(drcom_config.password); i++) {
222 | x = (int)MD5A[i] ^ (int)drcom_config.password[i];
223 | login_packet[counter + i] = (unsigned char)(((x << 3) & 0xff) + (x >> 5));
224 | }
225 | counter += strlen(drcom_config.password);
226 | // print_packet("TEST ", ror, strlen(drcom_config.password));
227 | } else {
228 | ror_padding = 2;
229 | }
230 | login_packet[counter] = 0x02;
231 | login_packet[counter + 1] = 0x0c;
232 | unsigned char checksum2_str[counter + 18]; // [counter + 14 + 4]
233 | memset(checksum2_str, 0, counter + 18);
234 | unsigned char checksum2_tmp[6] = {0x01, 0x26, 0x07, 0x11};
235 | memcpy(checksum2_str, login_packet, counter + 2);
236 | memcpy(checksum2_str + counter + 2, checksum2_tmp, 6);
237 | memcpy(checksum2_str + counter + 8, drcom_config.mac, 6);
238 | sum = 1234;
239 | uint64_t ret = 0;
240 | for (int i = 0; i < counter + 14; i += 4) {
241 | ret = 0;
242 | // reverse unsigned char array[4]
243 | for (int j = 4; j > 0; j--) {
244 | ret = ret * 256 + (int)checksum2_str[i + j - 1];
245 | }
246 | sum ^= ret;
247 | }
248 | sum = (1968 * sum) & 0xffffffff;
249 | for (int j = 0; j < 4; j++) {
250 | checksum2[j] = (unsigned char)(sum >> (j * 8) & 0xff);
251 | }
252 | memcpy(login_packet + counter + 2, checksum2, 4);
253 | memcpy(login_packet + counter + 8, drcom_config.mac, 6);
254 | login_packet[counter + ror_padding + 14] = 0xe9;
255 | login_packet[counter + ror_padding + 15] = 0x13;
256 | if (try_JLUversion) {
257 | login_packet[counter + ror_padding + 14] = 0x60;
258 | login_packet[counter + ror_padding + 15] = 0xa2;
259 | }
260 |
261 | sendto(sockfd, login_packet, sizeof(login_packet), 0, (struct sockaddr *)&addr, sizeof(addr));
262 |
263 | if (verbose_flag) {
264 | print_packet("[Login sent] ", login_packet, sizeof(login_packet));
265 | }
266 | if (logging_flag) {
267 | logging("[Login sent] ", login_packet, sizeof(login_packet));
268 | }
269 |
270 | #ifdef TEST
271 | unsigned char test[16] = {0x44, 0x72, 0x63, 0x6f, 0x77, 0x27, 0x20, 0xca, 0xed, 0x05, 0x6e, 0x35, 0xaa, 0x8b, 0x01, 0xfb};
272 | memcpy(auth_information, test, 16);
273 | print_packet("[TEST MODE] ", auth_information, 16);
274 | return 0;
275 | #endif
276 |
277 | socklen_t addrlen = sizeof(addr);
278 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
279 | #ifdef WIN32
280 | get_lasterror("Failed to recv data");
281 | #else
282 | perror("Failed to recv data");
283 | #endif
284 | return 1;
285 | }
286 |
287 | if (recv_packet[0] != 0x04) {
288 | if (verbose_flag) {
289 | print_packet("[login recv] ", recv_packet, 100);
290 | }
291 | printf("<<< Login failed >>>\n");
292 | if (logging_flag) {
293 | logging("[login recv] ", recv_packet, 100);
294 | logging("<<< Login failed >>>", NULL, 0);
295 | }
296 | char err_msg[256];
297 | if (recv_packet[0] == 0x05) {
298 | switch (recv_packet[4]) {
299 | case CHECK_MAC:
300 | strcpy(err_msg, "[Tips] Someone is using this account with wired.");
301 | break;
302 | case SERVER_BUSY:
303 | strcpy(err_msg, "[Tips] The server is busy, please log back in again.");
304 | break;
305 | case WRONG_PASS:
306 | strcpy(err_msg, "[Tips] Account and password not match.");
307 | break;
308 | case NOT_ENOUGH:
309 | strcpy(err_msg, "[Tips] The cumulative time or traffic for this account has exceeded the limit.");
310 | break;
311 | case FREEZE_UP:
312 | strcpy(err_msg, "[Tips] This account is suspended.");
313 | break;
314 | case NOT_ON_THIS_IP:
315 | strcpy(err_msg, "[Tips] IP address does not match, this account can only be used in the specified IP address.");
316 | break;
317 | case NOT_ON_THIS_MAC:
318 | strcpy(err_msg, "[Tips] MAC address does not match, this account can only be used in the specified IP and MAC address.");
319 | break;
320 | case TOO_MUCH_IP:
321 | strcpy(err_msg, "[Tips] This account has too many IP addresses.");
322 | break;
323 | case UPDATE_CLIENT:
324 | strcpy(err_msg, "[Tips] The client version is incorrect.");
325 | break;
326 | case NOT_ON_THIS_IP_MAC:
327 | strcpy(err_msg, "[Tips] This account can only be used on specified MAC and IP address.");
328 | break;
329 | case MUST_USE_DHCP:
330 | strcpy(err_msg, "[Tips] Your PC set up a static IP, please change to DHCP, and then re-login.");
331 | break;
332 | default:
333 | strcpy(err_msg, "[Tips] Unknown error number.");
334 | break;
335 | }
336 | printf("%s\n", err_msg);
337 | if (logging_flag) {
338 | logging(err_msg, NULL, 0);
339 | }
340 | }
341 | return 1;
342 | } else {
343 | if (verbose_flag) {
344 | print_packet("[login recv] ", recv_packet, 100);
345 | }
346 | printf("<<< Logged in >>>\n");
347 | if (logging_flag) {
348 | logging("[login recv] ", recv_packet, 100);
349 | logging("<<< Logged in >>>", NULL, 0);
350 | }
351 | }
352 |
353 | memcpy(auth_information, &recv_packet[23], 16);
354 | #ifdef DEBUG
355 | print_packet(" ", auth_information, 16);
356 | #endif
357 |
358 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) >= 0) {
359 | DEBUG_PRINT(("Get notice packet."));
360 | }
361 |
362 | return 0;
363 | }
364 |
365 | int pppoe_challenge(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigned char seed[], unsigned char sip[], int *encrypt_mode) {
366 | unsigned char challenge_packet[8], recv_packet[1024];
367 | memset(challenge_packet, 0, 8);
368 | unsigned char challenge_tmp[5] = {0x07, 0x00, 0x08, 0x00, 0x01};
369 | memcpy(challenge_packet, challenge_tmp, 5);
370 | challenge_packet[1] = *pppoe_counter % 0xFF;
371 | (*pppoe_counter)++;
372 |
373 | sendto(sockfd, challenge_packet, 8, 0, (struct sockaddr *)&addr, sizeof(addr));
374 |
375 | if (verbose_flag) {
376 | print_packet("[Challenge sent] ", challenge_packet, 8);
377 | }
378 | if (logging_flag) {
379 | logging("[Challenge sent] ", challenge_packet, 8);
380 | }
381 | #ifdef TEST
382 | unsigned char test1[4] = {0x26, 0xe6, 0xe1, 0x02};
383 | unsigned char test2[4] = {0xc0, 0xa8, 0x01, 0x0b};
384 | memcpy(seed, test1, 4);
385 | memcpy(sip, test2, 4);
386 | *encrypt_mode = 1; /* encrypt_mode test switch [0 or 1] */
387 | print_packet("[TEST MODE] ", seed, 4);
388 | print_packet("[TEST MODE] ", sip, 4);
389 | printf("[TEST MODE] %d\n", *encrypt_mode);
390 | return 0;
391 | #endif
392 |
393 | socklen_t addrlen = sizeof(addr);
394 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
395 | #ifdef WIN32
396 | get_lasterror("Failed to recv data");
397 | #else
398 | perror("Failed to recv data");
399 | #endif
400 | return 1;
401 | }
402 |
403 | if (verbose_flag) {
404 | print_packet("[Challenge recv] ", recv_packet, 32);
405 | }
406 | if (logging_flag) {
407 | logging("[Challenge recv] ", recv_packet, 32);
408 | }
409 |
410 | if (recv_packet[0] != 0x07) {
411 | printf("Bad challenge response received.\n");
412 | return 1;
413 | }
414 | if (recv_packet[5] != 0x00) {
415 | *encrypt_mode = 1;
416 | } else {
417 | *encrypt_mode = 0;
418 | }
419 |
420 | #ifdef FORCE_ENCRYPT
421 | *encrypt_mode = 1;
422 | #endif
423 |
424 | memcpy(seed, &recv_packet[8], 4);
425 | memcpy(sip, &recv_packet[12], 4);
426 | memcpy(drcom_config.KEEP_ALIVE_VERSION, &recv_packet[28], 2);
427 | #ifdef DEBUG
428 | print_packet(" ", seed, 4);
429 | print_packet(" ", sip, 4);
430 | printf(" %d", *encrypt_mode);
431 | #endif
432 |
433 | return 0;
434 | }
435 |
436 | int pppoe_login(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigned char seed[], unsigned char sip[], int *login_first, int *encrypt_mode, int *encrypt_type) {
437 | unsigned char login_packet[96], recv_packet[1024];
438 | memset(login_packet, 0, 96);
439 | unsigned char login_tmp[5] = {0x07, 0x00, 0x60, 0x00, 0x03};
440 | memcpy(login_packet, login_tmp, 5);
441 | login_packet[1] = *pppoe_counter % 0xFF;
442 | (*pppoe_counter)++;
443 | memcpy(login_packet + 12, sip, 4);
444 | if (*login_first) {
445 | login_packet[17] = 0x62;
446 | } else {
447 | login_packet[17] = 0x63;
448 | }
449 | memcpy(login_packet + 19, &drcom_config.pppoe_flag, 1);
450 | memcpy(login_packet + 20, seed, 4);
451 | unsigned char crc[8] = {0};
452 | *encrypt_type = seed[0] & 3;
453 | if (!*encrypt_mode) {
454 | *encrypt_type = 0;
455 | }
456 | gen_crc(seed, *encrypt_type, crc);
457 | unsigned char crc_tmp[32] = {0};
458 | memcpy(crc_tmp, login_packet, 32);
459 | memcpy(crc_tmp + 24, crc, 8);
460 | uint64_t ret = 0;
461 | uint64_t sum = 0;
462 | unsigned char crc2[4] = {0};
463 | if (*encrypt_type == 0) {
464 | for (int i = 0; i < 32; i += 4) {
465 | ret = 0;
466 | for (int j = 4; j > 0; j--) {
467 | ret = ret * 256 + (int)crc_tmp[i + j - 1];
468 | }
469 | sum ^= ret;
470 | sum &= 0xffffffff;
471 | }
472 | sum = sum * 19680126 & 0xffffffff;
473 | for (int i = 0; i < 4; i++) {
474 | crc2[i] = (unsigned char)(sum % 256);
475 | sum /= 256;
476 | }
477 | memcpy(login_packet + 24, crc2, 4);
478 | } else {
479 | memcpy(login_packet + 24, crc, 8);
480 | }
481 | // login_packet[39] = 0x8b;
482 | // memcpy(login_packet + 40, sip, 4);
483 | // unsigned char smask[4] = {0xff, 0xff, 0xff, 0xff};
484 | // memcpy(login_packet + 44, smask, 4);
485 | // login_packet[54] = 0x40;
486 |
487 | sendto(sockfd, login_packet, 96, 0, (struct sockaddr *)&addr, sizeof(addr));
488 | if (verbose_flag) {
489 | print_packet("[PPPoE_login sent] ", login_packet, 96);
490 | }
491 | if (logging_flag) {
492 | logging("[PPPoE_login sent] ", login_packet, 96);
493 | }
494 | #ifdef TEST
495 | return 0;
496 | #endif
497 |
498 | socklen_t addrlen = sizeof(addr);
499 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
500 | #ifdef WIN32
501 | get_lasterror("Failed to recv data");
502 | #else
503 | perror("Failed to recv data");
504 | #endif
505 | return 1;
506 | }
507 |
508 | if (verbose_flag) {
509 | print_packet("[PPPoE_login recv] ", recv_packet, 48);
510 | }
511 | if (logging_flag) {
512 | logging("[PPPoE_login recv] ", recv_packet, 48);
513 | }
514 |
515 | if (recv_packet[0] != 0x07) {
516 | printf("Bad pppoe_login response received.\n");
517 | return 1;
518 | }
519 |
520 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) >= 0) {
521 | DEBUG_PRINT(("Get notice packet."));
522 | }
523 |
524 | return 0;
525 | }
526 |
527 | int dogcom(int try_times) {
528 | #ifdef WIN32
529 | WORD sockVersion = MAKEWORD(2, 2);
530 | WSADATA wsaData;
531 | if (WSAStartup(sockVersion, &wsaData) != 0) {
532 | return 1;
533 | }
534 | #endif
535 | int sockfd;
536 |
537 | struct sockaddr_in bind_addr;
538 | memset(&bind_addr, 0, sizeof(bind_addr));
539 | bind_addr.sin_family = AF_INET;
540 | if (verbose_flag) {
541 | printf("You are binding at %s!\n\n", bind_ip);
542 | }
543 | #ifdef WIN32
544 | bind_addr.sin_addr.S_un.S_addr = inet_addr(bind_ip);
545 | #else
546 | bind_addr.sin_addr.s_addr = inet_addr(bind_ip);
547 | #endif
548 | bind_addr.sin_port = htons(BIND_PORT);
549 |
550 | struct sockaddr_in dest_addr;
551 | memset(&dest_addr, 0, sizeof(dest_addr));
552 | dest_addr.sin_family = AF_INET;
553 | #ifdef WIN32
554 | dest_addr.sin_addr.S_un.S_addr = inet_addr(drcom_config.server);
555 | #else
556 | dest_addr.sin_addr.s_addr = inet_addr(drcom_config.server);
557 | #endif
558 | dest_addr.sin_port = htons(DEST_PORT);
559 |
560 | srand(time(NULL));
561 |
562 | // create socket
563 | if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
564 | #ifdef WIN32
565 | get_lasterror("Failed to create socket");
566 | #else
567 | perror("Failed to create socket");
568 | #endif
569 | return 1;
570 | }
571 | // bind socket
572 | if (bind(sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
573 | #ifdef WIN32
574 | get_lasterror("Failed to bind socket");
575 | #else
576 | perror("Failed to bind socket");
577 | #endif
578 | return 1;
579 | }
580 |
581 | // set timeout
582 | #ifdef WIN32
583 | int timeout = 3000;
584 | #else
585 | struct timeval timeout;
586 | timeout.tv_sec = 3;
587 | timeout.tv_usec = 0;
588 | #endif
589 | if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
590 | #ifdef WIN32
591 | get_lasterror("Failed to set sock opt");
592 | #else
593 | perror("Failed to set sock opt");
594 | #endif
595 | return 1;
596 | }
597 |
598 | // start dogcoming
599 | if (strcmp(mode, "dhcp") == 0) {
600 | int login_failed_attempts = 0;
601 | int try_JLUversion = 0;
602 | for (int try_counter = 0; try_counter < try_times; try_counter++) {
603 | if (eternal_flag) {
604 | try_counter = 0;
605 | }
606 | unsigned char seed[4];
607 | unsigned char auth_information[16];
608 | if (dhcp_challenge(sockfd, dest_addr, seed)) {
609 | printf("Retrying...\n");
610 | if (logging_flag) {
611 | logging("Retrying...", NULL, 0);
612 | }
613 | sleep(3);
614 | } else {
615 | usleep(200000); // 0.2 sec
616 | if (login_failed_attempts > 2) {
617 | try_JLUversion = 1;
618 | }
619 | if (!dhcp_login(sockfd, dest_addr, seed, auth_information, try_JLUversion)) {
620 | int keepalive_counter = 0;
621 | int keepalive_try_counter = 0;
622 | int first = 1;
623 | while (1) {
624 | if (!keepalive_1(sockfd, dest_addr, seed, auth_information)) {
625 | usleep(200000); // 0.2 sec
626 | if (keepalive_2(sockfd, dest_addr, &keepalive_counter, &first, 0)) {
627 | continue;
628 | }
629 | if (verbose_flag) {
630 | printf("Keepalive in loop.\n");
631 | }
632 | if (logging_flag) {
633 | logging("Keepalive in loop.", NULL, 0);
634 | }
635 | sleep(20);
636 | } else {
637 | if (keepalive_try_counter > 5) {
638 | break;
639 | }
640 | keepalive_try_counter++;
641 | continue;
642 | }
643 | }
644 | } else {
645 | login_failed_attempts += 1;
646 | printf("Retrying...\n");
647 | if (logging_flag) {
648 | logging("Retrying...", NULL, 0);
649 | }
650 | sleep(3);
651 | };
652 | }
653 | }
654 | } else if (strcmp(mode, "pppoe") == 0) {
655 | int pppoe_counter = 0;
656 | int keepalive_counter = 0;
657 | unsigned char seed[4], sip[4]; /* pppoe's seed == dhcp's KEEP_ALIVE_VERSION */
658 | int login_first = 1;
659 | int first = 1;
660 | int encrypt_mode = 0;
661 | int encrypt_type = 0;
662 | int try_counter = 0;
663 | while (1) {
664 | if (pppoe_challenge(sockfd, dest_addr, &pppoe_counter, seed, sip, &encrypt_mode)) {
665 | printf("Retrying...\n");
666 | if (logging_flag) {
667 | logging("Retrying...", NULL, 0);
668 | }
669 | login_first = 1;
670 | try_counter++;
671 | if (eternal_flag) {
672 | try_counter = 0;
673 | }
674 | if (try_counter >= try_times) {
675 | break;
676 | }
677 | sleep(5);
678 | continue;
679 | } else {
680 | usleep(200000); // 0.2 sec
681 | if (pppoe_login(sockfd, dest_addr, &pppoe_counter, seed, sip, &login_first, &encrypt_mode, &encrypt_type)) {
682 | continue;
683 | } else {
684 | login_first = 0;
685 | if (keepalive_2(sockfd, dest_addr, &keepalive_counter, &first, &encrypt_type)) {
686 | continue;
687 | } else {
688 | if (verbose_flag) {
689 | printf("PPPoE in loop.\n");
690 | }
691 | if (logging_flag) {
692 | logging("PPPoE in loop.", NULL, 0);
693 | }
694 | sleep(10);
695 | continue;
696 | }
697 | }
698 | }
699 | }
700 | }
701 |
702 | printf(">>>>> Failed to keep in touch with server, exiting <<<<<\n\n");
703 | if (logging_flag) {
704 | logging(">>>>> Failed to keep in touch with server, exiting <<<<<", NULL, 0);
705 | }
706 | #ifdef WIN32
707 | closesocket(sockfd);
708 | WSACleanup();
709 | #else
710 | close(sockfd);
711 | #endif
712 | return 1;
713 | }
714 |
715 | void print_packet(char msg[10], unsigned char *packet, int length) {
716 | printf("%s", msg);
717 | for (int i = 0; i < length; i++) {
718 | printf("%02x", packet[i]);
719 | }
720 | printf("\n");
721 | }
722 |
723 | void logging(char msg[10], unsigned char *packet, int length) {
724 | FILE *ptr_file;
725 | ptr_file = fopen(log_path, "a");
726 |
727 | char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
728 | time_t timep;
729 | struct tm *p;
730 | time(&timep);
731 | p = localtime(&timep);
732 | fprintf(ptr_file, "[%04d/%02d/%02d %s %02d:%02d:%02d] ",
733 | (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
734 |
735 | fprintf(ptr_file, "%s", msg);
736 | for (int i = 0; i < length; i++) {
737 | fprintf(ptr_file, "%02x", packet[i]);
738 | }
739 | fprintf(ptr_file, "\n");
740 |
741 | fclose(ptr_file);
742 | }
743 |
744 | #ifdef WIN32
745 | void get_lasterror(char *msg) {
746 | char err_msg[256];
747 | err_msg[0] = '\0';
748 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
749 | NULL,
750 | WSAGetLastError(),
751 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
752 | err_msg,
753 | sizeof(err_msg),
754 | NULL);
755 | fprintf(stderr, "%s: %s", msg, err_msg);
756 | }
757 | #endif
--------------------------------------------------------------------------------
/auth.h:
--------------------------------------------------------------------------------
1 | #ifndef AUTH_H_
2 | #define AUTH_H_
3 |
4 | #ifdef WIN32
5 | #include
6 | #else
7 | #include
8 | #endif
9 |
10 | enum {
11 | CHECK_MAC = 0x01,
12 | SERVER_BUSY = 0x02,
13 | WRONG_PASS = 0x03,
14 | NOT_ENOUGH = 0x04,
15 | FREEZE_UP = 0x05,
16 | NOT_ON_THIS_IP = 0x07,
17 | NOT_ON_THIS_MAC = 0x0B,
18 | TOO_MUCH_IP = 0x14,
19 | UPDATE_CLIENT = 0x15,
20 | NOT_ON_THIS_IP_MAC = 0x16,
21 | MUST_USE_DHCP = 0x17
22 | };
23 |
24 | int dhcp_challenge(int sockfd, struct sockaddr_in addr, unsigned char seed[]);
25 | int dhcp_login(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[], int try_JLUversion);
26 | int pppoe_challenge(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigned char seed[], unsigned char sip[], int *encrypt_mode);
27 | int pppoe_login(int sockfd, struct sockaddr_in addr, int *pppoe_counter, unsigned char seed[], unsigned char sip[], int *first, int *encrypt_mode, int *encrypt_type);
28 | int dogcom(int try_times);
29 | void print_packet(char msg[10], unsigned char *packet, int length);
30 | void logging(char msg[10], unsigned char *packet, int length);
31 | void get_lasterror(char *msg);
32 |
33 | #endif // AUTH_H_
--------------------------------------------------------------------------------
/configparse.c:
--------------------------------------------------------------------------------
1 | #include "configparse.h"
2 | #include
3 | #include
4 | #include
5 | #include "debug.h"
6 |
7 | int verbose_flag = 0;
8 | int logging_flag = 0;
9 | int eapol_flag = 0;
10 | int eternal_flag = 0;
11 | char *log_path;
12 | char mode[10];
13 | char bind_ip[20];
14 | struct config drcom_config;
15 |
16 | static int read_d_config(char *buf, int size);
17 | static int read_p_config(char *buf, int size);
18 |
19 | int config_parse(char *filepath) {
20 | FILE *ptr_file;
21 | char buf[100];
22 |
23 | ptr_file = fopen(filepath, "r");
24 | if (!ptr_file) {
25 | printf("Failed to read config file.\n");
26 | exit(1);
27 | }
28 |
29 | while (fgets(buf, sizeof(buf), ptr_file)) {
30 | if (strcmp(mode, "dhcp") == 0) {
31 | read_d_config(buf, sizeof(buf));
32 | } else if (strcmp(mode, "pppoe") == 0) {
33 | read_p_config(buf, sizeof(buf));
34 | }
35 | }
36 | if (verbose_flag) {
37 | printf("\n\n");
38 | }
39 | fclose(ptr_file);
40 |
41 | return 0;
42 | }
43 |
44 | static int read_d_config(char *buf, int size) {
45 | if (verbose_flag) {
46 | printf("%s", buf);
47 | }
48 |
49 | char *delim = " ='\r\n";
50 | char *delim2 = "\\x";
51 | char *key;
52 | char *value;
53 | if (strlen(key = strtok(buf, delim))) {
54 | value = strtok(NULL, delim);
55 | }
56 | drcom_config.keepalive1_mod = 0;
57 |
58 | if (strcmp(key, "server") == 0) {
59 | strcpy(drcom_config.server, value);
60 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.server));
61 | } else if (strcmp(key, "username") == 0) {
62 | strcpy(drcom_config.username, value);
63 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.username));
64 | } else if (strcmp(key, "password") == 0) {
65 | strcpy(drcom_config.password, value);
66 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.password));
67 | } else if (strcmp(key, "CONTROLCHECKSTATUS") == 0) {
68 | value = strtok(value, delim2);
69 | sscanf(value, "%02hhx", &drcom_config.CONTROLCHECKSTATUS);
70 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.CONTROLCHECKSTATUS));
71 | } else if (strcmp(key, "ADAPTERNUM") == 0) {
72 | value = strtok(value, delim2);
73 | sscanf(value, "%02hhx", &drcom_config.ADAPTERNUM);
74 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.ADAPTERNUM));
75 | } else if (strcmp(key, "host_ip") == 0) {
76 | strcpy(drcom_config.host_ip, value);
77 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_ip));
78 | } else if (strcmp(key, "IPDOG") == 0) {
79 | value = strtok(value, delim2);
80 | sscanf(value, "%02hhx", &drcom_config.IPDOG);
81 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.IPDOG));
82 | } else if (strcmp(key, "host_name") == 0) {
83 | strcpy(drcom_config.host_name, value);
84 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_name));
85 | } else if (strcmp(key, "PRIMARY_DNS") == 0) {
86 | strcpy(drcom_config.PRIMARY_DNS, value);
87 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.PRIMARY_DNS));
88 | } else if (strcmp(key, "dhcp_server") == 0) {
89 | strcpy(drcom_config.dhcp_server, value);
90 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.dhcp_server));
91 | } else if (strcmp(key, "AUTH_VERSION") == 0) {
92 | char *v1 = strtok(value, delim2);
93 | char *v2 = strtok(NULL, delim2);
94 | sscanf(v1, "%02hhx", v1);
95 | sscanf(v2, "%02hhx", v2);
96 | memcpy(&drcom_config.AUTH_VERSION[0], v1, 1);
97 | memcpy(&drcom_config.AUTH_VERSION[1], v2, 1);
98 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.AUTH_VERSION[0]));
99 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.AUTH_VERSION[1]));
100 | } else if (strcmp(key, "mac") == 0) {
101 | char *delim3 = "x";
102 | // strsep(&value, delim3);
103 | value = strtok(value, delim3);
104 | value = strtok(NULL, delim3);
105 | sscanf(value, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
106 | &drcom_config.mac[0],
107 | &drcom_config.mac[1],
108 | &drcom_config.mac[2],
109 | &drcom_config.mac[3],
110 | &drcom_config.mac[4],
111 | &drcom_config.mac[5]);
112 | #ifdef DEBUG
113 | printf("[PARSER_DEBUG]0x");
114 | for (int i = 0; i < 6; i++) {
115 | printf("%02x", drcom_config.mac[i]);
116 | }
117 | printf("\n");
118 | #endif
119 | } else if (strcmp(key, "host_os") == 0) {
120 | strcpy(drcom_config.host_os, value);
121 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.host_os));
122 | } else if (strcmp(key, "KEEP_ALIVE_VERSION") == 0) {
123 | char *v1 = strtok(value, delim2);
124 | char *v2 = strtok(NULL, delim2);
125 | sscanf(v1, "%02hhx", v1);
126 | sscanf(v2, "%02hhx", v2);
127 | memcpy(&drcom_config.KEEP_ALIVE_VERSION[0], v1, 1);
128 | memcpy(&drcom_config.KEEP_ALIVE_VERSION[1], v2, 1);
129 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[0]));
130 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.KEEP_ALIVE_VERSION[1]));
131 | } else if (strcmp(key, "ror_version") == 0) {
132 | if (strcmp(value, "True") == 0) {
133 | drcom_config.ror_version = 1;
134 | } else {
135 | drcom_config.ror_version = 0;
136 | }
137 | DEBUG_PRINT(("\n[PARSER_DEBUG]\n%d\n", drcom_config.ror_version));
138 | } else if (strcmp(key, "keepalive1_mod") == 0) {
139 | if (strcmp(value, "True") == 0) {
140 | drcom_config.keepalive1_mod = 1;
141 | } else {
142 | drcom_config.keepalive1_mod = 0;
143 | }
144 | DEBUG_PRINT(("\n[PARSER_DEBUG]\n%d\n", drcom_config.keepalive1_mod));
145 | } else {
146 | return 1;
147 | }
148 |
149 | return 0;
150 | }
151 |
152 | static int read_p_config(char *buf, int size) {
153 | if (verbose_flag) {
154 | printf("%s", buf);
155 | }
156 |
157 | char *delim = " ='\r\n";
158 | char *delim2 = "\\x";
159 | char *key;
160 | char *value;
161 | if (strlen(key = strtok(buf, delim))) {
162 | value = strtok(NULL, delim);
163 | }
164 |
165 | if (strcmp(key, "server") == 0) {
166 | strcpy(drcom_config.server, value);
167 | DEBUG_PRINT(("[PARSER_DEBUG]%s\n", drcom_config.server));
168 | } else if (strcmp(key, "pppoe_flag") == 0) {
169 | value = strtok(value, delim2);
170 | sscanf(value, "%02hhx", &drcom_config.pppoe_flag);
171 | DEBUG_PRINT(("[PARSER_DEBUG]0x%02x\n", drcom_config.pppoe_flag));
172 | } else if (strcmp(key, "keep_alive2_flag") == 0) {
173 | value = strtok(value, delim2);
174 | sscanf(value, "%02hhx", &drcom_config.keep_alive2_flag);
175 | DEBUG_PRINT(("\n[PARSER_DEBUG]0x%02x\n", drcom_config.keep_alive2_flag));
176 | } else {
177 | return 1;
178 | }
179 |
180 | return 0;
181 | }
--------------------------------------------------------------------------------
/configparse.h:
--------------------------------------------------------------------------------
1 | #ifndef CONFIGPARSE_H_
2 | #define CONFIGPARSE_H_
3 |
4 | struct config {
5 | char server[20];
6 | char username[36];
7 | char password[20];
8 | unsigned char CONTROLCHECKSTATUS;
9 | unsigned char ADAPTERNUM;
10 | char host_ip[20];
11 | unsigned char IPDOG;
12 | char host_name[20];
13 | char PRIMARY_DNS[20];
14 | char dhcp_server[20];
15 | unsigned char AUTH_VERSION[2];
16 | unsigned char mac[6];
17 | char host_os[20];
18 | unsigned char KEEP_ALIVE_VERSION[2];
19 | int ror_version;
20 | int keepalive1_mod;
21 | unsigned char pppoe_flag;
22 | unsigned char keep_alive2_flag; /* abandoned */
23 | };
24 |
25 | extern struct config drcom_config;
26 | extern int verbose_flag;
27 | extern int logging_flag;
28 | extern int eapol_flag;
29 | extern int eternal_flag;
30 | extern char *log_path;
31 | extern char mode[10];
32 | extern char bind_ip[20];
33 |
34 | int config_parse(char *filepath);
35 |
36 | #endif // CONFIGPARSE_H_
--------------------------------------------------------------------------------
/daemon.c:
--------------------------------------------------------------------------------
1 | #ifdef linux
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include "debug.h"
11 |
12 | int daemon_flag = 0;
13 | int pid_file_handle;
14 |
15 | void kill_daemon() {
16 | close(pid_file_handle);
17 | remove("/tmp/dogcom.pid");
18 | }
19 |
20 | void signal_handler(int signal) {
21 | switch (signal) {
22 | case SIGHUP:
23 | break;
24 | case SIGINT:
25 | break;
26 | case SIGTERM:
27 | kill_daemon();
28 | exit(0);
29 | break;
30 | default:
31 | break;
32 | }
33 | }
34 |
35 | void daemonise() {
36 | pid_t pid;
37 | struct sigaction sig_action;
38 | sigset_t sigset;
39 |
40 | pid = fork();
41 | if (pid < 0) {
42 | printf("Fork failed!\n");
43 | exit(1);
44 | } else if (pid > 0) {
45 | DEBUG_PRINT(("PID is %d.\n", pid));
46 | exit(0);
47 | }
48 | if (setsid() < 0) {
49 | printf("Setsid failed!\n");
50 | exit(1);
51 | }
52 |
53 | sigemptyset(&sigset);
54 | sigaddset(&sigset, SIGCHLD);
55 | sigaddset(&sigset, SIGTSTP);
56 | sigaddset(&sigset, SIGTTOU);
57 | sigaddset(&sigset, SIGTTIN);
58 | sigprocmask(SIG_BLOCK, &sigset, NULL);
59 | sig_action.sa_handler = signal_handler;
60 | sigemptyset(&sig_action.sa_mask);
61 | sig_action.sa_flags = 0;
62 | sigaction(SIGHUP, &sig_action, NULL);
63 | sigaction(SIGTERM, &sig_action, NULL);
64 | sigaction(SIGINT, &sig_action, NULL);
65 |
66 | pid = fork();
67 | if (pid < 0) {
68 | printf("Fork failed!\n");
69 | exit(1);
70 | } else if (pid > 0) {
71 | DEBUG_PRINT(("PID is %d.\n", pid));
72 | exit(0);
73 | }
74 |
75 | chdir("/tmp/");
76 | umask(027);
77 |
78 | close(STDIN_FILENO);
79 | close(STDOUT_FILENO);
80 | close(STDERR_FILENO);
81 | open("/dev/null", O_RDONLY);
82 | open("/dev/null", O_WRONLY);
83 | open("/dev/null", O_RDWR);
84 |
85 | pid_file_handle = open("/tmp/dogcom.pid", O_RDWR | O_CREAT, 0600);
86 | if (pid_file_handle < 0) {
87 | exit(1);
88 | }
89 | if (lockf(pid_file_handle, F_TLOCK, 0) < 0) {
90 | exit(1);
91 | }
92 |
93 | char spid[10];
94 | sprintf(spid, "%d\n", getpid());
95 | write(pid_file_handle, spid, strlen(spid));
96 | }
97 |
98 | #endif
--------------------------------------------------------------------------------
/daemon.h:
--------------------------------------------------------------------------------
1 | #ifndef DAEMON_H_
2 | #define DAEMON_H_
3 |
4 | void kill_daemon();
5 | void signal_handler(int signal);
6 | void daemonise();
7 |
8 | extern int daemon_flag;
9 | extern int pid_file_handle;
10 |
11 | #endif // DAEMON_H_
--------------------------------------------------------------------------------
/debug.h:
--------------------------------------------------------------------------------
1 | #ifdef DEBUG
2 | #define DEBUG_PRINT(s) printf s
3 | #else
4 | #define DEBUG_PRINT(s) \
5 | do { \
6 | } while (0)
7 | #endif
--------------------------------------------------------------------------------
/eapol.c:
--------------------------------------------------------------------------------
1 | #ifdef linux
2 |
3 | #include "eapol.h"
4 | #include "libs/common.h"
5 | #include "libs/md5.h"
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 |
25 | #define BUFF_LEN (512)
26 |
27 | static uchar client_mac[ETH_ALEN];
28 |
29 | static uchar sendbuff[BUFF_LEN];
30 | static uchar recvbuff[BUFF_LEN];
31 | static char ifname[IFNAMSIZ] = "eth0";
32 | static ethII_t *sendethii, *recvethii;
33 | static eapol_t *sendeapol, *recveapol;
34 | static eap_t *sendeap, *recveap;
35 | static eapbody_t *sendeapbody, *recveapbody;
36 |
37 | static char _uname[UNAME_LEN];
38 | static char _pwd[PWD_LEN];
39 | static int pwdlen;
40 |
41 | static int eap_keep_alive(int skfd, struct sockaddr const *skaddr);
42 | static int eap_md5_clg(int skfd, struct sockaddr const *skaddr);
43 | static int eap_res_identity(int skfd, struct sockaddr const *skaddr);
44 | static int eapol_init(int *skfd, struct sockaddr *skaddr);
45 | static int eapol_start(int skfd, struct sockaddr const *skaddr);
46 | static int eapol_logoff(int skfd, struct sockaddr const *skaddr);
47 | static int filte_req_identity(int skfd, struct sockaddr const *skaddr);
48 | static int filte_req_md5clg(int skfd, struct sockaddr const *skaddr);
49 | static int filte_success(int skfd, struct sockaddr const *skaddr);
50 | static int eap_daemon(int skfd, struct sockaddr const *skaddr);
51 |
52 | /*
53 | * 初始化缓存区,生产套接字和地址接口信息
54 | * skfd: 被初始化的socket
55 | * skaddr: 被初始化地址接口信息
56 | * @return: 0: 成功
57 | * -1: 初始化套接字失败
58 | * -2: 初始化地址信息失败
59 | */
60 | static int eapol_init(int *skfd, struct sockaddr *skaddr) {
61 | struct ifreq ifr;
62 | struct sockaddr_ll *skllp = (struct sockaddr_ll *)skaddr;
63 | sendethii = (ethII_t *)sendbuff;
64 | sendeapol = (eapol_t *)((uchar *)sendethii + sizeof(ethII_t));
65 | sendeap = (eap_t *)((uchar *)sendeapol + sizeof(eapol_t));
66 | sendeapbody = (eapbody_t *)((uchar *)sendeap + sizeof(eap_t));
67 | recvethii = (ethII_t *)recvbuff;
68 | recveapol = (eapol_t *)((uchar *)recvethii + sizeof(ethII_t));
69 | recveap = (eap_t *)((uchar *)recveapol + sizeof(eapol_t));
70 | recveapbody = (eapbody_t *)((uchar *)recveap + sizeof(eap_t));
71 |
72 | if (-1 == (*skfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))) {
73 | perror("Socket");
74 | return -1;
75 | }
76 | /* 先假定就是eth0接口 */
77 | memset(skaddr, 0, sizeof(struct sockaddr_ll));
78 | memset(&ifr, 0, sizeof(struct ifreq));
79 | strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
80 | if (-1 == ioctl(*skfd, SIOCGIFINDEX, &ifr)) {
81 | perror("Get index");
82 | goto addr_err;
83 | }
84 | skllp->sll_ifindex = ifr.ifr_ifindex;
85 | _D("%s's index: %d\n", ifname, skllp->sll_ifindex);
86 | if (-1 == ioctl(*skfd, SIOCGIFHWADDR, &ifr)) {
87 | perror("Get MAC");
88 | goto addr_err;
89 | }
90 | memcpy(client_mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
91 | _D("%s's MAC: %02X-%02X-%02X-%02X-%02X-%02X\n", ifname,
92 | client_mac[0], client_mac[1], client_mac[2],
93 | client_mac[3], client_mac[4], client_mac[5]);
94 | skllp->sll_family = PF_PACKET;
95 | /*skllp->sll_protocol = ETH_P_ARP;*/
96 | /*skllp->sll_ifindex = ? 已给出 */
97 | skllp->sll_hatype = ARPHRD_ETHER;
98 | skllp->sll_pkttype = PACKET_HOST;
99 | skllp->sll_halen = ETH_ALEN;
100 | return 0;
101 |
102 | addr_err:
103 | close(*skfd);
104 | return -2;
105 | }
106 |
107 | /*
108 | * 过滤得到eap-request-identity包
109 | * @return: 0: 成功获取
110 | * -1: 超时
111 | */
112 | static int filte_req_identity(int skfd, struct sockaddr const *skaddr) {
113 | (void)skaddr;
114 | int stime = time((time_t *)NULL);
115 | for (; difftime(time((time_t *)NULL), stime) <= TIMEOUT;) {
116 | /* TODO 看下能不能只接受某类包,包过滤 */
117 | recvfrom(skfd, recvbuff, BUFF_LEN, 0, NULL, NULL);
118 | /* eap包且是request */
119 | if (recvethii->type == htons(ETHII_8021X) && mac_equal(recvethii->dst_mac, client_mac) && recveapol->type == EAPOL_PACKET && recveap->code == EAP_CODE_REQ && recveap->type == EAP_TYPE_IDEN) {
120 | return 0;
121 | }
122 | }
123 | return -1;
124 | }
125 | /*
126 | * 过滤得到eap-request-md5clg包
127 | * @return: 0: 成功获取
128 | * -1: 超时
129 | * -2: 服务器中止登录,用户名不存在
130 | */
131 | static int filte_req_md5clg(int skfd, struct sockaddr const *skaddr) {
132 | (void)skaddr;
133 | int stime = time((time_t *)NULL);
134 | for (; difftime(time((time_t *)NULL), stime) <= TIMEOUT;) {
135 | recvfrom(skfd, recvbuff, BUFF_LEN, 0, NULL, NULL);
136 | /* 是request且是eap-request-md5clg */
137 | if (recvethii->type == htons(ETHII_8021X) && mac_equal(recvethii->dst_mac, client_mac) && recveapol->type == EAPOL_PACKET) {
138 | if (recveap->code == EAP_CODE_REQ && recveap->type == EAP_TYPE_MD5) {
139 | #ifdef DEBUG
140 | _M("id: %d\n", sendeap->id);
141 | _M("md5: ");
142 | int i;
143 | for (i = 0; i < recveapbody->md5size; ++i)
144 | _M("%.2x", recveapbody->md5value[i]);
145 | _M("\n");
146 | _M("ex-md5: ");
147 | for (i = 0; i < ntohs(recveap->len) - recveapbody->md5size - 2; ++i)
148 | _M("%.2x", recveapbody->md5exdata[i]);
149 | _M("\n");
150 | #endif
151 | return 0;
152 | } else if (recveap->id == sendeap->id && recveap->code == EAP_CODE_FAIL) {
153 | _D("id: %d fail.\n", sendeap->id);
154 | return -2;
155 | }
156 | }
157 | }
158 | return -1;
159 | }
160 | /*
161 | * 过滤得到登录成功包
162 | * @return: 0: 成功获取
163 | * -1: 超时
164 | * -2: 服务器中止登录,密码错误吧
165 | */
166 | static int filte_success(int skfd, struct sockaddr const *skaddr) {
167 | (void)skaddr;
168 | int stime = time((time_t *)NULL);
169 | for (; difftime(time((time_t *)NULL), stime) <= TIMEOUT;) {
170 | recvfrom(skfd, recvbuff, BUFF_LEN, 0, NULL, NULL);
171 | if (recvethii->type == htons(ETHII_8021X) && mac_equal(recvethii->dst_mac, client_mac) && recveapol->type == EAPOL_PACKET) {
172 | if (recveap->id == sendeap->id && recveap->code == EAP_CODE_SUCS) {
173 | _D("id: %d login success.\n", sendeap->id);
174 | return 0;
175 | } else if (recveap->id == sendeap->id && recveap->code == EAP_CODE_FAIL) {
176 | _D("id: %d fail.\n", sendeap->id);
177 | return -2;
178 | }
179 | }
180 | }
181 | return -1;
182 | }
183 | /*
184 | * 广播发送eapol-start
185 | */
186 | static int eapol_start(int skfd, struct sockaddr const *skaddr) {
187 | /* 这里采用eap标记的组播mac地址,也许采用广播也可以吧 */
188 | uchar broadcast_mac[ETH_ALEN] = {
189 | // 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
190 | 0xff,
191 | 0xff,
192 | 0xff,
193 | 0xff,
194 | 0xff,
195 | 0xff,
196 | };
197 | memcpy(sendethii->dst_mac, broadcast_mac, ETH_ALEN);
198 | memcpy(sendethii->src_mac, client_mac, ETH_ALEN);
199 | sendethii->type = htons(ETHII_8021X);
200 | sendeapol->ver = EAPOL_VER;
201 | sendeapol->type = EAPOL_START;
202 | sendeapol->len = 0x0;
203 | sendto(skfd, sendbuff, ETH_ALEN * 2 + 6, 0, skaddr, sizeof(struct sockaddr_ll));
204 | return 0;
205 | }
206 | /* 退出登录 */
207 | static int eapol_logoff(int skfd, struct sockaddr const *skaddr) {
208 | uchar broadcast_mac[ETH_ALEN] = {
209 | // 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03,
210 | 0xff,
211 | 0xff,
212 | 0xff,
213 | 0xff,
214 | 0xff,
215 | 0xff,
216 | };
217 | memcpy(sendethii->dst_mac, broadcast_mac, ETH_ALEN);
218 | memcpy(sendethii->src_mac, client_mac, ETH_ALEN);
219 | sendethii->type = htons(ETHII_8021X);
220 | sendeapol->ver = EAPOL_VER;
221 | sendeapol->type = EAPOL_LOGOFF;
222 | sendeapol->len = 0x0;
223 | sendeap->id = EAPOL_LOGOFF_ID;
224 | sendto(skfd, sendbuff, ETH_ALEN * 2 + 6, 0, skaddr, sizeof(struct sockaddr_ll));
225 | return 0;
226 | }
227 | /* 回应request-identity */
228 | static int eap_res_identity(int skfd, struct sockaddr const *skaddr) {
229 | memcpy(sendethii->dst_mac, recvethii->src_mac, ETH_ALEN);
230 | sendeapol->type = EAPOL_PACKET;
231 | sendeapol->len = htons(sizeof(eap_t) + sizeof(eapbody_t));
232 | sendeap->code = EAP_CODE_RES;
233 | sendeap->id = recveap->id;
234 | sendeap->len = htons(sizeof(eapbody_t));
235 | sendeap->type = EAP_TYPE_IDEN;
236 | strncpy((char *)sendeapbody->identity, _uname, UNAME_LEN);
237 | sendto(skfd, sendbuff, ETH_ALEN * 2 + 6 + 5 + sizeof(eapbody_t),
238 | 0, skaddr, sizeof(struct sockaddr_ll));
239 | return 0;
240 | }
241 | /* 回应md5clg */
242 | static int eap_md5_clg(int skfd, struct sockaddr const *skaddr) {
243 | uchar md5buff[BUFF_LEN];
244 | sendeap->id = recveap->id;
245 | sendeap->len = htons(sizeof(eapbody_t));
246 | sendeap->type = EAP_TYPE_MD5;
247 | sendeapbody->md5size = recveapbody->md5size;
248 | memcpy(md5buff, &sendeap->id, 1);
249 | memcpy(md5buff + 1, _pwd, pwdlen);
250 | memcpy(md5buff + 1 + pwdlen, recveapbody->md5value, recveapbody->md5size);
251 | MD5(md5buff, 1 + pwdlen + recveapbody->md5size, sendeapbody->md5value);
252 | memcpy((char *)sendeapbody->md5exdata, _uname, strlen(_uname));
253 | sendto(skfd, sendbuff, ETH_ALEN * 2 + 6 + 5 + sizeof(eapbody_t),
254 | 0, skaddr, sizeof(struct sockaddr_ll));
255 | return 0;
256 | }
257 |
258 | /*
259 | * 保持在线
260 | * eap心跳包
261 | * 某些eap实现需要心跳或多次认证
262 | * 目前有些服务器会有如下特征
263 | * 每一分钟,服务端发送一个request-identity包来判断是否在线
264 | */
265 | static int eap_keep_alive(int skfd, struct sockaddr const *skaddr) {
266 | int status;
267 | time_t stime, etime;
268 | /* EAP_KPALV_TIMEOUT时间内已经不再有心跳包,我们认为服务器不再需要心跳包了 */
269 | //for (; difftime(time((time_t*)NULL), stime) <= EAP_KPALV_TIMEOUT; ) {
270 | stime = time((time_t *)NULL);
271 | for (;;) {
272 | status = filte_req_identity(skfd, skaddr);
273 | //_D("%s: [EAP:KPALV] get status: %d\n", format_time(), status);
274 | if (0 == status) {
275 | etime = time((time_t *)NULL);
276 | _D("dtime: %fs\n", difftime(etime, stime));
277 | if (difftime(etime, stime) <= 10) {
278 | stime = time((time_t *)NULL);
279 | continue;
280 | }
281 | stime = time((time_t *)NULL);
282 | #if 0
283 | #ifdef DEBUG
284 | _D("[KPALV] get eap request identity:\n");
285 | _D("dst<-src: %2X:%2X:%2X:%2X:%2X:%2X <- %2X:%2X:%2X:%2X:%2X:%2X\n",
286 | recvethii->dst_mac[0], recvethii->dst_mac[1], recvethii->dst_mac[2],
287 | recvethii->dst_mac[3], recvethii->dst_mac[4], recvethii->dst_mac[5],
288 | recvethii->src_mac[0], recvethii->src_mac[1], recvethii->src_mac[2],
289 | recvethii->src_mac[3], recvethii->src_mac[4], recvethii->src_mac[5]);
290 | _D("ethII.type: 0x%4x\n", ntohs(recvethii->type));
291 | _D("recveapol.type: %s\n", recveapol->type==EAPOL_PACKET?"EAPOL_PACKET":"UNKNOWN");
292 | _D("recveapol.len: %d\n", ntohs(recveapol->len));
293 | _D("recveap.code: %s\n", recveap->code==EAP_CODE_REQ?"EAP_CODE_REQ":"UNKNOWN");
294 | _D("recveap.id: %d\n", recveap->id);
295 | _D("recveap.type: %s\n", recveap->type==EAP_TYPE_IDEN?"EAP_TYPE_IDEN":"UNKNOWN");
296 | #endif
297 | #endif
298 | _M("%s: [EAP:KPALV] get a request-identity\n", format_time());
299 | eap_res_identity(skfd, skaddr);
300 | #if 0
301 | #ifdef DEBUG
302 | _D("[EAP:KPALV] send eap response identity:\n");
303 | _D("dst<-src: %2X:%2X:%2X:%2X:%2X:%2X <- %2X:%2X:%2X:%2X:%2X:%2X\n",
304 | sendethii->dst_mac[0], sendethii->dst_mac[1], sendethii->dst_mac[2],
305 | sendethii->dst_mac[3], sendethii->dst_mac[4], sendethii->dst_mac[5],
306 | sendethii->src_mac[0], sendethii->src_mac[1], sendethii->src_mac[2],
307 | sendethii->src_mac[3], sendethii->src_mac[4], sendethii->src_mac[5]);
308 | _D("ethII.type: 0x%4x\n", ntohs(sendethii->type));
309 | _D("sendeapol.type: %s\n", sendeapol->type==EAPOL_PACKET?"EAPOL_PACKET":"UNKNOWN");
310 | _D("sendeapol.len: %d\n", ntohs(sendeapol->len));
311 | _D("sendeap.code: %s\n", sendeap->code==EAP_CODE_RES?"EAP_CODE_RES":"UNKNOWN");
312 | _D("sendeap.id: %d\n", sendeap->id);
313 | _D("sendeap.type: %s\n", sendeap->type==EAP_TYPE_IDEN?"EAP_TYPE_IDEN":"UNKNOWN");
314 | _D("sendeapbody.identity: %s\n", sendeapbody->identity);
315 | #endif
316 | #endif
317 | }
318 | status = -1;
319 | }
320 | return 0;
321 | }
322 | /*
323 | * 后台心跳进程
324 | * @return: 0, 正常运行
325 | * -1, 运行失败
326 | */
327 | static int eap_daemon(int skfd, struct sockaddr const *skaddr) {
328 | /* 如果存在原来的keep alive进程,就干掉他 */
329 | #define PID_FILE "/tmp/cwnu-drcom-eap.pid"
330 | FILE *kpalvfd = fopen(PID_FILE, "r+");
331 | if (NULL == kpalvfd) {
332 | _M("[EAP:KPALV] No process pidfile. %s: %s\n", PID_FILE, strerror(errno));
333 | kpalvfd = fopen(PID_FILE, "w+"); /* 不存在,创建 */
334 | if (NULL == kpalvfd) {
335 | _M("[EAP:KPALV] Detect pid file eror(%s)! quit!\n", strerror(errno));
336 | return -1;
337 | }
338 | }
339 | pid_t oldpid;
340 |
341 | fseek(kpalvfd, 0L, SEEK_SET);
342 | if ((1 == fscanf(kpalvfd, "%d", (int *)&oldpid)) && (oldpid != (pid_t)-1)) {
343 | _D("oldkpalv pid: %d\n", oldpid);
344 | kill(oldpid, SIGKILL);
345 | }
346 | setsid();
347 | if (0 != chdir("/"))
348 | _M("[EAP:KPALV:WARN] %s\n", strerror(errno));
349 | umask(0);
350 | /* 在/tmp下写入自己(keep alive)pid */
351 | pid_t curpid = getpid();
352 | _D("kpalv curpid: %d\n", curpid);
353 | /*
354 | * if (0 != ftruncate(fileno(kpalvfd), 0))
355 | * 这个写法有时不能正常截断文件,截断后前面有\0?
356 | */
357 | if (NULL == (kpalvfd = freopen(PID_FILE, "w+", kpalvfd)))
358 | _M("[EAP:KPALV:WARN] truncat pidfile '%s': %s\n", PID_FILE, strerror(errno));
359 | fprintf(kpalvfd, "%d", curpid);
360 | fflush(kpalvfd);
361 | if (0 == eap_keep_alive(skfd, skaddr)) {
362 | _M("%s: [EAP:KPALV] Server maybe not need keep alive paket.\n", format_time());
363 | _M("%s: [EAP:KPALV] Now, keep alive process quit!\n", format_time());
364 | }
365 | if (NULL == (kpalvfd = freopen(PID_FILE, "w+", kpalvfd)))
366 | _M("[EAP:KPALV:WARN] truncat pidfile '%s': %s\n", PID_FILE, strerror(errno));
367 | fprintf(kpalvfd, "-1"); /* 写入-1表示已经离开 */
368 | fflush(kpalvfd);
369 | fclose(kpalvfd);
370 |
371 | return 0;
372 | }
373 |
374 | /*
375 | * eap认证
376 | * uname: 用户名
377 | * pwd: 密码
378 | * @return: 0: 成功
379 | * 1: 用户不存在
380 | * 2: 密码错误
381 | * 3: 其他超时
382 | * 4: 服务器拒绝请求登录
383 | * -1: 没有找到合适网络接口
384 | * -2: 没有找到服务器
385 | */
386 | int eaplogin(char const *uname, char const *pwd) {
387 | int i;
388 | int state;
389 | int skfd;
390 | struct sockaddr_ll ll;
391 |
392 | _M("Use user '%s' to login...\n", uname);
393 | _M("[EAP:0] Initilize interface...\n");
394 | strncpy(_uname, uname, UNAME_LEN);
395 | strncpy(_pwd, pwd, PWD_LEN);
396 | pwdlen = strlen(_pwd);
397 | if (0 != eapol_init(&skfd, (struct sockaddr *)&ll))
398 | return -1;
399 | /* 无论如何先请求一下下线 */
400 | eapol_logoff(skfd, (struct sockaddr *)&ll);
401 | /* eap-start */
402 | _M("[EAP:1] Send eap-start...\n");
403 | for (i = 0; i < TRY_TIMES; ++i) {
404 | eapol_start(skfd, (struct sockaddr *)&ll);
405 | if (0 == filte_req_identity(skfd, (struct sockaddr *)&ll))
406 | break;
407 | _M(" [EAP:1] %dth Try send eap-start...\n", i + 1);
408 | }
409 | if (i >= TRY_TIMES) goto _timeout;
410 |
411 | /* response-identity */
412 | _M("[EAP:2] Send response-identity...\n");
413 | for (i = 0; i < TRY_TIMES; ++i) {
414 | eap_res_identity(skfd, (struct sockaddr *)&ll);
415 | state = filte_req_md5clg(skfd, (struct sockaddr *)&ll);
416 | if (0 == state)
417 | break;
418 | else if (-2 == state)
419 | goto _no_uname;
420 | _M(" [EAP:2] %dth Try send response-identity...\n", i + 1);
421 | }
422 | if (i >= TRY_TIMES) goto _timeout;
423 |
424 | /* response-md5clg */
425 | _M("[EAP:3] Send response-md5clg...\n");
426 | for (i = 0; i < TRY_TIMES; ++i) {
427 | eap_md5_clg(skfd, (struct sockaddr *)&ll);
428 | state = filte_success(skfd, (struct sockaddr *)&ll);
429 | if (0 == state) {
430 | _M("[EAP:4] Login success.\n");
431 | break; /* 登录成功 */
432 | } else if (-2 == state)
433 | goto _pwd_err;
434 | _M(" [EAP:3] %dth Try send response-md5clg...\n", i + 1);
435 | }
436 | if (i >= TRY_TIMES) goto _timeout;
437 |
438 | /* 登录成功,生成心跳进程 */
439 | switch (fork()) {
440 | case 0:
441 | if (0 != eap_daemon(skfd, (struct sockaddr *)&ll)) {
442 | _M("[EAP:ERROR] Create daemon process to keep alive error!\n");
443 | close(skfd);
444 | exit(1);
445 | }
446 | exit(0);
447 | break;
448 | case -1:
449 | _M("[EAP:WARN] Cant create daemon, maybe `OFFLINE` after soon.\n");
450 | }
451 | close(skfd);
452 | return 0;
453 |
454 | _timeout:
455 | _M("[EAP:ERROR] Not server in range.\n");
456 | close(skfd);
457 | return -2;
458 | _no_uname:
459 | _M("[EAP:ERROR] No this user(%s).\n", uname);
460 | close(skfd);
461 | return 1;
462 | _pwd_err:
463 | _M("[EAP:ERROR] The server refuse to login. Password error.\n");
464 | close(skfd);
465 | return 4;
466 | }
467 |
468 | int eaplogoff(void) {
469 | int skfd;
470 | struct sockaddr_ll ll;
471 | int state;
472 | int i;
473 |
474 | _M("[EAP:0] Initilize interface...\n");
475 | if (0 != eapol_init(&skfd, (struct sockaddr *)&ll))
476 | return -1;
477 | _M("[EAP:1] Requset logoff...\n");
478 | for (i = 0; i < TRY_TIMES; ++i) {
479 | eapol_logoff(skfd, (struct sockaddr *)&ll);
480 | state = filte_success(skfd, (struct sockaddr *)&ll);
481 | if (-2 == state) {
482 | _M("[EAP:2] Logoff!\n");
483 | return 0;
484 | }
485 | _M(" [EAP:1] %dth Try Requset logoff...\n", i + 1);
486 | }
487 | _M("[EAP:ERROR] Not server in range. or You were logoff.\n");
488 | return -1;
489 | }
490 |
491 | int eaprefresh(char const *uname, char const *pwd) {
492 | return eaplogin(uname, pwd);
493 | }
494 |
495 | /* 设置ifname */
496 | void setifname(char const *_ifname) {
497 | strncpy(ifname, _ifname, IFNAMSIZ);
498 | }
499 |
500 | #endif
--------------------------------------------------------------------------------
/eapol.h:
--------------------------------------------------------------------------------
1 | #ifndef EAPOL_H__
2 | #define EAPOL_H__
3 |
4 | #include "libs/common.h"
5 |
6 | #define IDEN_LEN UNAME_LEN
7 |
8 | #define TRY_TIMES (3)
9 | /* 每次请求超过TIMEOUT秒,就重新请求一次 */
10 | #define TIMEOUT (3)
11 | /* eap 在EAP_KPALV_TIMEOUT秒内没有回应,认为不需要心跳 */
12 | #define EAP_KPALV_TIMEOUT (420) /* 7分钟 */
13 |
14 | /* ethii层取0x888e表示上层是8021.x */
15 | #define ETHII_8021X (0x888e)
16 |
17 | #define EAPOL_VER (0x01)
18 | #define EAPOL_PACKET (0x00)
19 | #define EAPOL_START (0x01)
20 | #define EAPOL_LOGOFF (0x02)
21 | /* 貌似请求下线的id都是这个 */
22 | #define EAPOL_LOGOFF_ID (255)
23 |
24 | #define EAP_CODE_REQ (0x01)
25 | #define EAP_CODE_RES (0x02)
26 | #define EAP_CODE_SUCS (0x03)
27 | #define EAP_CODE_FAIL (0x04)
28 | #define EAP_TYPE_IDEN (0x01)
29 | #define EAP_TYPE_MD5 (0x04)
30 |
31 | #pragma pack(1)
32 | /* ethii 帧 */
33 | /* 其实这个和struct ether_header是一样的结构 */
34 | typedef struct {
35 | uchar dst_mac[ETH_ALEN];
36 | uchar src_mac[ETH_ALEN];
37 | uint16 type; /* 取值0x888e,表明是8021.x */
38 | } ethII_t;
39 | /* eapol 帧 */
40 | typedef struct {
41 | uchar ver; /* 取值0x01 */
42 | /*
43 | * 0x00: eapol-packet
44 | * 0x01: eapol-start
45 | * 0x02: eapol-logoff
46 | */
47 | uchar type;
48 | uint16 len;
49 | } eapol_t;
50 | /* eap报文头 */
51 | typedef struct {
52 | /*
53 | * 0x01: request
54 | * 0x02: response
55 | * 0x03: success
56 | * 0x04: failure
57 | */
58 | uchar code;
59 | uchar id;
60 | uint16 len;
61 | /*
62 | * 0x01: identity
63 | * 0x04: md5-challenge
64 | */
65 | uchar type;
66 | } eap_t;
67 | /* 报文体 */
68 | #define MD5_SIZE 16
69 | #define STUFF_LEN (64)
70 | typedef union {
71 | uchar identity[IDEN_LEN];
72 | struct {
73 | uchar _size;
74 | uchar _md5value[MD5_SIZE];
75 | uchar _exdata[STUFF_LEN];
76 | } md5clg;
77 | } eapbody_t;
78 | #define md5size md5clg._size
79 | #define md5value md5clg._md5value
80 | #define md5exdata md5clg._exdata
81 | #pragma pack()
82 |
83 | /*
84 | * eap认证
85 | * uname: 用户名
86 | * pwd: 密码
87 | * @return: 0: 成功
88 | * 1: 用户不存在
89 | * 2: 密码错误
90 | * 3: 其他超时
91 | * 4: 服务器拒绝请求登录
92 | * -1: 没有找到合适网络接口
93 | * -2: 没有找到服务器
94 | */
95 | extern int eaplogin(char const *uname, char const *pwd);
96 | /*
97 | * eap下线
98 | */
99 | extern int eaplogoff(void);
100 | /*
101 | * eap重新登录
102 | */
103 | extern int eaprefresh(char const *uname, char const *pwd);
104 | /*
105 | * 用来设置ifname
106 | */
107 | extern void setifname(char const *ifname);
108 | // #ifdef WIN32
109 | /*
110 | * 由于windows下实现进程的特殊性,这里把eap_daemon导出给main_cli使用
111 | * ifname: 心跳的物理接口名字
112 | * @return: 0: keep alive 进程正常退出,也许并不需要心跳进程
113 | * !0: 错误原因导致keep alive 进程退出,也许是没法创建进程
114 | */
115 | // extern int eap_daemon(char const *ifname);
116 | // #endif /* WINDOWS */
117 | #undef IDEN_LEN
118 |
119 | #endif
120 |
--------------------------------------------------------------------------------
/keepalive.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #ifdef WIN32
6 | #include
7 | typedef int socklen_t;
8 | #else
9 | #include
10 | #include
11 | #endif
12 |
13 | #include "auth.h"
14 | #include "configparse.h"
15 | #include "debug.h"
16 | #include "keepalive.h"
17 | #include "libs/md4.h"
18 | #include "libs/md5.h"
19 | #include "libs/sha1.h"
20 |
21 | int keepalive_1(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[]) {
22 | if (drcom_config.keepalive1_mod) {
23 | unsigned char keepalive_1_packet1[8] = {0x07, 0x01, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00};
24 | unsigned char recv_packet1[1024], keepalive_1_packet2[38], recv_packet2[1024];
25 | memset(keepalive_1_packet2, 0, 38);
26 | sendto(sockfd, keepalive_1_packet1, 8, 0, (struct sockaddr *)&addr, sizeof(addr));
27 | if (verbose_flag) {
28 | print_packet("[Keepalive1_packet1 sent] ", keepalive_1_packet1, 8);
29 | }
30 | if (logging_flag) {
31 | logging("[Keepalive1_packet1 sent] ", keepalive_1_packet1, 8);
32 | }
33 | #ifdef TEST
34 | printf("[TEST MODE]IN TEST MODE, PASS\n");
35 | return 0;
36 | #endif
37 | socklen_t addrlen = sizeof(addr);
38 | while (1) {
39 | if (recvfrom(sockfd, recv_packet1, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
40 | #ifdef WIN32
41 | get_lasterror("Failed to recv data");
42 | #else
43 | perror("Failed to recv data");
44 | #endif
45 | return 1;
46 | } else {
47 | if (verbose_flag) {
48 | print_packet("[Keepalive1 challenge_recv] ", recv_packet1, 100);
49 | }
50 | if (logging_flag) {
51 | logging("[Keepalive1 challenge_recv] ", recv_packet1, 100);
52 | }
53 |
54 | if (recv_packet1[0] == 0x07) {
55 | break;
56 | } else if (recv_packet1[0] == 0x4d) {
57 | DEBUG_PRINT(("Get notice packet.\n"));
58 | continue;
59 | } else {
60 | printf("Bad keepalive1 challenge response received.\n");
61 | return 1;
62 | }
63 | }
64 | }
65 |
66 | unsigned char keepalive1_seed[4] = {0};
67 | int encrypt_type;
68 | unsigned char crc[8] = {0};
69 | memcpy(keepalive1_seed, &recv_packet1[8], 4);
70 | encrypt_type = keepalive1_seed[0] & 3;
71 | gen_crc(keepalive1_seed, encrypt_type, crc);
72 | keepalive_1_packet2[0] = 0xff;
73 | memcpy(keepalive_1_packet2 + 8, keepalive1_seed, 4);
74 | memcpy(keepalive_1_packet2 + 12, crc, 8);
75 | memcpy(keepalive_1_packet2 + 20, auth_information, 16);
76 | keepalive_1_packet2[36] = rand() & 0xff;
77 | keepalive_1_packet2[37] = rand() & 0xff;
78 |
79 | sendto(sockfd, keepalive_1_packet2, 38, 0, (struct sockaddr *)&addr, sizeof(addr));
80 | if (verbose_flag) {
81 | print_packet("[Keepalive1_packet2 sent] ", keepalive_1_packet2, 38);
82 | }
83 | if (logging_flag) {
84 | logging("[Keepalive1_packet2 sent] ", keepalive_1_packet2, 38);
85 | }
86 |
87 | if (recvfrom(sockfd, recv_packet2, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
88 | #ifdef WIN32
89 | get_lasterror("Failed to recv data");
90 | #else
91 | perror("Failed to recv data");
92 | #endif
93 | return 1;
94 | } else {
95 | if (verbose_flag) {
96 | print_packet("[Keepalive1 recv] ", recv_packet2, 100);
97 | }
98 | if (logging_flag) {
99 | logging("[Keepalive1 recv] ", recv_packet2, 100);
100 | }
101 |
102 | if (recv_packet2[0] != 0x07) {
103 | printf("Bad keepalive1 response received.\n");
104 | return 1;
105 | }
106 | }
107 |
108 | } else {
109 | unsigned char keepalive_1_packet[42], recv_packet[1024], MD5A[16];
110 | memset(keepalive_1_packet, 0, 42);
111 | keepalive_1_packet[0] = 0xff;
112 | int MD5A_len = 6 + strlen(drcom_config.password);
113 | unsigned char MD5A_str[MD5A_len];
114 | MD5A_str[0] = 0x03;
115 | MD5A_str[1] = 0x01;
116 | memcpy(MD5A_str + 2, seed, 4);
117 | memcpy(MD5A_str + 6, drcom_config.password, strlen(drcom_config.password));
118 | MD5(MD5A_str, MD5A_len, MD5A);
119 | memcpy(keepalive_1_packet + 1, MD5A, 16);
120 | memcpy(keepalive_1_packet + 20, auth_information, 16);
121 | keepalive_1_packet[36] = rand() & 0xff;
122 | keepalive_1_packet[37] = rand() & 0xff;
123 |
124 | sendto(sockfd, keepalive_1_packet, 42, 0, (struct sockaddr *)&addr, sizeof(addr));
125 |
126 | if (verbose_flag) {
127 | print_packet("[Keepalive1 sent] ", keepalive_1_packet, 42);
128 | }
129 | if (logging_flag) {
130 | logging("[Keepalive1 sent] ", keepalive_1_packet, 42);
131 | }
132 |
133 | #ifdef TEST
134 | printf("[TEST MODE]IN TEST MODE, PASS\n");
135 | return 0;
136 | #endif
137 |
138 | socklen_t addrlen = sizeof(addr);
139 | while (1) {
140 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
141 | #ifdef WIN32
142 | get_lasterror("Failed to recv data");
143 | #else
144 | perror("Failed to recv data");
145 | #endif
146 | return 1;
147 | } else {
148 | if (verbose_flag) {
149 | print_packet("[Keepalive1 recv] ", recv_packet, 100);
150 | }
151 | if (logging_flag) {
152 | logging("[Keepalive1 recv] ", recv_packet, 100);
153 | }
154 |
155 | if (recv_packet[0] == 0x07) {
156 | break;
157 | } else if (recv_packet[0] == 0x4d) {
158 | DEBUG_PRINT(("Get notice packet."));
159 | continue;
160 | } else {
161 | printf("Bad keepalive1 response received.\n");
162 | return 1;
163 | }
164 | }
165 | }
166 | }
167 |
168 | return 0;
169 | }
170 |
171 | void gen_crc(unsigned char seed[], int encrypt_type, unsigned char crc[]) {
172 | if (encrypt_type == 0) {
173 | char DRCOM_DIAL_EXT_PROTO_CRC_INIT[4] = {0xc7, 0x2f, 0x31, 0x01};
174 | char gencrc_tmp[4] = {0x7e};
175 | memcpy(crc, DRCOM_DIAL_EXT_PROTO_CRC_INIT, 4);
176 | memcpy(crc + 4, gencrc_tmp, 4);
177 | } else if (encrypt_type == 1) {
178 | unsigned char hash[32] = {0};
179 | MD5(seed, 4, hash);
180 | crc[0] = hash[2];
181 | crc[1] = hash[3];
182 | crc[2] = hash[8];
183 | crc[3] = hash[9];
184 | crc[4] = hash[5];
185 | crc[5] = hash[6];
186 | crc[6] = hash[13];
187 | crc[7] = hash[14];
188 | } else if (encrypt_type == 2) {
189 | unsigned char hash[32] = {0};
190 | MD4(seed, 4, hash);
191 | crc[0] = hash[1];
192 | crc[1] = hash[2];
193 | crc[2] = hash[8];
194 | crc[3] = hash[9];
195 | crc[4] = hash[4];
196 | crc[5] = hash[5];
197 | crc[6] = hash[11];
198 | crc[7] = hash[12];
199 | } else if (encrypt_type == 3) {
200 | unsigned char hash[32] = {0};
201 | SHA1(seed, 4, hash);
202 | crc[0] = hash[2];
203 | crc[1] = hash[3];
204 | crc[2] = hash[9];
205 | crc[3] = hash[10];
206 | crc[4] = hash[5];
207 | crc[5] = hash[6];
208 | crc[6] = hash[15];
209 | crc[7] = hash[16];
210 | }
211 | }
212 |
213 | void keepalive_2_packetbuilder(unsigned char keepalive_2_packet[], int keepalive_counter, int filepacket, int type, int encrypt_type) {
214 | keepalive_2_packet[0] = 0x07;
215 | keepalive_2_packet[1] = keepalive_counter;
216 | keepalive_2_packet[2] = 0x28;
217 | keepalive_2_packet[4] = 0x0b;
218 | keepalive_2_packet[5] = type;
219 | if (filepacket) {
220 | keepalive_2_packet[6] = 0x0f;
221 | keepalive_2_packet[7] = 0x27;
222 | } else {
223 | memcpy(keepalive_2_packet + 6, drcom_config.KEEP_ALIVE_VERSION, 2);
224 | }
225 | keepalive_2_packet[8] = 0x2f;
226 | keepalive_2_packet[9] = 0x12;
227 | if (type == 3) {
228 | unsigned char host_ip[4] = {0};
229 | if (strcmp(mode, "dhcp") == 0) {
230 | sscanf(drcom_config.host_ip, "%hhd.%hhd.%hhd.%hhd",
231 | &host_ip[0],
232 | &host_ip[1],
233 | &host_ip[2],
234 | &host_ip[3]);
235 | memcpy(keepalive_2_packet + 28, host_ip, 4);
236 | } else if (strcmp(mode, "pppoe") == 0) {
237 | unsigned char crc[8] = {0};
238 | gen_crc(keepalive_2_packet, encrypt_type, crc);
239 | memcpy(keepalive_2_packet + 32, crc, 8);
240 | }
241 | }
242 | }
243 |
244 | int keepalive_2(int sockfd, struct sockaddr_in addr, int *keepalive_counter, int *first, int *encrypt_type) {
245 | unsigned char keepalive_2_packet[40], recv_packet[1024], tail[4];
246 | socklen_t addrlen = sizeof(addr);
247 |
248 | #ifdef TEST
249 | printf("[TEST MODE]IN TEST MODE, PASS\n");
250 | #else
251 | if (*first) {
252 | // send the file packet
253 | memset(keepalive_2_packet, 0, 40);
254 | if (strcmp(mode, "pppoe") == 0) {
255 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, *encrypt_type);
256 | } else {
257 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, 0);
258 | }
259 | (*keepalive_counter)++;
260 |
261 | sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
262 |
263 | if (verbose_flag) {
264 | print_packet("[Keepalive2_file sent] ", keepalive_2_packet, 40);
265 | }
266 | if (logging_flag) {
267 | logging("[Keepalive2_file sent] ", keepalive_2_packet, 40);
268 | }
269 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
270 | #ifdef WIN32
271 | get_lasterror("Failed to recv data");
272 | #else
273 | perror("Failed to recv data");
274 | #endif
275 | return 1;
276 | }
277 | if (verbose_flag) {
278 | print_packet("[Keepalive2_file recv] ", recv_packet, 40);
279 | }
280 | if (logging_flag) {
281 | logging("[Keepalive2_file recv] ", recv_packet, 40);
282 | }
283 |
284 | if (recv_packet[0] == 0x07) {
285 | if (recv_packet[2] == 0x10) {
286 | if (verbose_flag) {
287 | printf("Filepacket received.\n");
288 | }
289 | } else if (recv_packet[2] != 0x28) {
290 | if (verbose_flag) {
291 | printf("Bad keepalive2 response received.\n");
292 | }
293 | return 1;
294 | }
295 | } else {
296 | printf("Bad keepalive2 response received.\n");
297 | return 1;
298 | }
299 | }
300 | #endif
301 |
302 | // send the first packet
303 | *first = 0;
304 | memset(keepalive_2_packet, 0, 40);
305 | if (strcmp(mode, "pppoe") == 0) {
306 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, *encrypt_type);
307 | } else {
308 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 1, 0);
309 | }
310 | (*keepalive_counter)++;
311 | sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
312 |
313 | if (verbose_flag) {
314 | print_packet("[Keepalive2_A sent] ", keepalive_2_packet, 40);
315 | }
316 | if (logging_flag) {
317 | logging("[Keepalive2_A sent] ", keepalive_2_packet, 40);
318 | }
319 |
320 | #ifdef TEST
321 | unsigned char test[4] = {0x13, 0x38, 0xe2, 0x11};
322 | memcpy(tail, test, 4);
323 | print_packet("[TEST MODE] ", tail, 4);
324 | #else
325 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
326 | #ifdef WIN32
327 | get_lasterror("Failed to recv data");
328 | #else
329 | perror("Failed to recv data");
330 | #endif
331 | return 1;
332 | }
333 | if (verbose_flag) {
334 | print_packet("[Keepalive2_B recv] ", recv_packet, 40);
335 | }
336 | if (logging_flag) {
337 | logging("[Keepalive2_B recv] ", recv_packet, 40);
338 | }
339 |
340 | if (recv_packet[0] == 0x07) {
341 | if (recv_packet[2] != 0x28) {
342 | printf("Bad keepalive2 response received.\n");
343 | return 1;
344 | }
345 | } else {
346 | printf("Bad keepalive2 response received.\n");
347 | return 1;
348 | }
349 | memcpy(tail, &recv_packet[16], 4);
350 | #endif
351 |
352 | #ifdef DEBUG
353 | print_packet(" ", tail, 4);
354 | #endif
355 |
356 | // send the third packet
357 | memset(keepalive_2_packet, 0, 40);
358 | if (strcmp(mode, "pppoe") == 0) {
359 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 3, *encrypt_type);
360 | } else {
361 | keepalive_2_packetbuilder(keepalive_2_packet, *keepalive_counter % 0xFF, *first, 3, 0);
362 | }
363 | memcpy(keepalive_2_packet + 16, tail, 4);
364 | (*keepalive_counter)++;
365 | sendto(sockfd, keepalive_2_packet, 40, 0, (struct sockaddr *)&addr, sizeof(addr));
366 |
367 | if (verbose_flag) {
368 | print_packet("[Keepalive2_C sent] ", keepalive_2_packet, 40);
369 | }
370 | if (logging_flag) {
371 | logging("[Keepalive2_C sent] ", keepalive_2_packet, 40);
372 | }
373 |
374 | #ifdef TEST
375 | printf("[TEST MODE]IN TEST MODE, PASS\n");
376 | exit(0);
377 | #endif
378 |
379 | if (recvfrom(sockfd, recv_packet, 1024, 0, (struct sockaddr *)&addr, &addrlen) < 0) {
380 | #ifdef WIN32
381 | get_lasterror("Failed to recv data");
382 | #else
383 | perror("Failed to recv data");
384 | #endif
385 | return 1;
386 | }
387 | if (verbose_flag) {
388 | print_packet("[Keepalive2_D recv] ", recv_packet, 40);
389 | }
390 | if (logging_flag) {
391 | logging("[Keepalive2_D recv] ", recv_packet, 40);
392 | }
393 |
394 | if (recv_packet[0] == 0x07) {
395 | if (recv_packet[2] != 0x28) {
396 | printf("Bad keepalive2 response received.\n");
397 | return 1;
398 | }
399 | } else {
400 | printf("Bad keepalive2 response received.\n");
401 | return 1;
402 | }
403 |
404 | return 0;
405 | }
--------------------------------------------------------------------------------
/keepalive.h:
--------------------------------------------------------------------------------
1 | #ifndef KEEPALIVE_H_
2 | #define KEEPALIVE_H_
3 |
4 | int keepalive_1(int sockfd, struct sockaddr_in addr, unsigned char seed[], unsigned char auth_information[]);
5 | int keepalive_2(int sockfd, struct sockaddr_in addr, int *keepalive_counter, int *first, int *encrypt_type);
6 | void gen_crc(unsigned char seed[], int encrypt_type, unsigned char crc[]);
7 | void keepalive_2_packetbuilder(unsigned char keepalive_2_packet[], int keepalive_counter, int filepacket, int type, int encrypt_type);
8 |
9 | #endif // KEEPALIVE_H_
--------------------------------------------------------------------------------
/libs/common.c:
--------------------------------------------------------------------------------
1 | #ifdef linux
2 |
3 | /*
4 | * 一些通用的代码
5 | */
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include "common.h"
13 |
14 | // #ifdef LINUX
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 | #include
23 | #define PATH_SEP '/'
24 | // #elif WIN32
25 | // # include
26 | // # include
27 | // # define PATH_SEP '\\'
28 | // #endif
29 |
30 |
31 | extern int getexedir(char *exedir)
32 | {
33 | // #ifdef LINUX
34 | int cnt = readlink("/proc/self/exe", exedir, EXE_PATH_MAX);
35 | // #elif WIN32
36 | // int cnt = GetModuleFileName(NULL, exedir, EXE_PATH_MAX);
37 | // #endif
38 | if (cnt < 0 || cnt >= EXE_PATH_MAX)
39 | return -1;
40 | _D("exedir: %s\n", exedir);
41 | char *end = strrchr(exedir, PATH_SEP);
42 | if (!end) return -1;
43 | *(end+1) = '\0';
44 | _D("exedir: %s\n", exedir);
45 | return 0;
46 | }
47 |
48 | extern int mac_equal(uchar const *mac1, uchar const *mac2)
49 | {
50 | int i;
51 | for (i = 0; i < ETH_ALEN; ++i) {
52 | if (mac1[i] != mac2[i])
53 | return 0;
54 | }
55 |
56 | return 1;
57 | }
58 | extern int ip_equal(int type, void const *ip1, void const *ip2)
59 | {
60 | uchar const *p1 = (uchar const*)ip1;
61 | uchar const *p2 = (uchar const*)ip2;
62 | int len = 4;
63 | if (AF_INET6 == type) {
64 | len = 16;
65 | }
66 | int i;
67 | for (i = 0; i < len; ++i) {
68 | if (p1[i] != p2[i])
69 | return 0;
70 | }
71 | return 1;
72 | }
73 |
74 | static int is_filter(char const *ifname)
75 | {
76 | /* 过滤掉无线,虚拟机接口等 */
77 | char const *filter[] = {
78 | /* windows */
79 | "Wireless", "Microsoft",
80 | "Virtual",
81 | /* linux */
82 | "lo", "wlan", "vboxnet",
83 | "ifb", "gre", "teql",
84 | "br", "imq", "ra",
85 | "wds", "sit", "apcli",
86 | };
87 | unsigned int i;
88 | for (i = 0; i < ARRAY_SIZE(filter); ++i) {
89 | if (strstr(ifname, filter[i]))
90 | return 1;
91 | }
92 | return 0;
93 | }
94 | // #ifdef LINUX
95 | static char *get_ifname_from_buff(char *buff)
96 | {
97 | char *s;
98 | while (isspace(*buff))
99 | ++buff;
100 | s = buff;
101 | while (':' != *buff && '\0' != *buff)
102 | ++buff;
103 | *buff = '\0';
104 | return s;
105 | }
106 | // #endif
107 | /*
108 | * 获取所有网络接口
109 | * ifnames 实际获取的接口
110 | * cnt 两个作用,1:传入表示ifnames最多可以存储的接口个数
111 | * 2:返回表示实际获取了的接口个数
112 | * 返回接口个数在cnt里
113 | * @return: >=0 成功,实际获取的接口个数
114 | * -1 获取失败
115 | * -2 cnt过小
116 | */
117 | extern int getall_ifs(iflist_t *ifs, int *cnt)
118 | {
119 | int i = 0;
120 | if (!ifs || *cnt <= 0) return -2;
121 |
122 | // #ifdef LINUX /* linux (unix osx?) */
123 | #define _PATH_PROCNET_DEV "/proc/net/dev"
124 | #define BUFF_LINE_MAX (1024)
125 | char buff[BUFF_LINE_MAX];
126 | FILE *fd = fopen(_PATH_PROCNET_DEV, "r");
127 | char *name;
128 | if (NULL == fd) {
129 | perror("fopen");
130 | return -1;
131 | }
132 | /* _PATH_PROCNET_DEV文件格式如下,...表示后面我们不关心
133 | * Inter-| Receive ...
134 | * face |bytes packets ...
135 | * eth0: 147125283 119599 ...
136 | * wlan0: 229230 2635 ...
137 | * lo: 10285509 38254 ...
138 | */
139 | /* 略过开始两行 */
140 | fgets(buff, BUFF_LINE_MAX, fd);
141 | fgets(buff, BUFF_LINE_MAX, fd);
142 | while (NULL != fgets(buff, BUFF_LINE_MAX, fd)) {
143 | name = get_ifname_from_buff(buff);
144 | _D("%s\n", name);
145 | /* 过滤无关网络接口 */
146 | if (is_filter(name)) {
147 | _D("filtered %s.\n", name);
148 | continue;
149 | }
150 | strncpy(ifs[i].name, name, IFNAMSIZ);
151 | _D("ifs[%d].name: %s\n", i, ifs[i].name);
152 | ++i;
153 | if (i >= *cnt) {
154 | fclose(fd);
155 | return -2;
156 | }
157 | }
158 | fclose(fd);
159 |
160 | // #elif WIN32
161 | // pcap_if_t *alldevs;
162 | // char errbuf[PCAP_ERRBUF_SIZE];
163 | // if (-1 == pcap_findalldevs(&alldevs, errbuf)) {
164 | // _M("Get interfaces handler error: %s\n", errbuf);
165 | // return -1;
166 | // }
167 | // for (pcap_if_t *d = alldevs; d; d = d->next) {
168 | // if (is_filter(d->description)) {
169 | // _D("filtered %s.\n", d->description);
170 | // continue;
171 | // }
172 | // if (i >= *cnt) return -2;
173 | // strncpy(ifs[i].name, d->name, IFNAMSIZ);
174 | // strncpy(ifs[i].desc, d->description, IFDESCSIZ);
175 | // ++i;
176 | // }
177 | // pcap_freealldevs(alldevs);
178 | // #endif
179 |
180 | *cnt = i;
181 | return i;
182 | }
183 |
184 | extern char const *format_time(void)
185 | {
186 | static char buff[FORMAT_TIME_MAX];
187 | time_t rawtime;
188 | struct tm *timeinfo;
189 |
190 | time(&rawtime);
191 | timeinfo = localtime(&rawtime);
192 | if (NULL == timeinfo) return NULL;
193 | strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", timeinfo);
194 |
195 | return buff;
196 | }
197 | extern int copy(char const *f1, char const *f2)
198 | {
199 | if (NULL == f1 || NULL == f2) return -1;
200 | FILE *src, *dst;
201 | src = fopen(f1, "r");
202 | dst = fopen(f2, "w");
203 | if (NULL == src || NULL == dst) return -1;
204 | char buff[1024];
205 | int n;
206 | while (0 < (n = fread(buff, 1, 1024, src)))
207 | fwrite(buff, 1, n, dst);
208 |
209 | fclose(src);
210 | fclose(dst);
211 |
212 | return 0;
213 | }
214 | /*
215 | * 本地是否是小端序
216 | * @return: !0: 是
217 | * 0: 不是(大端序)
218 | */
219 | static int islsb()
220 | {
221 | static uint16 a = 0x0001;
222 | return (int)(*(uchar*)&a);
223 | }
224 | static uint16 exorders(uint16 n)
225 | {
226 | return ((n>>8)|(n<<8));
227 | }
228 | static uint32 exorderl(uint32 n)
229 | {
230 | return (n>>24)|((n&0x00ff0000)>>8)|((n&0x0000ff00)<<8)|(n<<24);
231 | }
232 | extern uint16 htols(uint16 n)
233 | {
234 | return islsb()?n:exorders(n);
235 | }
236 | extern uint16 htoms(uint16 n)
237 | {
238 | return islsb()?exorders(n):n;
239 | }
240 | extern uint16 ltohs(uint16 n)
241 | {
242 | return islsb()?n:exorders(n);
243 | }
244 | extern uint16 mtohs(uint16 n)
245 | {
246 | return islsb()?exorders(n):n;
247 | }
248 | extern uint32 htoll(uint32 n)
249 | {
250 | return islsb()?n:exorderl(n);
251 | }
252 | extern uint32 htoml(uint32 n)
253 | {
254 | return islsb()?exorderl(n):n;
255 | }
256 | extern uint32 ltohl(uint32 n)
257 | {
258 | return islsb()?n:exorderl(n);
259 | }
260 | extern uint32 mtohl(uint32 n)
261 | {
262 | return islsb()?exorderl(n):n;
263 | }
264 | extern uchar const *format_mac(uchar const *macarr)
265 | {
266 | static uchar formatmac[] =
267 | "xx:xx:xx:xx:xx:xx";
268 | if (NULL == macarr)
269 | return NULL;
270 | sprintf((char*)formatmac, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
271 | macarr[0], macarr[1], macarr[2],
272 | macarr[3], macarr[4], macarr[5]);
273 | return formatmac;
274 | }
275 | /*
276 | * 以16进制打印数据
277 | */
278 | extern void format_data(uchar const *d, size_t len)
279 | {
280 | int i;
281 | for (i = 0; i < (long)len; ++i) {
282 | if (i != 0 && i%16 == 0)
283 | _M("\n");
284 | _M("%02x ", d[i]);
285 | }
286 | _M("\n");
287 | }
288 |
289 | #ifdef LINUX
290 | /*
291 | * 返回t1-t0的时间差
292 | * 由于这里精度没必要达到ns,故返回相差微秒ms
293 | * @return: 时间差,单位微秒(1s == 1000ms)
294 | */
295 | extern long difftimespec(struct timespec t1, struct timespec t0)
296 | {
297 | long d = t1.tv_sec-t0.tv_sec;
298 | d *= 1000;
299 | d += (t1.tv_nsec-t0.tv_nsec)/(long)(1e6);
300 | return d;
301 | }
302 |
303 | /*
304 | * 判断网络是否连通
305 | * 最长延时3s,也就是说如果3s内没有检测到数据回应,那么认为网络不通
306 | * TODO 使用icmp协议判断
307 | * @return: !0: 连通
308 | * 0: 没有连通
309 | */
310 | extern int isnetok(char const *ifname)
311 | {
312 | static char baidu[] = "baidu.com";
313 | sleep(100);
314 | return 1;
315 | }
316 |
317 | /*
318 | * 休眠ms微秒
319 | */
320 | extern void msleep(long ms)
321 | {
322 | struct timeval tv;
323 | tv.tv_sec = ms/1000;
324 | tv.tv_usec = ms%1000*1000;
325 | select(0, 0, 0, 0, &tv);
326 | }
327 | #endif /* LINUX */
328 |
329 | #endif
--------------------------------------------------------------------------------
/libs/common.h:
--------------------------------------------------------------------------------
1 | #ifndef COMMON_H__
2 | #define COMMON_H__
3 | /*
4 | * 通用的代码和定义
5 | */
6 | typedef unsigned char uchar; /* 一个字节 */
7 | typedef unsigned short uint16; /* 两个字节 */
8 | typedef unsigned int uint32; /* 四个字节 */
9 |
10 | /* 用户名和密码长度 */
11 | #define UNAME_LEN (32)
12 | #define PWD_LEN (32)
13 |
14 | #define FORMAT_TIME_MAX (64)
15 |
16 | // #ifdef LINUX
17 | #include
18 | #include
19 | #include
20 | #include
21 | #define EXE_PATH_MAX (PATH_MAX+1)
22 | // #elif WIN32
23 | // # include
24 | // # define ETH_ALEN (6)
25 | // # define IF_NAMSIZE (64)
26 | // # define MTU_MAX (65536)
27 | // # define EXE_PATH_MAX (MAX_PATH+1)
28 | // # define IFDESCSIZ (126)
29 | // #endif
30 |
31 | typedef struct {
32 | char name[IF_NAMESIZE]; /* linux下是eth0, windows采用的是注册表类似的(\Device\NPF_{xxxx-xxx-xx-xx-xxx}) */
33 | // #ifdef WIN32
34 | // char desc[IFDESCSIZ]; /* windows下描述(AMD PCNET Family PCI Ethernet Adapter) */
35 | // #endif
36 | }iflist_t;
37 |
38 |
39 | #undef ARRAY_SIZE
40 | #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
41 |
42 | #define MAX(x, y) ((x)>(y)?(x):(y))
43 | #define MIN(x, y) ((x)>(y)?(y):(x))
44 |
45 | #undef PRINT
46 | #ifdef GUI
47 | # include
48 | # define PRINT(...) g_print(__VA_ARGS__)
49 | #else
50 | # include
51 | # define PRINT(...) fprintf(stderr, __VA_ARGS__)
52 | #endif
53 |
54 | #ifdef DEBUG
55 | # define _D(...) \
56 | do { \
57 | PRINT("%s:%s:%d:", format_time(), __FILE__, __LINE__); \
58 | PRINT(__VA_ARGS__); \
59 | } while(0)
60 | #else
61 | # define _D(...) ((void)0)
62 | #endif
63 |
64 | #define _M(...) PRINT(__VA_ARGS__);
65 |
66 | /*
67 | * 获取程序所在的实际绝对路径的目录
68 | * exedir: 返回目录,加上\0一起长度是EXE_PATH_MAX,
69 | * 如果本上长度达到了EXE_PATH_MAX(不包括\0),那么也会返回失败
70 | * @return: 0: 成功
71 | * !0: 失败
72 | */
73 | extern int getexedir(char *exedir);
74 | /*
75 | * 比较两个mac是否相同
76 | * @return: 0: 不同
77 | * !0: 相同
78 | */
79 | extern int mac_equal(uchar const *mac1, uchar const *mac2);
80 |
81 | /*
82 | * 判断两个ip是否相等
83 | * type: AF_INET or AF_INET6, 分别对应ipv4,ipv6
84 | * ip1, ip2: 比较的两个ip,同为struct in_addr或struct in6_addr的指针
85 | * @return: 0: 不同
86 | * !0: 相同
87 | */
88 | extern int ip_equal(int type, void const *ip1, void const *ip2);
89 |
90 | /*
91 | * 获取所有网络接口
92 | * ifnames 实际获取的接口
93 | * cnt 两个作用,1:传入表示ifnames最多可以存储的接口个数
94 | * 2:返回表示实际获取了的接口个数
95 | * 返回接口个数在cnt里
96 | * @return: >=0 成功,实际获取的接口个数
97 | * -1 获取失败
98 | * -2 cnt过小
99 | */
100 | extern int getall_ifs(iflist_t *ifs, int *cnt);
101 | /*
102 | * 获取当前时间按照
103 | * yyyy-MM-dd HH:mm:ss
104 | * 格式返回
105 | * NOTE 不要去修改返回结果,并且不是线程安全的
106 | * @return: NULL: 失败
107 | * !NULL: 存储的结果
108 | */
109 | extern char const *format_time(void);
110 | /*
111 | * 简单的复制文件,暂时不进行细致错误检查
112 | * NOTE 是绝对路径
113 | * scr: 源文件
114 | * dst: 目标文件
115 | * @return: 0: 成功
116 | * -1: 失败
117 | */
118 | extern int copy(char const *src, char const *dst);
119 |
120 | /*
121 | * 字节序转换相关函数
122 | * host to lsb/msb short/long (host->l/m)
123 | * lsb/msb to host short/long (l/m->host)
124 | */
125 | extern uint16 htols(uint16 n);
126 | extern uint16 htoms(uint16 n);
127 | extern uint16 ltohs(uint16 n);
128 | extern uint16 mtohs(uint16 n);
129 |
130 | extern uint32 htoll(uint32 n);
131 | extern uint32 htoml(uint32 n);
132 | extern uint32 ltohl(uint32 n);
133 | extern uint32 mtohl(uint32 n);
134 |
135 | extern uchar const *format_mac(uchar const *mac);
136 |
137 | /*
138 | * 判断网络是否连通
139 | * ifname: 接口名字
140 | * @return: !0: 连通
141 | * 0: 没有连通
142 | */
143 | extern int isnetok(char const *ifname);
144 | /*
145 | * 返回t1-t0的时间差
146 | * 由于这里精度没必要达到ns,故返回相差微秒ms
147 | * @return: 时间差,单位微秒(1s == 1000ms)
148 | */
149 | extern long difftimespec(struct timespec t1, struct timespec t0);
150 |
151 | /*
152 | * 休眠ms微秒
153 | */
154 | extern void msleep(long ms);
155 |
156 | /*
157 | * 以16进制打印数据
158 | */
159 | extern void format_data(uchar const *d, size_t len);
160 |
161 | #endif
--------------------------------------------------------------------------------
/libs/md4.c:
--------------------------------------------------------------------------------
1 | /*
2 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 | * MD4 Message-Digest Algorithm (RFC 1320).
4 | *
5 | * Homepage:
6 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
7 | *
8 | * Author:
9 | * Alexander Peslyak, better known as Solar Designer
10 | *
11 | * This software was written by Alexander Peslyak in 2001. No copyright is
12 | * claimed, and the software is hereby placed in the public domain.
13 | * In case this attempt to disclaim copyright and place the software in the
14 | * public domain is deemed null and void, then the software is
15 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 | * general public under the following terms:
17 | *
18 | * Redistribution and use in source and binary forms, with or without
19 | * modification, are permitted.
20 | *
21 | * There's ABSOLUTELY NO WARRANTY, express or implied.
22 | *
23 | * (This is a heavily cut-down "BSD license".)
24 | *
25 | * This differs from Colin Plumb's older public domain implementation in that
26 | * no exactly 32-bit integer data type is required (any 32-bit or wider
27 | * unsigned integer data type will do), there's no compile-time endianness
28 | * configuration, and the function prototypes match OpenSSL's. No code from
29 | * Colin Plumb's implementation has been reused; this comment merely compares
30 | * the properties of the two independent implementations.
31 | *
32 | * The primary goals of this implementation are portability and ease of use.
33 | * It is meant to be fast, but not as fast as possible. Some known
34 | * optimizations are not included to reduce source code size and avoid
35 | * compile-time configuration.
36 | */
37 |
38 | #ifndef HAVE_OPENSSL
39 |
40 | #include
41 |
42 | #include "md4.h"
43 |
44 | /*
45 | * The basic MD4 functions.
46 | *
47 | * F and G are optimized compared to their RFC 1320 definitions, with the
48 | * optimization for F borrowed from Colin Plumb's MD5 implementation.
49 | */
50 | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
51 | #define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
52 | #define H(x, y, z) ((x) ^ (y) ^ (z))
53 |
54 | /*
55 | * The MD4 transformation for all three rounds.
56 | */
57 | #define STEP(f, a, b, c, d, x, s) \
58 | (a) += f((b), (c), (d)) + (x); \
59 | (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
60 |
61 | /*
62 | * SET reads 4 input bytes in little-endian byte order and stores them in a
63 | * properly aligned word in host byte order.
64 | *
65 | * The check for little-endian architectures that tolerate unaligned memory
66 | * accesses is just an optimization. Nothing will break if it fails to detect
67 | * a suitable architecture.
68 | *
69 | * Unfortunately, this optimization may be a C strict aliasing rules violation
70 | * if the caller's data buffer has effective type that cannot be aliased by
71 | * MD4_u32plus. In practice, this problem may occur if these MD4 routines are
72 | * inlined into a calling function, or with future and dangerously advanced
73 | * link-time optimizations. For the time being, keeping these MD4 routines in
74 | * their own translation unit avoids the problem.
75 | */
76 | #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
77 | #define SET(n) \
78 | (*(MD4_u32plus *)&ptr[(n) * 4])
79 | #define GET(n) \
80 | SET(n)
81 | #else
82 | #define SET(n) \
83 | (ctx->block[(n)] = \
84 | (MD4_u32plus)ptr[(n) * 4] | \
85 | ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
86 | ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
87 | ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
88 | #define GET(n) \
89 | (ctx->block[(n)])
90 | #endif
91 |
92 | /*
93 | * This processes one or more 64-byte data blocks, but does NOT update the bit
94 | * counters. There are no alignment requirements.
95 | */
96 | static const void *body(MD4_CTX *ctx, const void *data, unsigned long size)
97 | {
98 | const unsigned char *ptr;
99 | MD4_u32plus a, b, c, d;
100 | MD4_u32plus saved_a, saved_b, saved_c, saved_d;
101 | const MD4_u32plus ac1 = 0x5a827999, ac2 = 0x6ed9eba1;
102 |
103 | ptr = (const unsigned char *)data;
104 |
105 | a = ctx->a;
106 | b = ctx->b;
107 | c = ctx->c;
108 | d = ctx->d;
109 |
110 | do {
111 | saved_a = a;
112 | saved_b = b;
113 | saved_c = c;
114 | saved_d = d;
115 |
116 | /* Round 1 */
117 | STEP(F, a, b, c, d, SET(0), 3)
118 | STEP(F, d, a, b, c, SET(1), 7)
119 | STEP(F, c, d, a, b, SET(2), 11)
120 | STEP(F, b, c, d, a, SET(3), 19)
121 | STEP(F, a, b, c, d, SET(4), 3)
122 | STEP(F, d, a, b, c, SET(5), 7)
123 | STEP(F, c, d, a, b, SET(6), 11)
124 | STEP(F, b, c, d, a, SET(7), 19)
125 | STEP(F, a, b, c, d, SET(8), 3)
126 | STEP(F, d, a, b, c, SET(9), 7)
127 | STEP(F, c, d, a, b, SET(10), 11)
128 | STEP(F, b, c, d, a, SET(11), 19)
129 | STEP(F, a, b, c, d, SET(12), 3)
130 | STEP(F, d, a, b, c, SET(13), 7)
131 | STEP(F, c, d, a, b, SET(14), 11)
132 | STEP(F, b, c, d, a, SET(15), 19)
133 |
134 | /* Round 2 */
135 | STEP(G, a, b, c, d, GET(0) + ac1, 3)
136 | STEP(G, d, a, b, c, GET(4) + ac1, 5)
137 | STEP(G, c, d, a, b, GET(8) + ac1, 9)
138 | STEP(G, b, c, d, a, GET(12) + ac1, 13)
139 | STEP(G, a, b, c, d, GET(1) + ac1, 3)
140 | STEP(G, d, a, b, c, GET(5) + ac1, 5)
141 | STEP(G, c, d, a, b, GET(9) + ac1, 9)
142 | STEP(G, b, c, d, a, GET(13) + ac1, 13)
143 | STEP(G, a, b, c, d, GET(2) + ac1, 3)
144 | STEP(G, d, a, b, c, GET(6) + ac1, 5)
145 | STEP(G, c, d, a, b, GET(10) + ac1, 9)
146 | STEP(G, b, c, d, a, GET(14) + ac1, 13)
147 | STEP(G, a, b, c, d, GET(3) + ac1, 3)
148 | STEP(G, d, a, b, c, GET(7) + ac1, 5)
149 | STEP(G, c, d, a, b, GET(11) + ac1, 9)
150 | STEP(G, b, c, d, a, GET(15) + ac1, 13)
151 |
152 | /* Round 3 */
153 | STEP(H, a, b, c, d, GET(0) + ac2, 3)
154 | STEP(H, d, a, b, c, GET(8) + ac2, 9)
155 | STEP(H, c, d, a, b, GET(4) + ac2, 11)
156 | STEP(H, b, c, d, a, GET(12) + ac2, 15)
157 | STEP(H, a, b, c, d, GET(2) + ac2, 3)
158 | STEP(H, d, a, b, c, GET(10) + ac2, 9)
159 | STEP(H, c, d, a, b, GET(6) + ac2, 11)
160 | STEP(H, b, c, d, a, GET(14) + ac2, 15)
161 | STEP(H, a, b, c, d, GET(1) + ac2, 3)
162 | STEP(H, d, a, b, c, GET(9) + ac2, 9)
163 | STEP(H, c, d, a, b, GET(5) + ac2, 11)
164 | STEP(H, b, c, d, a, GET(13) + ac2, 15)
165 | STEP(H, a, b, c, d, GET(3) + ac2, 3)
166 | STEP(H, d, a, b, c, GET(11) + ac2, 9)
167 | STEP(H, c, d, a, b, GET(7) + ac2, 11)
168 | STEP(H, b, c, d, a, GET(15) + ac2, 15)
169 |
170 | a += saved_a;
171 | b += saved_b;
172 | c += saved_c;
173 | d += saved_d;
174 |
175 | ptr += 64;
176 | } while (size -= 64);
177 |
178 | ctx->a = a;
179 | ctx->b = b;
180 | ctx->c = c;
181 | ctx->d = d;
182 |
183 | return ptr;
184 | }
185 |
186 | void MD4_Init(MD4_CTX *ctx)
187 | {
188 | ctx->a = 0x67452301;
189 | ctx->b = 0xefcdab89;
190 | ctx->c = 0x98badcfe;
191 | ctx->d = 0x10325476;
192 |
193 | ctx->lo = 0;
194 | ctx->hi = 0;
195 | }
196 |
197 | void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
198 | {
199 | MD4_u32plus saved_lo;
200 | unsigned long used, available;
201 |
202 | saved_lo = ctx->lo;
203 | if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
204 | ctx->hi++;
205 | ctx->hi += size >> 29;
206 |
207 | used = saved_lo & 0x3f;
208 |
209 | if (used) {
210 | available = 64 - used;
211 |
212 | if (size < available) {
213 | memcpy(&ctx->buffer[used], data, size);
214 | return;
215 | }
216 |
217 | memcpy(&ctx->buffer[used], data, available);
218 | data = (const unsigned char *)data + available;
219 | size -= available;
220 | body(ctx, ctx->buffer, 64);
221 | }
222 |
223 | if (size >= 64) {
224 | data = body(ctx, data, size & ~(unsigned long)0x3f);
225 | size &= 0x3f;
226 | }
227 |
228 | memcpy(ctx->buffer, data, size);
229 | }
230 |
231 | #define OUT(dst, src) \
232 | (dst)[0] = (unsigned char)(src); \
233 | (dst)[1] = (unsigned char)((src) >> 8); \
234 | (dst)[2] = (unsigned char)((src) >> 16); \
235 | (dst)[3] = (unsigned char)((src) >> 24);
236 |
237 | void MD4_Final(unsigned char *result, MD4_CTX *ctx)
238 | {
239 | unsigned long used, available;
240 |
241 | used = ctx->lo & 0x3f;
242 |
243 | ctx->buffer[used++] = 0x80;
244 |
245 | available = 64 - used;
246 |
247 | if (available < 8) {
248 | memset(&ctx->buffer[used], 0, available);
249 | body(ctx, ctx->buffer, 64);
250 | used = 0;
251 | available = 64;
252 | }
253 |
254 | memset(&ctx->buffer[used], 0, available - 8);
255 |
256 | ctx->lo <<= 3;
257 | OUT(&ctx->buffer[56], ctx->lo)
258 | OUT(&ctx->buffer[60], ctx->hi)
259 |
260 | body(ctx, ctx->buffer, 64);
261 |
262 | OUT(&result[0], ctx->a)
263 | OUT(&result[4], ctx->b)
264 | OUT(&result[8], ctx->c)
265 | OUT(&result[12], ctx->d)
266 |
267 | memset(ctx, 0, sizeof(*ctx));
268 | }
269 |
270 | void MD4(const void *data, unsigned long size, unsigned char *result) {
271 | MD4_CTX ctx;
272 | MD4_Init(&ctx);
273 | MD4_Update(&ctx, data, size);
274 | MD4_Final(result, &ctx);
275 | }
276 |
277 | #endif
278 |
--------------------------------------------------------------------------------
/libs/md4.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 | * MD4 Message-Digest Algorithm (RFC 1320).
4 | *
5 | * Homepage:
6 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
7 | *
8 | * Author:
9 | * Alexander Peslyak, better known as Solar Designer
10 | *
11 | * This software was written by Alexander Peslyak in 2001. No copyright is
12 | * claimed, and the software is hereby placed in the public domain.
13 | * In case this attempt to disclaim copyright and place the software in the
14 | * public domain is deemed null and void, then the software is
15 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 | * general public under the following terms:
17 | *
18 | * Redistribution and use in source and binary forms, with or without
19 | * modification, are permitted.
20 | *
21 | * There's ABSOLUTELY NO WARRANTY, express or implied.
22 | *
23 | * See md4.c for more information.
24 | */
25 |
26 | #ifdef HAVE_OPENSSL
27 | #include
28 | #elif !defined(_MD4_H)
29 | #define _MD4_H
30 |
31 | /* Any 32-bit or wider unsigned integer data type will do */
32 | typedef unsigned int MD4_u32plus;
33 |
34 | typedef struct {
35 | MD4_u32plus lo, hi;
36 | MD4_u32plus a, b, c, d;
37 | unsigned char buffer[64];
38 | MD4_u32plus block[16];
39 | } MD4_CTX;
40 |
41 | extern void MD4_Init(MD4_CTX *ctx);
42 | extern void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
43 | extern void MD4_Final(unsigned char *result, MD4_CTX *ctx);
44 |
45 | void MD4(const void *data, unsigned long size, unsigned char *result);
46 |
47 | #endif
48 |
--------------------------------------------------------------------------------
/libs/md5.c:
--------------------------------------------------------------------------------
1 | /*
2 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 | * MD5 Message-Digest Algorithm (RFC 1321).
4 | *
5 | * Homepage:
6 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
7 | *
8 | * Author:
9 | * Alexander Peslyak, better known as Solar Designer
10 | *
11 | * This software was written by Alexander Peslyak in 2001. No copyright is
12 | * claimed, and the software is hereby placed in the public domain.
13 | * In case this attempt to disclaim copyright and place the software in the
14 | * public domain is deemed null and void, then the software is
15 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 | * general public under the following terms:
17 | *
18 | * Redistribution and use in source and binary forms, with or without
19 | * modification, are permitted.
20 | *
21 | * There's ABSOLUTELY NO WARRANTY, express or implied.
22 | *
23 | * (This is a heavily cut-down "BSD license".)
24 | *
25 | * This differs from Colin Plumb's older public domain implementation in that
26 | * no exactly 32-bit integer data type is required (any 32-bit or wider
27 | * unsigned integer data type will do), there's no compile-time endianness
28 | * configuration, and the function prototypes match OpenSSL's. No code from
29 | * Colin Plumb's implementation has been reused; this comment merely compares
30 | * the properties of the two independent implementations.
31 | *
32 | * The primary goals of this implementation are portability and ease of use.
33 | * It is meant to be fast, but not as fast as possible. Some known
34 | * optimizations are not included to reduce source code size and avoid
35 | * compile-time configuration.
36 | */
37 |
38 | #ifndef HAVE_OPENSSL
39 |
40 | #include
41 |
42 | #include "md5.h"
43 |
44 | /*
45 | * The basic MD5 functions.
46 | *
47 | * F and G are optimized compared to their RFC 1321 definitions for
48 | * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
49 | * implementation.
50 | */
51 | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
52 | #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
53 | #define H(x, y, z) (((x) ^ (y)) ^ (z))
54 | #define H2(x, y, z) ((x) ^ ((y) ^ (z)))
55 | #define I(x, y, z) ((y) ^ ((x) | ~(z)))
56 |
57 | /*
58 | * The MD5 transformation for all four rounds.
59 | */
60 | #define STEP(f, a, b, c, d, x, t, s) \
61 | (a) += f((b), (c), (d)) + (x) + (t); \
62 | (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
63 | (a) += (b);
64 |
65 | /*
66 | * SET reads 4 input bytes in little-endian byte order and stores them in a
67 | * properly aligned word in host byte order.
68 | *
69 | * The check for little-endian architectures that tolerate unaligned memory
70 | * accesses is just an optimization. Nothing will break if it fails to detect
71 | * a suitable architecture.
72 | *
73 | * Unfortunately, this optimization may be a C strict aliasing rules violation
74 | * if the caller's data buffer has effective type that cannot be aliased by
75 | * MD5_u32plus. In practice, this problem may occur if these MD5 routines are
76 | * inlined into a calling function, or with future and dangerously advanced
77 | * link-time optimizations. For the time being, keeping these MD5 routines in
78 | * their own translation unit avoids the problem.
79 | */
80 | #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
81 | #define SET(n) \
82 | (*(MD5_u32plus *)&ptr[(n) * 4])
83 | #define GET(n) \
84 | SET(n)
85 | #else
86 | #define SET(n) \
87 | (ctx->block[(n)] = \
88 | (MD5_u32plus)ptr[(n) * 4] | \
89 | ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
90 | ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
91 | ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
92 | #define GET(n) \
93 | (ctx->block[(n)])
94 | #endif
95 |
96 | /*
97 | * This processes one or more 64-byte data blocks, but does NOT update the bit
98 | * counters. There are no alignment requirements.
99 | */
100 | static const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
101 | {
102 | const unsigned char *ptr;
103 | MD5_u32plus a, b, c, d;
104 | MD5_u32plus saved_a, saved_b, saved_c, saved_d;
105 |
106 | ptr = (const unsigned char *)data;
107 |
108 | a = ctx->a;
109 | b = ctx->b;
110 | c = ctx->c;
111 | d = ctx->d;
112 |
113 | do {
114 | saved_a = a;
115 | saved_b = b;
116 | saved_c = c;
117 | saved_d = d;
118 |
119 | /* Round 1 */
120 | STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
121 | STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
122 | STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
123 | STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
124 | STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
125 | STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
126 | STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
127 | STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
128 | STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
129 | STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
130 | STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
131 | STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
132 | STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
133 | STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
134 | STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
135 | STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
136 |
137 | /* Round 2 */
138 | STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
139 | STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
140 | STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
141 | STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
142 | STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
143 | STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
144 | STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
145 | STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
146 | STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
147 | STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
148 | STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
149 | STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
150 | STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
151 | STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
152 | STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
153 | STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
154 |
155 | /* Round 3 */
156 | STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
157 | STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
158 | STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
159 | STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
160 | STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
161 | STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
162 | STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
163 | STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
164 | STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
165 | STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
166 | STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
167 | STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
168 | STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
169 | STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
170 | STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
171 | STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
172 |
173 | /* Round 4 */
174 | STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
175 | STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
176 | STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
177 | STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
178 | STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
179 | STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
180 | STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
181 | STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
182 | STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
183 | STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
184 | STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
185 | STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
186 | STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
187 | STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
188 | STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
189 | STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
190 |
191 | a += saved_a;
192 | b += saved_b;
193 | c += saved_c;
194 | d += saved_d;
195 |
196 | ptr += 64;
197 | } while (size -= 64);
198 |
199 | ctx->a = a;
200 | ctx->b = b;
201 | ctx->c = c;
202 | ctx->d = d;
203 |
204 | return ptr;
205 | }
206 |
207 | void MD5_Init(MD5_CTX *ctx)
208 | {
209 | ctx->a = 0x67452301;
210 | ctx->b = 0xefcdab89;
211 | ctx->c = 0x98badcfe;
212 | ctx->d = 0x10325476;
213 |
214 | ctx->lo = 0;
215 | ctx->hi = 0;
216 | }
217 |
218 | void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
219 | {
220 | MD5_u32plus saved_lo;
221 | unsigned long used, available;
222 |
223 | saved_lo = ctx->lo;
224 | if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
225 | ctx->hi++;
226 | ctx->hi += size >> 29;
227 |
228 | used = saved_lo & 0x3f;
229 |
230 | if (used) {
231 | available = 64 - used;
232 |
233 | if (size < available) {
234 | memcpy(&ctx->buffer[used], data, size);
235 | return;
236 | }
237 |
238 | memcpy(&ctx->buffer[used], data, available);
239 | data = (const unsigned char *)data + available;
240 | size -= available;
241 | body(ctx, ctx->buffer, 64);
242 | }
243 |
244 | if (size >= 64) {
245 | data = body(ctx, data, size & ~(unsigned long)0x3f);
246 | size &= 0x3f;
247 | }
248 |
249 | memcpy(ctx->buffer, data, size);
250 | }
251 |
252 | #define OUT(dst, src) \
253 | (dst)[0] = (unsigned char)(src); \
254 | (dst)[1] = (unsigned char)((src) >> 8); \
255 | (dst)[2] = (unsigned char)((src) >> 16); \
256 | (dst)[3] = (unsigned char)((src) >> 24);
257 |
258 | void MD5_Final(unsigned char *result, MD5_CTX *ctx)
259 | {
260 | unsigned long used, available;
261 |
262 | used = ctx->lo & 0x3f;
263 |
264 | ctx->buffer[used++] = 0x80;
265 |
266 | available = 64 - used;
267 |
268 | if (available < 8) {
269 | memset(&ctx->buffer[used], 0, available);
270 | body(ctx, ctx->buffer, 64);
271 | used = 0;
272 | available = 64;
273 | }
274 |
275 | memset(&ctx->buffer[used], 0, available - 8);
276 |
277 | ctx->lo <<= 3;
278 | OUT(&ctx->buffer[56], ctx->lo)
279 | OUT(&ctx->buffer[60], ctx->hi)
280 |
281 | body(ctx, ctx->buffer, 64);
282 |
283 | OUT(&result[0], ctx->a)
284 | OUT(&result[4], ctx->b)
285 | OUT(&result[8], ctx->c)
286 | OUT(&result[12], ctx->d)
287 |
288 | memset(ctx, 0, sizeof(*ctx));
289 | }
290 |
291 | void MD5(const void *data, unsigned long size, unsigned char *result) {
292 | MD5_CTX ctx;
293 | MD5_Init(&ctx);
294 | MD5_Update(&ctx, data, size);
295 | MD5_Final(result, &ctx);
296 | }
297 |
298 | #endif
299 |
--------------------------------------------------------------------------------
/libs/md5.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
3 | * MD5 Message-Digest Algorithm (RFC 1321).
4 | *
5 | * Homepage:
6 | * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
7 | *
8 | * Author:
9 | * Alexander Peslyak, better known as Solar Designer
10 | *
11 | * This software was written by Alexander Peslyak in 2001. No copyright is
12 | * claimed, and the software is hereby placed in the public domain.
13 | * In case this attempt to disclaim copyright and place the software in the
14 | * public domain is deemed null and void, then the software is
15 | * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
16 | * general public under the following terms:
17 | *
18 | * Redistribution and use in source and binary forms, with or without
19 | * modification, are permitted.
20 | *
21 | * There's ABSOLUTELY NO WARRANTY, express or implied.
22 | *
23 | * See md5.c for more information.
24 | */
25 |
26 | #ifdef HAVE_OPENSSL
27 | #include
28 | #elif !defined(_MD5_H)
29 | #define _MD5_H
30 |
31 | /* Any 32-bit or wider unsigned integer data type will do */
32 | typedef unsigned int MD5_u32plus;
33 |
34 | typedef struct {
35 | MD5_u32plus lo, hi;
36 | MD5_u32plus a, b, c, d;
37 | unsigned char buffer[64];
38 | MD5_u32plus block[16];
39 | } MD5_CTX;
40 |
41 | extern void MD5_Init(MD5_CTX *ctx);
42 | extern void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size);
43 | extern void MD5_Final(unsigned char *result, MD5_CTX *ctx);
44 |
45 | void MD5(const void *data, unsigned long size, unsigned char *result);
46 |
47 | #endif
48 |
--------------------------------------------------------------------------------
/libs/sha1.c:
--------------------------------------------------------------------------------
1 |
2 | /* from valgrind tests */
3 |
4 | /* ================ sha1.c ================ */
5 | /*
6 | SHA-1 in C
7 | By Steve Reid
8 | 100% Public Domain
9 |
10 | Test Vectors (from FIPS PUB 180-1)
11 | "abc"
12 | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
13 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
14 | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
15 | A million repetitions of "a"
16 | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
17 | */
18 |
19 | /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
20 | /* #define SHA1HANDSOFF * Copies data before messing with it. */
21 |
22 | #define SHA1HANDSOFF
23 |
24 | #include
25 | #include
26 | #include
27 | #include "sha1.h"
28 |
29 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
30 |
31 | /* blk0() and blk() perform the initial expand. */
32 | /* I got the idea of expanding during the round function from SSLeay */
33 | #if BYTE_ORDER == LITTLE_ENDIAN
34 | #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
35 | |(rol(block->l[i],8)&0x00FF00FF))
36 | #elif BYTE_ORDER == BIG_ENDIAN
37 | #define blk0(i) block->l[i]
38 | #else
39 | #error "Endianness not defined!"
40 | #endif
41 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
42 | ^block->l[(i+2)&15]^block->l[i&15],1))
43 |
44 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
45 | #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
46 | #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
47 | #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
48 | #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
49 | #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
50 |
51 |
52 | /* Hash a single 512-bit block. This is the core of the algorithm. */
53 |
54 | void SHA1Transform(uint32_t state[5], const unsigned char buffer[64])
55 | {
56 | uint32_t a, b, c, d, e;
57 | typedef union {
58 | unsigned char c[64];
59 | uint32_t l[16];
60 | } CHAR64LONG16;
61 | #ifdef SHA1HANDSOFF
62 | CHAR64LONG16 block[1]; /* use array to appear as a pointer */
63 | memcpy(block, buffer, 64);
64 | #else
65 | /* The following had better never be used because it causes the
66 | * pointer-to-const buffer to be cast into a pointer to non-const.
67 | * And the result is written through. I threw a "const" in, hoping
68 | * this will cause a diagnostic.
69 | */
70 | CHAR64LONG16* block = (const CHAR64LONG16*)buffer;
71 | #endif
72 | /* Copy context->state[] to working vars */
73 | a = state[0];
74 | b = state[1];
75 | c = state[2];
76 | d = state[3];
77 | e = state[4];
78 | /* 4 rounds of 20 operations each. Loop unrolled. */
79 | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
80 | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
81 | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
82 | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
83 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
84 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
85 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
86 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
87 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
88 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
89 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
90 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
91 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
92 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
93 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
94 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
95 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
96 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
97 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
98 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
99 | /* Add the working vars back into context.state[] */
100 | state[0] += a;
101 | state[1] += b;
102 | state[2] += c;
103 | state[3] += d;
104 | state[4] += e;
105 | /* Wipe variables */
106 | a = b = c = d = e = 0;
107 | #ifdef SHA1HANDSOFF
108 | memset(block, '\0', sizeof(block));
109 | #endif
110 | }
111 |
112 |
113 | /* SHA1Init - Initialize new context */
114 |
115 | void SHA1Init(SHA1_CTX* context)
116 | {
117 | /* SHA1 initialization constants */
118 | context->state[0] = 0x67452301;
119 | context->state[1] = 0xEFCDAB89;
120 | context->state[2] = 0x98BADCFE;
121 | context->state[3] = 0x10325476;
122 | context->state[4] = 0xC3D2E1F0;
123 | context->count[0] = context->count[1] = 0;
124 | }
125 |
126 |
127 | /* Run your data through this. */
128 |
129 | void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len)
130 | {
131 | uint32_t i, j;
132 |
133 | j = context->count[0];
134 | if ((context->count[0] += len << 3) < j)
135 | context->count[1]++;
136 | context->count[1] += (len>>29);
137 | j = (j >> 3) & 63;
138 | if ((j + len) > 63) {
139 | memcpy(&context->buffer[j], data, (i = 64-j));
140 | SHA1Transform(context->state, context->buffer);
141 | for ( ; i + 63 < len; i += 64) {
142 | SHA1Transform(context->state, &data[i]);
143 | }
144 | j = 0;
145 | }
146 | else i = 0;
147 | memcpy(&context->buffer[j], &data[i], len - i);
148 | }
149 |
150 |
151 | /* Add padding and return the message digest. */
152 |
153 | void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
154 | {
155 | unsigned i;
156 | unsigned char finalcount[8];
157 | unsigned char c;
158 |
159 | #if 0 /* untested "improvement" by DHR */
160 | /* Convert context->count to a sequence of bytes
161 | * in finalcount. Second element first, but
162 | * big-endian order within element.
163 | * But we do it all backwards.
164 | */
165 | unsigned char *fcp = &finalcount[8];
166 |
167 | for (i = 0; i < 2; i++)
168 | {
169 | uint32_t t = context->count[i];
170 | int j;
171 |
172 | for (j = 0; j < 4; t >>= 8, j++)
173 | *--fcp = (unsigned char) t;
174 | }
175 | #else
176 | for (i = 0; i < 8; i++) {
177 | finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
178 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
179 | }
180 | #endif
181 | c = 0200;
182 | SHA1Update(context, &c, 1);
183 | while ((context->count[0] & 504) != 448) {
184 | c = 0000;
185 | SHA1Update(context, &c, 1);
186 | }
187 | SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
188 | for (i = 0; i < 20; i++) {
189 | digest[i] = (unsigned char)
190 | ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
191 | }
192 | /* Wipe variables */
193 | memset(context, '\0', sizeof(*context));
194 | memset(&finalcount, '\0', sizeof(finalcount));
195 | }
196 | /* ================ end of sha1.c ================ */
197 |
198 | void SHA1(const unsigned char* data, uint32_t len, unsigned char digest[20]) {
199 | SHA1_CTX ctx;
200 | SHA1Init(&ctx);
201 | SHA1Update(&ctx, data, len);
202 | SHA1Final(digest, &ctx);
203 | }
--------------------------------------------------------------------------------
/libs/sha1.h:
--------------------------------------------------------------------------------
1 | #ifndef SHA1_H
2 | #define SHA1_H
3 | /* ================ sha1.h ================ */
4 | /*
5 | SHA-1 in C
6 | By Steve Reid
7 | 100% Public Domain
8 | */
9 | #include
10 |
11 | typedef struct {
12 | uint32_t state[5];
13 | uint32_t count[2];
14 | unsigned char buffer[64];
15 | } SHA1_CTX;
16 |
17 | void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
18 | void SHA1Init(SHA1_CTX* context);
19 | void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len);
20 | void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
21 |
22 | void SHA1(const unsigned char* data, uint32_t len, unsigned char digest[20]);
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include "auth.h"
6 | #include "configparse.h"
7 |
8 | #ifdef linux
9 | #include
10 | #include "daemon.h"
11 | #include "eapol.h"
12 | #include "libs/common.h"
13 | #endif
14 |
15 | #define VERSION "1.6.2"
16 |
17 | void print_help(int exval);
18 | int try_smart_eaplogin(void);
19 |
20 | static const char default_bind_ip[20] = "0.0.0.0";
21 |
22 | int main(int argc, char *argv[]) {
23 | if (argc == 1) {
24 | print_help(1);
25 | }
26 |
27 | char *file_path;
28 |
29 | while (1) {
30 | static const struct option long_options[] = {
31 | {"mode", required_argument, 0, 'm'},
32 | {"conf", required_argument, 0, 'c'},
33 | {"bindip", required_argument, 0, 'b'},
34 | {"log", required_argument, 0, 'l'},
35 | #ifdef linux
36 | {"daemon", no_argument, 0, 'd'},
37 | {"802.1x", no_argument, 0, 'x'},
38 | #endif
39 | {"eternal", no_argument, 0, 'e'},
40 | {"verbose", no_argument, 0, 'v'},
41 | {"help", no_argument, 0, 'h'},
42 | {0, 0, 0, 0}};
43 |
44 | int c;
45 | int option_index = 0;
46 | #ifdef linux
47 | c = getopt_long(argc, argv, "m:c:b:l:dxevh", long_options, &option_index);
48 | #else
49 | c = getopt_long(argc, argv, "m:c:b:l:evh", long_options, &option_index);
50 | #endif
51 |
52 | if (c == -1) {
53 | break;
54 | }
55 | switch (c) {
56 | case 'm':
57 | if (strcmp(optarg, "dhcp") == 0) {
58 | strcpy(mode, optarg);
59 | } else if (strcmp(optarg, "pppoe") == 0) {
60 | strcpy(mode, optarg);
61 | } else {
62 | printf("unknown mode\n");
63 | exit(1);
64 | }
65 | break;
66 | case 'c':
67 | #ifndef __APPLE__
68 | if (mode != NULL) {
69 | #endif
70 | #ifdef linux
71 | char path_c[PATH_MAX];
72 | realpath(optarg, path_c);
73 | file_path = strdup(path_c);
74 | #else
75 | file_path = optarg;
76 | #endif
77 | #ifndef __APPLE__
78 | }
79 | #endif
80 | break;
81 | case 'b':
82 | strcpy(bind_ip, optarg);
83 | break;
84 | case 'l':
85 | #ifndef __APPLE__
86 | if (mode != NULL) {
87 | #endif
88 | #ifdef linux
89 | char path_l[PATH_MAX];
90 | realpath(optarg, path_l);
91 | log_path = strdup(path_l);
92 | #else
93 | log_path = optarg;
94 | #endif
95 | logging_flag = 1;
96 | #ifndef __APPLE__
97 | }
98 | #endif
99 | break;
100 | #ifdef linux
101 | case 'd':
102 | daemon_flag = 1;
103 | break;
104 | case 'x':
105 | eapol_flag = 1;
106 | break;
107 | #endif
108 | case 'e':
109 | eternal_flag = 1;
110 | break;
111 | case 'v':
112 | verbose_flag = 1;
113 | break;
114 | case 'h':
115 | print_help(0);
116 | break;
117 | case '?':
118 | print_help(1);
119 | break;
120 | default:
121 | break;
122 | }
123 | }
124 |
125 | #ifndef __APPLE__
126 | if (mode != NULL && file_path != NULL) {
127 | #endif
128 | #ifdef linux
129 | if (daemon_flag) {
130 | daemonise();
131 | }
132 | #endif
133 |
134 | #ifdef WIN32 // dirty fix with win32
135 | char tmp[10] = {0};
136 | strcpy(tmp, mode);
137 | #endif
138 | if (!config_parse(file_path)) {
139 | #ifdef WIN32 // dirty fix with win32
140 | strcpy(mode, tmp);
141 | #endif
142 |
143 | #ifdef linux
144 | if (eapol_flag) { // eable 802.1x authorization
145 | if (0 != try_smart_eaplogin()) {
146 | printf("Can't finish 802.1x authorization!\n");
147 | return 1;
148 | }
149 | }
150 | #endif
151 | if (strlen(bind_ip) == 0) {
152 | memcpy(bind_ip, default_bind_ip, sizeof(default_bind_ip));
153 | }
154 | dogcom(5);
155 | } else {
156 | return 1;
157 | }
158 | #ifndef __APPLE__
159 | } else {
160 | printf("Need more options!\n\n");
161 | return 1;
162 | }
163 | #endif
164 | return 0;
165 | }
166 |
167 | void print_help(int exval) {
168 | printf("\nDrcom-generic implementation in C.\n");
169 | printf("Version: %s\n\n", VERSION);
170 |
171 | printf("Usage:\n");
172 | printf("\tdogcom -m -c [options ]...\n\n");
173 |
174 | printf("Options:\n");
175 | printf("\t--mode , -m set your dogcom mode \n");
176 | printf("\t--conf , -c import configuration file\n");
177 | printf("\t--bindip , -b bind your ip address(default is 0.0.0.0)\n");
178 | printf("\t--log , -l specify log file\n");
179 | #ifdef linux
180 | printf("\t--daemon, -d set daemon flag\n");
181 | printf("\t--802.1x, -x enable 802.1x\n");
182 | #endif
183 | printf("\t--eternal, -e set eternal flag\n");
184 | printf("\t--verbose, -v set verbose flag\n");
185 | printf("\t--help, -h display this help\n\n");
186 | exit(exval);
187 | }
188 |
189 | #ifdef linux
190 | int try_smart_eaplogin(void) {
191 | #define IFS_MAX (64)
192 | int ifcnt = IFS_MAX;
193 | iflist_t ifs[IFS_MAX];
194 | if (0 > getall_ifs(ifs, &ifcnt))
195 | return -1;
196 |
197 | for (int i = 0; i < ifcnt; ++i) {
198 | setifname(ifs[i].name);
199 | if (0 == eaplogin(drcom_config.username, drcom_config.password))
200 | return 0;
201 | }
202 | return -1;
203 | }
204 | #endif
--------------------------------------------------------------------------------
/sample-d.conf:
--------------------------------------------------------------------------------
1 | server = '192.168.1.14'
2 | username = 'a'
3 | password = 'a'
4 | CONTROLCHECKSTATUS = '\x20'
5 | ADAPTERNUM = '\x01'
6 | host_ip = '10.30.22.17'
7 | IPDOG = '\x01'
8 | host_name = 'LIYUANYUAN'
9 | PRIMARY_DNS = '114.114.114.114'
10 | dhcp_server = '0.0.0.0'
11 | AUTH_VERSION = '\x0A\x00'
12 | mac = 0xb888e3051680
13 | host_os = '8089D'
14 | KEEP_ALIVE_VERSION = '\xDC\x02'
15 | ror_version = True
16 | keepalive1_mod = True
--------------------------------------------------------------------------------
/sample-p.conf:
--------------------------------------------------------------------------------
1 | server = '192.168.1.14'
2 | pppoe_flag = '\x18'
3 | keep_alive2_flag = '\xd8'
--------------------------------------------------------------------------------