pax_global_header00006660000000000000000000000064147161354750014527gustar00rootroot0000000000000052 comment=4e574f3ea9189e7677c7b14f557ce2212bd357f5
libpam-net-0.4/000077500000000000000000000000001471613547500134225ustar00rootroot00000000000000libpam-net-0.4/CMakeLists.txt000066400000000000000000000030401471613547500161570ustar00rootroot00000000000000cmake_minimum_required(VERSION 3.12)
project("libpam-net"
VERSION 0.3
DESCRIPTION "create/join network namespaces at login"
HOMEPAGE_URL "https://github.com/rd235/libpam-net"
LANGUAGES C)
include(GNUInstallDirs)
include(CheckIncludeFile)
add_definitions(-D_GNU_SOURCE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
unset(NLINLINE_INCLUDE CACHE)
check_include_file(nlinline.h NLINLINE_INCLUDE)
if(NOT NLINLINE_INCLUDE)
message(FATAL_ERROR "*** required nlinline.h include file not found.\nInstall nlinline: https://github.com/virtualsquare/nlinline")
endif()
if(LIBSECURITYDIR)
set(CMAKE_INSTALL_PAMDIR ${LIBSECURITYDIR})
else()
set(CMAKE_INSTALL_PAMDIR ${CMAKE_INSTALL_LIBDIR}/security)
endif()
add_library(pam_newnet SHARED pam_newnet.c pam_net_checkgroup.c)
set_target_properties(pam_newnet PROPERTIES PREFIX "")
add_library(pam_usernet SHARED pam_usernet.c pam_net_checkgroup.c)
set_target_properties(pam_usernet PROPERTIES PREFIX "")
target_compile_definitions(pam_usernet PUBLIC PAM_USERNET)
add_library(pam_groupnet SHARED pam_groupnet.c pam_net_checkgroup.c)
set_target_properties(pam_groupnet PROPERTIES PREFIX "")
target_compile_definitions(pam_groupnet PUBLIC PAM_GROUPNET)
install(TARGETS pam_newnet pam_usernet pam_groupnet
DESTINATION ${CMAKE_INSTALL_PAMDIR})
file(GLOB MAN8_PAGES ${CMAKE_CURRENT_SOURCE_DIR}/*.8)
install(FILES ${MAN8_PAGES}
DESTINATION ${CMAKE_INSTALL_MANDIR}/man8)
add_custom_target(uninstall
"${CMAKE_COMMAND}"
-DCADO_SPOOL_DIR=${CADO_SPOOL_DIR}
-P "${PROJECT_SOURCE_DIR}/Uninstall.cmake"
)
libpam-net-0.4/COPYING000066400000000000000000000431231471613547500144600ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C) 19yy
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
libpam-net-0.4/README.md000066400000000000000000000102631471613547500147030ustar00rootroot00000000000000## LIBPAM-NET: create/join network namespaces at login
**libpam-net** implements three pam modules:
- **pam_newnet.so**: users belonging to the *newnet* group get a new
network namespace at login
- **pam_usernet.so** users belonging to the *usernet* group get their own
network name at login. If a network namespace having the same name as the
username exists, pam runs the user's shell in that namespace. If such a
namespace does does not exist, it is created during the login process.
- **pam_groupnet.so** users belonging to any group starting with *groupnet-*
join the network namespace named after the dash of the group.
If the specified network namespace exists, pam runs the user shell in that
namespace. If such a namespace does does not exist, it is created during the
login process.
### INSTALL:
#### Requirements
**libpam-net** needs [**nlinline**](https://github.com/virtualsquare/nlinline).
#### Get the source code:
```
git clone https://github.com/rd235/libpam-net.git
```
#### Update the source code:
```
git pull
```
#### Compile and install
Run the following commands from the root of the source tree:
```
mkdir build
cd build
cmake .. -DLIBSECURITYDIR=/lib/x86_64-linux-gnu/security/
make
sudo make install
```
#### Configuration
Add the rules to the pam configuration files: e.g. */etc/pam.d/sshd* or
*/etc/pam.d/login*
```
session required pam_newnet.so
session required pam_usernet.so
session required pam_groupnet.so
```
Create the groups *newnet*, *usernet* and any (or none) *groupnet-**
including all the users that must be subject to one or the other service:
e.g. in /etc/group:
```
newnet:x:149:renzononet
usernet:x:150:renzousernet
groupnet-vpn:x:151:renzogroupnet
```
### Use Cases
#### Disallowing external network access for selected users
Using **pam_newnet.so** users in the *newnet* group can log-in through a network
connection (e.g. by ssh) but their processes cannot communicate with the network
at all. The only interface they can see is an isolated loopback **lo** interface
created at login time.
##### Segregated network namespaces for selected user
Using **pam_usernet.so** the system administrator can create network namespaces
for each user in the *usernet* group. Each namespace must be named after each
username.
Alternatively, using **pam_groupnet.so** the system administrator can create
different network namespaces to be shared by multiple users. e.g. for forced VPN
tunneling without affecting other users.
Users will *land* in their assigned network namespace at login. e.g. the
sysadmin can create a network namespace for user *renzousernet* as follows:
```
# ip netns add renzousernet
# ip -netns renzousernet link set dev lo up
# ip -netns renzousernet tuntap add name eth0 mode tap
# ...
```
If the directory `/etc/netns//` exists files directly underneath it are
mounted over files in `/etc`. This can be used for overriding the DNS nameserver
settings in the user's netns.
Taking *renzousernet* as an example again, this is what you'd do:
```
# cat > /etc/netns/renzousernet/resolv.conf <.
*
*/
#include
#include
#include
#include
/* check if "user" belongs to "group" */
int checkgroup(const char *user, const char *group) {
struct passwd *pw = getpwnam(user);
struct group *gr = getgrnam(group);
int ngroups=0;
if (pw == NULL) return -1;
if (gr == NULL) return 0;
if (getgrouplist(user, pw->pw_gid, NULL, &ngroups) < 0) {
gid_t gids[ngroups];
if (getgrouplist(user, pw->pw_gid, gids, &ngroups) == ngroups) {
int i;
for (i=0; igr_gid == gids[i])
return 1;
}
return 0;
}
}
return -1;
}
char *get_groupnet_netns(const char *user, const char *group) {
struct passwd *pw = getpwnam(user);
int ngroups=0;
if (pw == NULL || getgrouplist(user, pw->pw_gid, NULL, &ngroups) >= 0) {
return NULL;
}
gid_t gids[ngroups];
if (getgrouplist(user, pw->pw_gid, gids, &ngroups) != ngroups) {
return NULL;
}
int grouplen = strlen(group);
int i;
struct group *gr;
for (i=0; igr_name, group, grouplen) == 0
&& strlen(gr->gr_name) > grouplen + 1
&& gr->gr_name[grouplen] == '-') {
return strdup(gr->gr_name + grouplen + 1);
}
}
return NULL;
}
libpam-net-0.4/pam_net_checkgroup.h000066400000000000000000000007551471613547500174370ustar00rootroot00000000000000#ifndef PAM_NET_COMMON_H
#define PAM_NET_COMMON_H
/* check if "user" belongs to "group" */
/* 0=NO 1=YES -1=error */
int checkgroup(const char *user, const char *group);
/* get the name specified after a dash char '-' of "group" on a user.
* if the user is part of multiple groups starting with "group", returns data from first matching group. */
/* NULL = not found/error. pointer must be freed. */
char *get_groupnet_netns(const char *user, const char *group);
#endif //PAM_NET_COMMON_H
libpam-net-0.4/pam_newnet.8000066400000000000000000000032741471613547500156560ustar00rootroot00000000000000.TH PAM_NEWNET 8 "October 5, 2019" "VirtualSquare Labs"
.SH "NAME"
pam_newnet \- create a new network namespace at login
.SH "SYNOPSIS"
\fBpam_newnet\&.so\fR
.SH DESCRIPTION
The pam_newnet PAM module creates a new network namespace at login for users in the
\fInewnet\fR group.
Users in the \fInewnet\fR group can log-in through a
network connection (e.g. by ssh) but their processes cannot communicate.
The only interface they can see is the localhost of the namespace created
at login time.
When pam_newnet is used together with a specific \fBcado(1)\fR configuration
users can configure their own networking services. (see https://github.com/rd235/cado)
The nsutils tools, and more specfically \fBnetnsjoin(1)\fR, allow users to
assign placeholders to keep namespaces alive, assign meaningful tags for an easier management,
and later join any of their own namespaces (see https://github.com/rd235/nsutils)
.SH "OPTIONS"
.PP
\fBgroup=\fR\fB\fIgroupname\fR\fR
.RS 4
the module operates on users in the group \fIgroupname\fR instead of \fInewnet\fR.
.RE
.PP
\fBlodown\fR
.RS 4
leave the localhost \fIlo\fR interface in the state DOWN.
.RE
.SH "RETURN VALUES"
.PP
PAM_IGNORE
.RS 4
User does not belong to the \fInewnet\fR group\&.
.RE
.PP
PAM_ABORT
.RS 4
Error in retrieving the user id or in the namespace creation\&.
.RE
.PP
PAM_SUCCESS
.RS 4
Success\&.
.RE
.SH "EXAMPLES"
.PP
Add the following lines to
/etc/pam\&.d/sshd
or /etc/pam\&.d/login
.sp
.RS 8
session required pam_newnet.so
.sp
session required pam_newnet.so group=lonet lodown
.RE
.sp
.SH "SEE ALSO"
.PP
\fBpam.conf\fR(5),
\fBpam.d\fR(5),
\fBpam\fR(7)
.SH "AUTHOR"
.PP
pam_newnet was written by Renzo Davoli and Eduard Caizer, University of Bologna
libpam-net-0.4/pam_newnet.c000066400000000000000000000056051471613547500157310ustar00rootroot00000000000000/*
* pam_newnet.
* Copyright (C) 2016 Renzo Davoli, Eduard Caizer University of Bologna
*
* pam_newnet module
* create a new network namespace at each login
* (for users belonging to the "newnet" group)
*
* Cado is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see .
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULT_GROUP "newnet"
/**
* module args:
* lodown, rootshared, group=....
*/
struct pam_net_args {
const char *group;
int flags;
};
#define LODOWN 0x1
/**
* parse_argv: parse module arguments
*/
static void parse_argv(struct pam_net_args *args, int argc, const char **argv) {
for(; argc-- > 0; argv++) {
if (strcmp(*argv, "lodown") == 0)
args->flags |= LODOWN;
else if (strncmp(*argv, "group=", 6) == 0)
args->group = (*argv) + 6;
else
syslog (LOG_ERR, "Unknown option: %s", *argv);
}
}
/**
* init_log: log initialization with the given name
*/
void init_log(const char * log_name)
{
setlogmask (LOG_UPTO (LOG_NOTICE));
openlog (log_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
}
/**
* end_log: closes the log previously initialized
*/
void end_log()
{
closelog ();
}
/*
* PAM entry point for session creation
*/
int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
const char *user;
int rv;
int isnewnet;
struct pam_net_args pam_args = {
.group = DEFAULT_GROUP,
.flags = 0};
init_log ("pam_newnet");
parse_argv(&pam_args, argc, argv);
if ((rv=pam_get_user(pamh, &user, NULL) != PAM_SUCCESS)) {
syslog (LOG_ERR, "get user: %s", strerror(errno));
goto close_log_and_exit;
}
isnewnet = checkgroup(user, pam_args.group);
if (isnewnet > 0) {
if (unshare(CLONE_NEWNET) < 0) {
syslog (LOG_ERR, "Failed to create a new netns: %s", strerror(errno));
goto close_log_and_abort;
}
if ((pam_args.flags & LODOWN) == 0)
nlinline_linksetupdown(1, 1); // bring lo up
} else
rv=PAM_IGNORE;
close_log_and_exit:
end_log();
return rv;
close_log_and_abort:
rv = PAM_ABORT;
end_log();
return rv;
}
/*
* PAM entry point for session cleanup
*/
int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return(PAM_IGNORE);
}
libpam-net-0.4/pam_usernet.8000066400000000000000000000034271471613547500160430ustar00rootroot00000000000000.TH PAM_USERNET 8 "August 17, 2016" "VirtualSquare Labs"
.SH "NAME"
pam_usernet \- join the user own network namespace at login
.SH "SYNOPSIS"
\fBpam_usernet\&.so\fR
.SH DESCRIPTION
The pam_usernet PAM module allow each user in \fIusernet\fR group to have their own
network namespace.
If a network namespace having the same name as the
username exists, pam runs the user shell in that namespace. If such a
namespace does does not exist, it is created during the login process.
The system administrator can create a network
namespace for each user in \fIusernet\fR group. Each namespace must be named
after each username.
Users will get their own network namespace at
login.
When pam_usernet is used together with a specific \fBcado(1)\fR configuration
users can configure their own networking services. (see https://github.com/rd235/cado)
.SH "OPTIONS"
.PP
\fBgroup=\fR\fB\fIgroupname\fR\fR
.RS 4
the module operates on users in the group \fIgroupname\fR instead of \fIusernet\fR.
.RE
.PP
\fBlodown\fR
.RS 4
leave the localhost \fIlo\fR interface in the state DOWN.
.RE
.PP
\fBrootshared\fR
.RS 4
Leave the root filesystem \fI/\fR as shared so mounts can propagate out to the
parent namespace. Warning: this feature can create security vulnerabilities if not
properly used.
.RE
.SH "RETURN VALUES"
.PP
PAM_IGNORE
.RS 4
User does not belong to the \fIusernet\fR group\&.
.RE
.PP
PAM_ABORT
.RS 4
Error in retrieving the user id or in the namespace creation/joining\&.
.RE
.PP
PAM_SUCCESS
.RS 4
Success\&.
.RE
.SH "EXAMPLES"
.PP
Add the following line to
/etc/pam\&.d/sshd
or /etc/pam\&.d/login
.sp
.RS 8
session required pam_usernet.so
.RE
.sp
.SH "SEE ALSO"
.PP
\fBpam.conf\fR(5),
\fBpam.d\fR(5),
\fBpam\fR(7)
.SH "AUTHOR"
.PP
pam_usernet was written by Renzo Davoli and Eduard Caizer, University of Bologna
libpam-net-0.4/pam_usernet.c000066400000000000000000000237101471613547500161130ustar00rootroot00000000000000/*
* pam_usernet / pam_groupnet (shared source)
* Copyright (C) 2017-2024 Renzo Davoli University of Bologna
* Copyright (C) 2018-2019 Daniel Gröber
* Copyright (C) 2016 Renzo Davoli, Eduard Caizer University of Bologna
* Copyright (C) 2011-2017 The iproute2 Authors
*
* pam_usernet module
* provide each user with their own network
* (for users belonging to the "usernet" group)
*
* pam_groupnet module
* join users to a specific new or existing network, specified after the dash on the group name.
* (for users belonging to the "groupnet-*" group)
*
* Cado is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see .
*
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef PAM_USERNET
#define DEFAULT_GROUP "usernet"
#elif PAM_GROUPNET
#define DEFAULT_GROUP "groupnet"
#else
#error either -DPAM_USERNET or -DPAM_GROUPNET must be defined
#endif
#define NETNS_RUN_DIR "/var/run/netns/"
#define NETNS_ETC_DIR "/etc/netns"
/**
* module args:
* lodown, rootshared, group=....
*/
struct pam_net_args {
const char *group;
int flags;
};
#define LODOWN 0x1
#define ROOTSHARED 0x2
/**
* parse_argv: parse module arguments
*/
static void parse_argv(struct pam_net_args *args, int argc, const char **argv) {
for(; argc-- > 0; argv++) {
if (strcmp(*argv, "lodown") == 0)
args->flags |= LODOWN;
else if (strcmp(*argv, "rootshared") == 0)
args->flags |= ROOTSHARED;
else if (strncmp(*argv, "group=", 6) == 0)
args->group = (*argv) + 6;
else
syslog (LOG_ERR, "Unknown option: %s", *argv);
}
}
/**
* init_log: log initialization with the given name
*/
void init_log(const char * log_name)
{
setlogmask (LOG_UPTO (LOG_NOTICE));
openlog (log_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
}
/**
* end_log: closes the log previously initialized
*/
void end_log()
{
closelog ();
}
/**
* bind_etc: Mount config files from /etc/netns// into current namespace.
*/
int bind_etc(const char *name, int flags)
{
int rv = 0;
char etc_netns_path[sizeof(NETNS_ETC_DIR) + NAME_MAX];
char netns_name[PATH_MAX];
char etc_name[PATH_MAX];
struct dirent *entry;
DIR *dir;
if (flags & ROOTSHARED) {
/* ROOTSHARED */
/* Make /etc a mount point, so we can apply a propagation policy to it
* below */
rv = mount("/etc", "/etc", "none", MS_BIND, NULL);
if (rv == -1) {
syslog (LOG_ERR, "mount --bind %s %s: %s",
etc_netns_path, etc_netns_path, strerror(errno));
return -1;
}
/* Don't let bind mounts from /etc/netns// -> /etc/
* propagate back to the parent namespace */
if (mount("", "/etc", "none", MS_PRIVATE, NULL)) {
syslog (LOG_ERR, "\"mount --make-private /%s\" failed: %s\n",
etc_netns_path, strerror(errno));
return -1;
}
}
snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
dir = opendir(etc_netns_path);
if (!dir)
return errno == ENOENT ? 0 : -1;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0)
continue;
if (strcmp(entry->d_name, "..") == 0)
continue;
snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
syslog (LOG_ERR, "Bind %s -> %s failed: %s",
netns_name, etc_name, strerror(errno));
}
}
closedir(dir);
return 0;
}
/**
* remount_sys: Mount a version of /sys that describes the new network namespace
*/
int remount_sys(const char *name, int flags)
{
unsigned long mountflags = MS_NOSUID | MS_NOEXEC | MS_NODEV;
if ((flags & ROOTSHARED) == 0) {
/* DEFAULT behavior: NOT ROOTSHARED */
/* Don't let any mounts propagate back to the parent */
if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
strerror(errno));
return -1;
}
} else {
/* ROOTSHARED */
/* Make /sys private so the remounting below doesn't
* propagate to the parent namespace, since we're leaving
* the root directory shared */
if (mount("", "/sys", "none", MS_PRIVATE | MS_REC, NULL)) {
syslog (LOG_ERR, "\"mount --make-rprivate /sys\" failed: %s\n",
strerror(errno));
return -1;
}
}
/* Mount a version of /sys that describes the network namespace */
if (umount2("/sys", MNT_DETACH) < 0) {
struct statvfs fsstat;
/* If this fails, perhaps there wasn't a sysfs instance
* mounted. Good. */
if (statvfs("/sys", &fsstat) == 0) {
/* We couldn't umount the sysfs, we'll attempt to
* overlay it. A read-only instance can't be shadowed
* with a read-write one. */
if (fsstat.f_flag & ST_RDONLY)
mountflags |= MS_RDONLY;
}
}
if (mount(name, "/sys", "sysfs", mountflags, NULL) < 0) {
syslog (LOG_ERR, "mount of /sys failed: %s", strerror(errno));
return -1;
}
if (mount("cgroup2", "/sys/fs/cgroup", "cgroup2", mountflags, NULL) < 0) {
syslog (LOG_ERR, "mount of /sys/fs/cgroup failed: %s", strerror(errno));
return -1;
}
return 0;
}
/**
* create_netns_rundir: Create /var/run/netns mount if it doesn't exist yet.
*/
int create_netns_rundir(void)
{
int rv = 0;
rv = mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
if (rv == -1 && errno != EEXIST) {
syslog (LOG_ERR, "cannot create netns dir %s: %s",
NETNS_RUN_DIR, strerror(errno));
return -1;
}
rv = mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL);
if (rv == 0) {
return 0;
}
if (errno != EINVAL) {
syslog (LOG_ERR, "mount --make-shared %s: %s",
NETNS_RUN_DIR, strerror(errno));
return -1;
}
rv = mount(NETNS_RUN_DIR, NETNS_RUN_DIR, "none", MS_BIND, NULL);
if (rv == -1) {
syslog (LOG_ERR, "mount --bind %s: %s",
NETNS_RUN_DIR, strerror(errno));
return -1;
}
rv = mount("", NETNS_RUN_DIR, "none", MS_SHARED | MS_REC, NULL);
if (rv == -1) {
syslog (LOG_ERR, "mount --make-shared after bind %s: %s",
NETNS_RUN_DIR, strerror(errno));
return -1;
}
return 0;
}
/**
* unshare_netns: Create new netns, including mounting the handle to ns_path.
*/
int unshare_netns(char *ns_path, int flags)
{
int rv;
int nsfd;
nsfd = open(ns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
if (nsfd < 0) {
syslog (LOG_ERR, "cannot create netns %s: %s",
ns_path, strerror(errno));
return -1;
}
close(nsfd);
rv = unshare(CLONE_NEWNET);
if (rv < 0) {
syslog (LOG_ERR, "Failed to create a new netns %s: %s",
ns_path, strerror(errno));
return -1;
}
rv = mount("/proc/self/ns/net", ns_path, "none", MS_BIND, NULL);
if (rv == -1) {
syslog (LOG_ERR, "mount /proc/self/ns/net -> %s failed: %s",
ns_path, strerror(errno));
return -1;
}
if ((flags & LODOWN) == 0)
nlinline_linksetupdown(1, 1); // bring lo up
return nsfd;
}
/**
* enter_netns: Ensure we are in the netns referred to by ns_path, either by
* creating it or entering it if it already exists.
*/
int enter_netns(char *ns_path, int flags)
{
int nsfd;
nsfd = open(ns_path, O_RDONLY);
if (nsfd < 0) {
if (errno == ENOENT) {
unshare_netns(ns_path, flags);
} else {
syslog (LOG_ERR, "netns open failed %s", ns_path);
return -1;
}
} else {
if (setns(nsfd, CLONE_NEWNET) != 0) {
syslog (LOG_ERR, "cannot join netns %s: %s",
ns_path, strerror(errno));
close(nsfd);
return -1;
}
close(nsfd);
}
return 0;
}
/*
* PAM entry point for session creation
*/
int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
const char *user;
int rv;
char *target_netns = NULL;
char ns_path[PATH_MAX];
struct pam_net_args pam_args = {
.group = DEFAULT_GROUP,
.flags = 0};
#ifdef PAM_USERNET
init_log ("pam_usernet");
#elif PAM_GROUPNET
init_log ("pam_groupnet");
#endif
parse_argv(&pam_args, argc, argv);
if ((rv=pam_get_user(pamh, &user, NULL) != PAM_SUCCESS)) {
syslog (LOG_ERR, "get user: %s", strerror(errno));
end_log();
return PAM_SUCCESS;
}
#ifdef PAM_USERNET
if (checkgroup(user, pam_args.group) > 0)
target_netns = (char *) user;
#elif PAM_GROUPNET
target_netns = get_groupnet_netns(user, pam_args.group);
#endif
if(target_netns == NULL) {
end_log();
return PAM_IGNORE;
}
if (create_netns_rundir() == -1)
goto close_log_and_abort;
snprintf(ns_path, sizeof(ns_path), "%s/%s", NETNS_RUN_DIR, target_netns);
rv = enter_netns(ns_path, pam_args.flags);
if(rv == -1)
goto close_log_and_abort;
if (unshare(CLONE_NEWNS) < 0) {
syslog (LOG_ERR, "unshare(mount) failed: %s", strerror(errno));
goto close_log_and_abort;
}
if(remount_sys(target_netns, pam_args.flags) == -1) {
syslog (LOG_ERR, "remounting /sys failed");
goto close_log_and_abort;
}
/* Setup bind mounts for config files in /etc */
if(bind_etc(target_netns, pam_args.flags) == -1) {
syslog (LOG_ERR, "mounting /etc/netns/%s config files failed",
target_netns);
goto close_log_and_abort;
}
#ifdef PAM_GROUPNET
free(target_netns);
#endif
end_log();
return PAM_SUCCESS;
close_log_and_abort:
#ifdef PAM_GROUPNET
free(target_netns);
#endif
end_log();
return PAM_ABORT;
}
/*
* PAM entry point for session cleanup
*/
int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return(PAM_IGNORE);
}