]>
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.
17 #define MHLOGIN_SWITCHES \
18 X("user username", 0, USERSW) \
19 X("saslmech", 0, SASLMECHSW) \
20 X("authservice", 0, AUTHSERVICESW) \
21 X("browser", 0, BROWSERSW) \
22 X("snoop", 0, SNOOPSW) \
23 X("help", 0, HELPSW) \
24 X("version", 0, VERSIONSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(MHLOGIN
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
35 /* XXX copied from install-mh.c */
39 static char line
[BUFSIZ
];
41 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
43 trim_suffix_c(line
, '\n');
49 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
55 int failed_to_lock
= 0;
59 adios(NULL
, "missing -authservice switch");
63 adios(NULL
, "missing -user switch");
66 if (!mh_oauth_new(&ctx
, svc
)) {
67 adios(NULL
, mh_oauth_get_err_string(ctx
));
71 mh_oauth_log_to(stderr
, ctx
);
74 fn
= mh_xstrdup(mh_oauth_cred_fn(svc
));
76 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
77 adios(NULL
, mh_oauth_get_err_string(ctx
));
81 char *command
= concat(browser
, " '", url
, "'", NULL
);
84 printf("Follow the prompts in your browser to authorize nmh"
86 mh_oauth_svc_display_name(ctx
));
89 status
= system(command
);
93 adios ((char *) browser
, "SYSTEM");
96 printf("Load the following URL in your browser and authorize nmh"
97 " to access %s:\n\n%s\n\n",
98 mh_oauth_svc_display_name(ctx
), url
);
100 printf("Enter the authorization code: ");
105 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
106 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
107 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
112 advise(NULL
, "error exchanging code for OAuth2 token");
113 adios(NULL
, mh_oauth_get_err_string(ctx
));
116 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
117 if (cred_file
== NULL
&& errno
== ENOENT
) {
118 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
120 if (cred_file
== NULL
|| failed_to_lock
) {
123 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
124 adios(NULL
, mh_oauth_get_err_string(ctx
));
126 if (lkfclosedata(cred_file
, fn
) != 0) {
131 mh_oauth_cred_free(cred
);
139 main(int argc
, char **argv
)
141 char *cp
, **argp
, **arguments
;
142 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
145 if (nmh_init(argv
[0], 1)) { return 1; }
147 arguments
= getarguments (invo_name
, argc
, argv
, 1);
150 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, switches
)) {
155 ambigsw (cp
, switches
);
158 adios (NULL
, "-%s unknown", cp
);
161 snprintf(help
, sizeof(help
), "%s [switches]",
163 print_help (help
, switches
, 1);
166 print_version(invo_name
);
170 if (!(user
= *argp
++) || *user
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
175 if (!(saslmech
= *argp
++) || *saslmech
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
180 if (!(svc
= *argp
++) || *svc
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (!(browser
= *argp
++) || *browser
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
194 adios(NULL
, "extraneous arguments");
197 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
198 /* xoauth is assumed */
199 adios(NULL
, "only -saslmech xoauth2 is supported");
204 return do_login(svc
, user
, browser
, snoop
);
209 adios(NULL
, "not built with OAuth support");