]>
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.
16 #include "sbr/lock_file.h"
18 #define MHLOGIN_SWITCHES \
19 X("user username", 0, USERSW) \
20 X("saslmech", 0, SASLMECHSW) \
21 X("authservice", 0, AUTHSERVICESW) \
22 X("browser", 0, BROWSERSW) \
23 X("snoop", 0, SNOOPSW) \
24 X("help", 0, HELPSW) \
25 X("version", 0, VERSIONSW) \
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(MHLOGIN
);
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
36 /* XXX copied from install-mh.c */
40 static char line
[BUFSIZ
];
42 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
44 trim_suffix_c(line
, '\n');
50 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
56 int failed_to_lock
= 0;
60 die("missing -authservice switch");
64 die("missing -user switch");
67 if (!mh_oauth_new(&ctx
, svc
)) {
68 die("%s", mh_oauth_get_err_string(ctx
));
72 mh_oauth_log_to(stderr
, ctx
);
75 fn
= mh_oauth_cred_fn(svc
);
77 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
78 die("%s", mh_oauth_get_err_string(ctx
));
82 char *command
= concat(browser
, " '", url
, "'", NULL
);
85 printf("Follow the prompts in your browser to authorize nmh"
87 mh_oauth_svc_display_name(ctx
));
90 status
= system(command
);
94 adios ((char *) browser
, "SYSTEM");
97 printf("Load the following URL in your browser and authorize nmh"
98 " to access %s:\n\n%s\n\n",
99 mh_oauth_svc_display_name(ctx
), url
);
101 fputs("Enter the authorization code: ", stdout
);
106 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
107 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
108 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
113 inform("error exchanging code for OAuth2 token");
114 die("%s", mh_oauth_get_err_string(ctx
));
117 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
118 if (cred_file
== NULL
&& errno
== ENOENT
) {
119 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
121 if (cred_file
== NULL
|| failed_to_lock
) {
124 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
125 die("%s", mh_oauth_get_err_string(ctx
));
127 if (lkfclosedata(cred_file
, fn
) != 0) {
132 mh_oauth_cred_free(cred
);
140 main(int argc
, char **argv
)
142 char *cp
, **argp
, **arguments
;
143 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
146 if (nmh_init(argv
[0], true, true)) { return 1; }
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 while ((cp
= *argp
++)) {
154 switch (smatch (++cp
, switches
)) {
156 ambigsw (cp
, switches
);
159 die("-%s unknown", cp
);
162 snprintf(help
, sizeof(help
), "%s [switches]",
164 print_help (help
, switches
, 1);
167 print_version(invo_name
);
171 if (!(user
= *argp
++) || *user
== '-')
172 die("missing argument to %s", argp
[-2]);
176 if (!(saslmech
= *argp
++) || *saslmech
== '-')
177 die("missing argument to %s", argp
[-2]);
181 if (!(svc
= *argp
++) || *svc
== '-')
182 die("missing argument to %s", argp
[-2]);
186 if (!(browser
= *argp
++) || *browser
== '-')
187 die("missing argument to %s", argp
[-2]);
195 die("extraneous arguments");
198 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
199 /* xoauth is assumed */
200 die("only -saslmech xoauth2 is supported");
205 return do_login(svc
, user
, browser
, snoop
);
210 die("not built with OAuth support");