aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/smugfs/main.c
blob: 31c9a752b6d25de09f2e70e6f33ffd230a89d243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "a.h"

char *keypattern = "";
char *sessid;
Json *userinfo;
int printerrors;

void
usage(void)
{
	fprint(2, "usage: smugfs [-k keypattern] [-m mtpt] [-s srv]\n");
	threadexitsall("usage");
}

void
smuglogin(void)
{
	Json *v;
	char *s;
	UserPasswd *up;

	printerrors = 1;
	up = auth_getuserpasswd(auth_getkey,
		"proto=pass role=client server=smugmug.com "
		"user? !password? %s", keypattern);
	if(up == nil)
		sysfatal("cannot get username/password: %r");

	v = ncsmug("smugmug.login.withPassword",
		"EmailAddress", up->user,
		"Password", up->passwd,
		nil);
	if(v == nil)
		sysfatal("login failed: %r");

	memset(up->user, 'X', strlen(up->user));
	memset(up->passwd, 'X', strlen(up->passwd));
	free(up);

	sessid = jstring(jwalk(v, "Login/Session/id"));
	if(sessid == nil)
		sysfatal("no session id");
	sessid = estrdup(sessid);
	s = jstring(jwalk(v, "Login/User/NickName"));
	if(s == nil)
		sysfatal("no nick name");
	if(nickindex(s) != 0)
		sysfatal("bad nick name");
	userinfo = jincref(jwalk(v, "Login"));
	jclose(v);
	printerrors = 0;
}

int
threadmaybackground(void)
{
	return 1;
}

void
threadmain(int argc, char **argv)
{
	char *mtpt, *name;

	mtpt = nil;
	name = nil;
	ARGBEGIN{
	case 'D':
		chatty9p++;
		break;
	case 'F':
		chattyfuse++;
		break;
	case 'H':
		chattyhttp++;
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		name = EARGF(usage());
		break;
	case 'k':
		keypattern = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND

	if(argc != 0)
		usage();

	if(name == nil && mtpt == nil)
		mtpt = "/n/smug";

	/*
	 * Check twice -- if there is an exited smugfs instance
	 * mounted there, the first access will fail but unmount it.
	 */
	if(mtpt && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0)
		sysfatal("mountpoint %s does not exist", mtpt);

	fmtinstall('H', encodefmt);
	fmtinstall('[', encodefmt);  // base-64
	fmtinstall('J', jsonfmt);
	fmtinstall('M', dirmodefmt);
	fmtinstall('T', timefmt);
	fmtinstall('U', urlencodefmt);

	xinit();
	smuglogin();
	threadpostmountsrv(&xsrv, name, mtpt, 0);
	threadexits(nil);
}