summaryrefslogtreecommitdiff
path: root/bin/meta-normalize-stdio
blob: ac5d51a45a6767420526c948fff750c9bc05bc22 (plain)
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
#!/usr/bin/env ruby
# coding: utf-8

# Copyright 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>.
# Copyright 2015 Márcio Alexandre Silva Delgado <coadde@parabola.nu>.
#
# This 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 software 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 manual; if not, see
# <http://www.gnu.org/licenses/>.

# First we define a bunch of code-generators, then at the end is a
# very neat and readable definition of the format of the YAML files.

require 'yaml'

def error(msg)
	$stderr.puts "ERROR: #{msg}"
	@err = 1
end

def warning(msg)
	$stderr.puts "WARNING: #{msg}"
end


# Generic validators/formatters

def semiordered_list(cnt, validator)
	lambda {|name,ary|
		if !ary.is_a?(Array)
			error "`#{name}' must be a list"
		else
			ary.each_index{|i| ary[i] = validator.call("#{name}[#{i}]", ary[i])}
			ary = ary.first(cnt).concat(ary.last(ary.count-cnt).sort)
		end
		ary
	}
end

def unordered_list(validator)
	semiordered_list(0, validator)
end

def _unknown(map_name, key)
	error "Unknown item: #{map_name}[#{key.inspect}]"
	0
end
def unordered_map1(validator)
	lambda {|name,hash|
		if !hash.is_a?(Hash)
			error "`#{name}' must be a map"
		else
			order = Hash[[*validator.keys.map.with_index]]
			hash = Hash[hash.sort_by{|k,v| order[k] || _unknown(name,k) }]
			hash.keys.each{|k|
				if validator[k]
					hash[k] = validator[k].call("#{name}[#{k.inspect}]", hash[k])
				end
			}
		end
		hash
	}
end

def unordered_map2(key_validator, val_validator)
	lambda {|name,hash|
		if !hash.is_a?(Hash)
			error "`#{name}' must be a map"
		else
			hash = Hash[hash.sort_by{|k,v| k}]
			hash.keys.each{|k|
				key_validator.call("#{name} key #{k.inspect}", k)
				hash[k] = val_validator.call("#{name}[#{k.inspect}]", hash[k])
			}
		end
		hash
	}
end

string = lambda {|name,str|
	if !str.is_a?(String)
		error "`#{name}' must be a string"
	else
		str
	end
}

# Regular Expression String
def restring(re)
	lambda {|name,str|
		if !str.is_a?(String)
			error "`#{name}' must be a string"
		else
			unless re =~ str
				error "`#{name}' does not match #{re.inspect}: #{str}"
			end
			str
		end
	}
end


# Specific validators/formatters

year = lambda {|name, num|
	if !num.is_a?(Fixnum)
		error "`#{name}' must be a year"
	else
		if (num < 1900 || num > 3000)
			error "`#{name}' is a number, but doesn't look like a year"
		end
		num
	end
}

# This regex is taken from http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
_email_regex = /^[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
email_list = lambda {|name, ary|
	if !ary.is_a?(Array)
		error "`#{name}' must be a list"
	elsif not ary.empty?
		preserve = 1
		if ary.first.end_with?("@parabola.nu") and ary.count >= 2
			preserve = 2
		end
		ary = semiordered_list(preserve, restring(_email_regex)).call(name, ary)
	end
	ary
}

shell = lambda {|name, sh|
	if !sh.is_a?(String)
		error "`#{name}' must be a string"
	else
		@valid_shells ||= open("/etc/shells").read.split("\n")
						  .find_all{|line| /^[^\#]/ =~ line}
						  .push("/usr/bin/nologin")
		unless @valid_shells.include?(sh)
			warning "shell not listed in /etc/shells: #{sh}"
		end
	end
	sh
}


# The format of the YAML files

format = unordered_map1(
	{
		"username" => restring(/^[a-z][a-z0-9-]*$/),
		"fullname" => string,
		"email" => email_list,
		"groups" => semiordered_list(1, string),
		"pgp_keyid" => restring(/^[0-9A-F]{40}$/),
		"pgp_revoked_keyids" => unordered_list(restring(/^[0-9A-F]{40}$/)),
		"ssh_keys" => unordered_map2(string, string),
		"shell" => shell,
		"extra" => unordered_map1(
			{
				"alias" => string,
				"other_contact" => string,
				"roles" => string,
				"website" => string,
				"occupation" => string,
				"yob" => year,
				"location" => string,
				"languages" => string,
				"interests" => string,
				"favorite_distros" => string,
			})
	})



@err = 0
user = format.call("user", YAML::load(STDIN))
if @err != 0
	exit @err
end
print user.to_yaml