]>
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 #define MHLOGIN_SWITCHES \
17 X("user username", 0, USERSW) \
18 X("saslmech", 0, SASLMECHSW) \
19 X("authservice", 0, AUTHSERVICESW) \
20 X("browser", 0, BROWSERSW) \
21 X("snoop", 0, SNOOPSW) \
22 X("help", 0, HELPSW) \
23 X("version", 0, VERSIONSW) \
25 #define X(sw, minchars, id) id,
26 DEFINE_SWITCH_ENUM(MHLOGIN
);
29 #define X(sw, minchars, id) { sw, minchars, id },
30 DEFINE_SWITCH_ARRAY(MHLOGIN
, switches
);
34 /* XXX copied from install-mh.c */
38 static char line
[BUFSIZ
];
40 if (fgets(line
, sizeof(line
), stdin
) == NULL
)
42 trim_suffix_c(line
, '\n');
48 do_login(const char *svc
, const char *user
, const char *browser
, int snoop
)
54 int failed_to_lock
= 0;
58 adios(NULL
, "missing -authservice switch");
62 adios(NULL
, "missing -user switch");
65 if (!mh_oauth_new(&ctx
, svc
)) {
66 adios(NULL
, mh_oauth_get_err_string(ctx
));
70 mh_oauth_log_to(stderr
, ctx
);
73 fn
= mh_xstrdup(mh_oauth_cred_fn(svc
));
75 if ((url
= mh_oauth_get_authorize_url(ctx
)) == NULL
) {
76 adios(NULL
, mh_oauth_get_err_string(ctx
));
80 char *command
= concat(browser
, " '", url
, "'", NULL
);
83 printf("Follow the prompts in your browser to authorize nmh"
85 mh_oauth_svc_display_name(ctx
));
88 status
= system(command
);
92 adios ((char *) browser
, "SYSTEM");
95 printf("Load the following URL in your browser and authorize nmh"
96 " to access %s:\n\n%s\n\n",
97 mh_oauth_svc_display_name(ctx
), url
);
99 printf("Enter the authorization code: ");
104 ((cred
= mh_oauth_authorize(code
, ctx
)) == NULL
105 && mh_oauth_get_err_code(ctx
) == MH_OAUTH_BAD_GRANT
)) {
106 printf(!*code
? "Empty code; try again? " : "Code rejected; try again? ");
111 inform("error exchanging code for OAuth2 token");
112 adios(NULL
, mh_oauth_get_err_string(ctx
));
115 cred_file
= lkfopendata(fn
, "r+", &failed_to_lock
);
116 if (cred_file
== NULL
&& errno
== ENOENT
) {
117 cred_file
= lkfopendata(fn
, "w+", &failed_to_lock
);
119 if (cred_file
== NULL
|| failed_to_lock
) {
122 if (!mh_oauth_cred_save(cred_file
, cred
, user
)) {
123 adios(NULL
, mh_oauth_get_err_string(ctx
));
125 if (lkfclosedata(cred_file
, fn
) != 0) {
130 mh_oauth_cred_free(cred
);
138 main(int argc
, char **argv
)
140 char *cp
, **argp
, **arguments
;
141 const char *user
= NULL
, *saslmech
= NULL
, *svc
= NULL
, *browser
= NULL
;
144 if (nmh_init(argv
[0], 1)) { return 1; }
146 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 while ((cp
= *argp
++)) {
152 switch (smatch (++cp
, switches
)) {
154 ambigsw (cp
, switches
);
157 adios (NULL
, "-%s unknown", cp
);
160 snprintf(help
, sizeof(help
), "%s [switches]",
162 print_help (help
, switches
, 1);
165 print_version(invo_name
);
169 if (!(user
= *argp
++) || *user
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
174 if (!(saslmech
= *argp
++) || *saslmech
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
179 if (!(svc
= *argp
++) || *svc
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (!(browser
= *argp
++) || *browser
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
193 adios(NULL
, "extraneous arguments");
196 if (saslmech
&& strcasecmp(saslmech
, "xoauth2")) {
197 /* xoauth is assumed */
198 adios(NULL
, "only -saslmech xoauth2 is supported");
203 return do_login(svc
, user
, browser
, snoop
);
208 adios(NULL
, "not built with OAuth support");