Install a moduli(5) manual page.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / lua / configure / menu / 200_add_user.lua
1 -- $Id: 200_add_user.lua,v 1.7 2005/03/29 12:12:15 den Exp $
2
3 require "gettext"
4
5 local is_gecos_clean = function(gecos)
6         local i, char
7
8         i = 1
9         while i <= string.len(gecos) do
10                 char = string.sub(gecos, i, i)
11                 if string.find(char, "%c") or             -- no ctrl chars
12                    string.byte(char) == 127 or            -- no 'DEL' char
13                    string.find(":!@", char, 1, true) then -- none of these
14                         return false
15                 end
16                 i = i + 1
17         end
18
19         return true
20 end
21
22 local is_name_clean = function(name)
23         local i, char
24
25         i = 1
26         while i <= string.len(name) do
27                 char = string.sub(name, i, i)
28                 if string.find(char, "%c") or           -- no ctrl chars
29                    string.byte(char) == 127 or          -- no 'DEL' char
30                    string.byte(char) > 127 or           -- no 8-bit chars
31                                                         -- and none of these:
32                    string.find(" ,\t:+&#%^()!@~*?<>=|\\/\"", char, 1, true) or
33                    (char == "-" and i == 1) or          -- no '-' at start
34                                                         -- '$' only at end:
35                    (char == "$" and i ~= string.len(name)) then
36                         return false
37                 end
38                 i = i + 1
39         end
40
41         return true
42 end
43
44 local add_user = function()
45         local done = false
46         local result
47         local cmds
48
49         local form = {
50             id = "add_user",
51             name = _("Add User"),
52             short_desc = _("Here you can add a user to an installed system.\n\n" ..
53                 "You can leave the Home Directory, User ID, and Login Group "   ..
54                 "fields empty if you want these items to be automatically "     ..
55                 "allocated by the system."),
56             fields = {
57                 {
58                     id = "username",
59                     name = _("Username"),
60                     short_desc = _("Enter the username the user will log in as")
61                 },
62                 {
63                     id = "gecos",
64                     name = _("Real Name"),
65                     short_desc = _("Enter the real name (or GECOS field) of this user")
66                 },
67                 {
68                     id = "passwd_1",
69                     name = _("Password"),
70                     short_desc = _("Enter the user's password (will not be displayed)"),
71                     obscured = "true"
72                 },
73                 {
74                     id = "passwd_2",
75                     name = _("Password (Again)"),
76                     short_desc = _("Re-enter the user's password to confirm"),
77                     obscured = "true"
78                 },
79                 {
80                     id = "shell",
81                     name = _("Shell"),
82                     short_desc = _("Enter the full path to the user's shell program")
83                 },
84                 {
85                     id = "home",
86                     name = _("Home Directory"),
87                     short_desc = _("Enter the full path to the user's home directory, or leave blank")
88                 },
89                 {
90                     id = "uid",
91                     name = _("User ID"),
92                     short_desc = _("Enter this account's numeric user id, or leave blank")
93                 },
94                 {
95                     id = "group",
96                     name = _("Login Group"),
97                     short_desc = _("Enter the primary group for this account, or leave blank")
98                 },
99                 {
100                     id = "groups",
101                     name = _("Other Group Memberships"),
102                     short_desc = _(
103                         "Enter a comma-separated list of other groups " ..
104                         "that this user should belong to"
105                     )
106                 }
107             },
108             actions = {
109                 {
110                     id = "ok",
111                     name = _("Accept and Add User")
112                 },
113                 {
114                     id = "cancel",
115                     name = _("Return to Configure Menu")
116                 }
117             },
118             datasets = {
119                 {
120                     username = "",
121                     gecos = "",
122                     passwd_1 = "",
123                     passwd_2 = "",
124                     shell = "/bin/tcsh",
125                     home = "",
126                     uid = "",
127                     group = "",
128                     groups = ""
129                 }
130             }
131         }
132
133         while not done do
134                 result = App.ui:present(form)
135                 if result.action_id == "ok" then
136                         form.datasets = result.datasets
137
138                         --
139                         -- Fetch form field values.
140                         --
141                         local username  = result.datasets[1].username
142                         local gecos     = result.datasets[1].gecos
143                         local passwd_1  = result.datasets[1].passwd_1
144                         local passwd_2  = result.datasets[1].passwd_2
145                         local shell     = result.datasets[1].shell
146                         local home      = result.datasets[1].home
147                         local uid       = result.datasets[1].uid
148                         local group     = result.datasets[1].group
149                         local groups    = result.datasets[1].groups
150
151                         --
152                         -- Valid field values.
153                         --
154
155                         if string.len(username) == 0 then
156                                 App.ui:inform(_(
157                                     "You must enter a username."
158                                 ))
159                         elseif passwd_1 ~= passwd_2 then
160                                 App.ui:inform(_(
161                                     "The passwords do not match."
162                                 ))
163                         elseif not is_name_clean(username) then
164                                 App.ui:inform(_(
165                                     "The username contains illegal characters."
166                                 ))
167                         elseif not is_gecos_clean(gecos) then
168                                 App.ui:inform(_(
169                                     "The text specified in the Real Name " ..
170                                     "field contains illegal characters."
171                                 ))
172                         elseif not is_name_clean(group) then
173                                 App.ui:inform(_(
174                                     "The name of the login group contains " ..
175                                     "illegal characters."
176                                 ))
177                         --[[
178                     !assert_clean(a->c, _("Home Directory"), home, FILENAME_NOT_ALLOWED) ||
179                     !assert_clean(a->c, _("User ID"), uid, PW_NOT_ALLOWED) ||
180                     !assert_clean(a->c, _("Group Memberships"), groups, MEMBERSHIPS_NOT_ALLOWED)) {
181                         done = 0;
182                         ]]--
183                         elseif not FileSystem.is_program(App.dir.root .. shell) and
184                                shell ~= "/nonexistent" then
185                                 App.ui:inform(_(
186                                     "The selected shell does not exist on the system."
187                                 ))
188                         else
189                                 local cmds = CmdChain.new()
190
191                                 App.state.target:cmds_add_user(cmds, {
192                                     username = username,
193                                     gecos = gecos,
194                                     shell = shell,
195                                     uid = uid,
196                                     group = group,
197                                     home = home,
198                                     groups = groups,
199                                     password = passwd_1
200                                 })
201                                 if cmds:execute() then
202                                         App.ui:inform(_(
203                                             "User `%s' was added.",
204                                             username
205                                         ))
206                                         done = true
207                                 else
208                                         App.ui:inform(_(
209                                             "User was not successfully added."
210                                         ))
211                                 end
212                         end
213                 else
214                         -- Cancelled.
215                         done = true
216                 end
217         end
218 end
219
220 return {
221     name = _("Add User"),
222     effect = function()
223         add_user()
224         return Menu.CONTINUE
225     end
226 }