13 #include <sys/types.h>
14 #include <sys/mount.h>
17 #define ERROR(x...) fprintf(stderr, x)
22 printf("Usage: lufsmnt mount-point [options]\n");
23 printf("This is a suid wrapper called by lufsd.\n");
28 fullpath(char *path, char *full){
32 if(strlen(path) >= PATH_MAX - 1)
35 if(!realpath(path, full))
46 ERROR("cannot chdir %s\n", mp);
51 ERROR("cannot stat %s\n", mp);
55 if(!S_ISDIR(st.st_mode)){
56 ERROR("%s is not a directory\n", mp);
60 if((getuid()) && ((getuid() != st.st_uid) || ((st.st_mode & S_IRWXU) != S_IRWXU))){
61 ERROR("you don't have proper permissions to mount on %s\n", mp);
69 hide_pass(char *opts){
72 if((c = strstr(opts, "password="))){
73 for(c = c + 9; (*c != 0) && (*c != ','); c++)
79 get_opt(char *opts, const char *optname, char *buf, int buflen){
82 for(begin = opts; (begin = strstr(begin, optname)); begin++){
83 if (begin > opts && begin[-1] != ',')
85 if (begin[strlen(optname)] != '=')
87 begin += strlen(optname) + 1;
89 if(!(end = strchr(begin, ','))){
90 end = begin + strlen(begin);
93 if( end - begin >= buflen)
94 end = begin + buflen - 1;
96 memcpy(buf, begin, end - begin);
103 /* From util-linux/mount/getusername.c */
106 const char *user = 0;
107 struct passwd *pw = getpwuid(getuid());
115 main(int argc, char **argv){
116 char *mountpoint, *opts, *s;
117 static char fpath[PATH_MAX];
118 static char mfs[PATH_MAX];
119 static char mopts[PATH_MAX];
120 static char mntent_mnt_type[PATH_MAX];
131 ERROR("%s needs to be installed suid root in order to be used by unprivileged users\n", argv[0]);
135 mountpoint = argv[1];
138 fullpath(mountpoint, fpath);
144 if (mount("none", fpath, "lufs", MS_NOSUID | MS_NODEV, opts) < 0){
145 ERROR("mount failed: %s\n", strerror(errno));
148 ERROR("you don't have kernel lufs support (check whether the lufs module is available/loaded)\n");
154 snprintf(mopts, sizeof(mopts), "user=%s,", getusername());
156 if(strlen(mopts) + strlen(opts) < PATH_MAX)
158 while ((s = strstr(mopts, ",user")) && (s[strlen(",user")]==0 || s[strlen(",user")]==','))
159 memmove(s, s+strlen(",user"), strlen(s+strlen(",user"))+1);
161 get_opt(opts, "fs", mfs, PATH_MAX);
162 get_opt(opts, "mntent.mnt_fsname", mfs, PATH_MAX);
164 ment.mnt_fsname = mfs;
165 ment.mnt_dir = fpath;
166 strcpy(mntent_mnt_type, "lufs");
167 get_opt(opts, "mntent.mnt_type", mntent_mnt_type, sizeof(mntent_mnt_type));
168 ment.mnt_type = mntent_mnt_type;
169 ment.mnt_opts = mopts;
173 if((fd = open(_PATH_MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) < 0){
174 ERROR("can't get "_PATH_MOUNTED"~ lock file\n");
179 if((mtab = setmntent(_PATH_MOUNTED, "a+")) == NULL){
180 ERROR("can't open "_PATH_MOUNTED"\n");
184 if(addmntent(mtab, &ment)){
185 ERROR("can't write mount entry\n");
189 if(fchmod(fileno(mtab), 0644) < 0){
190 ERROR("can't set [erms on "_PATH_MOUNTED"\n");
196 if(unlink(_PATH_MOUNTED"~") < 0){
197 ERROR("can't remove "_PATH_MOUNTED"~\n");