]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
2 * "$Id: auth.c 5043 2006-02-01 18:55:16Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddLocation() - Add a location for authorization.
27 * cupsdAddName() - Add a name to a location...
28 * cupsdAllowHost() - Add a host name that is allowed to access the
30 * cupsdAllowIP() - Add an IP address or network that is allowed to
31 * access the location.
32 * cupsdCheckAuth() - Check authorization masks.
33 * cupsdCheckGroup() - Check for a user's group membership.
34 * cupsdCopyLocation() - Make a copy of a location...
35 * cupsdDeleteAllLocations() - Free all memory used for location authorization.
36 * cupsdDeleteLocation() - Free all memory used by a location.
37 * cupsdDenyHost() - Add a host name that is not allowed to access the
39 * cupsdDenyIP() - Add an IP address or network that is not allowed
40 * to access the location.
41 * cupsdFindBest() - Find the location entry that best matches the
43 * cupsdFindLocation() - Find the named location.
44 * cupsdGetMD5Passwd() - Get an MD5 password.
45 * cupsdIsAuthorized() - Check to see if the user is authorized...
46 * add_allow() - Add an allow mask to the location.
47 * add_deny() - Add a deny mask to the location.
48 * cups_crypt() - Encrypt the password using the DES or MD5
49 * algorithms, as needed.
50 * pam_func() - PAM conversation function.
51 * to64() - Base64-encode an integer value...
55 * Include necessary headers...
62 #endif /* HAVE_SHADOW_H */
65 #endif /* HAVE_CRYPT_H */
67 # ifdef HAVE_PAM_PAM_APPL_H
68 # include <pam/pam_appl.h>
70 # include <security/pam_appl.h>
71 # endif /* HAVE_PAM_PAM_APPL_H */
72 #endif /* HAVE_LIBPAM */
75 #endif /* HAVE_USERSEC_H */
82 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
83 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
85 static char *cups_crypt(const char *pw
, const char *salt
);
86 #endif /* !HAVE_LIBPAM */
88 static int pam_func(int, const struct pam_message
**,
89 struct pam_response
**, void *);
91 static void to64(char *s
, unsigned long v
, int n
);
92 #endif /* HAVE_LIBPAM */
100 typedef struct cupsd_authdata_s
/**** Authentication data ****/
102 char username
[33], /* Username string */
103 password
[33]; /* Password string */
105 #endif /* HAVE_LIBPAM */
112 #if defined(__hpux) && defined(HAVE_LIBPAM)
113 static cupsd_authdata_t
*auth_datat
; /* Current client being authenticated */
114 #endif /* __hpux && HAVE_LIBPAM */
118 * 'cupsdAddLocation()' - Add a location for authorization.
121 cupsd_location_t
* /* O - Pointer to new location record */
122 cupsdAddLocation(const char *location
) /* I - Location path */
124 cupsd_location_t
*temp
; /* New location */
128 * Try to allocate memory for the new location.
131 if (NumLocations
== 0)
132 temp
= malloc(sizeof(cupsd_location_t
));
134 temp
= realloc(Locations
, sizeof(cupsd_location_t
) * (NumLocations
+ 1));
140 temp
+= NumLocations
;
144 * Initialize the record and copy the name over...
147 memset(temp
, 0, sizeof(cupsd_location_t
));
148 strlcpy(temp
->location
, location
, sizeof(temp
->location
));
149 temp
->length
= strlen(temp
->location
);
151 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAddLocation: added location \'%s\'",
155 * Return the new record...
163 * 'cupsdAddName()' - Add a name to a location...
167 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
168 char *name
) /* I - Name to add */
170 char **temp
; /* Pointer to names array */
173 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
176 if (loc
->num_names
== 0)
177 temp
= malloc(sizeof(char *));
179 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
183 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
184 loc
->location
, strerror(errno
));
190 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
192 cupsdLogMessage(CUPSD_LOG_ERROR
,
193 "Unable to duplicate name for location %s: %s",
194 loc
->location
, strerror(errno
));
203 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
207 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
208 char *name
) /* I - Name of host or domain to add */
210 cupsd_authmask_t
*temp
; /* New host/domain mask */
211 char ifname
[32], /* Interface name */
212 *ifptr
; /* Pointer to end of name */
215 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
216 loc
, loc
->location
, name
);
218 if ((temp
= add_allow(loc
)) == NULL
)
221 if (strcasecmp(name
, "@LOCAL") == 0)
224 * Allow *interface*...
227 temp
->type
= AUTH_INTERFACE
;
228 temp
->mask
.name
.name
= strdup("*");
229 temp
->mask
.name
.length
= 1;
231 else if (strncasecmp(name
, "@IF(", 4) == 0)
234 * Allow *interface*...
237 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
239 ifptr
= ifname
+ strlen(ifname
);
241 if (ifptr
[-1] == ')')
247 temp
->type
= AUTH_INTERFACE
;
248 temp
->mask
.name
.name
= strdup(ifname
);
249 temp
->mask
.name
.length
= ifptr
- ifname
;
257 temp
->type
= AUTH_NAME
;
258 temp
->mask
.name
.name
= strdup(name
);
259 temp
->mask
.name
.length
= strlen(name
);
265 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
270 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
271 unsigned address
[4], /* I - IP address to add */
272 unsigned netmask
[4]) /* I - Netmask of address */
274 cupsd_authmask_t
*temp
; /* New host/domain mask */
277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
278 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
279 loc
, loc
->location
, address
[0], address
[1], address
[2],
280 address
[3], netmask
[0], netmask
[1], netmask
[2],
283 if ((temp
= add_allow(loc
)) == NULL
)
286 temp
->type
= AUTH_IP
;
287 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
288 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
293 * 'cupsdAuthorize()' - Validate any authorization credentials.
297 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
299 int type
; /* Authentication type */
300 char *authorization
, /* Pointer into Authorization string */
301 *ptr
, /* Pointer into string */
302 username
[65], /* Username string */
303 password
[33]; /* Password string */
304 const char *localuser
; /* Certificate username */
305 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
306 md5
[33], /* MD5 password */
307 basicmd5
[33]; /* MD5 of Basic password */
308 static const char * const states
[] = /* HTTP client states... */
328 * Locate the best matching location so we know what kind of
329 * authentication to expect...
332 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
334 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
335 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
336 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
338 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
339 type
= con
->best
->type
;
341 type
= DefaultAuthType
;
344 * Decode the Authorization string...
347 authorization
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
349 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
355 if (type
== AUTH_NONE
)
358 * No authorization required, return early...
361 cupsdLogMessage(CUPSD_LOG_DEBUG
,
362 "cupsdAuthorize: No authentication required.");
365 else if (!*authorization
)
368 * No authorization data provided, return early...
371 cupsdLogMessage(CUPSD_LOG_DEBUG
,
372 "cupsdAuthorize: No authentication data provided.");
375 else if (!strncmp(authorization
, "Local", 5) &&
376 !strcasecmp(con
->http
.hostname
, "localhost"))
379 * Get Local certificate authentication data...
383 while (isspace(*authorization
))
386 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
387 strlcpy(username
, localuser
, sizeof(username
));
390 cupsdLogMessage(CUPSD_LOG_ERROR
,
391 "cupsdAuthorize: Local authentication certificate not "
396 else if (!strncmp(authorization
, "Basic", 5) &&
397 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
400 * Get the Basic authentication data...
403 int userlen
; /* Username:password length */
407 while (isspace(*authorization
))
410 userlen
= sizeof(username
);
411 httpDecode64_2(username
, &userlen
, authorization
);
414 * Pull the username and password out...
417 if ((ptr
= strchr(username
, ':')) == NULL
)
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "cupsdAuthorize: Missing Basic password!");
429 * Username must not be empty...
432 cupsdLogMessage(CUPSD_LOG_ERROR
,
433 "cupsdAuthorize: Empty Basic username!");
440 * Password must not be empty...
443 cupsdLogMessage(CUPSD_LOG_ERROR
,
444 "cupsdAuthorize: Empty Basic password!");
448 strlcpy(password
, ptr
, sizeof(password
));
451 * Validate the username and password...
460 * Only use PAM to do authentication. This supports MD5
461 * passwords, among other things...
464 pam_handle_t
*pamh
; /* PAM authentication handle */
465 int pamerr
; /* PAM error code */
466 struct pam_conv pamdata
;/* PAM conversation data */
467 cupsd_authdata_t data
; /* Authentication data */
470 strlcpy(data
.username
, username
, sizeof(data
.username
));
471 strlcpy(data
.password
, password
, sizeof(data
.password
));
473 pamdata
.conv
= pam_func
;
474 pamdata
.appdata_ptr
= &data
;
478 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
485 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
486 if (pamerr
!= PAM_SUCCESS
)
488 cupsdLogMessage(CUPSD_LOG_ERROR
,
489 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
490 pamerr
, pam_strerror(pamh
, pamerr
));
495 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
496 if (pamerr
!= PAM_SUCCESS
)
498 cupsdLogMessage(CUPSD_LOG_ERROR
,
499 "cupsdAuthorize: pam_authenticate() returned %d "
501 pamerr
, pam_strerror(pamh
, pamerr
));
506 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
507 if (pamerr
!= PAM_SUCCESS
)
509 cupsdLogMessage(CUPSD_LOG_ERROR
,
510 "cupsdAuthorize: pam_acct_mgmt() returned %d "
512 pamerr
, pam_strerror(pamh
, pamerr
));
517 pam_end(pamh
, PAM_SUCCESS
);
519 #elif defined(HAVE_USERSEC_H)
521 * Use AIX authentication interface...
524 char *authmsg
; /* Authentication message */
525 char *loginmsg
; /* Login message */
526 int reenter
; /* ??? */
529 cupsdLogMessage(CUPSD_LOG_DEBUG
,
530 "cupsdAuthorize: AIX authenticate of username \"%s\"",
534 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
536 cupsdLogMessage(CUPSD_LOG_DEBUG
,
537 "cupsdAuthorize: Unable to authenticate username "
539 username
, strerror(errno
));
545 * Use normal UNIX password file-based authentication...
548 char *pass
; /* Encrypted password */
549 struct passwd
*pw
; /* User password data */
550 # ifdef HAVE_SHADOW_H
551 struct spwd
*spw
; /* Shadow password data */
552 # endif /* HAVE_SHADOW_H */
555 pw
= getpwnam(username
); /* Get the current password */
556 endpwent(); /* Close the password file */
564 cupsdLogMessage(CUPSD_LOG_ERROR
,
565 "cupsdAuthorize: Unknown username \"%s\"!",
567 return (HTTP_UNAUTHORIZED
);
570 # ifdef HAVE_SHADOW_H
571 spw
= getspnam(username
);
574 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
577 * Don't allow blank passwords!
580 cupsdLogMessage(CUPSD_LOG_ERROR
,
581 "cupsdAuthorize: Username \"%s\" has no shadow "
582 "password!", username
);
586 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
588 if (!pw
->pw_passwd
[0])
589 # endif /* HAVE_SHADOW_H */
592 * Don't allow blank passwords!
595 cupsdLogMessage(CUPSD_LOG_ERROR
,
596 "cupsdAuthorize: Username \"%s\" has no password!",
602 * OK, the password isn't blank, so compare with what came from the
606 pass
= cups_crypt(password
, pw
->pw_passwd
);
608 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
609 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
610 pw
->pw_passwd
, pass
);
612 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
614 # ifdef HAVE_SHADOW_H
617 pass
= cups_crypt(password
, spw
->sp_pwdp
);
619 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
620 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
623 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
625 cupsdLogMessage(CUPSD_LOG_ERROR
,
626 "cupsdAuthorize: Authentication failed for "
633 # endif /* HAVE_SHADOW_H */
635 cupsdLogMessage(CUPSD_LOG_ERROR
,
636 "cupsdAuthorize: Authentication failed for "
642 #endif /* HAVE_LIBPAM */
646 case AUTH_BASICDIGEST
:
648 * Do Basic authentication with the Digest password file...
651 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
653 cupsdLogMessage(CUPSD_LOG_ERROR
,
654 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
659 httpMD5(username
, "CUPS", password
, basicmd5
);
661 if (strcmp(md5
, basicmd5
))
663 cupsdLogMessage(CUPSD_LOG_ERROR
,
664 "cupsdAuthorize: Authentication failed for \"%s\"!",
671 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
674 * Get the username, password, and nonce from the Digest attributes...
677 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
678 username
, sizeof(username
)) || !username
[0])
681 * Username must not be empty...
684 cupsdLogMessage(CUPSD_LOG_ERROR
,
685 "cupsdAuthorize: Empty or missing Digest username!");
689 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
690 password
, sizeof(password
)) || !password
[0])
693 * Password must not be empty...
696 cupsdLogMessage(CUPSD_LOG_ERROR
,
697 "cupsdAuthorize: Empty or missing Digest password!");
701 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
704 cupsdLogMessage(CUPSD_LOG_ERROR
,
705 "cupsdAuthorize: No nonce value for Digest "
710 if (strcmp(con
->http
.hostname
, nonce
))
712 cupsdLogMessage(CUPSD_LOG_ERROR
,
713 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
714 "got \"%s\"!", con
->http
.hostname
, nonce
);
719 * Validate the username and password...
722 if (!cupsdGetMD5Passwd(username
, NULL
, md5
))
724 cupsdLogMessage(CUPSD_LOG_ERROR
,
725 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
730 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
732 if (strcmp(md5
, password
))
734 cupsdLogMessage(CUPSD_LOG_ERROR
,
735 "cupsdAuthorize: Authentication failed for \"%s\"!",
742 cupsdLogMessage(CUPSD_LOG_DEBUG
,
743 "cupsdAuthorize: Bad authentication data.");
748 * If we get here, then we were able to validate the username and
749 * password - copy the validated username and password to the client
753 strlcpy(con
->username
, username
, sizeof(con
->username
));
754 strlcpy(con
->password
, password
, sizeof(con
->password
));
756 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
762 * 'cupsdCheckAuth()' - Check authorization masks.
765 int /* O - 1 if mask matches, 0 otherwise */
767 unsigned ip
[4], /* I - Client address */
768 char *name
, /* I - Client hostname */
769 int name_len
, /* I - Length of hostname */
770 int num_masks
, /* I - Number of masks */
771 cupsd_authmask_t
*masks
) /* I - Masks */
773 int i
; /* Looping var */
774 cupsd_netif_t
*iface
; /* Network interface */
775 unsigned netip4
; /* IPv4 network address */
777 unsigned netip6
[4]; /* IPv6 network address */
778 #endif /* AF_INET6 */
780 while (num_masks
> 0)
784 case AUTH_INTERFACE
:
786 * Check for a match with a network interface...
789 netip4
= htonl(ip
[3]);
792 netip6
[0] = htonl(ip
[0]);
793 netip6
[1] = htonl(ip
[1]);
794 netip6
[2] = htonl(ip
[2]);
795 netip6
[3] = htonl(ip
[3]);
796 #endif /* AF_INET6 */
798 if (!strcmp(masks
->mask
.name
.name
, "*"))
801 * Check against all local interfaces...
806 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
808 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
811 * Only check local interfaces...
814 if (!iface
->is_local
)
817 if (iface
->address
.addr
.sa_family
== AF_INET
)
820 * Check IPv4 address...
823 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
824 (iface
->address
.ipv4
.sin_addr
.s_addr
&
825 iface
->mask
.ipv4
.sin_addr
.s_addr
))
832 * Check IPv6 address...
835 for (i
= 0; i
< 4; i
++)
836 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
837 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
838 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
844 #endif /* AF_INET6 */
850 * Check the named interface...
853 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
854 iface
&& !strcmp(masks
->mask
.name
.name
, iface
->name
);
855 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
857 if (iface
->address
.addr
.sa_family
== AF_INET
)
860 * Check IPv4 address...
863 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
864 (iface
->address
.ipv4
.sin_addr
.s_addr
&
865 iface
->mask
.ipv4
.sin_addr
.s_addr
))
872 * Check IPv6 address...
875 for (i
= 0; i
< 4; i
++)
876 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
877 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
878 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
884 #endif /* AF_INET6 */
891 * Check for exact name match...
894 if (!strcasecmp(name
, masks
->mask
.name
.name
))
898 * Check for domain match...
901 if (name_len
>= masks
->mask
.name
.length
&&
902 masks
->mask
.name
.name
[0] == '.' &&
903 strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
904 masks
->mask
.name
.name
) == 0)
910 * Check for IP/network address match...
913 for (i
= 0; i
< 4; i
++)
914 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
915 masks
->mask
.ip
.address
[i
])
932 * 'cupsdCheckGroup()' - Check for a user's group membership.
935 int /* O - 1 if user is a member, 0 otherwise */
937 const char *username
, /* I - User name */
938 struct passwd
*user
, /* I - System user info */
939 const char *groupname
) /* I - Group name */
941 int i
; /* Looping var */
942 struct group
*group
; /* System group info */
943 char junk
[33]; /* MD5 password (not used) */
946 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
947 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
948 username
, user
, groupname
);
954 if (!username
|| !groupname
)
958 * Check to see if the user is a member of the named group...
961 group
= getgrnam(groupname
);
967 * Group exists, check it...
970 for (i
= 0; group
->gr_mem
[i
]; i
++)
971 if (!strcasecmp(username
, group
->gr_mem
[i
]))
976 * Group doesn't exist or user not in group list, check the group ID
977 * against the user's group ID...
980 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
984 * Username not found, group not found, or user is not part of the
985 * system group... Check for a user and group in the MD5 password
989 if (cupsdGetMD5Passwd(username
, groupname
, junk
) != NULL
)
993 * If we get this far, then the user isn't part of the named group...
1001 * 'cupsdCopyLocation()' - Make a copy of a location...
1004 cupsd_location_t
* /* O - New location */
1006 cupsd_location_t
**loc
) /* IO - Original location */
1008 int i
; /* Looping var */
1009 int locindex
; /* Index into Locations array */
1010 cupsd_location_t
*temp
; /* New location */
1011 char location
[HTTP_MAX_URI
];
1012 /* Location of resource */
1016 * Add the new location, updating the original location
1017 * pointer as needed...
1020 locindex
= *loc
- Locations
;
1023 * Use a local copy of location because cupsdAddLocation may cause
1024 * this memory to be moved...
1027 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1029 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1032 *loc
= Locations
+ locindex
;
1035 * Copy the information from the original location to the new one.
1038 temp
->limit
= (*loc
)->limit
;
1039 temp
->order_type
= (*loc
)->order_type
;
1040 temp
->type
= (*loc
)->type
;
1041 temp
->level
= (*loc
)->level
;
1042 temp
->satisfy
= (*loc
)->satisfy
;
1043 temp
->encryption
= (*loc
)->encryption
;
1045 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1048 * Copy the names array...
1051 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1053 cupsdLogMessage(CUPSD_LOG_ERROR
,
1054 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1055 temp
->num_names
, strerror(errno
));
1060 for (i
= 0; i
< temp
->num_names
; i
++)
1061 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1063 cupsdLogMessage(CUPSD_LOG_ERROR
,
1064 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1065 (*loc
)->names
[i
], strerror(errno
));
1072 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1075 * Copy allow rules...
1078 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1082 temp
->num_allow
, strerror(errno
));
1087 for (i
= 0; i
< temp
->num_allow
; i
++)
1088 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1091 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1092 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1094 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1096 cupsdLogMessage(CUPSD_LOG_ERROR
,
1097 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1098 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1104 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1105 sizeof(cupsd_ipmask_t
));
1110 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1113 * Copy deny rules...
1116 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1118 cupsdLogMessage(CUPSD_LOG_ERROR
,
1119 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1120 temp
->num_deny
, strerror(errno
));
1125 for (i
= 0; i
< temp
->num_deny
; i
++)
1126 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1129 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1130 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1132 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1134 cupsdLogMessage(CUPSD_LOG_ERROR
,
1135 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1136 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1142 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1143 sizeof(cupsd_ipmask_t
));
1153 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1157 cupsdDeleteAllLocations(void)
1159 int i
; /* Looping var */
1160 cupsd_location_t
*loc
; /* Current location */
1164 * Free all of the allow/deny records first...
1167 for (i
= NumLocations
, loc
= Locations
; i
> 0; i
--, loc
++)
1168 cupsdDeleteLocation(loc
);
1171 * Then free the location array...
1174 if (NumLocations
> 0)
1183 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1187 cupsdDeleteLocation(
1188 cupsd_location_t
*loc
) /* I - Location to delete */
1190 int i
; /* Looping var */
1191 cupsd_authmask_t
*mask
; /* Current mask */
1194 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1195 free(loc
->names
[i
]);
1197 if (loc
->num_names
> 0)
1200 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1201 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1202 free(mask
->mask
.name
.name
);
1204 if (loc
->num_allow
> 0)
1207 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1208 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1209 free(mask
->mask
.name
.name
);
1211 if (loc
->num_deny
> 0)
1217 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1222 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1223 char *name
) /* I - Name of host or domain to add */
1225 cupsd_authmask_t
*temp
; /* New host/domain mask */
1226 char ifname
[32], /* Interface name */
1227 *ifptr
; /* Pointer to end of name */
1230 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1231 loc
, loc
->location
, name
);
1233 if ((temp
= add_deny(loc
)) == NULL
)
1236 if (strcasecmp(name
, "@LOCAL") == 0)
1239 * Deny *interface*...
1242 temp
->type
= AUTH_INTERFACE
;
1243 temp
->mask
.name
.name
= strdup("*");
1244 temp
->mask
.name
.length
= 1;
1246 else if (strncasecmp(name
, "@IF(", 4) == 0)
1249 * Deny *interface*...
1252 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1254 ifptr
= ifname
+ strlen(ifname
);
1256 if (ifptr
[-1] == ')')
1262 temp
->type
= AUTH_INTERFACE
;
1263 temp
->mask
.name
.name
= strdup(ifname
);
1264 temp
->mask
.name
.length
= ifptr
- ifname
;
1272 temp
->type
= AUTH_NAME
;
1273 temp
->mask
.name
.name
= strdup(name
);
1274 temp
->mask
.name
.length
= strlen(name
);
1280 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1281 * access the location.
1285 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1286 unsigned address
[4],/* I - IP address to add */
1287 unsigned netmask
[4])/* I - Netmask of address */
1289 cupsd_authmask_t
*temp
; /* New host/domain mask */
1292 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1293 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1294 loc
, loc
->location
, address
[0], address
[1], address
[2],
1295 address
[3], netmask
[0], netmask
[1], netmask
[2],
1298 if ((temp
= add_deny(loc
)) == NULL
)
1301 temp
->type
= AUTH_IP
;
1302 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1303 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1308 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1311 cupsd_location_t
* /* O - Location that matches */
1312 cupsdFindBest(const char *path
, /* I - Resource path */
1313 http_state_t state
) /* I - HTTP state/request */
1315 int i
; /* Looping var */
1316 char uri
[HTTP_MAX_URI
],
1317 /* URI in request... */
1318 *uriptr
; /* Pointer into URI */
1319 cupsd_location_t
*loc
, /* Current location */
1320 *best
; /* Best match for location so far */
1321 int bestlen
; /* Length of best match */
1322 int limit
; /* Limit field */
1323 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1343 * First copy the connection URI to a local string so we have drop
1344 * any .ppd extension from the pathname in /printers or /classes
1348 strlcpy(uri
, path
, sizeof(uri
));
1350 if (!strncmp(uri
, "/printers/", 10) ||
1351 !strncmp(uri
, "/classes/", 9))
1354 * Check if the URI has .ppd on the end...
1357 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1359 if (!strcmp(uriptr
, ".ppd"))
1363 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1366 * Loop through the list of locations to find a match...
1369 limit
= limits
[state
];
1373 for (i
= NumLocations
, loc
= Locations
; i
> 0; i
--, loc
++)
1375 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1376 loc
->location
, loc
->limit
);
1378 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1381 * Use case-insensitive comparison for queue names...
1384 if (loc
->length
> bestlen
&&
1385 strncasecmp(uri
, loc
->location
, loc
->length
) == 0 &&
1386 loc
->location
[0] == '/' &&
1387 (limit
& loc
->limit
) != 0)
1390 bestlen
= loc
->length
;
1396 * Use case-sensitive comparison for other URIs...
1399 if (loc
->length
> bestlen
&&
1400 !strncmp(uri
, loc
->location
, loc
->length
) &&
1401 loc
->location
[0] == '/' &&
1402 (limit
& loc
->limit
) != 0)
1405 bestlen
= loc
->length
;
1411 * Return the match, if any...
1414 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1415 best
? best
->location
: "NONE");
1422 * 'cupsdFindLocation()' - Find the named location.
1425 cupsd_location_t
* /* O - Location that matches */
1426 cupsdFindLocation(const char *location
) /* I - Connection */
1428 int i
; /* Looping var */
1432 * Loop through the list of locations to find a match...
1435 for (i
= 0; i
< NumLocations
; i
++)
1436 if (!strcasecmp(Locations
[i
].location
, location
))
1437 return (Locations
+ i
);
1444 * 'cupsdGetMD5Passwd()' - Get an MD5 password.
1447 char * /* O - MD5 password string */
1448 cupsdGetMD5Passwd(const char *username
, /* I - Username */
1449 const char *group
, /* I - Group */
1450 char passwd
[33])/* O - MD5 password string */
1452 cups_file_t
*fp
; /* passwd.md5 file */
1453 char filename
[1024], /* passwd.md5 filename */
1454 line
[256], /* Line from file */
1455 tempuser
[33], /* User from file */
1456 tempgroup
[33]; /* Group from file */
1459 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1460 "cupsdGetMD5Passwd(username=\"%s\", group=\"%s\", passwd=%p)",
1461 username
, group
? group
: "(null)", passwd
);
1463 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
1464 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1466 if (errno
!= ENOENT
)
1467 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
1473 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1475 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
1477 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
1481 if (strcmp(username
, tempuser
) == 0 &&
1482 (group
== NULL
|| strcmp(group
, tempgroup
) == 0))
1485 * Found the password entry!
1488 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
1489 username
, tempgroup
);
1497 * Didn't find a password entry - return NULL!
1506 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1509 http_status_t
/* O - HTTP_OK if authorized or error code */
1510 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1511 const char *owner
)/* I - Owner of object */
1513 int i
, j
, /* Looping vars */
1514 auth
; /* Authorization status */
1515 unsigned address
[4]; /* Authorization address */
1516 cupsd_location_t
*best
; /* Best match for location so far */
1517 int hostlen
; /* Length of hostname */
1518 const char *username
; /* Username to authorize */
1519 struct passwd
*pw
; /* User password data */
1520 static const char * const levels
[] = /* Auth levels */
1526 static const char * const types
[] = /* Auth types */
1535 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1536 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1537 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
1539 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1540 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1543 * If there is no "best" authentication rule for this request, then
1544 * access is allowed from the local system and denied from other
1550 if (!strcmp(con
->http
.hostname
, "localhost") ||
1551 !strcmp(con
->http
.hostname
, ServerName
))
1554 return (HTTP_FORBIDDEN
);
1559 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1560 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, satisfy=AUTH_SATISFY_%s, num_names=%d",
1561 levels
[best
->level
], types
[best
->type
],
1562 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1564 if (best
->limit
== AUTH_LIMIT_IPP
)
1565 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1566 best
->op
, ippOpString(best
->op
));
1569 * Check host/ip-based accesses...
1573 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1576 * Copy IPv6 address...
1579 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1580 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1581 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1582 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1585 #endif /* AF_INET6 */
1586 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1589 * Copy IPv4 address...
1595 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1598 memset(address
, 0, sizeof(address
));
1600 hostlen
= strlen(con
->http
.hostname
);
1602 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1605 * Access from localhost (127.0.0.1 or :::1) is always allowed...
1613 * Do authorization checks on the domain/address...
1616 switch (best
->order_type
)
1619 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1622 case AUTH_ALLOW
: /* Order Deny,Allow */
1625 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1626 best
->num_deny
, best
->deny
))
1629 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1630 best
->num_allow
, best
->allow
))
1634 case AUTH_DENY
: /* Order Allow,Deny */
1637 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1638 best
->num_allow
, best
->allow
))
1641 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1642 best
->num_deny
, best
->deny
))
1648 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1649 auth
? "DENY" : "ALLOW");
1651 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1652 return (HTTP_FORBIDDEN
);
1656 * See if encryption is required...
1659 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
)
1661 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1662 "cupsdIsAuthorized: Need upgrade to TLS...");
1663 return (HTTP_UPGRADE_REQUIRED
);
1665 #endif /* HAVE_SSL */
1668 * Now see what access level is required...
1671 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1672 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1675 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1676 best
->limit
== AUTH_LIMIT_IPP
)
1679 * Check for unauthenticated username...
1682 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1685 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1688 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1689 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1690 attr
->values
[0].string
.text
);
1691 username
= attr
->values
[0].string
.text
;
1693 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1694 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1696 return (HTTP_OK
); /* unless overridden with Satisfy */
1700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1703 if (!con
->username
[0])
1705 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1706 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1708 return (HTTP_OK
); /* unless overridden with Satisfy */
1711 username
= con
->username
;
1715 * OK, got a username. See if we need normal user access, or group
1716 * access... (root always matches)
1719 if (!strcmp(username
, "root"))
1723 * Get the user info...
1726 pw
= getpwnam(username
);
1729 if (best
->level
== AUTH_USER
)
1732 * If there are no names associated with this location, then
1733 * any valid user is OK...
1736 if (best
->num_names
== 0)
1740 * Otherwise check the user list and return OK if this user is
1744 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1745 "cupsdIsAuthorized: Checking user membership...");
1747 for (i
= 0; i
< best
->num_names
; i
++)
1749 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1750 !strcasecmp(username
, owner
))
1752 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1754 for (j
= 0; j
< NumSystemGroups
; j
++)
1755 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1758 else if (best
->names
[i
][0] == '@')
1760 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1763 else if (!strcasecmp(username
, best
->names
[i
]))
1767 return (HTTP_UNAUTHORIZED
);
1771 * Check to see if this user is in any of the named groups...
1774 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1775 "cupsdIsAuthorized: Checking group membership...");
1778 * Check to see if this user is in any of the named groups...
1781 for (i
= 0; i
< best
->num_names
; i
++)
1783 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1784 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1787 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1789 for (j
= 0; j
< NumSystemGroups
; j
++)
1790 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1793 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1798 * The user isn't part of the specified group, so deny access...
1801 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1802 "cupsdIsAuthorized: User not in group(s)!");
1804 return (HTTP_UNAUTHORIZED
);
1809 * 'add_allow()' - Add an allow mask to the location.
1812 static cupsd_authmask_t
* /* O - New mask record */
1813 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1815 cupsd_authmask_t
*temp
; /* New mask record */
1826 * Try to allocate memory for the record...
1829 if (loc
->num_allow
== 0)
1830 temp
= malloc(sizeof(cupsd_authmask_t
));
1832 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1838 temp
+= loc
->num_allow
;
1842 * Clear the mask record and return...
1845 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1851 * 'add_deny()' - Add a deny mask to the location.
1854 static cupsd_authmask_t
* /* O - New mask record */
1855 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1857 cupsd_authmask_t
*temp
; /* New mask record */
1868 * Try to allocate memory for the record...
1871 if (loc
->num_deny
== 0)
1872 temp
= malloc(sizeof(cupsd_authmask_t
));
1874 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1880 temp
+= loc
->num_deny
;
1884 * Clear the mask record and return...
1887 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1894 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1898 static char * /* O - Encrypted password */
1899 cups_crypt(const char *pw
, /* I - Password string */
1900 const char *salt
) /* I - Salt (key) string */
1902 if (strncmp(salt
, "$1$", 3) == 0)
1905 * Use MD5 passwords without the benefit of PAM; this is for
1906 * Slackware Linux, and the algorithm was taken from the
1907 * old shadow-19990827/lib/md5crypt.c source code... :(
1910 int i
; /* Looping var */
1911 unsigned long n
; /* Output number */
1912 int pwlen
; /* Length of password string */
1913 const char *salt_end
; /* End of "salt" data for MD5 */
1914 char *ptr
; /* Pointer into result string */
1915 _cups_md5_state_t state
; /* Primary MD5 state info */
1916 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1917 unsigned char digest
[16]; /* MD5 digest result */
1918 static char result
[120]; /* Final password string */
1922 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1923 * Get a maximum of 8 characters of salt data after $1$...
1926 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1927 if (*salt_end
== '$')
1931 * Compute the MD5 sum we need...
1936 _cups_md5_init(&state
);
1937 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1938 _cups_md5_append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1940 _cups_md5_init(&state2
);
1941 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1942 _cups_md5_append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1943 _cups_md5_append(&state2
, (unsigned char *)pw
, pwlen
);
1944 _cups_md5_finish(&state2
, digest
);
1946 for (i
= pwlen
; i
> 0; i
-= 16)
1947 _cups_md5_append(&state
, digest
, i
> 16 ? 16 : i
);
1949 for (i
= pwlen
; i
> 0; i
>>= 1)
1950 _cups_md5_append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1952 _cups_md5_finish(&state
, digest
);
1954 for (i
= 0; i
< 1000; i
++)
1956 _cups_md5_init(&state
);
1959 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1961 _cups_md5_append(&state
, digest
, 16);
1964 _cups_md5_append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1967 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1970 _cups_md5_append(&state
, digest
, 16);
1972 _cups_md5_append(&state
, (unsigned char *)pw
, pwlen
);
1974 _cups_md5_finish(&state
, digest
);
1978 * Copy the final sum to the result string and return...
1981 memcpy(result
, salt
, salt_end
- salt
);
1982 ptr
= result
+ (salt_end
- salt
);
1985 for (i
= 0; i
< 5; i
++, ptr
+= 4)
1987 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
1990 n
|= digest
[i
+ 12];
1997 to64(ptr
, digest
[11], 2);
2006 * Use the standard crypt() function...
2009 return (crypt(pw
, salt
));
2012 #endif /* !HAVE_LIBPAM */
2017 * 'pam_func()' - PAM conversation function.
2020 static int /* O - Success or failure */
2022 int num_msg
, /* I - Number of messages */
2023 const struct pam_message
**msg
, /* I - Messages */
2024 struct pam_response
**resp
, /* O - Responses */
2026 /* I - Pointer to connection */
2028 int i
; /* Looping var */
2029 struct pam_response
*replies
; /* Replies */
2030 cupsd_authdata_t
*data
; /* Pointer to auth data */
2034 * Allocate memory for the responses...
2037 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2038 return (PAM_CONV_ERR
);
2041 * Answer all of the messages...
2044 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2048 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2049 * module. This is a workaround...
2055 data
= (cupsd_authdata_t
*)appdata_ptr
;
2058 for (i
= 0; i
< num_msg
; i
++)
2060 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2062 switch (msg
[i
]->msg_style
)
2064 case PAM_PROMPT_ECHO_ON
:
2065 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2067 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2068 replies
[i
].resp
= strdup(data
->username
);
2071 case PAM_PROMPT_ECHO_OFF
:
2072 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2074 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2075 replies
[i
].resp
= strdup(data
->password
);
2079 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2080 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2081 replies
[i
].resp
= NULL
;
2085 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2086 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2087 replies
[i
].resp
= NULL
;
2091 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2092 msg
[i
]->msg_style
));
2094 return (PAM_CONV_ERR
);
2099 * Return the responses back to PAM...
2104 return (PAM_SUCCESS
);
2110 * 'to64()' - Base64-encode an integer value...
2114 to64(char *s
, /* O - Output string */
2115 unsigned long v
, /* I - Value to encode */
2116 int n
) /* I - Number of digits */
2118 const char *itoa64
= "./0123456789"
2119 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2120 "abcdefghijklmnopqrstuvwxyz";
2123 for (; n
> 0; n
--, v
>>= 6)
2124 *s
++ = itoa64
[v
& 0x3f];
2126 #endif /* HAVE_LIBPAM */
2130 * End of "$Id: auth.c 5043 2006-02-01 18:55:16Z mike $".