]>
diplodocus.org Git - nmh/blob - uip/mhlogin.c
2 * mhlogin.c -- login to external (OAuth) services
4 * This code is Copyright (c) 2014, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
15 #define MHLOGIN_SWITCHES \
16 X("saslmech", 0, SASLMECHSW) \
17 X("authservice", 0, AUTHSERVICESW) \
18 X("browser", 0, BROWSERSW) \
19 X("snoop", 0, SNOOPSW) \
20 X("help", 0, HELPSW) \
21 X("version", 0, VERSIONSW) \
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(MHLOGIN
);
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
32 /* XXX copied from install-mh.c */
37 static char line
[BUFSIZ
];
39 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
41 if ((cp
= strchr(line
, '\n')))
47 do_login(const char *svc
, const char *browser
, int snoop
)
53 int failed_to_lock
= 0;
57 adios(NULL
, "missing -authservice switch");
60 if (!mh_oauth_new(&ctx
, svc
)) {
61 adios(NULL
, mh_oauth_get_err_string(ctx
));
65 mh_oauth_log_to(stderr
, ctx
);
68 fn
= getcpy(mh_oauth_cred_fn(ctx
));
70 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
71 adios(NULL
, mh_oauth_get_err_string(ctx
));
75 char *command
= concat(browser
, " '", url
, "'", NULL
);
78 printf("Follow the prompts in your browser to authorize nmh"
80 mh_oauth_svc_display_name(ctx
));
83 status
= system(command
);
87 adios ((char *) browser
, "SYSTEM");
90 printf("Load the following URL in your browser and authorize nmh"
91 " to access %s:\n\n%s\n\n",
92 mh_oauth_svc_display_name(ctx
), url
);
94 printf("Enter the authorization code: ");
98 while ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
99 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
) {
100 printf("Code rejected; try again? ");
105 advise(NULL
, "error exchanging code for OAuth2 token");
106 adios(NULL
, mh_oauth_get_err_string(ctx
));
109 cred_file
= lkfopendata(fn
, "w", &failed_to_lock
);
110 if (cred_file
== NULL
|| failed_to_lock
) {
113 if (!mh_oauth_cred_save(cred_file
, cred
)) {
114 adios(NULL
, mh_oauth_get_err_string(ctx
));
116 if (lkfclosedata(cred_file
, fn
) != 0) {
120 mh_oauth_cred_free(cred
);
128 main(int argc
, char **argv
)
130 char *cp
, **argp
, **arguments
;
131 const char *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
134 if (nmh_init(argv
[0], 1)) { return 1; }
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf(help
, sizeof(help
), "%s [switches]",
152 print_help (help
, switches
, 1);
155 print_version(invo_name
);
159 if (!(saslmech
= *argp
++) || *saslmech
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
164 if (!(svc
= *argp
++) || *svc
== '-')
165 adios (NULL
, "missing argument to %s", argp
[-2]);
169 if (!(browser
= *argp
++) || *browser
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
178 adios(NULL
, "extraneous arguments");
181 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
182 /* xoauth is assumed */
183 adios(NULL
, "only -saslmech xoauth2 is supported");
187 return do_login(svc
, browser
, snoop
);
192 adios(NULL
, "not built with OAuth support");