]>
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/concat.h"
14 #include "sbr/smatch.h"
15 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
22 #include "sbr/lock_file.h"
24 #define MHLOGIN_SWITCHES \
25 X("user username", 0, USERSW) \
26 X("saslmech", 0, SASLMECHSW) \
27 X("authservice", 0, AUTHSERVICESW) \
28 X("browser", 0, BROWSERSW) \
29 X("snoop", 0, SNOOPSW) \
30 X("help", 0, HELPSW) \
31 X("version", 0, VERSIONSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(MHLOGIN
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
42 /* XXX copied from install-mh.c */
46 static char line
[BUFSIZ
];
48 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
50 trim_suffix_c(line
, '\n');
56 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
62 int failed_to_lock
= 0;
66 die("missing -authservice switch");
70 die("missing -user switch");
73 if (!mh_oauth_new(&ctx
, svc
)) {
74 die("%s", mh_oauth_get_err_string(ctx
));
78 mh_oauth_log_to(stderr
, ctx
);
81 fn
= mh_oauth_cred_fn(svc
);
83 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
84 die("%s", mh_oauth_get_err_string(ctx
));
88 char *command
= concat(browser
, " '", url
, "'", NULL
);
91 printf("Follow the prompts in your browser to authorize nmh"
93 mh_oauth_svc_display_name(ctx
));
96 status
= system(command
);
100 adios ((char *) browser
, "SYSTEM");
103 printf("Load the following URL in your browser and authorize nmh"
104 " to access %s:\n\n%s\n\n",
105 mh_oauth_svc_display_name(ctx
), url
);
107 fputs("Enter the authorization code: ", stdout
);
112 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
113 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
114 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
119 inform("error exchanging code for OAuth2 token");
120 die("%s", mh_oauth_get_err_string(ctx
));
123 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
124 if (cred_file
== NULL
&& errno
== ENOENT
) {
125 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
127 if (cred_file
== NULL
|| failed_to_lock
) {
130 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
131 die("%s", mh_oauth_get_err_string(ctx
));
133 if (lkfclosedata(cred_file
, fn
) != 0) {
138 mh_oauth_cred_free(cred
);
146 main(int argc
, char **argv
)
148 char *cp
, **argp
, **arguments
;
149 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
152 if (nmh_init(argv
[0], true, true)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
157 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 die("-%s unknown", cp
);
168 snprintf(help
, sizeof(help
), "%s [switches]",
170 print_help (help
, switches
, 1);
173 print_version(invo_name
);
177 if (!(user
= *argp
++) || *user
== '-')
178 die("missing argument to %s", argp
[-2]);
182 if (!(saslmech
= *argp
++) || *saslmech
== '-')
183 die("missing argument to %s", argp
[-2]);
187 if (!(svc
= *argp
++) || *svc
== '-')
188 die("missing argument to %s", argp
[-2]);
192 if (!(browser
= *argp
++) || *browser
== '-')
193 die("missing argument to %s", argp
[-2]);
201 die("extraneous arguments");
204 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
205 /* xoauth is assumed */
206 die("only -saslmech xoauth2 is supported");
211 return do_login(svc
, user
, browser
, snoop
);
216 die("not built with OAuth support");