"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-6.0.0/zuluCrypt-cli/bin/create_mount_point.c" (29 Jan 2021, 8465 Bytes) of package /linux/misc/zuluCrypt-6.0.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "create_mount_point.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.7.1_vs_6.0.0.

    1 /*
    2  *
    3  *  Copyright (c) 2013-2015
    4  *  name : Francis Banyikwa
    5  *  email: mhogomchungu@gmail.com
    6  *  This program is free software: you can redistribute it and/or modify
    7  *  it under the terms of the GNU General Public License as published by
    8  *  the Free Software Foundation, either version 2 of the License, or
    9  *  (at your option) any later version.
   10  *
   11  *  This program is distributed in the hope that it will be useful,
   12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  *  GNU General Public License for more details.
   15  *
   16  *  You should have received a copy of the GNU General Public License
   17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include "includes.h"
   21 #include "../lib/includes.h"
   22 #include <sys/stat.h>
   23 #include <unistd.h>
   24 #include "mount_prefix_path.h"
   25 #include "reuse_mount_point.h"
   26 #include <grp.h>
   27 #include <pwd.h>
   28 
   29 static void _chown( const char * x,uid_t y,gid_t z )
   30 {
   31     struct passwd * usr ;
   32 
   33     if( z == ( gid_t ) -1 ){
   34 
   35         usr = getpwuid( y ) ;
   36 
   37         if( usr != NULL ){
   38 
   39             z = usr->pw_gid ;
   40         }
   41     }
   42 
   43     if( chown( x,y,z ) ){}
   44 }
   45 static void _chmod( const char * x,mode_t y )
   46 {
   47     if( chmod( x,y ) ){}
   48 }
   49 static void _mkdir( const char * x,mode_t y )
   50 {
   51     if( mkdir( x,y ) ){}
   52 }
   53 static void _stat( const char * x,struct stat * y )
   54 {
   55     if( stat( x,y ) ){}
   56 }
   57 
   58 int zuluCryptReuseMountPoint( void )
   59 {
   60     return REUSE_MOUNT_POINT ;
   61 }
   62 
   63 static string_t _create_path_0( const char * m_point,uid_t uid,string_t path )
   64 {
   65     struct stat st ;
   66 
   67     if( zuluCryptReuseMountPoint() ){
   68 
   69         if( stat( m_point,&st ) == 0 ){
   70 
   71             if( S_ISDIR( st.st_mode ) ){
   72 
   73                 /*
   74                  *  zuluCryptMountPointIsActive() is defined in ../lib/mountinfo.c
   75                  */
   76                 if( zuluCryptMountPointIsActive( m_point ) ){
   77 
   78                     StringDelete( &path ) ;
   79                 }else{
   80                     _chown( m_point,uid,( gid_t )-1 ) ;
   81                 }
   82             }else{
   83                 StringDelete( &path ) ;
   84             }
   85         }else{
   86             if( mkdir( m_point,S_IRWXU ) == 0 ){
   87 
   88                 _chown( m_point,uid,( gid_t )-1 ) ;
   89             }else{
   90                 StringDelete( &path ) ;
   91             }
   92         }
   93     }else{
   94         if( mkdir( m_point,S_IRWXU ) == 0 ){
   95 
   96             _chown( m_point,uid,( gid_t )-1 ) ;
   97         }else{
   98             StringDelete( &path ) ;
   99         }
  100     }
  101 
  102     return path ;
  103 }
  104 
  105 static string_t _create_path( uid_t uid,string_t path,int need_privileges )
  106 {
  107     string_t st = StringVoid ;
  108 
  109     const char * m_point = StringContent( path ) ;
  110 
  111     if( m_point != NULL ){
  112 
  113         if( need_privileges ){
  114 
  115             zuluCryptSecurityGainElevatedPrivileges() ;
  116 
  117             st = _create_path_0( m_point,uid,path ) ;
  118 
  119             zuluCryptSecurityDropElevatedPrivileges() ;
  120         }else{
  121             st = _create_path_0( m_point,uid,path ) ;
  122         }
  123     }
  124 
  125     return st ;
  126 }
  127 
  128 static string_t _create_mount_point_1( const char * device,uid_t uid,string_t path,int need_privileges )
  129 {
  130     string_t st ;
  131     char * loop_path = NULL ;
  132 
  133     if( StringPrefixEqual( device,"/dev/loop" ) ){
  134         /*
  135          * zuluCryptLoopDeviceAddress_1() is defined in ../lib/create_loop_device.c
  136          */
  137         device = loop_path = zuluCryptLoopDeviceAddress_1( device ) ;
  138     }
  139 
  140     StringMultipleAppend( path,device + StringLastIndexOfChar_1( device,'/' ) + 1,NULL ) ;
  141 
  142     st = _create_path( uid,path,need_privileges ) ;
  143 
  144     StringFree( loop_path ) ;
  145 
  146     return st ;
  147 }
  148 
  149 static string_t _create_home_default_mount_point( const char * device,uid_t uid,string_t path )
  150 {
  151     return _create_mount_point_1( device,uid,path,0 ) ;
  152 }
  153 
  154 static string_t _create_default_mount_point( const char * device,uid_t uid,string_t path )
  155 {
  156     return _create_mount_point_1( device,uid,path,1 ) ;
  157 }
  158 
  159 static string_t _create_mount_point_0( const char * label,uid_t uid,string_t path,int need_privileges )
  160 {
  161     const char * q = strrchr( label,'/' ) ;
  162     const char * e ;
  163 
  164     if( q == NULL ){
  165 
  166         StringAppend( path,label ) ;
  167     }else{
  168         if( *( q + 1 ) == '\0' ){
  169             /*
  170              * -m option was given with a path that ends with "/",backtrack until you find the second "/"
  171              * from the right and use it as the last "/".
  172              */
  173             e = q - 1 ;
  174 
  175             if( e < label ){
  176                 /*
  177                  * -m option was given with a single "/".
  178                  */
  179                 StringDelete( &path ) ;
  180                 return StringVoid ;
  181             }
  182             while( 1 ){
  183 
  184                 if( e == label ){
  185 
  186                     StringAppend( path,e + 1 ) ;
  187                     StringRemoveRight( path,1 ) ;
  188 
  189                     break ;
  190 
  191                 }else if( *e == '/' ){
  192 
  193                     StringAppend( path,e + 1 ) ;
  194                     StringRemoveRight( path,1 ) ;
  195 
  196                     break ;
  197                 }else{
  198                     e-- ;
  199                 }
  200             }
  201         }else{
  202             StringAppend( path,q + 1 ) ;
  203         }
  204     }
  205 
  206     return _create_path( uid,path,need_privileges ) ;
  207 }
  208 
  209 static string_t _create_home_custom_mount_point( const char * label,uid_t uid,string_t path )
  210 {
  211     return _create_mount_point_0( label,uid,path,0 ) ;
  212 }
  213 
  214 static string_t _create_custom_mount_point( const char * label,uid_t uid,string_t path )
  215 {
  216     return _create_mount_point_0( label,uid,path,1 ) ;
  217 }
  218 
  219 static string_t create_home_mount_point( const char * device,const char * label,uid_t uid )
  220 {
  221     /*
  222      * zuluCryptGetUserHomePath() is defined in ../lib/user_home_path.c
  223      */
  224     string_t path = zuluCryptGetUserHomePath( uid ) ;
  225 
  226     if( label == NULL ){
  227 
  228         return _create_home_default_mount_point( device,uid,path ) ;
  229     }else{
  230         return _create_home_custom_mount_point( label,uid,path ) ;
  231     }
  232 }
  233 
  234 static int mount_point_prefix_match_0( const char * m_path,uid_t uid,string_t * m_point,int home_prefix )
  235 {
  236     int st ;
  237     /*
  238      * zuluCryptGetUserName() is defined in ../lib/user_home_path.c
  239      */
  240     string_t uname ;
  241     /*
  242      * below constant are set in ../constants.h
  243      */
  244     const char * str ;
  245 
  246     if( home_prefix ){
  247 
  248         uname = zuluCryptGetUserHomePath( uid ) ;
  249         str = StringContent( uname ) ;
  250     }else{
  251         uname = zuluCryptGetUserName( uid ) ;
  252         StringPrepend( uname,"/run/media/private/" ) ;
  253         str = StringAppendChar( uname,'/' ) ;
  254     }
  255 
  256     st = StringPrefixEqual( m_path,str ) ;
  257 
  258     if( m_point ){
  259 
  260         *m_point = uname ;
  261     }else{
  262         StringDelete( &uname ) ;
  263     }
  264 
  265     return st ;
  266 }
  267 
  268 static int home_mount_point_prefix_match( const char * m_path,uid_t uid,string_t * m_point )
  269 {
  270     return mount_point_prefix_match_0( m_path,uid,m_point,1 ) ;
  271 }
  272 
  273 static int mount_point_prefix_match( const char * m_path,uid_t uid,string_t * m_point )
  274 {
  275     return mount_point_prefix_match_0( m_path,uid,m_point,0 ) ;
  276 }
  277 
  278 static void _zuluCryptCreateMountPath( const char * path )
  279 {
  280     struct stat st ;
  281 
  282     _mkdir( path,S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH ) ;
  283 
  284     _stat( path,&st ) ;
  285 
  286     _chown( path,0,0 ) ;
  287 
  288     _chmod( path,st.st_mode | S_IXOTH | S_IROTH ) ;
  289 }
  290 
  291 void zuluCryptCreateMountPath( const char * path )
  292 {
  293     string_t p = String( path ) ;
  294 
  295     StringIterator it ;
  296     StringIterator end ;
  297 
  298     char * e ;
  299     char * f ;
  300 
  301     StringGetIterators( p,&it,&end ) ;
  302 
  303     if( end ){}
  304 
  305     f = it + 1 ;
  306 
  307     while( 1 ){
  308 
  309         e = strchr( f,'/' ) ;
  310 
  311         if( e == NULL ){
  312 
  313             _zuluCryptCreateMountPath( it ) ;
  314             break ;
  315         }else{
  316             *e = '\0' ;
  317 
  318             _zuluCryptCreateMountPath( it ) ;
  319 
  320             *e = '/' ;
  321 
  322             f = e + 1 ;
  323         }
  324     }
  325 
  326     StringDelete( &p ) ;
  327 }
  328 
  329 static string_t create_mount_point( const char * device,const char * label,uid_t uid )
  330 {
  331     string_t path = zuluCryptGetUserName( uid ) ;
  332 
  333     const char * e = StringPrepend( path,"/run/media/private/" ) ;
  334 
  335     zuluCryptSecurityGainElevatedPrivileges() ;
  336 
  337     zuluCryptCreateMountPath( e ) ;
  338 
  339     _chmod( e,S_IREAD | S_IXUSR ) ;
  340     _chown( e,uid,( gid_t )-1 ) ;
  341 
  342     zuluCryptSecurityDropElevatedPrivileges() ;
  343 
  344     StringAppendChar( path,'/' ) ;
  345 
  346     if( label == NULL ){
  347 
  348         return _create_default_mount_point( device,uid,path ) ;
  349     }else{
  350         return _create_custom_mount_point( label,uid,path ) ;
  351     }
  352 }
  353 
  354 static int home_mount_prefix( void )
  355 {
  356     return USE_HOME_PATH_AS_MOUNT_PREFIX ;
  357 }
  358 
  359 void zuluCryptCreateMountPointPrefix( uid_t uid )
  360 {
  361     string_t st = zuluCryptGetUserName( uid ) ;
  362 
  363     const char * e = StringPrepend( st,"/run/media/private/" ) ;
  364 
  365     zuluCryptCreateMountPath( e ) ;
  366 
  367     StringDelete( &st ) ;
  368 }
  369 
  370 string_t zuluCryptCreateMountPoint( const char * device,const char * label,const char * m_opts,uid_t uid )
  371 {
  372     if( home_mount_prefix() ){
  373 
  374         return create_home_mount_point( device,label,uid ) ;
  375     }else{
  376         if( StringHasComponent( m_opts,"mount-prefix=home" ) ){
  377 
  378             if( zuluCryptUserIsAMemberOfAGroup( uid,"zulumount" ) ){
  379 
  380                 return create_home_mount_point( device,label,uid ) ;
  381             }else{
  382                 return StringVoid ;
  383             }
  384         }else{
  385             return create_mount_point( device,label,uid ) ;
  386         }
  387     }
  388 }
  389 
  390 int zuluCryptMountPointPrefixMatch( const char * m_path,uid_t uid,string_t * m_point )
  391 {
  392     if( home_mount_prefix() ){
  393 
  394         return home_mount_point_prefix_match( m_path,uid,m_point ) ;
  395     }else{
  396         if( mount_point_prefix_match( m_path,uid,m_point ) ){
  397 
  398             return 1 ;
  399         }else{
  400             return home_mount_point_prefix_match( m_path,uid,m_point ) ;
  401         }
  402     }
  403 }