ask-password.c revision 490aed584944b684026a3fd01f8d81f2881e38d6
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signalfd.h>
#include <getopt.h>
#include "log.h"
#include "macro.h"
#include "util.h"
static const char *arg_message = NULL;
static int create_socket(char **name) {
int fd;
union {
struct sockaddr_un un;
} sa;
int one = 1, r;
char *c;
log_error("socket() failed: %m");
return -errno;
}
snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, "/org/freedesktop/systemd1/ask-password/%llu", random_ull());
r = -errno;
log_error("bind() failed: %m");
goto fail;
}
r = -errno;
log_error("SO_PASSCRED failed: %m");
goto fail;
}
r = -ENOMEM;
log_error("Out of memory");
goto fail;
}
*name = c;
return fd;
fail:
return r;
}
static int help(void) {
printf("%s [OPTIONS...] MESSAGE\n\n"
"Query the user for a passphrase.\n\n"
" -h --help Show this help\n"
" --icon=NAME Icon name\n"
" --timeout=USEC Timeout in usec\n",
return 0;
}
enum {
ARG_ICON = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_ICON:
break;
case ARG_TIMEOUT:
return -EINVAL;
}
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
help();
return -EINVAL;
}
return 0;
}
char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
char *socket_name = NULL;
log_open();
r = k < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
goto finish;
}
log_error("Failed to create password file: %m");
goto finish;
}
log_error("Failed to allocate FILE: %m");
goto finish;
}
fd = -1;
log_error("signalfd(): %m");
goto finish;
}
goto finish;
fprintf(f,
"[Ask]\n"
"Socket=%s\n"
"NotAfter=%llu\n",
(unsigned long long) not_after);
if (arg_message)
if (arg_icon)
fflush(f);
if (ferror(f)) {
log_error("Failed to write query file: %m");
goto finish;
}
log_error("Failed to rename query file: %m");
goto finish;
}
for (;;) {
enum {
};
union {
} control;
ssize_t n;
continue;
goto finish;
}
if (k <= 0) {
log_notice("Timed out");
goto finish;
}
break;
log_error("Unexpected poll() event.");
goto finish;
}
continue;
log_error("recvmsg() failed: %m");
goto finish;
}
if (n <= 0) {
log_error("Message too short");
continue;
}
log_warning("Received message without credentials. Ignoring.");
continue;
}
log_warning("Got request from unprivileged user. Ignoring.");
continue;
}
if (passphrase[0] == '+') {
passphrase[n] = 0;
} else if (passphrase[0] == '-')
goto finish;
else {
log_error("Invalid packet");
continue;
}
break;
}
r = EXIT_SUCCESS;
if (fd >= 0)
if (socket_fd >= 0)
if (f)
fclose(f);
if (final[0])
return r;
}