12 #include <sys/types.h>
13 #include <sys/mount.h>
16 #define ERROR(x...) fprintf(stderr, x)
21 printf("Usage: lufsmnt mount-point [options]\n");
22 printf("This is a suid wrapper called by lufsd.\n");
27 fullpath(char *path, char *full){
31 if(strlen(path) >= PATH_MAX - 1)
34 if(!realpath(path, full))
45 ERROR("cannot chdir %s\n", mp);
50 ERROR("cannot stat %s\n", mp);
54 if(!S_ISDIR(st.st_mode)){
55 ERROR("%s is not a directory\n", mp);
59 if((getuid()) && ((getuid() != st.st_uid) || ((st.st_mode & S_IRWXU) != S_IRWXU))){
60 ERROR("you don't have proper permissions to mount on %s\n", mp);
68 hide_pass(char *opts){
71 if((c = strstr(opts, "password="))){
72 for(c = c + 9; (*c != 0) && (*c != ','); c++)
78 get_opt(char *opts, const char *optname, char *buf, int buflen){
81 for(begin = opts; (begin = strstr(begin, optname)); begin++){
82 if (begin > opts && begin[-1] != ',')
84 if (begin[strlen(optname)] != '=')
86 begin += strlen(optname) + 1;
88 if(!(end = strchr(begin, ','))){
89 end = begin + strlen(begin);
92 if( end - begin >= buflen)
93 end = begin + buflen - 1;
95 memcpy(buf, begin, end - begin);
103 main(int argc, char **argv){
104 char *mountpoint, *opts;
105 static char fpath[PATH_MAX];
106 static char mfs[PATH_MAX];
107 static char mopts[PATH_MAX];
108 static char mntent_mnt_type[PATH_MAX];
119 ERROR("%s needs to be installed suid root in order to be used by unprivileged users\n", argv[0]);
123 mountpoint = argv[1];
126 fullpath(mountpoint, fpath);
132 if (mount("none", fpath, "lufs", MS_NOSUID | MS_NODEV, opts) < 0){
133 ERROR("mount failed: %s\n", strerror(errno));
136 ERROR("you don't have kernel lufs support (check whether the lufs module is available/loaded)\n");
142 sprintf(mopts, "owner=%d,", getuid());
144 if(strlen(mopts) + strlen(opts) < PATH_MAX)
147 get_opt(opts, "fs", mfs, PATH_MAX);
148 get_opt(opts, "mntent.mnt_fsname", mfs, PATH_MAX);
150 ment.mnt_fsname = mfs;
151 ment.mnt_dir = fpath;
152 strcpy(mntent_mnt_type, "lufs");
153 get_opt(opts, "mntent.mnt_type", mntent_mnt_type, sizeof(mntent_mnt_type));
154 ment.mnt_type = mntent_mnt_type;
155 ment.mnt_opts = mopts;
159 if((fd = open(_PATH_MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) < 0){
160 ERROR("can't get "_PATH_MOUNTED"~ lock file\n");
165 if((mtab = setmntent(_PATH_MOUNTED, "a+")) == NULL){
166 ERROR("can't open "_PATH_MOUNTED"\n");
170 if(addmntent(mtab, &ment)){
171 ERROR("can't write mount entry\n");
175 if(fchmod(fileno(mtab), 0644) < 0){
176 ERROR("can't set [erms on "_PATH_MOUNTED"\n");
182 if(unlink(_PATH_MOUNTED"~") < 0){
183 ERROR("can't remove "_PATH_MOUNTED"~\n");