aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mount_smbfs.c
blob: 7aec92d60cf8a36183d00ed670e210de5c4ee468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 * mount_smbfs.c
 *
 *  Module for Linux automountd to mount an SMB/CIFS filesystem
 *
 *  Mount point input format expected is: //server/service[/root_path...]
 *
 *   Copyright 1997 Transmeta Corporation - All Rights Reserved
 *
 *   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 <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <syslog.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#define MODULE_MOUNT
#include "automount.h"

#define MODPREFIX "mount(smbfs): "
int mount_version = AUTOFS_MOUNT_VERSION; /* Required by protocol */

struct smb_mount_opt {
  char *optname;		/* mount-style option name */
  char *optflag;		/* command-line flag for smbmount */
  int hasarg;			/* takes argument? */
  int satisfies_pwd;		/* true if this means we don't need a -n */
};

static struct smb_mount_opt mount_opt_list[] = {
  { "nocaps",    "-C", 0, 0 },
  { "guest",     "-n", 0, 1 },
  { "passwd",    "-P", 1, 1 },
  { "srvname",   "-s", 1, 0 },
  { "mysmbname", "-c", 1, 0 },
  { "login",     "-U", 1, 0 },
  { "uid",       "-u", 1, 0 },
  { "gid",       "-g", 1, 0 },
  { "filemod",   "-f", 1, 0 },
  { "dirmod",    "-d", 1, 0 },
  { "port",      "-p", 1, 0 },
  { "maxxmit",   "-m", 1, 0 },
  { NULL, NULL, 0, 0 }
};

int mount_init(void **context)
{
  return 0;
}

static int smb_parse_options(char *optstr, const char **argv,
			     char *qbuf, int *qbuflen)
{
  char *opt;
  int ln;
  int argc;
  int has_pwd;
  int qbufchr, qln;
  struct smb_mount_opt *mount_opt;

  has_pwd = 0;
  qbufchr = 0;
  argc = 0;

  if ( optstr ) {
    for ( opt = strtok(optstr, ",") ; opt ; opt = strtok(NULL, ",") ) {
      for ( mount_opt = mount_opt_list ; mount_opt->optname ; mount_opt++ ) {
	if ( mount_opt->hasarg ) {
	  ln = strlen(mount_opt->optname);
	  if ( !strncmp(opt, mount_opt->optname, ln) && opt[ln] == '=' ) {
	    qln = strlen(opt)-ln;
	    if ( argv ) {
	      *(argv++) = mount_opt->optflag;
	      memcpy(qbuf, opt+ln+1, qln);
	      *(argv++) = qbuf;
	      qbuf += qln;
	    }
	    qbufchr += qln;
	    has_pwd = has_pwd || mount_opt->satisfies_pwd;
	    argc += 2;
	    break;
	  }
	} else {
	  if ( !strcmp(opt, mount_opt->optname) ) {
	    if ( argv )
	      *(argv++) = mount_opt->optflag;
	    has_pwd = has_pwd || mount_opt->satisfies_pwd;
	    argc++;
	    break;
	  }
	}
      }
      /* Ignore unknown options */
    }
  }

  if ( !has_pwd ) {
    syslog(LOG_DEBUG, MODPREFIX "no password option, adding -n");
    if ( argv )
      *(argv++) = "-n";
    argc++;
  }
  
  if ( argv )
    *argv = NULL;

  if ( qbuflen )
    *qbuflen = qbufchr;

  return argc;
}

int mount_mount(const char *root, const char *name, int name_len,
		const char *what, const char *fstype, const char *options,
		void *context)
{
  char *fullpath, *optcopy;
  int err;
  char *qbuf;
  int argc, optsize, qbuflen;
  const char **argv;

  fullpath = alloca(strlen(root)+name_len+2);
  optcopy  = alloca(optsize = strlen(options)+1);
  if ( !fullpath || !optcopy ) {
    syslog(LOG_ERR, MODPREFIX "alloca: %m");
    return 1;
  }
  sprintf(fullpath, "%s/%s", root, name);

  memcpy(optcopy, options, optsize);
  argc = smb_parse_options(optcopy, NULL, NULL, &qbuflen) + 4;
  argv = alloca(sizeof(char *) * argc);
  qbuf = alloca(qbuflen);
  if ( !argv || (qbuflen && !qbuf) ) {
    syslog(LOG_ERR, MODPREFIX "alloca: %m");
    return 1;
  }
  argv[0] = _PATH_SMBMOUNT;
  argv[1] = what;
  argv[2] = fullpath;
  memcpy(optcopy, options, optsize);
  smb_parse_options(optcopy, argv+3, qbuf, NULL);

  syslog(LOG_DEBUG, MODPREFIX "calling mkdir %s", fullpath);
  if ( mkdir(fullpath, 0555) && errno != EEXIST ) {
    syslog(LOG_NOTICE, MODPREFIX "mkdir %s failed: %m", name);
    return 1;
  }

  err = spawnv(LOG_NOTICE, _PATH_SMBMOUNT, argv);
 
  if ( err ) {
    rmdir(fullpath);
    syslog(LOG_NOTICE, MODPREFIX "failed to mount %s on %s", what, fullpath);
    return 1;
  } else {
    syslog(LOG_DEBUG, MODPREFIX "mounted %s on %s", what, fullpath);
    return 0;
  }
}

int mount_done(void *context)
{
  return 0;
}