autofs-5.1.9 - add cifs mount module

From: Ian Kent <raven@themaw.net>

Add a mount module for cifs mounts to allow Kerbreros map entries to
have negative caching disabled so authentication failures don't prevent
a subsequent mount request from succeeding. Also do some additional
validation before passing it on the the generic mount module.

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG            |    1 
 daemon/mount.c       |    2 -
 modules/Makefile     |    6 +-
 modules/mount_cifs.c |  151 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 156 insertions(+), 4 deletions(-)
 create mode 100644 modules/mount_cifs.c

diff --git a/CHANGELOG b/CHANGELOG
index f2d13424a..f167f4a3e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -103,6 +103,7 @@
 - don't open code negative update in lookup.
 - make negative timeout check consistent.
 - add ability to disable negative cache.
+- add cifs mount module.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/mount.c b/daemon/mount.c
index 4031eda62..8327249c4 100644
--- a/daemon/mount.c
+++ b/daemon/mount.c
@@ -27,7 +27,7 @@
 /* These filesystems are known not to work with the "generic" module */
 /* Note: starting with Samba 2.0.6, smbfs is handled generically.    */
 static char *not_generic[] = { "nfs", "userfs", "afs", "autofs",
-			       "changer", "bind", NULL
+			       "changer", "bind", "cifs", NULL
 };
 
 int do_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
diff --git a/modules/Makefile b/modules/Makefile
index 6908da062..0b9540da6 100644
--- a/modules/Makefile
+++ b/modules/Makefile
@@ -9,13 +9,13 @@ SRCS :=	lookup_file.c lookup_program.c  lookup_userhome.c \
 	lookup_multi.c lookup_hosts.c lookup_dir.c \
 	parse_sun.c parse_amd.c \
 	mount_generic.c  mount_nfs.c  mount_afs.c  mount_autofs.c \
-	mount_changer.c  mount_bind.c
+	mount_changer.c  mount_bind.c mount_cifs.c
 
 MODS :=	lookup_file.so lookup_program.so lookup_userhome.so \
 	lookup_multi.so lookup_hosts.so lookup_dir.so \
 	parse_sun.so parse_amd.so \
-	mount_generic.so mount_nfs.so mount_afs.so mount_autofs.so \
-	mount_changer.so mount_bind.so
+	mount_generic.so mount_nfs.so  mount_afs.so mount_autofs.so \
+	mount_changer.so mount_bind.so mount_cifs.so
 
 YACCSRC = amd_tok.c amd_parse.tab.c amd_parse.tab.h
 
diff --git a/modules/mount_cifs.c b/modules/mount_cifs.c
new file mode 100644
index 000000000..43a313174
--- /dev/null
+++ b/modules/mount_cifs.c
@@ -0,0 +1,151 @@
+/* ----------------------------------------------------------------------- *
+ *
+ *  mount_cifs.c      - module to mount a cifs filesystem.
+ *
+ *   Copyright 2026 Ian Kent <raven@themaw.net>
+ *   Copyright 2026 Red Hat, Inc.
+ *
+ *   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, Inc., 675 Mass Ave, Cambridge MA 02139,
+ *   USA; either version 2 of the License, or (at your option) any later
+ *   version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include <stdio.h>
+#include <malloc.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#define MODULE_MOUNT
+#include "automount.h"
+
+#define MODPREFIX "mount(cifs): "
+
+int mount_version = AUTOFS_MOUNT_VERSION;	/* Required by protocol */
+
+static struct mount_mod *mount_generic = NULL;
+static int init_ctr = 0;
+
+int mount_init(void **context)
+{
+	/* Make sure we have the local mount method available */
+	if (!mount_generic) {
+		if ((mount_generic = open_mount("generic", MODPREFIX)))
+			init_ctr++;
+	} else
+		init_ctr++;
+
+	return !mount_generic;
+}
+
+int mount_reinit(void **context)
+{
+	return 0;
+}
+
+int mount_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
+		const char *what, const char *fstype, const char *options,
+		void *context)
+{
+	void (*mountlog)(unsigned int, const char*, ...) = &log_debug;
+	char *cifsoptions = NULL;
+	struct mapent *me;
+	int err = 1;
+
+	if (ap->flags & MOUNT_FLAG_REMOUNT)
+		return 0;
+
+	if (defaults_get_mount_verbose())
+		mountlog = &log_info;
+
+	mountlog(ap->logopt,
+		 MODPREFIX "root=%s name=%s what=%s, fstype=%s, options=%s",
+		 root, name, what, fstype, options);
+
+	if (options) {
+		const char *comma;
+		char *opts;
+		int krb5 = 0;
+		int cruid = 0;
+		int opts_len;
+
+		opts_len = strlen(options) + 1;
+		opts = cifsoptions = malloc(opts_len + 1);
+		if (!cifsoptions)
+			return 1;
+
+		memset(cifsoptions, '\0', opts_len + 1);
+
+		for (comma = options; *comma != '\0';) {
+			const char *cp;
+			const char *end;
+
+			while (*comma == ',')
+				comma++;
+
+			/* Skip leading white space */
+			while (*comma == ' ' || *comma == '\t')
+				comma++;
+
+			cp = comma;
+			while (*comma != '\0' && *comma != ',')
+				comma++;
+
+			/* Skip trailing white space */
+			end = comma - 1;
+			while (*comma == ' ' || *comma == '\t')
+				end--;
+
+			opts_len = end - cp + 1;
+
+			if (_strncmp("sec=krb5", cp, opts_len) == 0 ||
+			    _strncmp("sec=krb5i", cp, opts_len) == 0)
+				krb5 = 1;
+			else if (strstr(cp, "cruid=") == cp)
+				cruid = 1;
+
+			memcpy(opts, cp, comma - cp + 1);
+			opts += comma - cp + 1;
+		}
+
+		if (krb5 && !cruid) {
+			error(ap->logopt, MODPREFIX
+				"Kerberos option requires the cruid option");
+			goto out;
+		}
+
+		if (krb5) {
+			me = lookup_source_mapent(ap, name, LKP_DISTINCT);
+			if (me) {
+				error(ap->logopt, MODPREFIX
+				      "failed to locate map entry for mount");
+				goto out;
+			}
+			cache_disable_negative(me);
+		}
+
+		mountlog(ap->logopt, MODPREFIX "cifs options=\"%s\"", cifsoptions);
+	}
+
+	err = mount_generic->mount_mount(ap, root, name, name_len,
+			       what, "cifs", cifsoptions, mount_generic->context);
+out:
+	if (cifsoptions)
+		free(cifsoptions);
+	return err;
+}
+
+int mount_done(void *context)
+{
+	int rv = 0;
+
+	if (--init_ctr == 0) {
+		rv = close_mount(mount_generic);
+		mount_generic = NULL;
+	}
+	return rv;
+}
