]>
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/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
20 #include "sbr/lock_file.h"
22 #define MHLOGIN_SWITCHES \
23 X("user username", 0, USERSW) \
24 X("saslmech", 0, SASLMECHSW) \
25 X("authservice", 0, AUTHSERVICESW) \
26 X("browser", 0, BROWSERSW) \
27 X("snoop", 0, SNOOPSW) \
28 X("help", 0, HELPSW) \
29 X("version", 0, VERSIONSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(MHLOGIN
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
40 /* XXX copied from install-mh.c */
44 static char line
[BUFSIZ
];
46 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
48 trim_suffix_c(line
, '\n');
54 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
60 int failed_to_lock
= 0;
64 die("missing -authservice switch");
68 die("missing -user switch");
71 if (!mh_oauth_new(&ctx
, svc
)) {
72 die("%s", mh_oauth_get_err_string(ctx
));
76 mh_oauth_log_to(stderr
, ctx
);
79 fn
= mh_oauth_cred_fn(svc
);
81 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
82 die("%s", mh_oauth_get_err_string(ctx
));
86 char *command
= concat(browser
, " '", url
, "'", NULL
);
89 printf("Follow the prompts in your browser to authorize nmh"
91 mh_oauth_svc_display_name(ctx
));
94 status
= system(command
);
98 adios ((char *) browser
, "SYSTEM");
101 printf("Load the following URL in your browser and authorize nmh"
102 " to access %s:\n\n%s\n\n",
103 mh_oauth_svc_display_name(ctx
), url
);
105 fputs("Enter the authorization code: ", stdout
);
110 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
111 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
112 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
117 inform("error exchanging code for OAuth2 token");
118 die("%s", mh_oauth_get_err_string(ctx
));
121 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
122 if (cred_file
== NULL
&& errno
== ENOENT
) {
123 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
125 if (cred_file
== NULL
|| failed_to_lock
) {
128 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
129 die("%s", mh_oauth_get_err_string(ctx
));
131 if (lkfclosedata(cred_file
, fn
) != 0) {
136 mh_oauth_cred_free(cred
);
144 main(int argc
, char **argv
)
146 char *cp
, **argp
, **arguments
;
147 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
150 if (nmh_init(argv
[0], true, true)) { return 1; }
152 arguments
= getarguments (invo_name
, argc
, argv
, 1);
155 while ((cp
= *argp
++)) {
158 switch (smatch (++cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 die("-%s unknown", cp
);
166 snprintf(help
, sizeof(help
), "%s [switches]",
168 print_help (help
, switches
, 1);
171 print_version(invo_name
);
175 if (!(user
= *argp
++) || *user
== '-')
176 die("missing argument to %s", argp
[-2]);
180 if (!(saslmech
= *argp
++) || *saslmech
== '-')
181 die("missing argument to %s", argp
[-2]);
185 if (!(svc
= *argp
++) || *svc
== '-')
186 die("missing argument to %s", argp
[-2]);
190 if (!(browser
= *argp
++) || *browser
== '-')
191 die("missing argument to %s", argp
[-2]);
199 die("extraneous arguments");
202 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
203 /* xoauth is assumed */
204 die("only -saslmech xoauth2 is supported");
209 return do_login(svc
, user
, browser
, snoop
);
214 die("not built with OAuth support");