/* * share.c -- MCDE: the shared folders, for SMB, NFS and AFP alike * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The list is /etc/mcde-shares, one absolute path a line. From it MCDE * writes one block into each server's configuration, between the lines * "# MCDE shares begin" and "# MCDE shares end", and leaves the rest of the * file as the administrator wrote it: * * exports(5) a line per folder, as the backend words it * (src/usr.sbin/mountd/exports.5; Linux: exports(5)); * smb.conf a section per folder: path, read only = no; * afp.conf a section per folder: path (netatalk's afp.conf(5)). * * The files are written beside the old ones and renamed over them. Where * a server is not installed its file is left alone. Running servers read * their configuration again (the backend's mcde_sys_shares_reload). * docs/reference/sharing.txt in the Makronix tree. * * POSIX (ext/src/mcde/AGENTS.md). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include "private.h" #define SHARES_FILE "/etc/mcde-shares" #define BEGIN_LINE "# MCDE shares begin" #define END_LINE "# MCDE shares end" int mcde_sys_shares(char (*paths)[MCDE_SYS_SHARE_LEN], size_t max) { FILE *fp = fopen(SHARES_FILE, "r"); char line[MCDE_SYS_SHARE_LEN + 8]; size_t n = 0; if (fp == NULL) return 0; while (n < max && fgets(line, sizeof line, fp) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (line[0] != '/') continue; snprintf(paths[n++], MCDE_SYS_SHARE_LEN, "%s", line); } fclose(fp); return (int) n; } /* the last part of PATH, for a section's name: "Public" */ static void ShareName(const char *path, char *buf, size_t len) { const char *base = strrchr(path, '/'); size_t i, n = 0; base = base != NULL && base[1] != '\0' ? base + 1 : path; /* a section name has no brackets */ for (i = 0; base[i] != '\0' && n + 1 < len; i++) if (base[i] != '[' && base[i] != ']') buf[n++] = base[i]; buf[n] = '\0'; } typedef enum { FORM_EXPORTS, FORM_SMB, FORM_AFP } Form; static void WriteBlock(FILE *to, Form form, char (*paths)[MCDE_SYS_SHARE_LEN], size_t n) { char name[MCDE_SYS_SHARE_LEN], line[MCDE_SYS_SHARE_LEN * 2]; size_t i; fprintf(to, "%s (System Settings, Sharing; edited there)\n", BEGIN_LINE); for (i = 0; i < n; i++) { ShareName(paths[i], name, sizeof name); switch (form) { case FORM_EXPORTS: mcde_sys_export_line(paths[i], line, sizeof line); fprintf(to, "%s\n", line); break; case FORM_SMB: fprintf(to, "[%s]\n\tpath = %s\n\tread only = no\n" "\tbrowseable = yes\n", name, paths[i]); break; case FORM_AFP: fprintf(to, "[%s]\n\tpath = %s\n", name, paths[i]); break; } } fprintf(to, "%s\n", END_LINE); } /* FILE with its MCDE block replaced (or added at the end) */ static int Rewrite(const char *file, Form form, char (*paths)[MCDE_SYS_SHARE_LEN], size_t n) { char tmp[512], line[2048]; FILE *from, *to; struct stat st; int inside = 0, written = 0; if (file == NULL) return MCDE_SYS_OK; snprintf(tmp, sizeof tmp, "%s.mcde", file); if ((to = fopen(tmp, "w")) == NULL) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_WRITE_BESIDE, "Cannot write beside %s: %s."), file, strerror(errno)); if ((from = fopen(file, "r")) != NULL) { while (fgets(line, sizeof line, from) != NULL) { if (strncmp(line, BEGIN_LINE, strlen(BEGIN_LINE)) == 0) { inside = 1; continue; } if (inside) { if (strncmp(line, END_LINE, strlen(END_LINE)) == 0) { inside = 0; WriteBlock(to, form, paths, n); written = 1; } continue; } fputs(line, to); } fclose(from); } if (!written) WriteBlock(to, form, paths, n); if (fclose(to) != 0) { unlink(tmp); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_WRITE, "Cannot write %s."), file); } if (stat(file, &st) == 0) (void) chmod(tmp, st.st_mode & 07777); else (void) chmod(tmp, 0644); if (rename(tmp, file) != 0) { unlink(tmp); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_REPLACE, "Cannot replace %s: %s."), file, strerror(errno)); } return MCDE_SYS_OK; } int mcde_sys_shares_set(char (*paths)[MCDE_SYS_SHARE_LEN], size_t n) { FILE *fp; size_t i; int r; if (geteuid() != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_SHR_EPERM, "Only the administrator can change what is shared.")); for (i = 0; i < n; i++) { struct stat st; if (paths[i][0] != '/' || strpbrk(paths[i], "\n\"") != NULL || stat(paths[i], &st) != 0 || !S_ISDIR(st.st_mode)) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_SHR_NOT_FOLDER, "\"%s\" is no folder that can be shared."), paths[i]); } if ((fp = fopen(SHARES_FILE ".new", "w")) == NULL) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_WRITE, "Cannot write %s."), SHARES_FILE); for (i = 0; i < n; i++) fprintf(fp, "%s\n", paths[i]); if (fclose(fp) != 0 || rename(SHARES_FILE ".new", SHARES_FILE) != 0) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_WRITE, "Cannot write %s."), SHARES_FILE); if ((r = Rewrite(mcde_sys_exports_file(), FORM_EXPORTS, paths, n)) != MCDE_SYS_OK || (r = Rewrite(mcde_sys_smb_conf(), FORM_SMB, paths, n)) != MCDE_SYS_OK || (r = Rewrite(mcde_sys_afp_conf(), FORM_AFP, paths, n)) != MCDE_SYS_OK) return r; mcde_sys_shares_reload(); return MCDE_SYS_OK; }