1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2018, Joyent, Inc.
  28  */
  29 
  30 /*
  31  * smbfs umount
  32  */
  33 
  34 #include <stdio.h>
  35 #include <stdlib.h>
  36 #include <string.h>
  37 #include <stdarg.h>
  38 #include <signal.h>
  39 #include <unistd.h>
  40 #include <kstat.h>
  41 #include <rpc/rpc.h>
  42 #include <sys/mnttab.h>
  43 #include <sys/mount.h>
  44 #include <sys/mntent.h>
  45 #include <errno.h>
  46 #include <locale.h>
  47 #include <fslib.h>
  48 #include <priv_utils.h>
  49 
  50 #define RET_OK  0
  51 #define RET_ERR 32
  52 
  53 static void pr_err(const char *fmt, ...);
  54 static void usage();
  55 static int smbfs_unmount(char *, int);
  56 static struct extmnttab *mnttab_find();
  57 
  58 static char *myname;
  59 static char typename[64];
  60 
  61 int
  62 main(int argc, char *argv[])
  63 {
  64         extern int optind;
  65         int c;
  66         int umnt_flag = 0;
  67 
  68         (void) setlocale(LC_ALL, "");
  69 
  70 #if !defined(TEXT_DOMAIN)
  71 #define TEXT_DOMAIN "SYS_TEST"
  72 #endif
  73         (void) textdomain(TEXT_DOMAIN);
  74 
  75         /*
  76          * Normal users are allowed to umount smbfs mounts they own.
  77          * To allow that, this program is installed setuid root, and
  78          * it adds SYS_MOUNT privilege here (if needed), and then
  79          * restores the user's normal privileges.
  80          */
  81         if (__init_suid_priv(0, PRIV_SYS_MOUNT, (char *)NULL) < 0) {
  82                 (void) fprintf(stderr,
  83                     gettext("Insufficient privileges, "
  84                     "%s must be set-uid root\n"), argv[0]);
  85                 exit(RET_ERR);
  86         }
  87 
  88         myname = strrchr(argv[0], '/');
  89         myname = myname ? myname+1 : argv[0];
  90         (void) sprintf(typename, "smbfs %s", myname);
  91         argv[0] = typename;
  92 
  93         /*
  94          * Set options
  95          */
  96         while ((c = getopt(argc, argv, "f")) != EOF) {
  97                 switch (c) {
  98                 case 'f':
  99                         umnt_flag |= MS_FORCE; /* forced unmount is desired */
 100                         break;
 101                 default:
 102                         usage();
 103                         exit(RET_ERR);
 104                 }
 105         }
 106         if (argc - optind != 1) {
 107                 usage();
 108                 exit(RET_ERR);
 109         }
 110 
 111         return (smbfs_unmount(argv[optind], umnt_flag));
 112 }
 113 
 114 static void
 115 pr_err(const char *fmt, ...)
 116 {
 117         va_list ap;
 118 
 119         va_start(ap, fmt);
 120         (void) fprintf(stderr, "%s: ", typename);
 121         (void) vfprintf(stderr, fmt, ap);
 122         (void) fflush(stderr);
 123         va_end(ap);
 124 }
 125 
 126 static void
 127 usage()
 128 {
 129         (void) fprintf(stderr,
 130             gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n"));
 131         exit(RET_ERR);
 132 }
 133 
 134 static int
 135 smbfs_unmount(char *pathname, int umnt_flag)
 136 {
 137         struct extmnttab *mntp;
 138         int rc;
 139 
 140         mntp = mnttab_find(pathname);
 141         if (mntp) {
 142                 pathname = mntp->mnt_mountp;
 143         }
 144 
 145         /* Need sys_mount privilege for the umount call. */
 146         (void) __priv_bracket(PRIV_ON);
 147         rc = umount2(pathname, umnt_flag);
 148         (void) __priv_bracket(PRIV_OFF);
 149 
 150         if (rc < 0) {
 151                 pr_err(gettext("%s: %s\n"), pathname, strerror(errno));
 152                 return (RET_ERR);
 153         }
 154 
 155         return (RET_OK);
 156 }
 157 
 158 /*
 159  *  Find the mnttab entry that corresponds to "name".
 160  *  We're not sure what the name represents: either
 161  *  a mountpoint name, or a special name (server:/path).
 162  *  Return the last entry in the file that matches.
 163  */
 164 static struct extmnttab *
 165 mnttab_find(dirname)
 166         char *dirname;
 167 {
 168         FILE *fp;
 169         struct extmnttab mnt;
 170         struct extmnttab *res = NULL;
 171 
 172         fp = fopen(MNTTAB, "r");
 173         if (fp == NULL) {
 174                 pr_err("%s: %s\n", MNTTAB, strerror(errno));
 175                 return (NULL);
 176         }
 177         while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
 178                 if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
 179                     strcmp(mnt.mnt_special, dirname) == 0) {
 180                         if (res)
 181                                 fsfreemnttab(res);
 182                         res = fsdupmnttab(&mnt);
 183                 }
 184         }
 185 
 186         (void) fclose(fp);
 187         return (res);
 188 }