/* ncpumount - Disconnects from permanent ncp connections Copyright (c) 1995 by Volker Lendecke Copyright (c) 2001 by Patrick Pollet Copyright (c) 2002 by Petr Vandrovec 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Revision history: 0.00 1995 Volker Lendecke Initial revision. 1.00 1999, November 20 Petr Vandrovec Added license. 0.00(ncplogout) 2001, Feb 27 Patrick Pollet Initial revision. 1.00(ncplogout) 2001, March 10 Patrick Pollet Added test for installation as suidroot Added emission of word "failed" in every error line to make parsing easier from TCL/tk Corrected #218: testing memory after malloc() Corrected #238: NWCXIsSameServer() should not be called in theServer=NULL) Corrected #264: should not ignore error from NWCXGetPermConnInfo() Corrected #365: if -a, no -T nor -S allowed no -T and -S option Corrected #368: use a #define instead of 125 128 ... 1.02 2002, January 20 Petr Vandrovec Merged ncpumount and ncplogout, polished... /etc/mtab~ lock is now always removed when failure occurs with lock held. Use lockf() on /etc/mtab~ like util-linux do, and retry lock attempt couple of times. */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "private/libintl.h" #define _(X) X #ifndef MS_REC #define MS_REC 16384 #endif #ifndef MS_SLAVE #define MS_SLAVE (1<<19) #endif static char *progname; static int is_ncplogout = 0; uid_t uid; static void usage(void) { printf(_("usage: %s [options]\n"), progname); printf(_("usage: %s mount-point\n"), progname); } static void help(void) { printf(_("\n" "usage: %s [options]\n" " %s mount-point\n"), progname, progname); printf(_("\n" "mount-point Disconnect specified mount-point\n" "-a Disconnect all my connections (NDS and Bindery)\n" "-S servername Disconnect all connections (NDS and/or Bindery) to specified\n" " server\n" "-T treename Disconnect all NDS connections to specified tree\n" "-g Disconnect all connections to Netware for all users! (only\n" " root can use this flag)\n" "-h Print this help text\n" "\n")); } static void veprintf(const char* message, va_list ap) { if (is_ncplogout) { fprintf(stderr, "failed:"); } vfprintf(stderr, message, ap); } static void eprintf(const char* message, ...) { va_list ap; va_start(ap, message); veprintf(message, ap); va_end(ap); } /* Mostly copied from ncpm_common.c */ void block_sigs(void) { sigset_t mask, orig_mask; sigfillset(&mask); sigdelset(&mask, SIGALRM); /* Need SIGALRM for ncpumount */ if(setresuid(0, 0, uid) < 0) { eprintf("Failed to raise privileges.\n"); exit(-1); } if(sigprocmask(SIG_SETMASK, &mask, &orig_mask) < 0) { eprintf("Blocking signals failed.\n"); exit(-1); } } void unblock_sigs(void) { sigset_t mask, orig_mask; sigemptyset(&mask); if(setresuid(uid, uid, 0) < 0) { eprintf("Failed to drop privileges.\n"); exit(-1); } if(sigprocmask(SIG_SETMASK, &mask, &orig_mask) < 0) { eprintf("Un-blocking signals failed.\n"); exit(-1); } } static void alarmSignal(int sig) { (void)sig; } static void enableAlarm(void) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = alarmSignal; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGALRM, &sa, NULL); sigemptyset(&sa.sa_mask); sigaddset(&sa.sa_mask, SIGALRM); sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL); } int ncp_mnt_umount(const char *abs_mnt, const char *rel_mnt) { if (umount(rel_mnt) != 0) { eprintf(_("Could not umount %s: %s\n"), abs_mnt, strerror(errno)); return -1; } return 0; } static int check_is_mount_child(void *p) { const char **a = p; const char *last = a[0]; const char *mnt = a[1]; int res; const char *procmounts = "/proc/mounts"; int found; FILE *fp; struct mntent *entp; res = mount("", "/", "", MS_SLAVE | MS_REC, NULL); if (res == -1) { eprintf(_("Failed to mark mounts slave: %s\n"), strerror(errno)); return 1; } res = mount(".", "/tmp", "", MS_BIND | MS_REC, NULL); if (res == -1) { eprintf(_("Failed to bind parent to /tmp: %s\n"), strerror(errno)); return 1; } fp = setmntent(procmounts, "r"); if (fp == NULL) { eprintf(_("Failed to open %s: %s\n"), procmounts, strerror(errno)); return 1; } found = 0; while ((entp = getmntent(fp)) != NULL) { if (strncmp(entp->mnt_dir, "/tmp/", 5) == 0 && strcmp(entp->mnt_dir + 5, last) == 0) { found = 1; break; } } endmntent(fp); if (!found) { eprintf(_("%s not mounted\n"), mnt); return 1; } return 0; } static int check_is_mount(const char *last, const char *mnt) { char buf[131072]; pid_t pid, p; int status; const char *a[2] = { last, mnt }; pid = clone(check_is_mount_child, buf + 65536, CLONE_NEWNS, (void *) a); if (pid == (pid_t) -1) { eprintf(_("Failed to clone namespace: %s\n"), strerror(errno)); return -1; } p = waitpid(pid, &status, __WCLONE); if (p == (pid_t) -1) { eprintf(_("Waitpid failed: %s\n"), strerror(errno)); return -1; } if (!WIFEXITED(status)) { eprintf(_("Child terminated abnormally (status %i)\n"), status); return -1; } if (WEXITSTATUS(status) != 0) return -1; return 0; } static int chdir_to_parent(char *copy, const char **lastp, int *currdir_fd) { char *tmp; const char *parent; char buf[PATH_MAX]; int res; tmp = strrchr(copy, '/'); if (tmp == NULL || tmp[1] == '\0') { eprintf(_("Internal error: invalid abs path: <%s>\n"), copy); return -1; } if (tmp != copy) { *tmp = '\0'; parent = copy; *lastp = tmp + 1; } else if (tmp[1] != '\0') { *lastp = tmp + 1; parent = "/"; } else { *lastp = "."; parent = "/"; } *currdir_fd = open(".", O_RDONLY); if (*currdir_fd == -1) { eprintf(_("Failed to open current directory: %s\n"), strerror(errno)); return -1; } res = chdir(parent); if (res == -1) { eprintf(_("Failed to chdir to %s: %s\n"), parent, strerror(errno)); return -1; } if (getcwd(buf, sizeof(buf)) == NULL) { eprintf(_("Failed to obtain current directory: %s\n"), strerror(errno)); return -1; } if (strcmp(buf, parent) != 0) { eprintf(_("Mountpoint moved (%s -> %s)\n"), parent, buf); return -1; } return 0; } static int unmount_ncp(const char *mount_point) { int currdir_fd = -1; char *copy; const char *last; int res; copy = strdup(mount_point); if (copy == NULL) { eprintf(_("Failed to allocate memory\n")); return -1; } res = chdir_to_parent(copy, &last, &currdir_fd); if (res == -1) goto out; res = check_is_mount(last, mount_point); if (res == -1) goto out; res = ncp_mnt_umount(mount_point, last); out: free(copy); if (currdir_fd != -1) { fchdir(currdir_fd); close(currdir_fd); } return res; } static int do_umount(const char *mount_point) { int fid = open(mount_point, O_RDONLY, 0); uid_t mount_uid; int res; if (fid == -1) { eprintf(_("Invalid or unauthorized mountpoint %s\n"), mount_point); return -1; } if (ncp_get_mount_uid(fid, &mount_uid) != 0) { close(fid); eprintf(_("Invalid or unauthorized mountpoint %s\n"), mount_point); return -1; } if ((getuid() != 0) && (mount_uid != getuid())) { close(fid); eprintf(_("You are not allowed to umount %s\n"), mount_point); return -1; } close(fid); res = unmount_ncp(mount_point); return res; } static NWCCODE processServer(NWCONN_HANDLE conn, const char *theServer, const char* mount_points[], unsigned int * numEntries) { /* close the connection if any error or just before trying do_umount(). Caller does not have to do it */ NWCCODE err; char buffer[PATH_MAX + 1]; if (!conn) return 0; // already seen buffer[0]=0; err = NWCCGetConnInfo(conn,NWCC_INFO_MOUNT_POINT, sizeof(buffer), buffer); NWCCCloseConn(conn); if (err) { eprintf(_("NWCC_INFO_MOUNT_POINT failed: %s\n"), strnwerror(err)); return -1; } /* Connection must be closed before doing do_umount, otherwise umount will fail */ err = do_umount(buffer); if (!err) { char * aux=strdup(buffer); if (!aux) { eprintf(_("Not enough memory for strdup()\n")); return ENOMEM; } mount_points[(*numEntries)++]= aux; printf(_("Successfully logged out from %s\n"), theServer); } return err; } static NWCCODE processBindServers(NWCONN_HANDLE conns[], unsigned int curEntries, const char * theServer, const char* mount_points[], unsigned int * numEntries) { unsigned int i; for (i=0; i