Skip to content

Commit

Permalink
Merge pull request #1 from AnilAntari/1.1-1
Browse files Browse the repository at this point in the history
The ability to specify credentials for connecting via SMB. Added NFS
  • Loading branch information
AnilAntari authored Jan 16, 2025
2 parents 4e4f7d2 + d728e10 commit 4757b54
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 266 deletions.
76 changes: 64 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# autofs-config

autofs-config is a script that automates the configuration of AutoFS using YAML files.

At the moment, autofs-config can only configure connectivity for network resources using the smb protocol.
autofs-config is a script that automates the process of configuring AutoFS using YAML format files. It is able to configure network folders running on SMB and NFS protocols.

Superuser rights are required to run.

Expand All @@ -12,15 +10,28 @@ OR
sudo afs-cf
```

Usage examples:

```
sudo afs-cf
OR
sudo afs-cf -a -p --file /path_to_yaml_file
# The -a and -p options are responsible for configuring the file for mounting network
# resources and the file with credentials for connection.
```

## Tools and libraries

* Perl
* YAML
* AutoFS
* cifs-utils
* nfs-common

## YAML file structure

### Samba

```yaml
---
mount:
Expand All @@ -30,6 +41,8 @@ mount:
autofs_mount_options:
- timeout=360
- browse
autofs_mount_passwd: password
autofs_mount_username: username
credentials_file: /etc/autofs/secret_file
fstype: cifs
name: share
Expand All @@ -43,20 +56,59 @@ mount:
- noperm
```
**autofs_mount_config** is used to determine where the settings file for the network folder is located.
* **autofs_mount_config** is used to determine where the settings file for the network folder is located.
* **autofs_mount_directory** is used to specify which directory the network folder will be mounted in.
* **autofs_mount_master_config** is used to indicate where auto.master is located.
* **autofs_mount_options** is used to specify the options to be added to auto.master.
* **autofs_mount_passwd** and **autofs_mount_username** are optional parameters that store the password and username to automatically create a credential file.
* **credentials_file points** to the file where the credentials are stored.
* **fstype** indicates which protocol will be used.
* **name** specifies the name of the directory when the network folder is attached to the file system.
* **network_paths** describes the following parameters: the name of the network folder to be created, after **autofs_mount_directory** and **name**, the ip address and the name of the network folder that will connect.
* **options** describes the options to be used in **autofs_mount_config**.
### NFS
```yaml
---
mount:
autofs_mount_config: /etc/autofs/documents
autofs_mount_directory: /mnt
autofs_mount_master_config: /etc/auto.master
autofs_mount_options:
- timeout=60
- browse
fstype: nfs
name: share
network_paths:
- ip: 192.168.122.204
share: /srv/nfsshare
options:
- rw
- soft
```
**autofs_mount_directory** is used to specify which directory the network folder will be mounted in.
* **autofs_mount_config** is used to determine where the settings file for the network folder is located.
**autofs_mount_master_config** is used to indicate where auto.master is located.
* **autofs_mount_directory** is used to specify which directory the network folder will be mounted in.
**autofs_mount_options** is used to specify the options to be added to auto.master.
* **autofs_mount_master_config** is used to indicate where auto.master is located.
**credentials_file points** to the file where the credentials are stored.
* **autofs_mount_options** is used to specify the options to be added to auto.master.
**fstype** indicates which protocol will be used.
* **fstype** indicates which protocol will be used.
**name** specifies the name of the directory when the network folder is attached to the file system.
* **name** specifies the name of the directory when the network folder is attached to the file system.
**network_paths** describes the following parameters: the name of the network folder to be created, after **autofs_mount_directory** and **name**, the ip address and the name of the network folder that will connect.
* **network_paths** describes the following parameters: the name of the network folder to be created, after **autofs_mount_directory** and **name**, the ip address and the name of the network folder that will connect.
**options** describes the options to be used in **autofs_mount_config**.
* **options** describes the options to be used in **autofs_mount_config**.
76 changes: 34 additions & 42 deletions afs-cf
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,45 @@
use strict;
use warnings;
use lib "modules";
use Getopt::Long;

use AuthFile qw(create_auth_file);
use YamlConfig qw(create_yaml_config generate_mount_command);
use YamlEditor qw(edit_yaml_config);

sub main_menu {

my $menu = << 'EOF';
1. Creating a new YAML file;
2. Edit an existing YAML file;
3. Creating a file to mount using a YAML file;
4. Creating a file with credentials;
5. Exit.
EOF
print $menu;
print "Choose an action (1/2/3/4/5): ";
my $choice = <STDIN>;
chomp $choice;

if ($choice == 1) {
create_yaml_config();
}
elsif ($choice == 2) {
edit_yaml_config();
}
elsif ($choice == 3) {
generate_mount_command();
}
elsif ($choice == 4) {
create_auth_file();
}
elsif ($choice == 5) {
print "Exiting the program...\n";
exit 0;
} else {
print "Wrong choice! Please select an action from the list..\n";
}
}

if (@ARGV == 0) {
main_menu();
}
my $yaml_file;
my $mount_flag = 0;
my $auth_flag = 0;
my $create_flag = 0;
my $help_flag = 0;

GetOptions(
'a' => \$mount_flag,
'p' => \$auth_flag,
'c' => \$create_flag,
'file=s' => \$yaml_file,
'help' => \$help_flag,
) or die "Invalid options passed to $0\n";

my $arg = shift @ARGV;
my $yaml_file = shift @ARGV;
if ($arg && $arg eq '-a') {
if ($mount_flag) {
generate_mount_command($yaml_file);
} elsif ($arg && $arg eq '-p') {
}

if ($auth_flag) {
create_auth_file($yaml_file);
}

if ($create_flag) {
create_yaml_config();
}

if ($help_flag) {
my $help = <<END_MESSAGE;
-a Create configuration files for the connection.
-p Create a file with credentials for connecting to a network resource (for SMB).
-c Create a YAML configuration file.
-f|--file=<filename> Specify the YAML file to use.
-h|--help Display this help message.
END_MESSAGE
print($help);
exit 0;
}
37 changes: 12 additions & 25 deletions modules/AuthFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,36 @@ use strict;
use warnings;
use YAML;
use Exporter;
use File::Basename;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(create_auth_file);
our @EXPORT_OK = qw(create_auth_file);

sub create_auth_file {

my ($yaml_file) = @_;

unless (defined $yaml_file && $yaml_file ne '') {
unless ($yaml_file && -f $yaml_file) {
print "Enter the path to the YAML file to read the configuration: ";
$yaml_file = <STDIN>;
chomp $yaml_file;
} else {
chomp $yaml_file;
}

my $config = YAML::LoadFile($yaml_file);
my $mount = $config->{mount} or die "There is no information about mounting in the YAML file!\n";

my $file = $mount->{credentials_file};

if (-e $file) {
print "The $file already exists! Do you want to overwrite it? (y/n): ";
my $overwrite = <STDIN>;
chomp $overwrite;
if ($overwrite ne 'y') {
print "Refusal to overwrite a file.\n";
return;
}
my $config = eval { YAML::LoadFile($yaml_file) };
if ($@) {
die "Error reading YAML file: $@\n";
}

print "Enter the username for CIFS: ";
my $username = <STDIN>;
chomp $username;
my $mount = $config->{mount} or die "There is no information about mounting in the YAML file!\n";

print "Enter the password for CIFS: ";
my $password = <STDIN>;
chomp $password;
my $username = $mount->{autofs_mount_username};
my $password = $mount->{autofs_mount_passwd};

my $file = $mount->{credentials_file};
my $content = "username=$username\n";
$content .= "password=$password\n";

open my $fh, '>', $file or die "Failed to create a $file: $!\n";
open my $fh, '>', $file or die "Failed to create the $file: $!\n";
print $fh $content;
close $fh;

Expand All @@ -55,4 +42,4 @@ sub create_auth_file {
print "The $file file has been successfully created with the necessary data.\n";
}

1;
1;
Loading

0 comments on commit 4757b54

Please sign in to comment.