PAM (Pluggable Authentication Modules) is a flexible mechanism for authenticating users, which has become a fundamental part of system security on Linux and other Unix-like operating systems. PAM abstracts a range of authentication tasks into modular plugins that can be tailored to suit the needs of system administrators, providing a way to develop programs that are independent of authentication scheme specifics. This modularity not only enhances security but also simplifies the management of user authentication.
In today’s guide, we will delve into the process of creating a program that interacts with the Linux passwd system using PAM. By the end of this article, you’ll have a clear understanding of how to harness PAM’s capabilities to authenticate users in your own applications, ensuring secure and efficient access control.
Architecture
PAM (Pluggable Authentication Modules) employ a unique architecture that separates the specific implementation of authentication methods from the application programming interface (API).
At the heart of this design is the concept of “conversations,” a mechanism that facilitates communication between the application and the authentication modules.
This conversation-based model allows PAM to present a uniform interface to the application, regardless of the underlying authentication process.
As a result, developers can integrate various authentication technologies into their applications without having to tailor their code to each method.
Instead, they rely on PAM to handle the specifics through configurable modules, each responsible for a different aspect of the authentication process.
This abstraction not only simplifies development but also enhances the flexibility and scalability of security systems, accommodating a wide range of authentication schemes with minimal changes to core application code.
Conversation
The conversation function is the callback that PAM uses for interactivity. In this code snippet, we’re using the password of "password" to stuff into the response to use. You could use the
conversation opportunity to ask for input from the user.
You can see that there’s a preference asked for here with the message style of PAM_PROMPT_ECHO_OFF. This would be an indicator back to the user interface to not echo the user’s keystrokes
to the interface as they type.
Test Harness
We can now use this conversation function with the pam library:
The pam_start function begins the authentication conversation. The first parameter supplied
is the service, and in this case it’s set to "login". PAM uses this value and looks for
a configuration file named /etc/pam.d/login. This is pretty standard on any system. This
particular service is for the “Shadow ‘login’ service.
The username variable should contain the name of a registered user.
Our conversation function converse is supplied to pam_start via the conv variable.
We then use pam_authenticate to preform the conversation, and pam_end will do any clean up for us.
A full example
Building
In order to build this test program you need to link with pam and pam_misc.
GMP is a library that will allow you to perform calculations on numbers that extend past the reach of what your standard data sizes can hold. From their website:
GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface.
This means you can embed large math into your programs and will only be limited by the amount of memory on your running system.
In today’s article, we’ll go through a few simple examples on how to use this library.
Getting setup
Get gmp installed locally on your system either by downloading the latest release from their site, or just using your package manager.
Building
For any program that will require the gmp library, we’ll need to add the -lgmp switch:
Now, we’re ready to go.
Factorial
As an example implementation, we’ll write a program to calculate the n’th factorial for us. First of all, we’ll implement this using traditional data types offered to us through C, and then we’ll swap this out to get greater numbers.
We build this application not needing the gmp library:
We can then start to test it out.
The wheels start to fall off once we want to look at numbers higher than !19.
We overflowed our integer to where it wrapped into negative numbers. 19 is our limit for traditional data types.
Make the numbers bigger!
Now we can introduce gmp to help us break out of these constraints.
We’ll rewrite the factorial function above to operate on gmp types, and we’ll also convert the body of our program into a input capture function that will parse text into a gmp type for us.
Let’s deal with the input first:
First, you’ll notice that we’re not returning anything here. The mpz_t type is typed as an array, and as such can’t be used as a return. So, we supply it as an output parameter. This pattern will reoccur through these examples.
This function also assumes that res has already had mpz_init run on it, so it’s not magically allocating resources on your behalf.
mpz_set_ui sets the initial state of an mpz_t with a value from an integer (the real world!). mpz_set_str is really doing most of the work for us here, parsing out a string that its given into a mpz_t. The base needs to be supplied.
Now the factorial function will need to change as you’d expect:
Again, res is an output parameter with no return value. We do have a “work” variable here, so we set it up and destroy it all within the context of our function so we don’t have a memory leak.
mpz_cmp takes care of the looping for us. We’ve substituted a for loop here for a while loop to accommodate.
Full program!
Now we can use these functions in a program of our own. Here’s a full listing of an application that will give you the factorial of an arbitrary length integer.
We write the result number here with mpz_out_str. This can redirected to any stream of your choice.
Building this program is just adding the lgmp switch.
And, now you can calculate factorials as high as you like:
systemd is a set of basic tools that any system can use to build more sophisticated service applications. Using these building you can
create units which can be a:
service
socket
device
mount
automount
swap
target
path
timer
slice
scope
In today’s article, we’ll go through an example that uses service and socket to build a simple server.
Hello, World
To start with, let’s create a “Hello, World” service that will do nothing more than take your connection and send you back the string "Hello, world". First we define our service in a file. Ours is hw.service.
You can install this at ~/.config/systemd/user/hw.service.
The ExecStart holds what systemd will hand the socket connection off to. In this case, we’re going to hand the connection off to a python socket server running from our ~/tmp directory.
You can see that our requires hw.socket. It needs to be up before it will respond to requests. You install this one at ~/.config/systemd/user/hw.socket.
Our socket will listen on 7777 waiting for connections.
The serve.py mentioned in the service file is what systemd will hand the connection off to. The implementation of that server is a simple socket server:
Inside of the Handler class, in the constructor you can see that we avoid the bind and listen steps. This is because systemd has already done this for us. We’re just
going to be handed a file descriptor with the socket already attached.
That’s exactly what’s happening with fromfd here. We’re given a socket to work with via descriptor 3.
The actual implementation of our handler is not doing much more than taking in the request data, and sending back "Hello World".
Getting it installed
You can start your server listening with the following now:
You should be up and running.
Testing
You can use telnet to take a look at your server:
Alternatively, you can just use netcat:
Check that it’s working
After you’ve tested it a few times, you’ll be albe to see requests in the logs.
You should see the lines from the logging.info calls.
Cleaning up
Once you’re done and you’d like to remove these, simply stop the service, remove the units, and reload.
The find utility is an extremely flexible query tool for
your file system. It allows you to navigate and traverse your folder structures.
From the man page:
This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.
In this article, we’ll go through some usages.
Find by name
You can simply find by name with the -name switch:
You can also ignore case by switching out -name for -iname.
Find by permissions
If you’d like to find files with a specific permission (in this case 777):
You can take the converse of this with !:
Average file size
Let’s say you have a directory of text files, and you’d like to find the average size of them.
By joining find with awk, you can use the following to do just that:
Random sampling
You may need to take a random sample of files in a given folder. You can start this process off
with find, and then through clever usage of sort, and tail you can get a random sample.
This will take 500 random files from a directory:
find /the/path -type f -print0 prints out the files using \0 as a delimiter thanks to -print0.
sort is told to use \0 as its delimiter with -z and -R is to sort them randomly.
head now takes the first 500, again using \0 as the delimiter.
The Apache Parquet file format is used widely in the data space. It’s a column-oriented format that focuses on storing data as efficiently as possible, with emphasis on data retrieval.
Why?
The most common storage format that you’d use to hold data is CSV. It’s a good, simple format but has quite a number of short-comings when it comes to data analysis.
Parquet is a column-oriented format which is much more sympathetic to data analysis. CSV is row-oriented, which is a much better application for an OLTP scenario.
Parquet offers compression and partitioning that is simply not available to the CSV format.
So, it stores information - but is better suited to the data warehouse idea.
Python
A really easy way to get started using Parquet is with Python. The PyArrow library is a set of utilities that allow you to work with in-memory analytics tools. PyArrow plays nicely with Pandas and NumPy so it’s a good fit.
Make sure you have pyarrow installed as a dependency.
Create a table
First off, we’ll create a DataFrame from some raw data.
We can then use from_pandas function to create a pyarrow.Table from this DataFrame.
Basic I/O
Now that we have loaded a Table, we can save this data using write_table and pick it back up off disk using read_table.
Finishing up
There’s a lot more benefits to picking a more optimal data storage solution when working in the data analytics space.