File: parse_fstab.c

package info (click to toggle)
zulucrypt 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,908 kB
  • sloc: ansic: 25,142; cpp: 23,281; makefile: 24; xml: 10; sh: 2
file content (163 lines) | stat: -rw-r--r-- 4,212 bytes parent folder | download
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
/*
 *
 *  Copyright (c) 2013-2015
 *  name : Francis Banyikwa
 *  email: mhogomchungu@gmail.com
 *  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, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "../lib/includes.h"

#include <blkid/blkid.h>
#include <sys/types.h>
#include <sys/stat.h>

static void _add_entry( string_t xt,ssize_t r,char * e )
{
	StringSubChar( xt,(size_t)r,' ' ) ;

	if( e != NULL ){

		StringRemoveLeft( xt,(size_t)r ) ;
		StringPrepend( xt,e ) ;
		StringFree( e ) ;
	}
}

stringList_t zuluCryptGetFstabList( uid_t uid )
{
	string_t xt = StringGetFromFile( "/etc/fstab" ) ;

	stringList_t stl = StringListVoid ;

	StringListIterator it  ;
	StringListIterator end ;

	ssize_t index ;

	const char * entry ;

	if( uid ){}

	stl = StringListStringSplit( xt,'\n' ) ;

	StringDelete( &xt ) ;

	StringListRemoveIfStringStartsWith( stl,"#" ) ;
	StringListRemoveIfStringStartsWith( stl,"//" ) ;

	if( StringListSize( stl ) < 1 ){
		StringListDelete( &stl ) ;
		return StringListVoid ;
	}

	StringListGetIterators( stl,&it,&end ) ;

	while( it != end ){

		xt = *it ;
		it++ ;

		index = StringIndexOfChar( xt,0,' ' ) ;

		if( index != -1 ){

			entry = StringSubChar( xt,(size_t)index,'\0' ) ;

			if( StringPrefixEqual( entry,"/dev/" ) ){

				/*
				 * zuluCryptResolvePath() is defined in resolve_paths.c
				 */
				_add_entry( xt,index,zuluCryptResolvePath( entry ) ) ;

			}else if( StringAtLeastOnePrefixMatch( entry,"UUID=","uuid=",NULL ) ){

				entry = StringRemoveString( xt,"\"" ) ;
				/*
				 * zuluCryptDeviceFromUUID() is defined in blkid_evaluate_tag.c
				 */
				_add_entry( xt,index,zuluCryptDeviceFromUUID( entry + 5 ) ) ;

			}else if( StringAtLeastOnePrefixMatch( entry,"LABEL=","label=",NULL ) ){

				entry = StringRemoveString( xt,"\"" ) ;
				/*
				 * zuluCryptDeviceFromLabel() is defined in blkid_evaluate_tag.c
				 */
				_add_entry( xt,index,zuluCryptDeviceFromLabel( entry + 6 ) ) ;

			}else if( StringAtLeastOnePrefixMatch( entry,"PARTUUID=","partuuid=",NULL ) ){

				entry = StringRemoveString( xt,"\"" ) ;
				/*
				 * zuluCryptDeviceFromPARTUUID() is defined in blkid_evaluate_tag.c
				 */
				_add_entry( xt,index,zuluCryptDeviceFromPARTUUID( entry + 9 ) ) ;

			}else if( StringAtLeastOnePrefixMatch( entry,"PARTLABEL=","partlabel=",NULL ) ){

				entry = StringRemoveString( xt,"\"" ) ;
				/*
				 * zuluCryptDeviceFromPARTLABEL() is defined in blkid_evaluate_tag.c
				 */
				_add_entry( xt,index,zuluCryptDeviceFromPARTLABEL( entry + 10 ) ) ;

			}else{
				entry = StringSubChar( xt,(size_t)index,' ' ) ;
			}
		}
	}

	return stl ;
}

string_t zuluCryptGetFstabEntry( const char * device,uid_t uid )
{
	string_t st = StringVoid ;
	string_t xt = String( device ) ;
	stringList_t stl = zuluCryptGetFstabList( uid ) ;
	ssize_t index = StringListHasStartSequence( stl,StringAppend( xt," " ) ) ;
	if( index >= 0 ){
		st = StringListCopyStringAt( stl,(size_t)index ) ;
	}
	StringDelete( &xt ) ;
	StringListDelete( &stl ) ;
	return st ;
}

string_t zuluCryptGetMountOptionsFromFstab( const char * device,int pos,uid_t uid )
{
	stringList_t stl ;
	string_t st = zuluCryptGetFstabEntry( device,uid ) ;
	if( st != StringVoid ){
		stl = StringListStringSplit( st,' ' ) ;
		StringDelete( &st ) ;
		st = StringListCopyStringAt( stl,(size_t)pos ) ;
		StringListDelete( &stl ) ;
	}
	return st ;
}

stringList_t zuluCryptGetFstabEntryList( const char * device,uid_t uid )
{
	stringList_t stl = StringListVoid;
	string_t st = zuluCryptGetFstabEntry( device,uid ) ;
	if( st != StringVoid ){
		stl = StringListStringSplit( st,' ' ) ;
		StringDelete( &st ) ;
	}
	return stl ;
}