PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/zuluCrypt-cli/bin/path_access.c

https://gitlab.com/m.schmidt/zuluCrypt
C | 185 lines | 103 code | 24 blank | 58 comment | 12 complexity | 307aa7c7bbef21f17eeea121e89d3802 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, BSD-2-Clause
  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. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include "../constants.h"
  22. #include "../lib/includes.h"
  23. #include "includes.h"
  24. #include <errno.h>
  25. #include <unistd.h>
  26. static int has_device_access( const char * path,int c )
  27. {
  28. int f ;
  29. if( c == ZULUCRYPTread ){
  30. f = open( path,O_RDONLY ) ;
  31. }else{
  32. f = open( path,O_WRONLY ) ;
  33. }
  34. if( f == -1 ){
  35. switch( errno ){
  36. case EACCES : return 1 ; /* permission denied */
  37. case ENOENT : return 2 ; /* invalid path*/
  38. default : return 4 ; /* common error */
  39. }
  40. }else{
  41. close( f ) ;
  42. return 0 ;
  43. }
  44. }
  45. /*
  46. * 1-permissions denied
  47. * 2-invalid path
  48. * 3-shenanigans
  49. * 4-common error
  50. */
  51. static int path_is_accessible( const char * path,uid_t uid,int action )
  52. {
  53. int st ;
  54. char * e ;
  55. if( uid ){;}
  56. if( StringPrefixEqual( path,"/dev/shm/" ) ){
  57. return 4 ;
  58. }
  59. if( StringPrefixEqual( path,"/dev/" ) ){
  60. if( StringPrefixEqual( path,"/dev/loop" ) ){
  61. /*
  62. * zuluCryptLoopDeviceAddress_1() is defined in ../zuluCrypt-cli/create_loop_device.c
  63. */
  64. e = zuluCryptLoopDeviceAddress_1( path ) ;
  65. if( e != NULL ){
  66. st = has_device_access( e,action ) ;
  67. StringFree( e ) ;
  68. }else{
  69. return 4 ;
  70. }
  71. }else{
  72. zuluCryptSecurityGainElevatedPrivileges() ;
  73. st = has_device_access( path,action ) ;
  74. zuluCryptSecurityDropElevatedPrivileges() ;
  75. }
  76. return st ;
  77. }else{
  78. zuluCryptSecurityDropElevatedPrivileges() ;
  79. return has_device_access( path,action ) ;
  80. }
  81. }
  82. int zuluCryptCanOpenPathForReading( const char * path,uid_t uid )
  83. {
  84. return path_is_accessible( path,uid,ZULUCRYPTread ) ;
  85. }
  86. int zuluCryptCanOpenPathForWriting( const char * path,uid_t uid )
  87. {
  88. return path_is_accessible( path,uid,ZULUCRYPTwrite ) ;
  89. }
  90. /*
  91. * return values:
  92. * 5 - couldnt get key from the socket
  93. * 4 -permission denied
  94. * 1 invalid path
  95. * 2 insufficient memory to open file
  96. * 0 success
  97. */
  98. int zuluCryptGetPassFromFile( const char * path,uid_t uid,string_t * st )
  99. {
  100. /*
  101. * zuluCryptGetUserHomePath() is defined in ../lib/user_home_path.c
  102. */
  103. string_t p = zuluCryptGetUserHomePath( uid ) ;
  104. const char * z = StringAppend( p,".zuluCrypt-socket" ) ;
  105. size_t s = StringLength( p ) ;
  106. int m = StringPrefixMatch( path,z,s ) ;
  107. StringDelete( &p ) ;
  108. zuluCryptSecurityDropElevatedPrivileges() ;
  109. if( m ){
  110. /*
  111. * path that starts with $HOME/.zuluCrypt-socket is treated not as a path to key file but as path
  112. * to a local socket to get a passphrase
  113. */
  114. /*
  115. * zuluCryptGetKeyFromSocket() is defined in ../pluginManager/zuluCryptPluginManager.c
  116. */
  117. zuluCryptGetKeyFromSocket( path,st,uid ) ;
  118. return 0 ;
  119. }else{
  120. /*
  121. * 8192000 bytes is the default cryptsetup maximum keyfile size
  122. */
  123. m = StringGetFromFileMemoryLocked( st,path,0,8192000 ) ;
  124. switch( m ){
  125. case 0 : return 0 ;
  126. case 1 : return 4 ;
  127. case 2 : return 2 ;
  128. }
  129. /*
  130. * not supposed to get here
  131. */
  132. return -1 ;
  133. }
  134. }
  135. char * zuluCryptEvaluateDeviceTags( const char * tag,const char * path )
  136. {
  137. char * r ;
  138. zuluCryptSecurityGainElevatedPrivileges() ;
  139. /*
  140. * zuluCryptDeviceFromUUID() is defined in ../lib/blkid_evaluate_tag.c
  141. * zuluCryptDeviceFromLabel() is defined in ../lib/blkid_evaluate_tag.c
  142. */
  143. if( StringsAreEqual( tag,"UUID" ) ){
  144. r = zuluCryptDeviceFromUUID( path ) ;
  145. }else{
  146. r = zuluCryptDeviceFromLabel( path ) ;
  147. }
  148. zuluCryptSecurityDropElevatedPrivileges() ;
  149. return r ;
  150. }
  151. char * zuluCryptUUIDFromPath( const char * device )
  152. {
  153. char * c ;
  154. zuluCryptSecurityGainElevatedPrivileges() ;
  155. /*
  156. * zuluCryptUUIDFromPath_1() is defined in ../lib/blkid_evaluate_tag.c
  157. */
  158. c = zuluCryptUUIDFromPath_1( device ) ;
  159. zuluCryptSecurityDropElevatedPrivileges() ;
  160. return c ;
  161. }