]>
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/smatch.h"
14 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
21 #include "sbr/lock_file.h"
23 #define MHLOGIN_SWITCHES \
24 X("user username", 0, USERSW) \
25 X("saslmech", 0, SASLMECHSW) \
26 X("authservice", 0, AUTHSERVICESW) \
27 X("browser", 0, BROWSERSW) \
28 X("snoop", 0, SNOOPSW) \
29 X("help", 0, HELPSW) \
30 X("version", 0, VERSIONSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(MHLOGIN
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
41 /* XXX copied from install-mh.c */
45 static char line
[BUFSIZ
];
47 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
49 trim_suffix_c(line
, '\n');
55 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
61 int failed_to_lock
= 0;
65 die("missing -authservice switch");
69 die("missing -user switch");
72 if (!mh_oauth_new(&ctx
, svc
)) {
73 die("%s", mh_oauth_get_err_string(ctx
));
77 mh_oauth_log_to(stderr
, ctx
);
80 fn
= mh_oauth_cred_fn(svc
);
82 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
83 die("%s", mh_oauth_get_err_string(ctx
));
87 char *command
= concat(browser
, " '", url
, "'", NULL
);
90 printf("Follow the prompts in your browser to authorize nmh"
92 mh_oauth_svc_display_name(ctx
));
95 status
= system(command
);
99 adios ((char *) browser
, "SYSTEM");
102 printf("Load the following URL in your browser and authorize nmh"
103 " to access %s:\n\n%s\n\n",
104 mh_oauth_svc_display_name(ctx
), url
);
106 fputs("Enter the authorization code: ", stdout
);
111 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
112 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
113 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
118 inform("error exchanging code for OAuth2 token");
119 die("%s", mh_oauth_get_err_string(ctx
));
122 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
123 if (cred_file
== NULL
&& errno
== ENOENT
) {
124 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
126 if (cred_file
== NULL
|| failed_to_lock
) {
129 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
130 die("%s", mh_oauth_get_err_string(ctx
));
132 if (lkfclosedata(cred_file
, fn
) != 0) {
137 mh_oauth_cred_free(cred
);
145 main(int argc
, char **argv
)
147 char *cp
, **argp
, **arguments
;
148 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
151 if (nmh_init(argv
[0], true, true)) { return 1; }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 while ((cp
= *argp
++)) {
159 switch (smatch (++cp
, switches
)) {
161 ambigsw (cp
, switches
);
164 die("-%s unknown", cp
);
167 snprintf(help
, sizeof(help
), "%s [switches]",
169 print_help (help
, switches
, 1);
172 print_version(invo_name
);
176 if (!(user
= *argp
++) || *user
== '-')
177 die("missing argument to %s", argp
[-2]);
181 if (!(saslmech
= *argp
++) || *saslmech
== '-')
182 die("missing argument to %s", argp
[-2]);
186 if (!(svc
= *argp
++) || *svc
== '-')
187 die("missing argument to %s", argp
[-2]);
191 if (!(browser
= *argp
++) || *browser
== '-')
192 die("missing argument to %s", argp
[-2]);
200 die("extraneous arguments");
203 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
204 /* xoauth is assumed */
205 die("only -saslmech xoauth2 is supported");
210 return do_login(svc
, user
, browser
, snoop
);
215 die("not built with OAuth support");