]>
diplodocus.org Git - nmh/blob - uip/mhlogin.c
8179a21fdb56492d297503548a45cdb694eb19c3
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_help.h"
14 #include "sbr/error.h"
18 #include "sbr/lock_file.h"
20 #define MHLOGIN_SWITCHES \
21 X("user username", 0, USERSW) \
22 X("saslmech", 0, SASLMECHSW) \
23 X("authservice", 0, AUTHSERVICESW) \
24 X("browser", 0, BROWSERSW) \
25 X("snoop", 0, SNOOPSW) \
26 X("help", 0, HELPSW) \
27 X("version", 0, VERSIONSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(MHLOGIN
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
38 /* XXX copied from install-mh.c */
42 static char line
[BUFSIZ
];
44 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
46 trim_suffix_c(line
, '\n');
52 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
58 int failed_to_lock
= 0;
62 die("missing -authservice switch");
66 die("missing -user switch");
69 if (!mh_oauth_new(&ctx
, svc
)) {
70 die("%s", mh_oauth_get_err_string(ctx
));
74 mh_oauth_log_to(stderr
, ctx
);
77 fn
= mh_oauth_cred_fn(svc
);
79 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
80 die("%s", mh_oauth_get_err_string(ctx
));
84 char *command
= concat(browser
, " '", url
, "'", NULL
);
87 printf("Follow the prompts in your browser to authorize nmh"
89 mh_oauth_svc_display_name(ctx
));
92 status
= system(command
);
96 adios ((char *) browser
, "SYSTEM");
99 printf("Load the following URL in your browser and authorize nmh"
100 " to access %s:\n\n%s\n\n",
101 mh_oauth_svc_display_name(ctx
), url
);
103 fputs("Enter the authorization code: ", stdout
);
108 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
109 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
110 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
115 inform("error exchanging code for OAuth2 token");
116 die("%s", mh_oauth_get_err_string(ctx
));
119 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
120 if (cred_file
== NULL
&& errno
== ENOENT
) {
121 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
123 if (cred_file
== NULL
|| failed_to_lock
) {
126 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
127 die("%s", mh_oauth_get_err_string(ctx
));
129 if (lkfclosedata(cred_file
, fn
) != 0) {
134 mh_oauth_cred_free(cred
);
142 main(int argc
, char **argv
)
144 char *cp
, **argp
, **arguments
;
145 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
148 if (nmh_init(argv
[0], true, true)) { return 1; }
150 arguments
= getarguments (invo_name
, argc
, argv
, 1);
153 while ((cp
= *argp
++)) {
156 switch (smatch (++cp
, switches
)) {
158 ambigsw (cp
, switches
);
161 die("-%s unknown", cp
);
164 snprintf(help
, sizeof(help
), "%s [switches]",
166 print_help (help
, switches
, 1);
169 print_version(invo_name
);
173 if (!(user
= *argp
++) || *user
== '-')
174 die("missing argument to %s", argp
[-2]);
178 if (!(saslmech
= *argp
++) || *saslmech
== '-')
179 die("missing argument to %s", argp
[-2]);
183 if (!(svc
= *argp
++) || *svc
== '-')
184 die("missing argument to %s", argp
[-2]);
188 if (!(browser
= *argp
++) || *browser
== '-')
189 die("missing argument to %s", argp
[-2]);
197 die("extraneous arguments");
200 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
201 /* xoauth is assumed */
202 die("only -saslmech xoauth2 is supported");
207 return do_login(svc
, user
, browser
, snoop
);
212 die("not built with OAuth support");