37 #include "ac/getopt.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
52 #if defined(HAVE_SASL_SASL_H)
53 #include <sasl/sasl.h>
54 #elif defined (HAVE_SASL_H)
62 const unsigned short int PORT = 5555;
71 "\t\t'i':\tswitches the incremental mode on\n"
72 "\t\t'p':\tincrements the drive\n"
73 "\t\t'm':\tdecrements the drive\n"
74 "\t\t'^D':\tquits\n\n");
81 "\n\tusage: autopilot [h:p:D:vw:Wx:] <label>\n\n"
82 "\t\t-D <user>\tuser name\n"
83 "\t\t-h <host>\thost name\n"
84 "\t\t-m <mech>\tSASL mechanism(s)\n"
85 "\t\t-p <port>\tport number\n"
86 "\t\t-P <path>\tpath for named socked\n"
93 "\t\t-w <cred>\tuser credentials\n"
94 "\t\t-W\t\tprompt for user credentials\n"
95 "\t\t-x <value>\tincrement\n"
97 "\t\t<label>:\tfile drive (base 1) index to modify\n\n");
113 struct termios tattr;
116 if (!isatty(STDIN_FILENO)) {
117 fprintf(stderr,
"Not a terminal.\n");
126 tcgetattr(STDIN_FILENO, &tattr);
127 tattr.c_lflag &= ~(ICANON|ECHO);
128 tattr.c_cc[VMIN] = 0;
129 tattr.c_cc[VTIME] = 0;
130 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr) < 0) {
138 static struct mbdyn_sasl_t mbdyn_sasl = MBDYN_SASL_INIT;
149 struct sockaddr_in peer_name;
163 if (0 > connect(sock, (
struct sockaddr *) &peer_name,
164 sizeof(peer_name))) {
170 if (mbdyn_sasl_client_auth(sock, NULL, &mbdyn_sasl) != SASL_OK) {
176 fd = fdopen(sock,
"w");
189 char *increment =
"1.";
209 opt =
getopt(argc, argv,
"D:h:m:p:P:Svw:Wx:");
227 fprintf(stderr,
"SASL not supported\n");
242 fprintf(stderr,
"SASL not supported\n");
256 char *tmp = getpass(
"password: ");
260 memset(tmp,
'\0', strlen(tmp));
281 size_t l = strlen(user);
283 l += strlen(cred) + 7 + 11;
285 auth = (
char *)calloc(
sizeof(
char), l + 1);
286 snprintf(auth, l + 1,
"user: %s\npassword: %s\n", user, cred);
290 auth = (
char *)calloc(
sizeof(
char), l + 1);
291 snprintf(auth, l + 1,
"user: %s\n", user);
297 printf(
"initializing SASL data...\n");
300 mbdyn_sasl.use_sasl = MBDYN_SASL_CLIENT;
301 mbdyn_sasl.sasl_flags = MBDYN_SASL_FLAG_CRITICAL | MBDYN_SASL_FLAG_USERAUTHZ;
302 mbdyn_sasl.sasl_mech = mech;
303 mbdyn_sasl.sasl_user = user;
304 mbdyn_sasl.sasl_cred = cred;
305 mbdyn_sasl.sasl_hostname =
host;
307 if (mbdyn_sasl_client_init(&mbdyn_sasl) != SASL_OK) {
308 fprintf(stderr,
"SASL init failed\n");
315 size_t l = strlen(auth) + 8 + strlen(label) + 9 + 2;
317 inc = (
char *)calloc(
sizeof(
char), l + 1);
318 snprintf(inc, l,
"%slabel: %s\ninc: yes\n.\n", auth, label);
320 l = strlen(auth) + 8 + strlen(label) + 8 + strlen(increment) + 2;
321 plus = (
char *)calloc(
sizeof(
char), l + 1);
322 minus = (
char *)calloc(
sizeof(
char), l + 1 + 1);
323 snprintf(plus, l + 1,
"%slabel: %s\nvalue: %s\n.\n", auth, label, increment);
324 snprintf(minus, l + 2,
"%slabel: %s\nvalue: -%s\n.\n", auth, label, increment);
327 size_t l = 8 + strlen(label) + 9 + 2;
329 inc = (
char *)calloc(
sizeof(
char), l + 1);
330 snprintf(inc, l + 1,
"label: %s\ninc: yes\n.\n", label);
332 l = 8 + strlen(label) + 8 + strlen(increment) + 2;
333 plus = (
char *)calloc(
sizeof(
char), l + 1);
334 minus = (
char *)calloc(
sizeof(
char), l + 1 + 1);
335 snprintf(plus, l + 1,
"label: %s\nvalue: %s\n.\n", label, increment);
336 snprintf(minus, l + 2,
"label: %s\nvalue: -%s\n.\n", label, increment);
342 fprintf(stdout,
"Connecting to host %s:%d\n",
host,
port);
345 fprintf(stdout,
"Accounting as \"%s\" (with creds)\n", user);
348 fprintf(stdout,
"Accounting as \"%s\"\n", user);
351 fprintf(stdout,
"Incrementing drive %s by %s\n", label, increment);
358 i =
read(STDIN_FILENO, &c, 1);
385 fprintf(stderr,
"unable to connect to host %s:%d\n",
void reset_input_mode(void)
const unsigned short int PORT
int mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname, unsigned short int port, int dobind, int *perror)
static void keys(FILE *fh)
int send_message(const char *message)
int main(int argc, char *argv[])
void set_input_mode(void)
int mbdyn_make_named_socket(struct sockaddr_un *name, const char *path, int dobind, int *perror)
static std::stack< cleanup * > c
int getopt(int argc, char *const argv[], const char *opts)
struct termios saved_attributes
static void * read(LoadableElem *pEl, DataManager *pDM, MBDynParser &HP)