diff --git a/lib/McdeSys/common.c b/lib/McdeSys/common.c index cbb33a0f..02d0f0e0 100644 --- a/lib/McdeSys/common.c +++ b/lib/McdeSys/common.c @@ -513,6 +513,19 @@ mcde_sys_file_locked_get(const char *path, int *locked) return mcde_sys_file_locked_get_backend(path, locked); } +/* a name the system takes as its own: letters, digits, '-' and '.', not + * starting with '-' */ +int +mcde_sys_hostname_set(const char *name) +{ + if (name == NULL || name[0] == '\0' || name[0] == '-' || + strspn(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789.-") != strlen(name)) + return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is not a name a " + "computer can take.", name != NULL ? name : ""); + return mcde_sys_hostname_set_backend(name); +} + int mcde_sys_ntp_get(int *on, char *server, size_t len) { diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 1fc69cc4..2bd60b90 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -981,6 +981,39 @@ mcde_sys_file_locked_set_backend(const char *path, int locked) return MCDE_SYS_OK; } +/* ---- the computer's name: /etc/hostname and hostnamectl(1), else + * hostname(1) (root) */ +int +mcde_sys_hostname_set_backend(const char *name) +{ + static const char *const hcPaths[] = { "/usr/bin/hostnamectl", + "/bin/hostnamectl", NULL }; + static const char *const hnPaths[] = { "/bin/hostname", + "/usr/bin/hostname", NULL }; + const char *hc = mcde_sys_which(hcPaths), *hn = mcde_sys_which(hnPaths); + FILE *fp; + char *argv[4]; + + if ((fp = fopen("/etc/hostname", "w")) == NULL) + return mcde_sys_fail(MCDE_SYS_EPERM, "Cannot write " + "/etc/hostname: %s.", strerror(errno)); + fprintf(fp, "%s\n", name); + fclose(fp); + if (hc != NULL) { + argv[0] = (char *) hc; + argv[1] = (char *) "set-hostname"; + argv[2] = (char *) name; + argv[3] = NULL; + return mcde_sys_run(argv); + } + if (hn == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No hostname tool."); + argv[0] = (char *) hn; + argv[1] = (char *) name; + argv[2] = NULL; + return mcde_sys_run(argv); +} + /* ---- NTP: systemd-timesyncd through timedatectl(1), the server in * /etc/systemd/timesyncd.conf ("NTP=", timesyncd.conf(5)) */ static const char *const timedatectlPaths[] = { "/usr/bin/timedatectl", diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index c387f323..7e39a1ec 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -1358,6 +1358,24 @@ mcde_sys_file_locked_set_backend(const char *path, int locked) return MCDE_SYS_OK; } +/* ---- the computer's name: /etc/myname and hostname(1) (root) */ +int +mcde_sys_hostname_set_backend(const char *name) +{ + FILE *fp; + char *argv[3]; + + if ((fp = fopen("/etc/myname", "w")) == NULL) + return mcde_sys_fail(MCDE_SYS_EPERM, "Cannot write /etc/myname: " + "%s.", strerror(errno)); + fprintf(fp, "%s\n", name); + fclose(fp); + argv[0] = (char *) "/bin/hostname"; + argv[1] = (char *) name; + argv[2] = NULL; + return mcde_sys_run(argv); +} + /* ---- NTP: the base system's ntpd, "ntpd=YES" in rc.conf and the pool * line of ntp.conf(5) */ #define NTP_CONF "/etc/ntp.conf" diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 2a5bdc70..de1b0dee 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -69,6 +69,9 @@ int mcde_sys_file_locked_set_backend(const char *path, int locked); * switch it on or off (needs root) */ int mcde_sys_ntp_get_backend(int *on, char *server, size_t len); int mcde_sys_ntp_set_backend(int on, const char *server); +/* the backend: the computer's name, now and at boot (rc.conf/myname on + * NetBSD, /etc/hostname and hostnamectl on Linux) */ +int mcde_sys_hostname_set_backend(const char *name); /* the backend, for share.c: where the servers keep their configuration * (NULL: not on this system), and how an exports(5) line reads */ const char *mcde_sys_exports_file(void); diff --git a/programs/dtsystemsettings/main.c b/programs/dtsystemsettings/main.c index d1bc3817..8a6903e0 100644 --- a/programs/dtsystemsettings/main.c +++ b/programs/dtsystemsettings/main.c @@ -1212,6 +1212,22 @@ SetTimeCommand(const char *when) return r == MCDE_SYS_OK ? 0 : 1; } +/* dtsystemsettings -set-hostname NAME, in the terminal of "Edit..." (root) */ +static int +SetHostnameCommand(const char *name) +{ + int r = mcde_sys_hostname_set(name); + + if (r == MCDE_SYS_OK) + printf("The computer's name is %s now.\n", name); + else + printf("The name was not changed: %s\n", mcde_sys_error()); + printf("\nPress Return to close this window.\n"); + fflush(stdout); + (void) getchar(); + return r == MCDE_SYS_OK ? 0 : 1; +} + /* dtsystemsettings -set-ntp on|off SERVER, in the terminal of "Apply Time * Service" (root) */ static int @@ -1251,6 +1267,8 @@ main(int argc, char **argv) return SetTimezoneCommand(argv[i + 1]); else if (strcmp(argv[i], "-set-time") == 0 && i + 1 < argc) return SetTimeCommand(argv[++i]); + else if (strcmp(argv[i], "-set-hostname") == 0 && i + 1 < argc) + return SetHostnameCommand(argv[++i]); else if (strcmp(argv[i], "-set-ntp") == 0 && i + 1 < argc) { int on = strcmp(argv[i + 1], "on") == 0; @@ -1267,6 +1285,7 @@ main(int argc, char **argv) fprintf(stderr, "usage: dtsystemsettings [-pane ID | " "-apply-update | -apply-displays | -set-timezone ZONE | " "-set-time EPOCH | -set-ntp on|off [SERVER] | " + "-set-hostname NAME | " "-join-wifi IF [SSID-HEX] | -renew-dhcp IF]\n"); return 1; } diff --git a/programs/dtsystemsettings/sharing.c b/programs/dtsystemsettings/sharing.c index 66b4a7da..2edc87cb 100644 --- a/programs/dtsystemsettings/sharing.c +++ b/programs/dtsystemsettings/sharing.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -53,10 +54,49 @@ static const struct { }; static Widget toggles[MCDE_SHARE_COUNT], states[MCDE_SHARE_COUNT]; +static Widget addrs[MCDE_SHARE_COUNT], detail[MCDE_SHARE_COUNT]; +static Widget svcList, nameField, nameNote, foldersBox; static Widget shareList, shareNote; static char shares[MAX_SHARES][MCDE_SYS_SHARE_LEN]; static int nshares; static int isRoot; +static int chosen; + +/* the address others use for this service: the first interface with one */ +static void +AddressOf(McdeSysShareService s, const char *host, char *buf, size_t len) +{ + McdeSysInterface ifs[16]; + int n = mcde_sys_interfaces(ifs, 16), i; + const char *addr = NULL; + + for (i = 0; i < n; i++) + if (ifs[i].up && ifs[i].address[0] != '\0') { + addr = ifs[i].address; + break; + } + switch (s) { + case MCDE_SHARE_SSH: + (void)snprintf(buf, len, "ssh %s@%s", getenv("USER") != NULL ? + getenv("USER") : "you", addr != NULL ? addr : host); + break; + case MCDE_SHARE_SMB: + (void)snprintf(buf, len, "smb://%s/", addr != NULL ? addr : + host); + break; + case MCDE_SHARE_NFS: + (void)snprintf(buf, len, "%s:/folder", addr != NULL ? addr : + host); + break; + case MCDE_SHARE_AFP: + (void)snprintf(buf, len, "afp://%s/", addr != NULL ? addr : + host); + break; + default: + (void)snprintf(buf, len, "the AppleTalk zone of %s", host); + break; + } +} static void ReadServices(void) @@ -65,26 +105,30 @@ ReadServices(void) int s; if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK) - snprintf(host, sizeof host, "this computer"); + (void)snprintf(host, sizeof host, "this computer"); for (s = 0; s < MCDE_SHARE_COUNT; s++) { McdeSysServiceState st; - char how[300]; + char addr[300]; if (mcde_sys_service_state((McdeSysShareService) s, &st) != MCDE_SYS_OK) memset(&st, 0, sizeof st); XmToggleButtonSetState(toggles[s], st.enabled, False); XtSetSensitive(toggles[s], isRoot && st.installed); - snprintf(how, sizeof how, service[s].how, host); + AddressOf((McdeSysShareService) s, host, addr, sizeof addr); + XtVaSetValues(addrs[s], XmNlabelString, XmStringCreateLocalized( + addr), NULL); if (!st.installed) - snprintf(text, sizeof text, "not installed"); + (void)snprintf(text, sizeof text, "not installed on " + "this computer"); else if (st.running) - snprintf(text, sizeof text, "on -- others reach it at %s", - how); + (void)snprintf(text, sizeof text, "On -- others reach " + "it at %s", addr); else if (st.enabled) - snprintf(text, sizeof text, "on at start, not running now"); + (void)snprintf(text, sizeof text, "On at start, not " + "running now (%s)", addr); else - snprintf(text, sizeof text, "off"); + (void)snprintf(text, sizeof text, "Off -- %s", addr); SetText(states[s], text); } } @@ -100,6 +144,71 @@ ServiceChanged(Widget w, XtPointer data, XtPointer call) ReadServices(); } +/* the service chosen at the left: its page at the right */ +static void +ServiceChosen(Widget w, XtPointer data, XtPointer call) +{ + XmListCallbackStruct *c = (XmListCallbackStruct *) call; + int i = c->item_position - 1; + + (void)w; + (void)data; + if (i < 0 || i >= MCDE_SHARE_COUNT) + return; + chosen = i; + for (i = 0; i < MCDE_SHARE_COUNT; i++) + if (i == chosen) + XtManageChild(detail[i]); + else + XtUnmanageChild(detail[i]); + XtManageChild(foldersBox); + XtUnmanageChild(foldersBox); + /* the folders belong to the file sharing services */ + if (chosen == MCDE_SHARE_SMB || chosen == MCDE_SHARE_NFS || + chosen == MCDE_SHARE_AFP) + XtManageChild(foldersBox); +} + +/* the host name: NAME.local, the name the neighbours see */ +static void +NameEdit(Widget w, XtPointer data, XtPointer call) +{ + char *name = XmTextFieldGetString(nameField); + char cmd[512], line[512]; + int i; + + (void)w; + (void)data; + (void)call; + if (name == NULL) + return; + /* a host name: letters, digits and '-', not starting with '-' */ + if (name[0] == '-' || name[0] == '\0' || + strspn(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789-.") != strlen(name)) { + SetText(nameNote, "A computer name has letters, digits and '-'."); + XtFree(name); + return; + } + if (!isRoot) { + (void)snprintf(cmd, sizeof cmd, "dtsystemsettings -set-hostname " + "%s", name); + AsRoot("Change Computer Name", cmd, line, sizeof line); + if (line[0] != '\0') + Spawn(line); + SetText(nameNote, "The administrator's password is asked for " + "in the window that opened."); + XtFree(name); + return; + } + if (mcde_sys_hostname_set(name) != MCDE_SYS_OK) + SetText(nameNote, mcde_sys_error()); + else + SetText(nameNote, "The name is set; the neighbours learn it " + "from the network."); + XtFree(name); +} + static void FillShares(void) { @@ -119,7 +228,7 @@ WriteShares(Widget w) { if (mcde_sys_shares_set(shares, (size_t) nshares) != MCDE_SYS_OK) { Message(w, mcde_sys_error(), 1); - nshares = mcde_sys_shares(shares, MAX_SHARES); + return; } FillShares(); } @@ -127,23 +236,24 @@ WriteShares(Widget w) static void FolderChosen(Widget w, XtPointer data, XtPointer call) { - XmFileSelectionBoxCallbackStruct *cb = + XmFileSelectionBoxCallbackStruct *c = (XmFileSelectionBoxCallbackStruct *) call; - char *dir = NULL; + char *path = NULL; - (void) data; - if (cb->reason == XmCR_OK && nshares < MAX_SHARES && - (dir = XmStringUnparse(cb->value, NULL, XmCHARSET_TEXT, - XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL)) != NULL && dir[0] == '/') { - size_t n = strlen(dir); - - while (n > 1 && dir[n - 1] == '/') - dir[--n] = '\0'; - snprintf(shares[nshares++], MCDE_SYS_SHARE_LEN, "%s", dir); - WriteShares(XtParent(w)); + (void)data; + if (c != NULL && c->reason == XmCR_OK && c->value != NULL && + XmStringGetLtoR(c->value, XmFONTLIST_DEFAULT_TAG, &path) && + path != NULL) { + if (nshares < MAX_SHARES) { + (void)snprintf(shares[nshares], sizeof shares[0], "%s", + path); + nshares++; + WriteShares(w); + } + XtFree(path); } - XtFree(dir); - XtDestroyWidget(XtParent(w)); + if (w != NULL) + XtDestroyWidget(XtParent(w)); } static void @@ -202,48 +312,98 @@ Widget MakeSharing(Widget parent) { Widget box = XtVaCreateWidget("sharing", xmRowColumnWidgetClass, parent, - XmNspacing, 4, NULL); - Widget row, table; - Arg a[4]; - int s; + XmNspacing, 6, NULL); + Widget nameRow, nameLab, edit, split, leftCol, rightCol; + Widget row; + XmString s; + char host[256], line[512]; + int i; isRoot = geteuid() == 0; - (void) Text(box, "label", "What this computer offers others on the " - "network:"); - table = XtVaCreateManagedWidget("services", xmRowColumnWidgetClass, - box, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_COLUMN, - XmNnumColumns, MCDE_SHARE_COUNT, XmNisAligned, False, - XmNspacing, 2, NULL); - /* a row each: the switch, then what it is now; packed in columns, a - * vertical row column fills its rows left to right */ - XtVaSetValues(table, XmNorientation, XmHORIZONTAL, XmNnumColumns, - MCDE_SHARE_COUNT, NULL); - for (s = 0; s < MCDE_SHARE_COUNT; s++) { - toggles[s] = XtVaCreateManagedWidget(service[s].label, - xmToggleButtonWidgetClass, table, - XmNalignment, XmALIGNMENT_BEGINNING, NULL); - XtAddCallback(toggles[s], XmNvalueChangedCallback, - ServiceChanged, (XtPointer) (intptr_t) s); - states[s] = Text(table, "state", ""); - } + if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK) + (void)snprintf(host, sizeof host, "computer"); + + /* the name the neighbours see */ + nameRow = XtVaCreateManagedWidget("nameRow", xmRowColumnWidgetClass, + box, XmNorientation, XmHORIZONTAL, XmNspacing, 6, + XmNmarginWidth, 0, NULL); + nameLab = Text(nameRow, "nameLabel", "Computer Name:"); + (void)nameLab; + nameField = XtVaCreateManagedWidget("computerName", + xmTextFieldWidgetClass, nameRow, XmNcolumns, 20, NULL); + XmTextFieldSetString(nameField, host); + edit = Button(nameRow, "Edit...", NameEdit, NULL); + (void)edit; + nameNote = Text(box, "nameNote", ""); + (void)snprintf(line, sizeof line, "Computers on your local network can " + "access this computer at %s.local.", host); + (void) Text(box, "netLine", line); XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL); - (void) Text(box, "label", "Folders shared by SMB, NFS and AFP:"); - XtSetArg(a[0], XmNvisibleItemCount, 5); - XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT); - XtSetArg(a[2], XmNwidth, 460); - shareList = XmCreateScrolledList(box, "shares", a, 3); - XtManageChild(shareList); - row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box, - XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL); + + /* the services at the left, what the chosen one is at the right */ + split = XtVaCreateManagedWidget("split", xmRowColumnWidgetClass, box, + XmNorientation, XmHORIZONTAL, XmNspacing, 12, XmNmarginWidth, 0, + NULL); + leftCol = XtVaCreateManagedWidget("serviceCol", xmRowColumnWidgetClass, + split, XmNspacing, 4, XmNmarginWidth, 0, NULL); + (void) Text(leftCol, "serviceLabel", "On Service"); + svcList = XmCreateScrolledList(leftCol, "services", NULL, 0); + XtVaSetValues(svcList, XmNvisibleItemCount, MCDE_SHARE_COUNT, + XmNselectionPolicy, XmBROWSE_SELECT, XmNwidth, 250, NULL); + for (i = 0; i < MCDE_SHARE_COUNT; i++) { + s = XmStringCreateLocalized((char *) service[i].label); + XmListAddItemUnselected(svcList, s, 0); + XmStringFree(s); + } + XtAddCallback(svcList, XmNbrowseSelectionCallback, ServiceChosen, NULL); + XtManageChild(svcList); + + rightCol = XtVaCreateManagedWidget("detailCol", xmRowColumnWidgetClass, + split, XmNspacing, 4, XmNmarginWidth, 0, NULL); + for (i = 0; i < MCDE_SHARE_COUNT; i++) { + detail[i] = XtVaCreateWidget("servicePage", + xmRowColumnWidgetClass, rightCol, XmNspacing, 4, + XmNmarginWidth, 0, NULL); + toggles[i] = XtVaCreateManagedWidget(service[i].label, + xmToggleButtonWidgetClass, detail[i], + XmNalignment, XmALIGNMENT_BEGINNING, NULL); + XtAddCallback(toggles[i], XmNvalueChangedCallback, + ServiceChanged, (XtPointer) (intptr_t) i); + states[i] = Text(detail[i], "state", ""); + addrs[i] = Text(detail[i], "address", ""); + } + chosen = MCDE_SHARE_SSH; + XtManageChild(detail[chosen]); + + /* the folders of the file sharing services */ + foldersBox = XtVaCreateManagedWidget("folders", + xmRowColumnWidgetClass, rightCol, XmNspacing, 4, + XmNmarginWidth, 0, NULL); + (void) Text(foldersBox, "foldersLabel", "Shared Folders:"); + { + Arg a[3]; + + XtSetArg(a[0], XmNvisibleItemCount, 4); + XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT); + XtSetArg(a[2], XmNwidth, 420); + shareList = XmCreateScrolledList(foldersBox, "shares", a, 3); + XtManageChild(shareList); + } + row = XtVaCreateManagedWidget("folderButtons", xmRowColumnWidgetClass, + foldersBox, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL); XtSetSensitive(Button(row, "Add...", AddShare, NULL), isRoot); XtSetSensitive(Button(row, "Remove", RemoveShare, NULL), isRoot); + XtUnmanageChild(foldersBox); /* SSH at first: no folders */ + + XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL); if (!isRoot) { - (void) Button(row, "Unlock...", Unlock, NULL); + (void) Button(box, "Unlock...", Unlock, NULL); shareNote = Text(box, "note", "Changing what is shared needs the " "administrator: Unlock... asks for the password."); } else shareNote = Text(box, "note", ""); + XtSetSensitive(nameField, True); nshares = mcde_sys_shares(shares, MAX_SHARES); FillShares(); ReadServices();