Compare commits
2 Commits
61dbcae179
...
2b7dc56f94
Author | SHA1 | Date |
---|---|---|
Daan De Meyer | 2b7dc56f94 | |
Daan De Meyer | 4d57cdd7ee |
|
@ -29,6 +29,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
#include "glyph-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "inotify-util.h"
|
||||
#include "io-util.h"
|
||||
|
@ -255,6 +256,66 @@ int ask_string(char **ret, const char *text, ...) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool press_any_key_to_proceed(void) {
|
||||
char k = 0;
|
||||
bool need_nl = true;
|
||||
|
||||
puts("-- Press any key to proceed --");
|
||||
|
||||
(void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
|
||||
|
||||
if (need_nl)
|
||||
putchar('\n');
|
||||
|
||||
return k != 'q';
|
||||
}
|
||||
|
||||
int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
|
||||
unsigned break_lines, break_modulo;
|
||||
size_t n, per_column, i, j;
|
||||
|
||||
assert(n_columns > 0);
|
||||
|
||||
n = strv_length(x);
|
||||
per_column = DIV_ROUND_UP(n, n_columns);
|
||||
|
||||
break_lines = lines();
|
||||
if (break_lines > 2)
|
||||
break_lines--;
|
||||
|
||||
/* The first page gets two extra lines, since we want to show
|
||||
* a title */
|
||||
break_modulo = break_lines;
|
||||
if (break_modulo > 3)
|
||||
break_modulo -= 3;
|
||||
|
||||
for (i = 0; i < per_column; i++) {
|
||||
|
||||
for (j = 0; j < n_columns; j++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
|
||||
if (j * per_column + i >= n)
|
||||
break;
|
||||
|
||||
e = ellipsize(x[j * per_column + i], width, percentage);
|
||||
if (!e)
|
||||
return log_oom();
|
||||
|
||||
printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
|
||||
/* on the first screen we reserve 2 extra lines for the title */
|
||||
if (i % break_lines == break_modulo) {
|
||||
if (!press_any_key_to_proceed())
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int open_terminal(const char *name, int mode) {
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
unsigned c = 0;
|
||||
|
|
|
@ -78,6 +78,8 @@ int chvt(int vt);
|
|||
int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
|
||||
int ask_char(char *ret, const char *replies, const char *text, ...) _printf_(3, 4);
|
||||
int ask_string(char **ret, const char *text, ...) _printf_(2, 3);
|
||||
bool press_any_key_to_proceed(void);
|
||||
int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage);
|
||||
|
||||
int vt_disallocate(const char *name);
|
||||
|
||||
|
|
|
@ -93,19 +93,6 @@ STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
|
|||
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
|
||||
|
||||
static bool press_any_key(void) {
|
||||
char k = 0;
|
||||
bool need_nl = true;
|
||||
|
||||
puts("-- Press any key to proceed --");
|
||||
|
||||
(void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
|
||||
|
||||
if (need_nl)
|
||||
putchar('\n');
|
||||
|
||||
return k != 'q';
|
||||
}
|
||||
|
||||
static void print_welcome(int rfd) {
|
||||
_cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
|
||||
|
@ -141,57 +128,11 @@ static void print_welcome(int rfd) {
|
|||
|
||||
printf("\nPlease configure your system!\n\n");
|
||||
|
||||
press_any_key();
|
||||
press_any_key_to_proceed();
|
||||
|
||||
done = true;
|
||||
}
|
||||
|
||||
static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
|
||||
unsigned break_lines, break_modulo;
|
||||
size_t n, per_column, i, j;
|
||||
|
||||
assert(n_columns > 0);
|
||||
|
||||
n = strv_length(x);
|
||||
per_column = DIV_ROUND_UP(n, n_columns);
|
||||
|
||||
break_lines = lines();
|
||||
if (break_lines > 2)
|
||||
break_lines--;
|
||||
|
||||
/* The first page gets two extra lines, since we want to show
|
||||
* a title */
|
||||
break_modulo = break_lines;
|
||||
if (break_modulo > 3)
|
||||
break_modulo -= 3;
|
||||
|
||||
for (i = 0; i < per_column; i++) {
|
||||
|
||||
for (j = 0; j < n_columns; j++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
|
||||
if (j * per_column + i >= n)
|
||||
break;
|
||||
|
||||
e = ellipsize(x[j * per_column + i], width, percentage);
|
||||
if (!e)
|
||||
return log_oom();
|
||||
|
||||
printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
|
||||
/* on the first screen we reserve 2 extra lines for the title */
|
||||
if (i % break_lines == break_modulo) {
|
||||
if (!press_any_key())
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
|
||||
int r;
|
||||
|
||||
|
|
|
@ -2461,7 +2461,7 @@ static int create_interactively(void) {
|
|||
continue;
|
||||
}
|
||||
|
||||
r = userdb_by_name(username, USERDB_SUPPRESS_SHADOW, /* ret= */ NULL);
|
||||
r = userdb_by_name(username, USERDB_SUPPRESS_SHADOW|USERDB_EXCLUDE_DYNAMIC_USER, /* ret= */ NULL);
|
||||
if (r == -ESRCH)
|
||||
break;
|
||||
if (r < 0)
|
||||
|
@ -2474,6 +2474,109 @@ static int create_interactively(void) {
|
|||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to set userName field: %m");
|
||||
|
||||
_cleanup_(userdb_iterator_freep) UserDBIterator *iterator = NULL;
|
||||
_cleanup_strv_free_ char **available = NULL, **groups = NULL;
|
||||
|
||||
r = groupdb_all(USERDB_SUPPRESS_SHADOW|USERDB_EXCLUDE_DYNAMIC_USER, &iterator);
|
||||
if (r == -ENOLINK)
|
||||
log_debug_errno(r, "No entries found. (Didn't check via Varlink.)");
|
||||
else if (r == -ESRCH)
|
||||
log_debug_errno(r, "No entries found.");
|
||||
else if (r < 0)
|
||||
return log_error_errno(r, "Failed to enumerate groups: %m");
|
||||
else {
|
||||
for (;;) {
|
||||
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL;
|
||||
|
||||
r = groupdb_iterator_get(iterator, &gr);
|
||||
if (r == -ESRCH)
|
||||
break;
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed acquire next group: %m");
|
||||
|
||||
if (!IN_SET(group_record_disposition(gr), USER_REGULAR, USER_SYSTEM))
|
||||
continue;
|
||||
|
||||
if (group_record_disposition(gr) == USER_REGULAR) {
|
||||
_cleanup_(user_record_unrefp) UserRecord *ur = NULL;
|
||||
|
||||
r = userdb_by_name(gr->group_name, USERDB_SUPPRESS_SHADOW|USERDB_EXCLUDE_DYNAMIC_USER, &ur);
|
||||
if (r < 0 && r != -ESRCH)
|
||||
return log_error_errno(r, "Failed to check if matching user exists for group '%s': %m", gr->group_name);
|
||||
|
||||
if (r >= 0 && user_record_disposition(ur) == USER_REGULAR)
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
r = strv_extend(&available, gr->group_name);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
unsigned u;
|
||||
|
||||
r = ask_string(&s,
|
||||
"%s Please enter an auxiliary group for user %s (empty to continue, \"list\" to list available groups): ",
|
||||
special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), username);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to query user for auxiliary group: %m");
|
||||
|
||||
if (isempty(s))
|
||||
break;
|
||||
|
||||
if (streq(s, "list")) {
|
||||
r = show_menu(available, /*n_columns=*/ 3, /*width=*/ 20, /*percentage=*/ 60);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
putchar('\n');
|
||||
continue;
|
||||
};
|
||||
|
||||
r = safe_atou(s, &u);
|
||||
if (r >= 0) {
|
||||
if (u <= 0 || u > strv_length(available)) {
|
||||
log_error("Specified entry number out of range.");
|
||||
continue;
|
||||
}
|
||||
|
||||
log_info("Selected '%s'.", available[u-1]);
|
||||
|
||||
r = strv_extend(&groups, available[u-1]);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!valid_user_group_name(s, /* flags= */ 0)) {
|
||||
log_notice("Specified group name is not a valid UNIX group name, try again: %s", s);
|
||||
continue;
|
||||
}
|
||||
|
||||
r = groupdb_by_name(s, USERDB_SUPPRESS_SHADOW|USERDB_EXCLUDE_DYNAMIC_USER, /*ret=*/ NULL);
|
||||
if (r == -ESRCH) {
|
||||
log_notice("Specified auxiliary group does not exist, try again: %s", s);
|
||||
continue;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to check if specified group '%s' already exists: %m", s);
|
||||
|
||||
r = strv_extend(&groups, s);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
if (groups) {
|
||||
r = sd_json_variant_set_field_strv(&arg_identity_extra, "memberOf", groups);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to set memberOf field: %m");
|
||||
}
|
||||
|
||||
return create_home_common(/* input= */ NULL);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue