]>
diplodocus.org Git - nmh/blob - uip/mhlogin.c
1 /* mhlogin.c -- login to external (OAuth) services
3 * This code is Copyright (c) 2014, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
19 #include "sbr/lock_file.h"
21 #define MHLOGIN_SWITCHES \
22 X("user username", 0, USERSW) \
23 X("saslmech", 0, SASLMECHSW) \
24 X("authservice", 0, AUTHSERVICESW) \
25 X("browser", 0, BROWSERSW) \
26 X("snoop", 0, SNOOPSW) \
27 X("help", 0, HELPSW) \
28 X("version", 0, VERSIONSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(MHLOGIN
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
39 /* XXX copied from install-mh.c */
43 static char line
[BUFSIZ
];
45 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
47 trim_suffix_c(line
, '\n');
53 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
59 int failed_to_lock
= 0;
63 die("missing -authservice switch");
67 die("missing -user switch");
70 if (!mh_oauth_new(&ctx
, svc
)) {
71 die("%s", mh_oauth_get_err_string(ctx
));
75 mh_oauth_log_to(stderr
, ctx
);
78 fn
= mh_oauth_cred_fn(svc
);
80 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
81 die("%s", mh_oauth_get_err_string(ctx
));
85 char *command
= concat(browser
, " '", url
, "'", NULL
);
88 printf("Follow the prompts in your browser to authorize nmh"
90 mh_oauth_svc_display_name(ctx
));
93 status
= system(command
);
97 adios ((char *) browser
, "SYSTEM");
100 printf("Load the following URL in your browser and authorize nmh"
101 " to access %s:\n\n%s\n\n",
102 mh_oauth_svc_display_name(ctx
), url
);
104 fputs("Enter the authorization code: ", stdout
);
109 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
110 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
111 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
116 inform("error exchanging code for OAuth2 token");
117 die("%s", mh_oauth_get_err_string(ctx
));
120 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
121 if (cred_file
== NULL
&& errno
== ENOENT
) {
122 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
124 if (cred_file
== NULL
|| failed_to_lock
) {
127 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
128 die("%s", mh_oauth_get_err_string(ctx
));
130 if (lkfclosedata(cred_file
, fn
) != 0) {
135 mh_oauth_cred_free(cred
);
143 main(int argc
, char **argv
)
145 char *cp
, **argp
, **arguments
;
146 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
149 if (nmh_init(argv
[0], true, true)) { return 1; }
151 arguments
= getarguments (invo_name
, argc
, argv
, 1);
154 while ((cp
= *argp
++)) {
157 switch (smatch (++cp
, switches
)) {
159 ambigsw (cp
, switches
);
162 die("-%s unknown", cp
);
165 snprintf(help
, sizeof(help
), "%s [switches]",
167 print_help (help
, switches
, 1);
170 print_version(invo_name
);
174 if (!(user
= *argp
++) || *user
== '-')
175 die("missing argument to %s", argp
[-2]);
179 if (!(saslmech
= *argp
++) || *saslmech
== '-')
180 die("missing argument to %s", argp
[-2]);
184 if (!(svc
= *argp
++) || *svc
== '-')
185 die("missing argument to %s", argp
[-2]);
189 if (!(browser
= *argp
++) || *browser
== '-')
190 die("missing argument to %s", argp
[-2]);
198 die("extraneous arguments");
201 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
202 /* xoauth is assumed */
203 die("only -saslmech xoauth2 is supported");
208 return do_login(svc
, user
, browser
, snoop
);
213 die("not built with OAuth support");