1 | #include <stdlib.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/wait.h> |
---|
5 | #include <sys/stat.h> |
---|
6 | #include <string.h> |
---|
7 | #include "owl.h" |
---|
8 | |
---|
9 | static const char fileIdent[] = "$Id$"; |
---|
10 | |
---|
11 | #ifdef HAVE_LIBZEPHYR |
---|
12 | Code_t ZResetAuthentication(); |
---|
13 | #endif |
---|
14 | |
---|
15 | int owl_zephyr_initialize() |
---|
16 | { |
---|
17 | #ifdef HAVE_LIBZEPHYR |
---|
18 | int ret; |
---|
19 | |
---|
20 | if ((ret = ZInitialize()) != ZERR_NONE) { |
---|
21 | com_err("owl",ret,"while initializing"); |
---|
22 | return(1); |
---|
23 | } |
---|
24 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
---|
25 | com_err("owl",ret,"while opening port"); |
---|
26 | return(1); |
---|
27 | } |
---|
28 | #endif |
---|
29 | return(0); |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | int owl_zephyr_shutdown() |
---|
34 | { |
---|
35 | #ifdef HAVE_LIBZEPHYR |
---|
36 | unsuball(); |
---|
37 | ZClosePort(); |
---|
38 | #endif |
---|
39 | return(0); |
---|
40 | } |
---|
41 | |
---|
42 | int owl_zephyr_zpending() |
---|
43 | { |
---|
44 | #ifdef HAVE_LIBZEPHYR |
---|
45 | return(ZPending()); |
---|
46 | #else |
---|
47 | return(0); |
---|
48 | #endif |
---|
49 | } |
---|
50 | |
---|
51 | char *owl_zephyr_get_realm() |
---|
52 | { |
---|
53 | #ifdef HAVE_LIBZEPHYR |
---|
54 | return(ZGetRealm()); |
---|
55 | #else |
---|
56 | return(""); |
---|
57 | #endif |
---|
58 | } |
---|
59 | |
---|
60 | char *owl_zephyr_get_sender() |
---|
61 | { |
---|
62 | #ifdef HAVE_LIBZEPHYR |
---|
63 | return(ZGetSender()); |
---|
64 | #else |
---|
65 | return(""); |
---|
66 | #endif |
---|
67 | } |
---|
68 | |
---|
69 | /* return 0 on success |
---|
70 | * -1 on file error |
---|
71 | * -2 on subscription error |
---|
72 | */ |
---|
73 | int owl_zephyr_loadsubs(char *filename) |
---|
74 | { |
---|
75 | #ifdef HAVE_LIBZEPHYR |
---|
76 | FILE *file; |
---|
77 | char *tmp, *start; |
---|
78 | char buffer[1024], subsfile[1024]; |
---|
79 | ZSubscription_t subs[3001]; |
---|
80 | int count, ret, i; |
---|
81 | struct stat statbuff; |
---|
82 | |
---|
83 | if (filename==NULL) { |
---|
84 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
---|
85 | } else { |
---|
86 | strcpy(subsfile, filename); |
---|
87 | } |
---|
88 | |
---|
89 | ret=stat(subsfile, &statbuff); |
---|
90 | if (ret) return(0); |
---|
91 | |
---|
92 | ret=0; |
---|
93 | |
---|
94 | ZResetAuthentication(); |
---|
95 | /* need to redo this to do chunks, not just bail after 3000 */ |
---|
96 | count=0; |
---|
97 | file=fopen(subsfile, "r"); |
---|
98 | if (file) { |
---|
99 | while ( fgets(buffer, 1024, file)!=NULL ) { |
---|
100 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
---|
101 | |
---|
102 | if (buffer[0]=='-') { |
---|
103 | start=buffer+1; |
---|
104 | } else { |
---|
105 | start=buffer; |
---|
106 | } |
---|
107 | |
---|
108 | if (count >= 3000) break; /* also tell the user */ |
---|
109 | |
---|
110 | /* add it to the list of subs */ |
---|
111 | if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue; |
---|
112 | subs[count].zsub_class=owl_strdup(tmp); |
---|
113 | if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue; |
---|
114 | subs[count].zsub_classinst=owl_strdup(tmp); |
---|
115 | if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue; |
---|
116 | subs[count].zsub_recipient=owl_strdup(tmp); |
---|
117 | |
---|
118 | /* if it started with '-' then add it to the global punt list */ |
---|
119 | if (buffer[0]=='-') { |
---|
120 | owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0); |
---|
121 | } |
---|
122 | |
---|
123 | count++; |
---|
124 | } |
---|
125 | fclose(file); |
---|
126 | } else { |
---|
127 | count=0; |
---|
128 | ret=-1; |
---|
129 | } |
---|
130 | |
---|
131 | /* sub without defaults */ |
---|
132 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
---|
133 | owl_function_error("Error subscribing to zephyr notifications."); |
---|
134 | ret=-2; |
---|
135 | } |
---|
136 | |
---|
137 | /* free stuff */ |
---|
138 | for (i=0; i<count; i++) { |
---|
139 | owl_free(subs[i].zsub_class); |
---|
140 | owl_free(subs[i].zsub_classinst); |
---|
141 | owl_free(subs[i].zsub_recipient); |
---|
142 | } |
---|
143 | |
---|
144 | return(ret); |
---|
145 | #else |
---|
146 | return(0); |
---|
147 | #endif |
---|
148 | } |
---|
149 | |
---|
150 | int owl_zephyr_loaddefaultsubs() |
---|
151 | { |
---|
152 | #ifdef HAVE_LIBZEPHYR |
---|
153 | ZSubscription_t subs[10]; |
---|
154 | |
---|
155 | if (ZSubscribeTo(subs,0,0) != ZERR_NONE) { |
---|
156 | owl_function_error("Error subscribing to default zephyr notifications."); |
---|
157 | return(-1); |
---|
158 | } |
---|
159 | return(0); |
---|
160 | #else |
---|
161 | return(0); |
---|
162 | #endif |
---|
163 | } |
---|
164 | |
---|
165 | int owl_zephyr_loadloginsubs(char *filename) |
---|
166 | { |
---|
167 | #ifdef HAVE_LIBZEPHYR |
---|
168 | FILE *file; |
---|
169 | ZSubscription_t subs[3001]; |
---|
170 | char subsfile[1024], buffer[1024]; |
---|
171 | int count, ret, i; |
---|
172 | struct stat statbuff; |
---|
173 | |
---|
174 | if (filename==NULL) { |
---|
175 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone"); |
---|
176 | } else { |
---|
177 | strcpy(subsfile, filename); |
---|
178 | } |
---|
179 | |
---|
180 | ret=stat(subsfile, &statbuff); |
---|
181 | if (ret) return(0); |
---|
182 | |
---|
183 | ret=0; |
---|
184 | |
---|
185 | ZResetAuthentication(); |
---|
186 | /* need to redo this to do chunks, not just bag out after 3000 */ |
---|
187 | count=0; |
---|
188 | file=fopen(subsfile, "r"); |
---|
189 | if (file) { |
---|
190 | while ( fgets(buffer, 1024, file)!=NULL ) { |
---|
191 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
---|
192 | |
---|
193 | if (count >= 3000) break; /* also tell the user */ |
---|
194 | |
---|
195 | buffer[strlen(buffer)-1]='\0'; |
---|
196 | subs[count].zsub_class="login"; |
---|
197 | subs[count].zsub_recipient="*"; |
---|
198 | if (strchr(buffer, '@')) { |
---|
199 | subs[count].zsub_classinst=owl_strdup(buffer); |
---|
200 | } else { |
---|
201 | subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm()); |
---|
202 | } |
---|
203 | |
---|
204 | count++; |
---|
205 | } |
---|
206 | fclose(file); |
---|
207 | } else { |
---|
208 | count=0; |
---|
209 | ret=-1; |
---|
210 | } |
---|
211 | |
---|
212 | /* sub with defaults */ |
---|
213 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
---|
214 | owl_function_error("Error subscribing to zephyr notifications."); |
---|
215 | ret=-2; |
---|
216 | } |
---|
217 | |
---|
218 | /* free stuff */ |
---|
219 | for (i=0; i<count; i++) { |
---|
220 | owl_free(subs[i].zsub_classinst); |
---|
221 | } |
---|
222 | |
---|
223 | return(ret); |
---|
224 | #else |
---|
225 | return(0); |
---|
226 | #endif |
---|
227 | } |
---|
228 | |
---|
229 | void unsuball() |
---|
230 | { |
---|
231 | #if HAVE_LIBZEPHYR |
---|
232 | int ret; |
---|
233 | |
---|
234 | ZResetAuthentication(); |
---|
235 | ret=ZCancelSubscriptions(0); |
---|
236 | if (ret != ZERR_NONE) { |
---|
237 | com_err("owl",ret,"while unsubscribing"); |
---|
238 | } |
---|
239 | #endif |
---|
240 | } |
---|
241 | |
---|
242 | int owl_zephyr_sub(char *class, char *inst, char *recip) |
---|
243 | { |
---|
244 | #ifdef HAVE_LIBZEPHYR |
---|
245 | ZSubscription_t subs[5]; |
---|
246 | |
---|
247 | subs[0].zsub_class=class; |
---|
248 | subs[0].zsub_classinst=inst; |
---|
249 | subs[0].zsub_recipient=recip; |
---|
250 | |
---|
251 | ZResetAuthentication(); |
---|
252 | if (ZSubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
253 | owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip); |
---|
254 | return(-2); |
---|
255 | } |
---|
256 | return(0); |
---|
257 | #else |
---|
258 | return(0); |
---|
259 | #endif |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | int owl_zephyr_unsub(char *class, char *inst, char *recip) |
---|
264 | { |
---|
265 | #ifdef HAVE_LIBZEPHYR |
---|
266 | ZSubscription_t subs[5]; |
---|
267 | |
---|
268 | subs[0].zsub_class=class; |
---|
269 | subs[0].zsub_classinst=inst; |
---|
270 | subs[0].zsub_recipient=recip; |
---|
271 | |
---|
272 | ZResetAuthentication(); |
---|
273 | if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
274 | owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip); |
---|
275 | return(-2); |
---|
276 | } |
---|
277 | return(0); |
---|
278 | #else |
---|
279 | return(0); |
---|
280 | #endif |
---|
281 | } |
---|
282 | |
---|
283 | /* return a pointer to the data in the Jth field, (NULL terminated by |
---|
284 | * definition). Caller must free the return. |
---|
285 | */ |
---|
286 | #ifdef HAVE_LIBZEPHYR |
---|
287 | char *owl_zephyr_get_field(ZNotice_t *n, int j) |
---|
288 | { |
---|
289 | int i, count, save; |
---|
290 | char *out; |
---|
291 | |
---|
292 | count=save=0; |
---|
293 | for (i=0; i<n->z_message_len; i++) { |
---|
294 | if (n->z_message[i]=='\0') { |
---|
295 | count++; |
---|
296 | if (count==j) { |
---|
297 | /* just found the end of the field we're looking for */ |
---|
298 | return(owl_strdup(n->z_message+save)); |
---|
299 | } else { |
---|
300 | save=i+1; |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | /* catch the last field, which might not be null terminated */ |
---|
305 | if (count==j-1) { |
---|
306 | out=owl_malloc(n->z_message_len-save+5); |
---|
307 | memcpy(out, n->z_message+save, n->z_message_len-save); |
---|
308 | out[n->z_message_len-save]='\0'; |
---|
309 | return(out); |
---|
310 | } |
---|
311 | |
---|
312 | return(owl_strdup("")); |
---|
313 | } |
---|
314 | #else |
---|
315 | char *owl_zephyr_get_field(void *n, int j) |
---|
316 | { |
---|
317 | return(owl_strdup("")); |
---|
318 | } |
---|
319 | #endif |
---|
320 | |
---|
321 | |
---|
322 | #ifdef HAVE_LIBZEPHYR |
---|
323 | int owl_zephyr_get_num_fields(ZNotice_t *n) |
---|
324 | { |
---|
325 | int i, fields; |
---|
326 | |
---|
327 | fields=1; |
---|
328 | for (i=0; i<n->z_message_len; i++) { |
---|
329 | if (n->z_message[i]=='\0') fields++; |
---|
330 | } |
---|
331 | |
---|
332 | return(fields); |
---|
333 | } |
---|
334 | #else |
---|
335 | int owl_zephyr_get_num_fields(void *n) |
---|
336 | { |
---|
337 | return(0); |
---|
338 | } |
---|
339 | #endif |
---|
340 | |
---|
341 | #ifdef HAVE_LIBZEPHYR |
---|
342 | /* return a pointer to the message, place the message length in k |
---|
343 | * caller must free the return |
---|
344 | */ |
---|
345 | char *owl_zephyr_get_message(ZNotice_t *n) |
---|
346 | { |
---|
347 | if (!strcasecmp(n->z_opcode, "ping")) { |
---|
348 | return(owl_strdup("")); |
---|
349 | } |
---|
350 | |
---|
351 | return(owl_zephyr_get_field(n, 2)); |
---|
352 | } |
---|
353 | #endif |
---|
354 | |
---|
355 | #ifdef HAVE_LIBZEPHYR |
---|
356 | char *owl_zephyr_get_zsig(ZNotice_t *n, int *k) |
---|
357 | { |
---|
358 | /* return a pointer to the zsig if there is one */ |
---|
359 | |
---|
360 | if (n->z_message_len==0) { |
---|
361 | *k=0; |
---|
362 | return(""); |
---|
363 | } |
---|
364 | *k=strlen(n->z_message); |
---|
365 | return(n->z_message); |
---|
366 | } |
---|
367 | #else |
---|
368 | char *owl_zephyr_get_zsig(void *n, int *k) |
---|
369 | { |
---|
370 | return(""); |
---|
371 | } |
---|
372 | #endif |
---|
373 | |
---|
374 | int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message) |
---|
375 | { |
---|
376 | #ifdef HAVE_LIBZEPHYR |
---|
377 | int ret; |
---|
378 | ZNotice_t notice; |
---|
379 | |
---|
380 | memset(¬ice, 0, sizeof(notice)); |
---|
381 | |
---|
382 | ZResetAuthentication(); |
---|
383 | |
---|
384 | if (!zsig) zsig=""; |
---|
385 | |
---|
386 | notice.z_kind=ACKED; |
---|
387 | notice.z_port=0; |
---|
388 | notice.z_class=class; |
---|
389 | notice.z_class_inst=instance; |
---|
390 | if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) { |
---|
391 | notice.z_recipient=""; |
---|
392 | } else { |
---|
393 | notice.z_recipient=recipient; |
---|
394 | } |
---|
395 | notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2"; |
---|
396 | notice.z_sender=NULL; |
---|
397 | if (opcode) notice.z_opcode=opcode; |
---|
398 | |
---|
399 | notice.z_message_len=strlen(zsig)+1+strlen(message); |
---|
400 | notice.z_message=owl_malloc(notice.z_message_len+10); |
---|
401 | strcpy(notice.z_message, zsig); |
---|
402 | memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message)); |
---|
403 | |
---|
404 | /* ret=ZSendNotice(¬ice, ZAUTH); */ |
---|
405 | ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper); |
---|
406 | |
---|
407 | /* free then check the return */ |
---|
408 | owl_free(notice.z_message); |
---|
409 | ZFreeNotice(¬ice); |
---|
410 | if (ret!=ZERR_NONE) { |
---|
411 | owl_function_error("Error sending zephyr"); |
---|
412 | return(ret); |
---|
413 | } |
---|
414 | return(0); |
---|
415 | #else |
---|
416 | return(0); |
---|
417 | #endif |
---|
418 | } |
---|
419 | |
---|
420 | #ifdef HAVE_LIBZEPHYR |
---|
421 | Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) |
---|
422 | { |
---|
423 | return(ZSendPacket(buf, len, 0)); |
---|
424 | } |
---|
425 | #endif |
---|
426 | |
---|
427 | void send_ping(char *to) |
---|
428 | { |
---|
429 | #ifdef HAVE_LIBZEPHYR |
---|
430 | send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, ""); |
---|
431 | #endif |
---|
432 | } |
---|
433 | |
---|
434 | #ifdef HAVE_LIBZEPHYR |
---|
435 | void owl_zephyr_handle_ack(ZNotice_t *retnotice) |
---|
436 | { |
---|
437 | char *tmp; |
---|
438 | |
---|
439 | /* if it's an HMACK ignore it */ |
---|
440 | if (retnotice->z_kind == HMACK) return; |
---|
441 | |
---|
442 | if (retnotice->z_kind == SERVNAK) { |
---|
443 | owl_function_error("Authorization failure sending zephyr"); |
---|
444 | } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) { |
---|
445 | owl_function_error("Detected server failure while receiving acknowledgement"); |
---|
446 | } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) { |
---|
447 | if (!strcasecmp(retnotice->z_opcode, "ping")) { |
---|
448 | return; |
---|
449 | } else if (!strcasecmp(retnotice->z_class, "message") && |
---|
450 | !strcasecmp(retnotice->z_class_inst, "personal")) { |
---|
451 | tmp=short_zuser(retnotice->z_recipient); |
---|
452 | owl_function_makemsg("Message sent to %s.", tmp); |
---|
453 | free(tmp); |
---|
454 | } else { |
---|
455 | owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst); |
---|
456 | } |
---|
457 | } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) { |
---|
458 | if (strcasecmp(retnotice->z_class, "message")) { |
---|
459 | char buff[1024]; |
---|
460 | owl_function_error("No one subscribed to class class %s", retnotice->z_class); |
---|
461 | sprintf(buff, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class); |
---|
462 | owl_function_adminmsg("", buff); |
---|
463 | } else { |
---|
464 | char buff[1024]; |
---|
465 | tmp = short_zuser(retnotice->z_recipient); |
---|
466 | owl_function_error("%s: Not logged in or subscribing to messages.", |
---|
467 | tmp); |
---|
468 | |
---|
469 | sprintf(buff, "Could not send message to %s: not logged in or subscribing to messages.\n", tmp); |
---|
470 | owl_function_adminmsg("", buff); |
---|
471 | if (owl_global_is_logging(&g)) owl_log_outgoing_zephyr_error(tmp, buff); |
---|
472 | owl_free(tmp); |
---|
473 | } |
---|
474 | } else { |
---|
475 | owl_function_error("Internal error on ack (%s)", retnotice->z_message); |
---|
476 | } |
---|
477 | } |
---|
478 | #else |
---|
479 | void owl_zephyr_handle_ack(void *retnotice) |
---|
480 | { |
---|
481 | } |
---|
482 | #endif |
---|
483 | |
---|
484 | #ifdef HAVE_LIBZEPHYR |
---|
485 | int owl_zephyr_notice_is_ack(ZNotice_t *n) |
---|
486 | { |
---|
487 | if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) { |
---|
488 | if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0); |
---|
489 | return(1); |
---|
490 | } |
---|
491 | return(0); |
---|
492 | } |
---|
493 | #else |
---|
494 | int owl_zephyr_notice_is_ack(void *n) |
---|
495 | { |
---|
496 | return(0); |
---|
497 | } |
---|
498 | #endif |
---|
499 | |
---|
500 | void owl_zephyr_zaway(owl_message *m) |
---|
501 | { |
---|
502 | #ifdef HAVE_LIBZEPHYR |
---|
503 | char *tmpbuff, *myuser, *to; |
---|
504 | |
---|
505 | /* bail if it doesn't look like a message we should reply to. Some |
---|
506 | * of this defined by the way zaway(1) works |
---|
507 | */ |
---|
508 | if (strcasecmp(owl_message_get_class(m), "message")) return; |
---|
509 | if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return; |
---|
510 | if (!strcasecmp(owl_message_get_sender(m), "")) return; |
---|
511 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return; |
---|
512 | if (!strcasecmp(owl_message_get_opcode(m), "auto")) return; |
---|
513 | if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return; |
---|
514 | if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return; |
---|
515 | if (owl_message_get_attribute_value(m, "isauto")) return; |
---|
516 | |
---|
517 | if (owl_global_is_smartstrip(&g)) { |
---|
518 | to=owl_zephyr_smartstripped_user(owl_message_get_sender(m)); |
---|
519 | } else { |
---|
520 | to=owl_strdup(owl_message_get_sender(m)); |
---|
521 | } |
---|
522 | |
---|
523 | send_zephyr("", |
---|
524 | "Automated reply:", |
---|
525 | owl_message_get_class(m), |
---|
526 | owl_message_get_instance(m), |
---|
527 | to, |
---|
528 | owl_global_get_zaway_msg(&g)); |
---|
529 | |
---|
530 | myuser=short_zuser(to); |
---|
531 | if (!strcasecmp(owl_message_get_instance(m), "personal")) { |
---|
532 | tmpbuff = owl_sprintf("zwrite %s", myuser); |
---|
533 | } else { |
---|
534 | tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser); |
---|
535 | } |
---|
536 | owl_free(myuser); |
---|
537 | owl_free(to); |
---|
538 | |
---|
539 | /* display the message as an admin message in the receive window */ |
---|
540 | owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:"); |
---|
541 | owl_free(tmpbuff); |
---|
542 | #endif |
---|
543 | } |
---|
544 | |
---|
545 | #ifdef HAVE_LIBZEPHYR |
---|
546 | void owl_zephyr_hackaway_cr(ZNotice_t *n) |
---|
547 | { |
---|
548 | /* replace \r's with ' '. Gross-ish */ |
---|
549 | int i; |
---|
550 | |
---|
551 | for (i=0; i<n->z_message_len; i++) { |
---|
552 | if (n->z_message[i]=='\r') { |
---|
553 | n->z_message[i]=' '; |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | #endif |
---|
558 | |
---|
559 | void owl_zephyr_zlocate(char *user, char *out, int auth) |
---|
560 | { |
---|
561 | #ifdef HAVE_LIBZEPHYR |
---|
562 | int ret, numlocs; |
---|
563 | int one = 1; |
---|
564 | ZLocations_t locations; |
---|
565 | char *myuser; |
---|
566 | |
---|
567 | strcpy(out, ""); |
---|
568 | ZResetAuthentication(); |
---|
569 | ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH); |
---|
570 | if (ret != ZERR_NONE) { |
---|
571 | owl_function_error("Error locating user %s", user); |
---|
572 | } |
---|
573 | |
---|
574 | if (numlocs==0) { |
---|
575 | myuser=short_zuser(user); |
---|
576 | sprintf(out, "%s: Hidden or not logged-in\n", myuser); |
---|
577 | owl_free(myuser); |
---|
578 | return; |
---|
579 | } |
---|
580 | |
---|
581 | for (;numlocs;numlocs--) { |
---|
582 | ZGetLocations(&locations,&one); |
---|
583 | myuser=short_zuser(user); |
---|
584 | sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time); |
---|
585 | owl_free(myuser); |
---|
586 | } |
---|
587 | #endif |
---|
588 | } |
---|
589 | |
---|
590 | void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) |
---|
591 | { |
---|
592 | #ifdef HAVE_LIBZEPHYR |
---|
593 | char *line, subsfile[LINE], buff[LINE]; |
---|
594 | FILE *file; |
---|
595 | |
---|
596 | line=owl_zephyr_makesubline(class, inst, recip); |
---|
597 | |
---|
598 | if (filename==NULL) { |
---|
599 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
---|
600 | } else { |
---|
601 | strcpy(subsfile, filename); |
---|
602 | } |
---|
603 | |
---|
604 | /* first check if it exists already */ |
---|
605 | file=fopen(subsfile, "r"); |
---|
606 | if (!file) { |
---|
607 | owl_function_error("Error opening file %s", subsfile); |
---|
608 | owl_free(line); |
---|
609 | return; |
---|
610 | } |
---|
611 | while (fgets(buff, LINE, file)!=NULL) { |
---|
612 | if (!strcasecmp(buff, line)) { |
---|
613 | owl_function_error("Subscription already present in %s", subsfile); |
---|
614 | owl_free(line); |
---|
615 | return; |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | /* if we get here then we didn't find it */ |
---|
620 | fclose(file); |
---|
621 | file=fopen(subsfile, "a"); |
---|
622 | if (!file) { |
---|
623 | owl_function_error("Error opening file %s for writing", subsfile); |
---|
624 | owl_free(line); |
---|
625 | return; |
---|
626 | } |
---|
627 | fputs(line, file); |
---|
628 | fclose(file); |
---|
629 | owl_function_makemsg("Subscription added"); |
---|
630 | |
---|
631 | owl_free(line); |
---|
632 | #endif |
---|
633 | } |
---|
634 | |
---|
635 | void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) |
---|
636 | { |
---|
637 | #ifdef HAVE_LIBZEPHYR |
---|
638 | char *line, *subsfile; |
---|
639 | |
---|
640 | line=owl_zephyr_makesubline(class, inst, recip); |
---|
641 | line[strlen(line)-1]='\0'; |
---|
642 | |
---|
643 | if (!filename) { |
---|
644 | subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g)); |
---|
645 | } else { |
---|
646 | subsfile=owl_strdup(filename); |
---|
647 | } |
---|
648 | |
---|
649 | owl_util_file_deleteline(subsfile, line, 1); |
---|
650 | owl_free(subsfile); |
---|
651 | owl_free(line); |
---|
652 | owl_function_makemsg("Subscription removed"); |
---|
653 | #endif |
---|
654 | } |
---|
655 | |
---|
656 | /* caller must free the return */ |
---|
657 | char *owl_zephyr_makesubline(char *class, char *inst, char *recip) |
---|
658 | { |
---|
659 | char *out; |
---|
660 | |
---|
661 | out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30); |
---|
662 | sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip); |
---|
663 | return(out); |
---|
664 | } |
---|
665 | |
---|
666 | |
---|
667 | void owl_zephyr_zlog_in(void) |
---|
668 | { |
---|
669 | #ifdef HAVE_LIBZEPHYR |
---|
670 | char *exposure, *eset; |
---|
671 | int ret; |
---|
672 | |
---|
673 | ZResetAuthentication(); |
---|
674 | |
---|
675 | eset=EXPOSE_REALMVIS; |
---|
676 | exposure=ZGetVariable("exposure"); |
---|
677 | if (exposure==NULL) { |
---|
678 | eset=EXPOSE_REALMVIS; |
---|
679 | } else if (!strcasecmp(exposure,EXPOSE_NONE)) { |
---|
680 | eset = EXPOSE_NONE; |
---|
681 | } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) { |
---|
682 | eset = EXPOSE_OPSTAFF; |
---|
683 | } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) { |
---|
684 | eset = EXPOSE_REALMVIS; |
---|
685 | } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) { |
---|
686 | eset = EXPOSE_REALMANN; |
---|
687 | } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) { |
---|
688 | eset = EXPOSE_NETVIS; |
---|
689 | } else if (!strcasecmp(exposure,EXPOSE_NETANN)) { |
---|
690 | eset = EXPOSE_NETANN; |
---|
691 | } |
---|
692 | |
---|
693 | ret=ZSetLocation(eset); |
---|
694 | if (ret != ZERR_NONE) { |
---|
695 | /* |
---|
696 | char buff[LINE]; |
---|
697 | sprintf(buff, "Error setting location: %s", error_message(ret)); |
---|
698 | owl_function_makemsg(buff); |
---|
699 | */ |
---|
700 | } |
---|
701 | #endif |
---|
702 | } |
---|
703 | |
---|
704 | void owl_zephyr_zlog_out(void) |
---|
705 | { |
---|
706 | #ifdef HAVE_LIBZEPHYR |
---|
707 | int ret; |
---|
708 | |
---|
709 | ZResetAuthentication(); |
---|
710 | ret=ZUnsetLocation(); |
---|
711 | if (ret != ZERR_NONE) { |
---|
712 | /* |
---|
713 | char buff[LINE]; |
---|
714 | sprintf(buff, "Error unsetting location: %s", error_message(ret)); |
---|
715 | owl_function_makemsg(buff); |
---|
716 | */ |
---|
717 | } |
---|
718 | #endif |
---|
719 | } |
---|
720 | |
---|
721 | void owl_zephyr_addbuddy(char *name) |
---|
722 | { |
---|
723 | char *filename; |
---|
724 | FILE *file; |
---|
725 | |
---|
726 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
---|
727 | file=fopen(filename, "a"); |
---|
728 | owl_free(filename); |
---|
729 | if (!file) { |
---|
730 | owl_function_error("Error opening zephyr buddy file for append"); |
---|
731 | return; |
---|
732 | } |
---|
733 | fprintf(file, "%s\n", name); |
---|
734 | fclose(file); |
---|
735 | } |
---|
736 | |
---|
737 | void owl_zephyr_delbuddy(char *name) |
---|
738 | { |
---|
739 | char *filename; |
---|
740 | |
---|
741 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
---|
742 | owl_util_file_deleteline(filename, name, 0); |
---|
743 | owl_free(filename); |
---|
744 | } |
---|
745 | |
---|
746 | /* return auth string */ |
---|
747 | #ifdef HAVE_LIBZEPHYR |
---|
748 | char *owl_zephyr_get_authstr(ZNotice_t *n) |
---|
749 | { |
---|
750 | |
---|
751 | if (!n) return("UNKNOWN"); |
---|
752 | |
---|
753 | if (n->z_auth == ZAUTH_FAILED) { |
---|
754 | return ("FAILED"); |
---|
755 | } else if (n->z_auth == ZAUTH_NO) { |
---|
756 | return ("NO"); |
---|
757 | } else if (n->z_auth == ZAUTH_YES) { |
---|
758 | return ("YES"); |
---|
759 | } else { |
---|
760 | return ("UNKNOWN"); |
---|
761 | } |
---|
762 | } |
---|
763 | #else |
---|
764 | char *owl_zephyr_get_authstr(void *n) |
---|
765 | { |
---|
766 | return(""); |
---|
767 | } |
---|
768 | #endif |
---|
769 | |
---|
770 | /* Returns a buffer of subscriptions or an error message. Caller must |
---|
771 | * free the return. |
---|
772 | */ |
---|
773 | char *owl_zephyr_getsubs() |
---|
774 | { |
---|
775 | #ifdef HAVE_LIBZEPHYR |
---|
776 | int ret, num, i, one; |
---|
777 | ZSubscription_t sub; |
---|
778 | char *out, *tmpbuff; |
---|
779 | one=1; |
---|
780 | |
---|
781 | ret=ZRetrieveSubscriptions(0, &num); |
---|
782 | if (ret==ZERR_TOOMANYSUBS) { |
---|
783 | return(owl_strdup("Zephyr: too many subscriptions\n")); |
---|
784 | } else if (ret) { |
---|
785 | return(owl_strdup("Zephyr: error retriving subscriptions\n")); |
---|
786 | } |
---|
787 | |
---|
788 | out=owl_malloc(num*500); |
---|
789 | tmpbuff=owl_malloc(num*500); |
---|
790 | strcpy(out, ""); |
---|
791 | for (i=0; i<num; i++) { |
---|
792 | if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) { |
---|
793 | owl_free(out); |
---|
794 | owl_free(tmpbuff); |
---|
795 | ZFlushSubscriptions(); |
---|
796 | out=owl_strdup("Error while getting subscriptions\n"); |
---|
797 | return(out); |
---|
798 | } else { |
---|
799 | sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out); |
---|
800 | strcpy(out, tmpbuff); |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | owl_free(tmpbuff); |
---|
805 | ZFlushSubscriptions(); |
---|
806 | return(out); |
---|
807 | #else |
---|
808 | return(owl_strdup("Zephyr not available")); |
---|
809 | #endif |
---|
810 | } |
---|
811 | |
---|
812 | char *owl_zephyr_get_variable(char *var) |
---|
813 | { |
---|
814 | #ifdef HAVE_LIBZEPHYR |
---|
815 | return(ZGetVariable(var)); |
---|
816 | #else |
---|
817 | return(""); |
---|
818 | #endif |
---|
819 | } |
---|
820 | |
---|
821 | void owl_zephyr_set_locationinfo(char *host, char *val) |
---|
822 | { |
---|
823 | #ifdef HAVE_LIBZEPHYR |
---|
824 | ZInitLocationInfo(host, val); |
---|
825 | #endif |
---|
826 | } |
---|
827 | |
---|
828 | /* Strip a local realm fron the zephyr user name. |
---|
829 | * The caller must free the return |
---|
830 | */ |
---|
831 | char *short_zuser(char *in) |
---|
832 | { |
---|
833 | char *out, *ptr; |
---|
834 | |
---|
835 | out=owl_strdup(in); |
---|
836 | ptr=strchr(out, '@'); |
---|
837 | if (ptr) { |
---|
838 | if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) { |
---|
839 | *ptr='\0'; |
---|
840 | } |
---|
841 | } |
---|
842 | return(out); |
---|
843 | } |
---|
844 | |
---|
845 | /* Append a local realm to the zephyr user name if necessary. |
---|
846 | * The caller must free the return. |
---|
847 | */ |
---|
848 | char *long_zuser(char *in) |
---|
849 | { |
---|
850 | char *ptr; |
---|
851 | |
---|
852 | if (NULL != (ptr=strchr(in, '@'))) { |
---|
853 | return owl_strdup(in); |
---|
854 | } else { |
---|
855 | return owl_sprintf("%s@%s", in, owl_zephyr_get_realm()); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | |
---|
860 | /* strip out the instance from a zsender's principal. Preserves the |
---|
861 | * realm if present. daemon.webzephyr is a special case. The |
---|
862 | * caller must free the return |
---|
863 | */ |
---|
864 | char *owl_zephyr_smartstripped_user(char *in) |
---|
865 | { |
---|
866 | char *ptr, *realm, *out; |
---|
867 | |
---|
868 | out=owl_strdup(in); |
---|
869 | |
---|
870 | /* bail immeaditly if we don't have to do any work */ |
---|
871 | ptr=strchr(in, '.'); |
---|
872 | if (!strchr(in, '/') && !ptr) { |
---|
873 | /* no '/' and no '.' */ |
---|
874 | return(out); |
---|
875 | } |
---|
876 | if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) { |
---|
877 | /* There's a '.' but it's in the realm */ |
---|
878 | return(out); |
---|
879 | } |
---|
880 | if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) { |
---|
881 | return(out); |
---|
882 | } |
---|
883 | |
---|
884 | /* remove the realm from ptr, but hold on to it */ |
---|
885 | realm=strchr(out, '@'); |
---|
886 | if (realm) realm[0]='\0'; |
---|
887 | |
---|
888 | /* strip */ |
---|
889 | ptr=strchr(out, '.'); |
---|
890 | if (!ptr) ptr=strchr(out, '/'); |
---|
891 | ptr[0]='\0'; |
---|
892 | |
---|
893 | /* reattach the realm if we had one */ |
---|
894 | if (realm) { |
---|
895 | strcat(out, "@"); |
---|
896 | strcat(out, realm+1); |
---|
897 | } |
---|
898 | |
---|
899 | return(out); |
---|
900 | } |
---|
901 | |
---|
902 | /* read the list of users in 'filename' as a .anyone file, and put the |
---|
903 | * names of the zephyr users in the list 'in'. If 'filename' is NULL, |
---|
904 | * use the default .anyone file in the users home directory. Returns |
---|
905 | * -1 on failure, 0 on success. |
---|
906 | */ |
---|
907 | int owl_zephyr_get_anyone_list(owl_list *in, char *filename) |
---|
908 | { |
---|
909 | #ifdef HAVE_LIBZEPHYR |
---|
910 | char *ourfile, *tmp, buff[LINE]; |
---|
911 | FILE *f; |
---|
912 | |
---|
913 | if (filename==NULL) { |
---|
914 | tmp=owl_global_get_homedir(&g); |
---|
915 | ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
---|
916 | } else { |
---|
917 | ourfile=owl_strdup(filename); |
---|
918 | } |
---|
919 | |
---|
920 | f=fopen(ourfile, "r"); |
---|
921 | if (!f) { |
---|
922 | owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : ""); |
---|
923 | owl_free(ourfile); |
---|
924 | return(-1); |
---|
925 | } |
---|
926 | |
---|
927 | while (fgets(buff, LINE, f)!=NULL) { |
---|
928 | /* ignore comments, blank lines etc. */ |
---|
929 | if (buff[0]=='#') continue; |
---|
930 | if (buff[0]=='\n') continue; |
---|
931 | if (buff[0]=='\0') continue; |
---|
932 | |
---|
933 | /* strip the \n */ |
---|
934 | buff[strlen(buff)-1]='\0'; |
---|
935 | |
---|
936 | /* ingore from # on */ |
---|
937 | tmp=strchr(buff, '#'); |
---|
938 | if (tmp) tmp[0]='\0'; |
---|
939 | |
---|
940 | /* ingore from SPC */ |
---|
941 | tmp=strchr(buff, ' '); |
---|
942 | if (tmp) tmp[0]='\0'; |
---|
943 | |
---|
944 | /* stick on the local realm. */ |
---|
945 | if (!strchr(buff, '@')) { |
---|
946 | strcat(buff, "@"); |
---|
947 | strcat(buff, ZGetRealm()); |
---|
948 | } |
---|
949 | owl_list_append_element(in, owl_strdup(buff)); |
---|
950 | } |
---|
951 | fclose(f); |
---|
952 | owl_free(ourfile); |
---|
953 | return(0); |
---|
954 | #else |
---|
955 | return(-1); |
---|
956 | #endif |
---|
957 | } |
---|