-
Notifications
You must be signed in to change notification settings - Fork 12
/
routines.ycp
247 lines (216 loc) · 7.71 KB
/
routines.ycp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* File:
* routines.ycp
*
* Module:
* Configuration of nfs
*
* Summary:
* Network NFS routines
*
* Authors:
* Jan Holesovsky <kendy@suse.cz>
* Dan Vesely <dan@suse.cz>
*
* $Id$
*
* Network NFS routines
*
*/
{
textdomain "nfs";
import "Package";
import "Report";
import "IP";
import "Hostname";
import "String";
/**
* @param spec "server:/path/specification"
* @return `couple("server", "/path/specification")
*/
define term SpecToServPath(string spec) ``{
// split using ":/" (because of IPv6)
integer path_begin = search( spec, ":/");
string serv = "";
// no :/ inside => <server>: or [/]<path>
if( path_begin == nil )
{
if(spec == filterchars(spec, "-_." + String::CAlnum() ) + ":")
// matches [a-zA-Z0-1.-_] and ends with colon? => <server>:
path_begin = size( spec) -1;
}
if (path_begin != nil) {
serv = substring(spec, 0, path_begin);
spec = substring(spec, path_begin+1);
}
return `couple(serv, spec);
}
/**
* Creates a list of ui table items for nfs fstab entries
* @param fstab list of nfs fstab entries
* @return itemized table entries
* @example UI::ChangeWidget(`id(`fstable), `Items, FstabTableItems(nfs_entries));
*/
define list<term> FstabTableItems(list<map> fstab) ``{
integer count = 0;
return maplist(map entry, fstab, ``{
term sp = SpecToServPath(entry["spec"]:"");
term it = `item(`id(count),
sp[0]:"" + " ",
sp[1]:"" + " ",
entry["file"]:"" + " ",
entry["vfstype"]:" ",
entry["mntops"]:"" + " ");
count = count+1;
return it;
});
}
/**
* Check for the validity of a hostname: nonempty, shorter than 50 chars,
* [-A-Za-z._]. If invalid, a message is displayed.
* @param name a hostname
* @return whether valid
*/
define boolean CheckHostName(string name) ``{
y2milestone( "CheckHostName: hostname=%1", name);
if (size(name) > 0 &&
size(name) < 50)
{
if( IP::Check4( name))
return true;
if( IP::Check6( IP::UndecorateIPv6( name)))
return true;
if( Hostname::CheckDomain( name))
return true;
}
// error popup message
Report::Error ( sformat(_("The hostname entered is invalid. It must be
shorter than 50 characters and only use
valid IPv4, IPv6 or domain name.
Valid IPv4: %1
Valid IPv6: %2
Valid domain: %3"), IP::Valid4(), IP::Valid6(), Hostname::ValidDomain()) );
return false;
};
/**
* Check if a mountpoint is in the fstab. If yes, display a message.
* @param fstab in .etc.fstab format (must contain the key "file")
* @param mpoint mount point
* @return is it there?
*/
define boolean IsMpInFstab(list<map> fstab, string mpoint) ``{
list tmp = filter(map fse, fstab, ``{
return (fse["file"]:"" == mpoint);
});
if (size(tmp) == 0)
return false;
else
// error popup message
Report::Error (sformat(_("fstab already contains an entry
with mount point '%1'."), mpoint));
return true;
};
/**
* Check for the validity of a path/mountpoint:
* nonempty, fewer than 70 chars, starts with a slash.
* If invalid, a message is displayed.
* @param name path
* @return whether valid
*/
define boolean CheckPath(string name) ``{
if (size(name) > 0 &&
size(name) < 70 &&
substring(name, 0, 1) == "/")
{
return true;
}
// error popup message (spaces are now allowed)
Report::Error (sformat(_("The path entered is invalid.
It must be shorter than 70 characters
and it must begin with a slash (/).")));
return false;
};
/**
* Checks the nfs options for /etc/fstab:
* nonempty, comma separated list of foo,nofoo,bar=baz (see nfs(5))
* @param options options
* @return a translated string with error message, emtpy string if ok
*/
define string check_options (string options) ``{
// To translators: error popup
if (size (options) == 0) return _("Empty option strings are not allowed.");
if (options == "defaults") return "";
list<string> option_list = splitstring (options, ",");
//the options must be easy to sync with mount.c and nfsmount.c
// these can be negated by "no"
list<string> non_value = ["bg", "fg", "soft", "hard", "intr", "posix", "cto", "ac", "acl",
"lock", "tcp", "udp", "rdirplus",
// these are common for all fs types
"atime", "auto", "dev", "exec", "group", "owner",
"suid", "user", "users"];
// these cannot be negated
// they are not nfs specific BTW
list non_value1 = ["defaults", "async", "sync", "dirsync", "ro", "rw",
"remount", "bind", "rbind", "_netdev", ];
list with_value = ["rsize", "wsize", "timeo", "retrans", "acregmin", "acregmax",
"acdirmin", "acdirmin", "acdirmax", "actimeo", "retry", "namlen",
"port", "proto", "clientaddr", "mountport", "mounthost",
"mountprog", "mountvers", "nfsprog", "nfsvers", "vers", "sec" ];
integer i = 0;
string current_option = "";
// first fiter out non value options and its nooptions forms (see nfs(5))
option_list = filter (string e, option_list, ``(!contains (non_value, e)));
non_value = maplist (string e, non_value, ``(sformat ("no%1", e)));
option_list = filter (string e, option_list, ``(!contains (non_value, e)));
option_list = filter (string e, option_list, ``(!contains (non_value1, e)));
while (i < size (option_list))
{
string opt = option_list[i]:"";
list<string> value = splitstring (opt, "=");
string v0 = value[0]:"";
string v1 = value[1]:"";
// FIXME: this also triggers for "intr=bogus"
// To translators: error popup
if (!contains (with_value, v0)) return sformat (_("Unknown option: %1"), v0);
// To translators: error popup
if (size (value) != 2) return sformat (_("Invalid option: %1"), opt);
// To translators: error popup
if (v1 == "") return sformat (_("Empty value for option: %1"), v0);
i = i + 1;
}
return "";
}
/**
* Strips a superfluous slash off the end of a pathname.
* @param p pathname
* @return stripped pathname
*/
define string StripExtraSlash (string p) ``{
if (regexpmatch (p, "^.+/$"))
{
return regexpsub (p, "^(.+)/$", "\\1");
}
else
{
return p;
}
}
/**
* Formats hostname into form suitable for fstab.
* If given param is IPv6 then encloses it into square brackets.
*/
define string FormatHostnameForFstab( string hostname)
{
y2milestone( "FormatHostnameForFstab: hostname=%1", hostname);
if( IP::Check6( IP::UndecorateIPv6( hostname)))
return sformat( regexpmatch( hostname, "\\[.*\\]") ? "%1" : "[%1]", hostname);
return hostname;
}
/**
* Check whether pormap is installed, ask user to install it if it is missing
* @return boolean true if portmap is installed
*/
define boolean IsPortmapperInstalled( string portmapper ) ``{
return Package::Install( portmapper );
}
}