9 #include <sys/socket.h>
10 #include <netinet/in.h>
21 con(struct addrinfo
*srv
)
25 s
= socket(srv
->ai_family
, srv
->ai_socktype
, 0);
28 if (connect(s
, srv
->ai_addr
, sizeof(*srv
->ai_addr
)) < 0) {
36 getTicket(Npcauth
*auth
, struct ticketreq
*treq
, char ctick
[72], char stick
[72])
44 return err("cant reach auth", EPERM
);
46 // XXX do we need to handle other type replies?
47 if(encTicketReq(treqbuf
, treq
) == -1
48 || send(fd
, treqbuf
, sizeof treqbuf
, 0) != sizeof treqbuf
49 || recv(fd
, &type
, 1, 0) != 1
51 || recv(fd
, ctick
, 72, 0) != 72
52 || recv(fd
, stick
, 72, 0) != 72) {
54 return err("botch", EINVAL
);
61 srvp9sk1(struct npsrvauth
*a
, char *msg
, int len
, char *resp
, int resplen
)
63 struct ticketreq treq
;
69 // state 0 is reserved for cases where server speaks first
71 return err("botch", EINVAL
);
77 return err("botch", EINVAL
);
78 memcpy(a
->chc
, msg
, 8);
88 || encTicketReq(resp
, &treq
) == -1)
89 return err("internal error", EINVAL
);
95 return err("botch", EINVAL
);
96 if(decTicket(msg
, &tick
, a
->key
) == -1
97 || tick
.type
!= AuthTs
98 || memcmp(tick
.ch
, a
->chs
, 8) != 0
99 || decAuth(msg
+72, &auth
, tick
.key
) == -1
100 || auth
.type
!= AuthAc
101 || memcmp(auth
.ch
, a
->chs
, 8) != 0
102 || auth
.gen
!= a
->gen
)
103 return err("bad auth", EPERM
);
105 a
->idc
= strdup(tick
.idc
);
106 a
->idr
= strdup(tick
.idr
);
108 memcpy(auth
.ch
, a
->chc
, 8);
111 || encAuth(resp
, &auth
, tick
.key
) == -1)
112 return err("internal error", EINVAL
);
118 return err("botch", EINVAL
);
123 authp9sk1(Npcfid
*afid
, Npuser
*user
, void *aux
)
125 char treqbuf
[141], ctickbuf
[72], stickbuf
[72];
126 char authbuf
[85], chc
[8];
127 struct ticketreq treq
;
130 struct npcauth
*a
= (struct npcauth
*)aux
;
134 if(put(afid
, chc
, 8) <= 0)
135 return err("botch", EINVAL
);
137 // S->C AuthTreq, IDs, DN, DHs, -, -
138 if(get(afid
, treqbuf
, 141) <= 0
139 || decTicketReq(treqbuf
, &treq
) == -1)
140 return err("botch", EINVAL
);
141 if(treq
.type
!= AuthTreq
)
142 return err("botch", EINVAL
);
144 // forward ticket request to authserver to get tickets.
145 treq
.idc
= user
->uname
;
146 treq
.idr
= user
->uname
;
147 if(getTicket(a
, &treq
, ctickbuf
, stickbuf
) == -1)
149 if(decTicket(ctickbuf
, &ctick
, a
->key
) == -1)
150 return err("botch", EINVAL
);
151 if(memcmp(treq
.ch
, ctick
.ch
, 8) != 0)
152 return err("bad auth", EPERM
);
154 // C->S Ks{AuthTs, CHs, IDc, IDr, Kn}, Kn{AuthAc, CHs}
158 memcpy(authbuf
, stickbuf
, 72);
159 if(encAuth(authbuf
+72, &auth
, ctick
.key
) == -1
160 || put(afid
, authbuf
, sizeof authbuf
) == -1)
161 return err("botch", EINVAL
);
163 // S->C Kn{AuthAs, Chc}
164 if(get(afid
, authbuf
, 13) == -1
165 || decAuth(authbuf
, &auth
, ctick
.key
))
166 return err("botch", EINVAL
);
167 if(auth
.type
!= AuthAs
168 || memcmp(auth
.ch
, chc
, 8) != 0
169 || auth
.gen
!= a
->gen
)
170 return err("bad server", EPERM
);